def _connect_to_global_db(self): ''' WebSocket to global idb. ''' while True: try: # Connects to global idb self.client = yield tornado.websocket.websocket_connect( WEBSOCKET_PUBSUB_URL.format(self.ghost, self.gport)) # Sets WebSocketSubscriber to self self.subscriber = WebSocketSubscriber(SCOPE.GLOBAL, self.client) # Subscribe forwarder def _on_subscribe(path, scope): self._send_subscribe(path, scope) self.subscriber.on_subscribe = _on_subscribe tega.idb.add_subscribe_forwarder(self.subscriber) # Sends SESSION to global idb. self.client.write_message('SESSION {} {}'.format( self.server_tega_id, SCOPE.GLOBAL.value)) # Sends SUBSCRIBE to global idb. for root_oid in self.config: self._send_subscribe(root_oid, SCOPE.GLOBAL) # (re-)sends SUBSCRIBE <global channel> <scope> # to global idb. global_channels = tega.idb.get_global_channels() for channel, scope in global_channels.items(): self._send_subscribe(channel, scope) break except socket.error as e: logging.warning('global idb does not seem to be running...') time.sleep(CONNECT_RETRY_TIMER) except Exception as e: traceback.print_exc()
def __init__(self, host=HOST, port=PORT, tega_id=None, subscriber=None): ''' HTTP connection initialization ''' self.conn = httplib2.Http() self.base_url = 'http://{}:{}'.format(host, port) self.txid = None self.pubsub_url = WEBSOCKET_PUBSUB_URL.format(host, port) self._tega_id = tega_id self.client = None self.parser = build_parser('driver') self._urlencode = _make_urlencode(self.base_url) if subscriber: self.subscriber = subscriber self._tega_id = subscriber.tega_id elif tega_id: self._tega_id = tega_id else: self._tega_id = str(uuid.uuid4()) for cmd in ('roots', 'old', 'channels', 'subscribers', 'ids', 'global', 'forwarders', 'plugins', 'reload'): setattr(self, cmd, self._build_cmd(cmd))