def test_decode_ack(self): """decoding a ack packet """ decoded_message = decode('6:::140') self.assertEqual(decoded_message, { 'type': 'ack', 'ackId': 140, 'endpoint': '', 'args': [] }) # Decode with endpoint decoded_message = decode('6::/chat:140') self.assertEqual(decoded_message, { 'type': 'ack', 'ackId': 140, 'endpoint': '/chat', 'args': [] }) # With args decoded_message = decode('6::/chat:140+["bob", "bob2"]') self.assertEqual( decoded_message, { 'type': 'ack', 'ackId': 140, 'endpoint': '/chat', 'args': [u"bob", u"bob2"] })
def test_decode_event(self): """decoding an event packet """ decoded_message = decode('5:::{"name":"woot", "args": ["foo"]}') self.assertEqual(decoded_message, { 'type': 'event', 'name': 'woot', 'endpoint': '', 'args': ["foo"] }) decoded_message = decode('5:::{"name":"woot"}') self.assertEqual(decoded_message, { 'type': 'event', 'name': 'woot', 'endpoint': '', 'args': [] }) # decoding an event packet with message id and ack decoded_message = decode('5:1+::{"name":"tobi"}') self.assertEqual( decoded_message, { 'type': 'event', 'id': 1, 'ack': 'data', 'name': 'tobi', 'endpoint': '', 'args': [] })
def test_decode_error(self): """decoding error packet """ decoded_message = decode('7:::') self.assertEqual(decoded_message, {'type': 'error', 'reason': '', 'advice': '', 'endpoint': ''}) decoded_message = decode('7:::0') self.assertEqual(decoded_message, {'type': 'error', 'reason': 'transport not supported', 'advice': '', 'endpoint': ''}) # decoding error packet with reason and advice decoded_message = decode('7:::2+0') self.assertEqual(decoded_message, {'type': 'error', 'reason': 'unauthorized', 'advice': 'reconnect', 'endpoint': ''}) # decoding error packet with endpoint decoded_message = decode('7::/woot') self.assertEqual(decoded_message, {'type': 'error', 'reason': '', 'advice': '', 'endpoint': '/woot'})
def test_decode_connect(self): """decoding a connection packet """ decoded_message = decode('1::/tobi') self.assertEqual(decoded_message, {'type': 'connect', 'endpoint': '/tobi', 'qs': '' }) # decoding a connection packet with query string decoded_message = decode('1::/test:?test=1') self.assertEqual(decoded_message, {'type': 'connect', 'endpoint': '/test', 'qs': '?test=1' })
def test_decode_message(self): """decoding a message packet """ decoded_message = decode('3:::woot') self.assertEqual(decoded_message, {'type': 'message', 'endpoint': '', 'data': 'woot'}) # decoding a message packet with id and endpoint decoded_message = decode('3:5:/tobi') self.assertEqual(decoded_message, {'type': 'message', 'id': 5, 'ack': True, 'endpoint': '/tobi', 'data': ''})
def test_decode_json(self): """decoding json packet """ decoded_message = decode('4:::"2"') self.assertEqual(decoded_message, {'type': 'json', 'endpoint': '', 'data': '2'}) # decoding json packet with message id and ack data decoded_message = decode('4:1+::{"a":"b"}') self.assertEqual(decoded_message, {'type': 'json', 'id': 1, 'endpoint': '', 'ack': 'data', 'data': {u'a': u'b'}})
def test_decode_heartbeat(self): """decoding a heartbeat packet """ decoded_message = decode('2:::') self.assertEqual(decoded_message, { 'type': 'heartbeat', 'endpoint': '' })
def test_decode_deconnect(self): """decoding a disconnection packet """ decoded_message = decode('0::/woot') self.assertEqual(decoded_message, { 'type': 'disconnect', 'endpoint': '/woot' })
def test_decode_ack(self): """decoding a ack packet """ decoded_message = decode('6:::140') self.assertEqual(decoded_message, {'type': 'ack', 'ackId': 140, 'endpoint': '', 'args': []})
def test_decode_event_error(self): """decoding an event packet """ decoded_message = decode('5:::') self.assertEqual(decoded_message, {'args': [], 'type': 'event', 'endpoint': '', })
def _receiver_loop(self): """This is the loop that takes messages from the queue for the server to consume, decodes them and dispatches them. """ while True: rawdata = self.get_server_msg() if not rawdata: continue # or close the connection ? try: pkt = packet.decode(rawdata, self.json_loads) except (ValueError, KeyError, Exception), e: self.error('invalid_packet', "There was a decoding error when dealing with packet " "with event: %s... (%s)" % (rawdata[:20], e)) continue if pkt['type'] == 'heartbeat': # This is already dealth with in put_server_msg() when # any incoming raw data arrives. continue if pkt['type'] == 'disconnect' and pkt['endpoint'] == '': # On global namespace, we kill everything. self.kill() continue endpoint = pkt['endpoint'] if endpoint not in self.namespaces: self.error("no_such_namespace", "The endpoint you tried to connect to " "doesn't exist: %s" % endpoint, endpoint=endpoint) continue elif endpoint in self.active_ns: pkt_ns = self.active_ns[endpoint] else: new_ns_class = self.namespaces[endpoint] pkt_ns = new_ns_class(self.environ, endpoint, request=self.request) pkt_ns.initialize() # use this instead of __init__, # for less confusion self.active_ns[endpoint] = pkt_ns retval = pkt_ns.process_packet(pkt) # Has the client requested an 'ack' with the reply parameters ? if pkt.get('ack') == "data" and pkt.get('id'): returning_ack = dict(type='ack', ackId=pkt['id'], args=retval, endpoint=pkt.get('endpoint', '')) self.send_packet(returning_ack) # Now, are we still connected ? if not self.connected: self.kill() # ?? what,s the best clean-up when its not a # user-initiated disconnect return
def test_decode_event(self): """decoding an event packet """ decoded_message = decode('5:::{"name":"woot"}') self.assertEqual(decoded_message, {'type': 'event', 'name': 'woot', 'endpoint': '', 'args': []}) # decoding an event packet with message id and ack decoded_message = decode('5:1+::{"name":"tobi"}') self.assertEqual(decoded_message, {'type': 'event', 'id': 1, 'ack': 'data', 'name': 'tobi', 'endpoint': '', 'args': []})
def test_decode_new_line(self): """test decoding newline """ decoded_message = decode('3:::\n') self.assertEqual(decoded_message, { 'type': 'message', 'data': '\n', 'endpoint': '' })
def test_except_on_invalid_message_type(self): """decoding a noop packet """ try: decoded_message = decode('99::') except Exception as e: self.assertEqual(e.message, "Unknown message type: 99") else: self.assertEqual(decoded_message, None, "We should not get a valid message")
def test_except_on_invalid_message_type(self): """decoding a noop packet """ try: decoded_message = decode('99::') except Exception as e: self.assertEqual(str(e), "Unknown message type: 99") else: self.assertEqual(decoded_message, None, "We should not get a valid message")
def test_decode_ack(self): """decoding a ack packet """ decoded_message = decode('6:::140') self.assertEqual(decoded_message, {'type': 'ack', 'ackId': 140, 'endpoint': '', 'args': []}) # Decode with endpoint decoded_message = decode('6::/chat:140') self.assertEqual(decoded_message, {'type': 'ack', 'ackId': 140, 'endpoint': '/chat', 'args': []}) # With args decoded_message = decode('6::/chat:140+["bob", "bob2"]') self.assertEqual(decoded_message, {'type': 'ack', 'ackId': 140, 'endpoint': '/chat', 'args': [u"bob", u"bob2"]})
def _receiver_loop(self): """This is the loop that takes messages from the queue for the server to consume, decodes them and dispatches them. """ while True: rawdata = self.get_server_msg() if not rawdata: continue # or close the connection ? try: pkt = packet.decode(rawdata) except (ValueError, KeyError, Exception), e: self.error('invalid_packet', "There was a decoding error when dealing with packet with event: %s... (%s)" % (rawdata[:20], e)) continue if pkt['type'] == 'heartbeat': # This is already dealth with in put_server_msg() when # any incoming raw data arrives. continue endpoint = pkt['endpoint'] if endpoint not in self.namespaces: self.error("no_such_namespace", "The endpoint you tried to connect to doesn't exist: %s" % endpoint, endpoint=endpoint) continue elif endpoint in self.active_ns: pkt_ns = self.active_ns[endpoint] else: new_ns_class = self.namespaces[endpoint] pkt_ns = new_ns_class(self.environ, endpoint, request=self.request) # Fire the initialize function for the namespace # This call ISN'T protected by ACLs! Beware! pkt_ns.call_method('initialize', pkt, pkt) self.active_ns[endpoint] = pkt_ns pkt_ns.process_packet(pkt) # Now, are we still connected ? if not self.connected: self.kill() # ?? what,s the best clean-up when its not a # user-initiated disconnect return
def test_decode_noop(self): """decoding a noop packet """ decoded_message = decode('8::') self.assertEqual(decoded_message, {'type': 'noop', 'endpoint': '' })
def test_decode_new_line(self): """test decoding newline """ decoded_message = decode('3:::\n') self.assertEqual(decoded_message, {'type': 'message', 'data': '\n', 'endpoint': ''})
def test_decode_noop(self): """decoding a noop packet """ decoded_message = decode('8::') self.assertEqual(decoded_message, {'type': 'noop', 'endpoint': ''})
def test_decode_heartbeat(self): """decoding a heartbeat packet """ decoded_message = decode('2:::') self.assertEqual(decoded_message, {'type': 'heartbeat', 'endpoint': '' })
def _receiver_loop(self): """This is the loop that takes messages from the queue for the server to consume, decodes them and dispatches them. """ while True: rawdata = self.get_server_msg() if not rawdata: continue # or close the connection ? try: pkt = packet.decode(rawdata, self.json_loads) except (ValueError, KeyError, Exception), e: self.error( 'invalid_packet', "There was a decoding error when dealing with packet " "with event: %s... (%s)" % (rawdata[:20], e)) continue if pkt['type'] == 'heartbeat': # This is already dealth with in put_server_msg() when # any incoming raw data arrives. continue if pkt['type'] == 'disconnect' and pkt['endpoint'] == '': # On global namespace, we kill everything. self.kill() continue endpoint = pkt['endpoint'] if endpoint not in self.namespaces: self.error("no_such_namespace", "The endpoint you tried to connect to " "doesn't exist: %s" % endpoint, endpoint=endpoint) continue elif endpoint in self.active_ns: pkt_ns = self.active_ns[endpoint] else: new_ns_class = self.namespaces[endpoint] pkt_ns = new_ns_class(self.environ, endpoint, request=self.request) pkt_ns.initialize() # use this instead of __init__, # for less confusion self.active_ns[endpoint] = pkt_ns retval = pkt_ns.process_packet(pkt) # Has the client requested an 'ack' with the reply parameters ? if pkt.get('ack') == "data" and pkt.get('id'): if type(retval) is tuple: args = list(retval) else: args = [retval] returning_ack = dict(type='ack', ackId=pkt['id'], args=retval, endpoint=pkt.get('endpoint', '')) self.send_packet(returning_ack) # Now, are we still connected ? if not self.connected: self.kill() # ?? what,s the best clean-up when its not a # user-initiated disconnect return
def test_decode_deconnect(self): """decoding a disconnection packet """ decoded_message = decode('0::/woot') self.assertEqual(decoded_message, {'type': 'disconnect', 'endpoint': '/woot' })
def _receiver_loop(self): """This is the loop that takes messages from the queue for the server to consume, decodes them and dispatches them. It is the main loop for a socket. We join on this process before returning control to the web framework. This process is not tracked by the socket itself, it is not going to be killed by the ``gevent.killall(socket.jobs)``, so it must exit gracefully itself. """ while True: rawdata = self.get_server_msg() if not rawdata: continue # or close the connection ? try: pkt = packet.decode(rawdata, self.json_loads) except (ValueError, KeyError, Exception), e: self.error('invalid_packet', "There was a decoding error when dealing with packet " "with event: %s... (%s)" % (rawdata[:20], e)) continue if pkt['type'] == 'heartbeat': # This is already dealt with in put_server_msg() when # any incoming raw data arrives. continue if pkt['type'] == 'disconnect' and pkt['endpoint'] == '': # On global namespace, we kill everything. self.kill(detach=True) continue with self.manager.lock_socket(self.sessid): endpoint = pkt['endpoint'] if endpoint not in self.namespaces: self.error("no_such_namespace", "The endpoint you tried to connect to " "doesn't exist: %s" % endpoint, endpoint=endpoint) continue elif endpoint in self.active_ns: pkt_ns = self.active_ns[endpoint] else: pkt_ns = self.add_namespace(endpoint) retval = pkt_ns.process_packet(pkt) # Has the client requested an 'ack' with the reply parameters ? if pkt.get('ack') == "data" and pkt.get('id'): if type(retval) is tuple: args = list(retval) else: args = [retval] returning_ack = dict(type='ack', ackId=pkt['id'], args=args, endpoint=pkt.get('endpoint', '')) self.send_packet(returning_ack) # Now, are we still connected ? if not self.connected: self.kill(detach=True) # ?? what,s the best clean-up # when its not a # user-initiated disconnect return
def _receiver_loop(self): """This is the loop that takes messages from the queue for the server to consume, decodes them and dispatches them. It is the main loop for a socket. We join on this process before returning control to the web framework. This process is not tracked by the socket itself, it is not going to be killed by the ``gevent.killall(socket.jobs)``, so it must exit gracefully itself. """ while True: rawdata = self.get_server_msg() if not rawdata: continue # or close the connection ? try: pkt = packet.decode(rawdata, self.json_loads) except (ValueError, KeyError, Exception): self.error( 'invalid_packet', "There was a decoding error when dealing with packet " "with event: %s... (%s)" % (rawdata[:20], e)) continue if pkt['type'] == 'heartbeat': # This is already dealth with in put_server_msg() when # any incoming raw data arrives. continue if pkt['type'] == 'disconnect' and pkt['endpoint'] == '': # On global namespace, we kill everything. self.kill(detach=True) continue endpoint = pkt['endpoint'] if endpoint not in self.namespaces: self.error("no_such_namespace", "The endpoint you tried to connect to " "doesn't exist: %s" % endpoint, endpoint=endpoint) continue elif endpoint in self.active_ns: pkt_ns = self.active_ns[endpoint] else: new_ns_class = self.namespaces[endpoint] pkt_ns = new_ns_class(self.environ, endpoint, request=self.request) # This calls initialize() on all the classes and mixins, etc.. # in the order of the MRO for cls in type(pkt_ns).__mro__: if hasattr(cls, 'initialize'): cls.initialize(pkt_ns) # use this instead of __init__, # for less confusion self.active_ns[endpoint] = pkt_ns retval = pkt_ns.process_packet(pkt) # Has the client requested an 'ack' with the reply parameters ? if pkt.get('ack') == "data" and pkt.get('id'): if type(retval) is tuple: args = list(retval) else: args = [retval] returning_ack = dict(type='ack', ackId=pkt['id'], args=args, endpoint=pkt.get('endpoint', '')) self.send_packet(returning_ack) # Now, are we still connected ? if not self.connected: self.kill(detach=True) # ?? what,s the best clean-up # when its not a # user-initiated disconnect return