diff options
-rw-r--r-- | server/socket.py | 55 | ||||
-rw-r--r-- | web/main.js | 10 |
2 files changed, 45 insertions, 20 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): diff --git a/web/main.js b/web/main.js index a4dbf08..37729b9 100644 --- a/web/main.js +++ b/web/main.js @@ -443,16 +443,16 @@ var connectPython = function connectPythonFunc() { pythonClient.on('connect', function () { // login to the python server by sending it the list of currently handled sessions - var sid, sessions = [], packet; + var sid, sessionList = [], packet; logger.debug('Connection to Python established'); for (sid in sessions) { - if (sessions.hasOwnProperty(sid)) { - sessions.push({'sid': sid, 'lastActivity': sessions[sid].lastActivity}); + if (sessionList.hasOwnProperty(sid)) { + sessionList.push({'sid': sid, 'lastActivity': sessionList[sid].lastActivity}); } } // this should/must succeed in sending right now, as it is the first write ever made on the socket - packet = JSON.stringify({'type': 'connect', 'sessions': sessions}); - packet = packet.length + ':' + packet; + packet = JSON.stringify({'type': 'connect', 'sessions': sessionList}); + packet = new Buffer(Buffer.byteLength(packet) + ':' + packet); sendPacketToPython(packet); }); |