8 from processor import Session, Dispatcher
9 from utils import print_log
12 class TcpSession(Session):
14 def __init__(self, connection, address, use_ssl, ssl_certfile, ssl_keyfile):
15 Session.__init__(self)
18 self._connection = ssl.wrap_socket(
21 certfile=ssl_certfile,
23 ssl_version=ssl.PROTOCOL_SSLv23)
25 self._connection = connection
27 self.address = address[0]
28 self.name = "TCP " if not use_ssl else "SSL "
32 raise Exception("Session was stopped")
34 return self._connection
41 self._connection.shutdown(socket.SHUT_RDWR)
43 # print_log("problem shutting down", self.address)
44 # traceback.print_exc(file=sys.stdout)
47 self._connection.close()
51 def send_response(self, response):
52 data = json.dumps(response) + "\n"
53 # Possible race condition here by having session
55 # I assume Python connections are thread safe interfaces
57 connection = self.connection()
59 l = connection.send(data)
65 class TcpClientRequestor(threading.Thread):
67 def __init__(self, dispatcher, session):
68 self.shared = dispatcher.shared
69 self.dispatcher = dispatcher
71 self.session = session
72 threading.Thread.__init__(self)
75 while not self.shared.stopped():
79 self.session.time = time.time()
96 return self.session.connection().recv(2048)
101 raw_buffer = self.message.find('\n')
105 raw_command = self.message[0:raw_buffer].strip()
106 self.message = self.message[raw_buffer + 1:]
107 if raw_command == 'quit':
112 command = json.loads(raw_command)
114 self.dispatcher.push_response({"error": "bad JSON", "request": raw_command})
118 # Try to load vital fields, and return an error if
120 message_id = command['id']
121 method = command['method']
123 # Return an error JSON in response.
124 self.dispatcher.push_response({"error": "syntax error", "request": raw_command})
126 self.dispatcher.push_request(self.session, command)
131 class TcpServer(threading.Thread):
133 def __init__(self, dispatcher, host, port, use_ssl, ssl_certfile, ssl_keyfile):
134 self.shared = dispatcher.shared
135 self.dispatcher = dispatcher.request_dispatcher
136 threading.Thread.__init__(self)
140 self.lock = threading.Lock()
141 self.use_ssl = use_ssl
142 self.ssl_keyfile = ssl_keyfile
143 self.ssl_certfile = ssl_certfile
147 print_log("TCP/SSL server started.")
149 print_log("TCP server started.")
150 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
151 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
152 sock.bind((self.host, self.port))
155 while not self.shared.stopped():
158 connection, address = sock.accept()
160 traceback.print_exc(file=sys.stdout)
165 session = TcpSession(connection, address, use_ssl=self.use_ssl, ssl_certfile=self.ssl_certfile, ssl_keyfile=self.ssl_keyfile)
166 except BaseException, e:
168 print_log("cannot start TCP session", error, address)
173 self.dispatcher.add_session(session)
174 self.dispatcher.collect_garbage()
175 client_req = TcpClientRequestor(self.dispatcher, session)