def __init__(self, url, session): self.url = url self.__hubs = {} self.qs = {} self.__send_counter = -1 self.token = None self.data = None self.received = EventHook() self.error = EventHook() self.starting = EventHook() self.stopping = EventHook() self.__transport = AutoTransport(session, self) self.__greenlet = None self.started = False def handle_error(**kwargs): error = kwargs["E"] if "E" in kwargs else None if error is None: return self.error.fire(error) self.received += handle_error self.starting += self.__set_data
def __init__(self, url, session): self.url = url self.__hubs = {} self.qs = {} self.__send_counter = -1 self.token = None self.id = None self.data = None self.received = EventHook() self.error = EventHook() self.starting = EventHook() self.stopping = EventHook() self.exception = EventHook() self.is_open = False self.__transport = AutoTransport(session, self) self.__listener_thread = None self.started = False def handle_error(**kwargs): error = kwargs["E"] if "E" in kwargs else None if error is None: return self.error.fire(error) self.received += handle_error self.starting += self.__set_data
class Connection: protocol_version = '1.5' def __init__(self, url, session): self.url = url self.__hubs = {} self.qs = {} self.__send_counter = -1 self.token = None self.id = None self.data = None self.received = EventHook() self.error = EventHook() self.starting = EventHook() self.stopping = EventHook() self.exception = EventHook() self.is_open = False self.__transport = AutoTransport(session, self) self.__listener_thread = None self.started = False def handle_error(**kwargs): error = kwargs["E"] if "E" in kwargs else None if error is None: return self.error.fire(error) self.received += handle_error self.starting += self.__set_data def __set_data(self): self.data = json.dumps([{ 'name': hub_name } for hub_name in self.__hubs]) def increment_send_counter(self): self.__send_counter += 1 return self.__send_counter def start(self): self.starting.fire() negotiate_data = self.__transport.negotiate() self.token = negotiate_data['ConnectionToken'] self.id = negotiate_data['ConnectionId'] listener = self.__transport.start() def wrapped_listener(): while self.is_open: try: listener() except: self.exception.fire(*sys.exc_info()) self.is_open = False self.is_open = True self.__listener_thread = Thread(target=wrapped_listener) self.__listener_thread.start() self.started = True def wait(self, timeout=30): Thread.join(self.__listener_thread, timeout) def send(self, data): self.__transport.send(data) def close(self): self.is_open = False self.__listener_thread.join() self.__transport.close() def register_hub(self, name): if name not in self.__hubs: if self.started: raise RuntimeError( 'Cannot create new hub because connection is already started.' ) self.__hubs[name] = Hub(name, self) return self.__hubs[name] def hub(self, name): return self.__hubs[name] def __enter__(self): self.start() return self def __exit__(self, exc_type, exc_val, exc_tb): self.close()
class Connection: protocol_version = '1.5' def __init__(self, url, session): self.url = url self.__hubs = {} self.__send_counter = -1 self.token = None self.data = None self.received = EventHook() self.error = EventHook() self.starting = EventHook() self.stopping = EventHook() self.__transport = AutoTransport(session, self) self.__greenlet = None self.started = False def handle_error(**kwargs): error = kwargs["E"] if "E" in kwargs else None if error is None: return self.error.fire(error) self.received += handle_error self.starting += self.__set_data def __set_data(self): self.data = json.dumps([{'name': hub_name} for hub_name in self.__hubs]) def increment_send_counter(self): self.__send_counter += 1 return self.__send_counter def start(self): self.starting.fire() negotiate_data = self.__transport.negotiate() self.token = negotiate_data['ConnectionToken'] listener = self.__transport.start() def wrapped_listener(): listener() gevent.sleep(0) self.__greenlet = gevent.spawn(wrapped_listener) self.started = True def wait(self, timeout=30): gevent.joinall([self.__greenlet], timeout) def send(self, data): self.__transport.send(data) def close(self): gevent.kill(self.__greenlet) self.__transport.close() def register_hub(self, name): if name not in self.__hubs: if self.started: raise RuntimeError( 'Cannot create new hub because connection is already started.') self.__hubs[name] = Hub(name, self) return self.__hubs[name] def hub(self, name): return self.__hubs[name] def __enter__(self): self.start() return self def __exit__(self, exc_type, exc_val, exc_tb): self.close()
def on(self, method, handler): if method not in self.__handlers: self.__handlers[method] = EventHook() self.__handlers[method] += handler
def __init__(self, name, connection): self.name = name self.server = HubServer(name, connection, self) self.client = HubClient(name, connection) self.error = EventHook()