Example #1
0
 def on_handshake(self,client_info):
     yield gen.listen(self._connection.on_handshake(client_info),self._connection.exception_callback)
     self.send_raw_message("%s:%i:%i:%s" % (
         self.id(),
         self._heartbeat_interval,
         self._cleanup_interval,
         ",".join(self._route.get_allowed_transports())
         )
     )
     session.Instance.on_handshake(self,client_info)
Example #2
0
 def on_handshake(self,client_info):
     result = yield listen(self._connection.on_handshake(client_info))
     if not isinstance(result,Exception):
         session.Instance.on_handshake(self,client_info)
         self.on_open()
Example #3
0
    def _parse_message(self,message):
        parts = message.split(':', 3)
        if len(parts) == 3:
            msg_type, msg_id, msg_endpoint = parts
            msg_data = None
        else:
            msg_type, msg_id, msg_endpoint, msg_data = parts

        if msg_type == '1':
            if not msg_endpoint:
                self.on_open_connection(self._connection)
                return
            conn = self.get_sub_connection(msg_endpoint)
            if conn:
                raise exception.Error("Subroute allready opened!")
            parsed = urlparse.urlparse(msg_endpoint)
            cl = self._sub_routes.get(parsed.path)
            if not cl:
                raise exception.SubRouteNotFound(msg_endpoint)
            conn = cl(self,msg_endpoint,self._connection,**self._kwargs)
            args = urlparse.parse_qs(parsed.query,keep_blank_values=1)
            info = connection.Info(self._client_info.ip,args,self._client_info.cookies)
            yield gen.listen(conn.on_handshake(info),conn.exception_callback)
            self._sub_connections[msg_endpoint] = conn
            self.on_open_connection(conn)
            return
        elif msg_type == '2':
            self._missed_heartbeats = 0
            return

        if msg_endpoint:
            conn = self.get_sub_connection(msg_endpoint)
            if not conn:
                raise exception.SubRouteNotFound(msg_endpoint)
        else:
            conn = self._connection

        if msg_type == '0':
            self.close(conn)
            return
        if msg_type == '4':
            # JSON message. Unserialize and change message type
            msg_data = json.loads(msg_data)
            msg_type = '3'

        if msg_type == '3':
            result = yield gen.listen(conn.on_message(msg_data),conn.exception_callback)
            if msg_id:
                self.send_ack(conn, msg_id, result)
        elif msg_type == '5':
            if not msg_data:
                raise exception.MessageFormatUnexpected
            event = json.loads(msg_data)
            if 'args' not in event:
                event['args'] = []
            result = yield gen.listen(conn.on_event(event['name'],event['args']),conn.exception_callback)
            if msg_id:
                if msg_id.endswith('+'):
                    msg_id = msg_id[:-1]
                self.send_ack(conn,msg_id,result)
            pass
        elif msg_type == '6':
            args = []
            parts = msg_data.split('+',1)
            if len(parts) is 2:
                msg_id, msg_data = parts
            else:
                msg_id, msg_data = parts[0], None
            if msg_data:
                args = json.loads(msg_data)
            self.final_ack(conn, msg_id, args)
            pass
        elif msg_type == '7':
            #error
            pass
        else:
            raise exception.MessageFormatUnexpected(message)