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)
16 self.use_ssl = use_ssl
19 self._connection = ssl.wrap_socket(
22 certfile=ssl_certfile,
24 ssl_version=ssl.PROTOCOL_SSLv23,
25 do_handshake_on_connect=False)
27 self._connection = connection
29 self.address = address[0]
30 self.name = "TCP " if not use_ssl else "SSL "
32 def do_handshake(self):
34 self._connection.do_handshake()
38 raise Exception("Session was stopped")
40 return self._connection
47 self._connection.shutdown(socket.SHUT_RDWR)
49 # print_log("problem shutting down", self.address)
50 # traceback.print_exc(file=sys.stdout)
53 self._connection.close()
57 def send_response(self, response):
58 data = json.dumps(response) + "\n"
59 # Possible race condition here by having session
61 # I assume Python connections are thread safe interfaces
63 connection = self.connection()
65 l = connection.send(data)
71 class TcpClientRequestor(threading.Thread):
73 def __init__(self, dispatcher, session):
74 self.shared = dispatcher.shared
75 self.dispatcher = dispatcher
77 self.session = session
78 threading.Thread.__init__(self)
82 self.session.do_handshake()
86 while not self.shared.stopped():
90 self.session.time = time.time()
107 return self.session.connection().recv(2048)
112 raw_buffer = self.message.find('\n')
116 raw_command = self.message[0:raw_buffer].strip()
117 self.message = self.message[raw_buffer + 1:]
118 if raw_command == 'quit':
123 command = json.loads(raw_command)
125 self.dispatcher.push_response({"error": "bad JSON", "request": raw_command})
129 # Try to load vital fields, and return an error if
131 message_id = command['id']
132 method = command['method']
134 # Return an error JSON in response.
135 self.dispatcher.push_response({"error": "syntax error", "request": raw_command})
137 self.dispatcher.push_request(self.session, command)
142 class TcpServer(threading.Thread):
144 def __init__(self, dispatcher, host, port, use_ssl, ssl_certfile, ssl_keyfile):
145 self.shared = dispatcher.shared
146 self.dispatcher = dispatcher.request_dispatcher
147 threading.Thread.__init__(self)
151 self.lock = threading.Lock()
152 self.use_ssl = use_ssl
153 self.ssl_keyfile = ssl_keyfile
154 self.ssl_certfile = ssl_certfile
157 print_log( ("SSL" if self.use_ssl else "TCP") + " server started on port %d"%self.port)
158 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
159 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
160 sock.bind((self.host, self.port))
163 while not self.shared.stopped():
166 connection, address = sock.accept()
168 traceback.print_exc(file=sys.stdout)
173 session = TcpSession(connection, address, use_ssl=self.use_ssl, ssl_certfile=self.ssl_certfile, ssl_keyfile=self.ssl_keyfile)
174 except BaseException, e:
176 print_log("cannot start TCP session", error, address)
181 self.dispatcher.add_session(session)
182 self.dispatcher.collect_garbage()
183 client_req = TcpClientRequestor(self.dispatcher, session)