Example #1
0
    def _route_rpc_response(self, param, body):
        global subscriber_clients
        tega_id = param[2]

        # RESPONSE back to global idb
        if not tega.idb.is_subscribe_forwarder(tega_id):
            on_response(param, body)
        # RESPONSE to be forwarded to local idb
        else:
            seq_no = int(param[0])
            _subscriber = subscriber_clients[tega_id]
            _subscriber.write_message('RESPONSE {} {} {}\n{}'.
                    format(seq_no,
                           REQUEST_TYPE.RPC.name,
                           tega_id,
                           json.dumps(body)))
Example #2
0
    def _process_message(self, msg):
        '''
        Handles a message from global idb.
        '''
        cmd, param, body = self.parser(msg)

        if cmd == 'SESSIONACK':
            remote_tega_id = param
            self.subscriber.tega_id = remote_tega_id
            tega.idb.add_tega_id(remote_tega_id)
        elif cmd == 'NOTIFY':
            tega.idb.crud_batch(body, self.subscriber)
        elif cmd == 'MESSAGE':
            channel = param[0]
            tega_id = param[1]
            tega.idb.publish(channel, tega_id,
                    body['message'], self.subscriber)
        elif cmd == 'SUBSCRIBE':
            channel = param[0]
            scope = SCOPE(param[1])  # Ignored
            tega.idb.subscribe(self.subscriber, channel)
        elif cmd == 'UNSUBSCRIBE':
            if param:
                tega.idb.unsubscribe(self.subscriber, param)
            else:
                tega.idb.unsubscribe_all(self.subscriber)
        elif cmd == 'REQUEST':
            seq_no = param[0]
            type_ = param[1]
            tega_id = param[2]
            path = param[3]
            if REQUEST_TYPE(type_) == REQUEST_TYPE.RPC:
                args, kwargs = parse_rpc_body(body)
                result = tega.idb.rpc(path, args, kwargs)
                self.client.write_message('RESPONSE {} {} {}\n{}'.
                        format(seq_no,
                               REQUEST_TYPE.RPC.name,
                               tega_id,
                               json.dumps(result)))
        elif cmd == 'RESPONSE':
            on_response(param, body)
Example #3
0
    def on_message(self, msg):
        '''
        Handles SESSION/SUBSCRIBE/UNSUBSCRIBE request from a subscriber.
        '''
        cmd, param, body = self.parser(msg)

        if cmd == 'SESSION':
            self.tega_id = param[0]  # tega ID from a tega client
            scope = SCOPE(param[1])
            self.subscriber = WebSocketSubscriber(scope, self)
            self.subscriber.tega_id = self.tega_id
            if scope == SCOPE.GLOBAL:
                def _on_subscribe(path, scope):
                    self.write_message(
                            'SUBSCRIBE {} {}'.format(path, scope.value))
                self.subscriber.on_subscribe = _on_subscribe
                tega.idb.add_subscribe_forwarder(self.subscriber)
            subscriber_clients[self.tega_id] = self.subscriber
            tega.idb.add_tega_id(self.tega_id)
            self.write_message('SESSIONACK {}'.format(server_tega_id))
        elif cmd == 'SUBSCRIBE':
            if param:
                channel = param[0]
                scope = SCOPE(param[1])
                regex_flag = str2bool(param[2])
                tega.idb.subscribe(self.subscriber, channel, scope, regex_flag)
            else:
                logging.warn('WebSocket(server): no channel indicated in SUBSCRIBE request')
        elif cmd == 'UNSUBSCRIBE':
            if param:
                channel = param[0]
                regex_flag = str2bool(param[1])
                tega.idb.unsubscribe(self.subscriber, channel, regex_flag)
                self.write_message('UNSUBSCRIBE {}'.format(param))
            else:
                tega.idb.unsubscribe_all(self.subscriber)
                self.write_message('UNSUBSCRIBE')
        elif cmd == 'PUBLISH':
            tega.idb.publish(param, self.tega_id, body['message'],
                    self.subscriber)
        elif cmd == 'NOTIFY':
            tega.idb.crud_batch(body, self.subscriber)
        elif cmd == 'MESSAGE':
            channel = param[0]
            tega_id = param[1]
            tega.idb.publish(channel, tega_id, body['message'], self.subscriber)
        elif cmd == 'REQUEST':
            type_ = REQUEST_TYPE(param[1])
            if type_ == REQUEST_TYPE.RPC:
                self._route_rpc_request(param, body)
            elif type_ == REQUEST_TYPE.SYNC:
                self._sync_request(param, body)
            elif type_ == REQUEST_TYPE.REFER:
                self._refer_request(param, body)
        elif cmd == 'RESPONSE':
            type_ = REQUEST_TYPE(param[1])
            if type_ == REQUEST_TYPE.RPC:
                self._route_rpc_response(param, body)
            elif type_ == REQUEST_TYPE.SYNC:
                on_response(param, body)
            elif type_ == REQUEST_TYPE.REFER:
                on_response(param, body)