diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/socket.py | 55 |
1 files changed, 40 insertions, 15 deletions
diff --git a/server/socket.py b/server/socket.py index bdf99bf..ae2068f 100644 --- a/server/socket.py +++ b/server/socket.py @@ -17,26 +17,51 @@ _sessions_to_socket = {} # keyed by sid: what session is bound to what socket _transactions_to_socket = {} # keyed by tid, used only when there is no sid available, so a request can be replied +def handle_connect(data): + """The first packet is 'connect', providing the list of connected sessions to the peer (that's us).""" + pass + + +def handle_unregister(data): + """When the client disconnects from the peer, but doesn't logout first, + leaving the possibility that it will reconnect in the same session, + possibly via other channel (nodejs instance).""" + pass + + +# TODO: meta handlers implement system protocol to support horizontal scaling, implement them when it becomes relevant +_meta_handlers = { + 'connect': handle_connect, + 'unregister': handle_unregister +} + + def processIncomingPacket(receiving_socket, packet): logging.debug('Decoding JSON: {}'.format(packet)) obj = json.loads(packet) req_type = obj.get('type') # private (meta) requests have the 'type' - if req_type == 'connect': - return # TODO: first packet is 'connect', providing the list of connected sessions to the peer - - tid = obj.get('tid') # transaction ID - if tid is None: - raise Exception('Transaction ID is missing from the request') - sid = obj.get('sid') # session ID - _mapping_lock.acquire() - try: - if sid is None: - _transactions_to_socket[tid] = receiving_socket + if req_type is None: + # user protocol, internally map the request so response can be delivered correctly, and forward it to the higher layer + tid = obj.get('tid') # transaction ID + if tid is None: + raise Exception('Transaction ID is missing from the request') + sid = obj.get('sid') # session ID + _mapping_lock.acquire() + try: + if sid is None: + _transactions_to_socket[tid] = receiving_socket + else: + _sessions_to_socket[sid] = receiving_socket + finally: + _mapping_lock.release() + serve_request(obj) + else: + # system protocol, connection-specific meta data + handler = _meta_handlers.get(req_type) + if handler is None: + logging.error('Received an unknown meta packet: {}'.format(req_type)) else: - _sessions_to_socket[sid] = receiving_socket - finally: - _mapping_lock.release() - serve_request(obj) + handler(obj) def sendPacket(tid, sid, json_obj): |