class BoardsController(object): def __init__(self): self.boards = {} self.message_handler = MessageHandler(self) self.message_parser = MessageParser(self.message_handler) def register(self, channel_id, handle): if channel_id in self.boards.keys(): self.boards[channel_id].append(handle) else: self.boards[channel_id] = [handle] def unregister(self, handle, channel_id=None): if channel_id is None: for channel_id in self.boards: if handle in self.boards[channel_id]: self.boards[channel_id].remove(handle) self.message_handler.publish(Message("disconnect"),channel_id) else: self.boards[channel_id].remove(handle) self.message_handler.publish(Message("disconnect"),channel_id) def get_board_handles(self, channel_id): return self.boards.get(channel_id) def count_users(self, channel_id): if channel_id in self.boards.keys(): return len(self.boards.get(channel_id)) return 0 def on_message(self, handle, message): self.message_parser.parse(handle, message)
def __init__(self, redis_host='localhost', redis_port=6379, redis_db=0, redis_password=None, redis_client=None): self.id = uuid.uuid4() if redis_client is not None: self.r = redis_client else: self.r = redis.Redis(host=redis_host, port=redis_port, db=redis_db, password=redis_password) self.handler = MessageHandler(self.r, 'rollout') self.handler.register(ExitMessage, self.exit) self.handler.register(PingMessage, self.handle_ping) self.retry_count = 0 self._heartbeat = None
class BoardsController(object): # Generate a Boards instance def __init__(self): self.boards = {} self.message_handler = MessageHandler(self) self.message_parser = MessageParser(self.message_handler) def register(self, decoded_message, handle): channel_id = decoded_message['args']['channel_id'] if channel_id in self.boards.keys(): self.boards[channel_id][handle] = decoded_message['args']['user'] else: self.boards[channel_id] = {} self.boards[channel_id][handle] = decoded_message['args']['user'] # Remove a board to the Boards instances def unregister(self, handle, channel_id=None): if channel_id is None: for channel_id in self.boards: if handle in self.boards[channel_id]: self.message_handler.publish( Message("disconnect", self.boards[channel_id][handle]), channel_id) del self.boards[channel_id][handle] else: self.message_handler.publish( Message("disconnect", self.boards[channel_id][handle]), channel_id) del self.boards[channel_id][handle] # Get a specific board def get_board_handles(self, channel_id): return self.boards.get(channel_id) def get_board_users(self, channel_id): return self.boards.get(channel_id).values() # Get number of users that are in the board at the moment def count_users(self, channel_id): if channel_id in self.boards.keys(): return len(self.boards.get(channel_id)) return 0 def on_message(self, handle, message): self.message_parser.parse(handle, message)
class BoardsController(object): # Generate a Boards instance def __init__(self): self.boards = {} self.message_handler = MessageHandler(self) self.message_parser = MessageParser(self.message_handler) def register(self, decoded_message, handle): channel_id = decoded_message['args']['channel_id'] if channel_id in self.boards.keys(): self.boards[channel_id][handle] = decoded_message['args']['user'] else: self.boards[channel_id] = {} self.boards[channel_id][handle] = decoded_message['args']['user'] # Remove a board to the Boards instances def unregister(self, handle, channel_id=None): if channel_id is None: for channel_id in self.boards: if handle in self.boards[channel_id]: self.message_handler.publish(Message("disconnect",self.boards[channel_id][handle]),channel_id) del self.boards[channel_id][handle] else: self.message_handler.publish(Message("disconnect",self.boards[channel_id][handle]),channel_id) del self.boards[channel_id][handle] # Get a specific board def get_board_handles(self, channel_id): return self.boards.get(channel_id) def get_board_users(self, channel_id): return self.boards.get(channel_id).values() # Get number of users that are in the board at the moment def count_users(self, channel_id): if channel_id in self.boards.keys(): return len(self.boards.get(channel_id)) return 0 def on_message(self, handle, message): self.message_parser.parse(handle, message)
class Server: def __init__(self, redis_host='localhost', redis_port=6379, redis_db=0, redis_password=None, redis_client=None): self.id = uuid.uuid4() if redis_client is not None: self.r = redis_client else: self.r = redis.Redis(host=redis_host, port=redis_port, db=redis_db, password=redis_password) self.handler = MessageHandler(self.r, 'rollout') self.handler.register(ExitMessage, self.exit) self.handler.register(PingMessage, self.handle_ping) self.retry_count = 0 self._heartbeat = None def main(self): while self.retry_count < 10: try: self.retry_count = 0 self.handler.listen() except redis.exceptions.ConnectionError as e: logger.error(e) self.retry_count += 1 sleep(self.retry_count) continue except Exception as e: logger.error(e) logger.debug(traceback.format_exc()) self.retry_count += 1 continue def exit(self, msg): raise SystemExit def handle_ping(self, msg): PongMessage(self.id, type(self).__name__).send(self.r) def start_heartbeat(self, heartbeat_freq_seconds, func, **kwargs): self._heartbeat = ServerHeartBeat(heartbeat_freq_seconds, func, **kwargs) self._heartbeat.start()
def __init__(self): self.boards = {} self.message_handler = MessageHandler(self) self.message_parser = MessageParser(self.message_handler)