コード例 #1
0
ファイル: server.py プロジェクト: lisidan/tega
 def get(self, id):
     '''
     GET(read) operation
     '''
     version = self.get_argument('version', None)
     internal = self.get_argument('internal', None)
     txid = self.get_argument('txid', None)
     tega_id = self.get_argument('tega_id')
     regex_flag = str2bool(self.get_argument('regex_flag', False))
     if version:
         version = int(version)
     internal = str2bool(internal)
     path = url2path(id)
     value = None
     try:
         value = tega.idb.get(url2path(id), version=version,
                 regex_flag=regex_flag)
         if isinstance(value, Cont) or is_builtin_type(value):
             self.write(value.dumps_(internal=internal))
         elif isinstance(value, list):
             dict_ = {v[0]: {'groups':v[2],
                 'instance':v[1].serialize_(internal=internal)} for v in
                 value}
             self.write(json.dumps(dict_))
         else:
             self.write(json.dumps(value))
         self.set_header('Content-Type', 'application/json')
     except KeyError:
         logging.info('path "{}" not found in global idb'.format(path))
         raise tornado.web.HTTPError(404)  # Not Found(404)
コード例 #2
0
ファイル: server.py プロジェクト: lisidan/tega
    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)