def on_show(self): '''Bind queues, set consuming and listen. ''' # Clear listboxes self.listbox_opened.delete(0, Tkinter.END) self.listbox_closed.delete(0, Tkinter.END) # Binding queues self.channel.queue_bind(exchange='direct_logs', queue=self.game_advertisements, routing_key=common.make_key_games( self.server_name)) self.channel.queue_bind(exchange='direct_logs', queue=self.game_advertisements, routing_key=common.make_key_game_advert( self.server_name)) self.channel.queue_bind(exchange='direct_logs', queue=self.client_queue, routing_key=self.client_queue) # Set consuming self.channel.basic_consume(self.update, queue=self.game_advertisements, no_ack=True) self.channel.basic_consume(self.on_response, queue=self.client_queue, no_ack=True) # Listening self.listening_thread = listen(self.channel, 'lobby') # Get list of games self.get_games_list()
def remove_game(self, name): '''Remove game from the dict of games. @param name: name of game ''' try: del self.games[name] # Send event about removed game msg = serverlib.make_e_game_end(name) send_message(self.channel, msg, common.make_key_game_advert(self.server_name)) except KeyError: pass
def run(self): '''Method for running the thread. ''' # Connection self.connection = pika.BlockingConnection( pika.ConnectionParameters(host=self.host, port=self.port)) self.channel = self.connection.channel() self.channel.exchange_declare(exchange='direct_logs', type='direct') # Routing keys self.key_game = common.make_key_game(self.server_name, self.name) self.key_events = common.make_key_game_events(self.server_name, self.name) self.key_adverts = common.make_key_game_advert(self.server_name) # Game queue self.game_queue =\ self.channel.queue_declare(exclusive=True).method.queue self.channel.queue_bind(exchange='direct_logs', queue=self.game_queue, routing_key=self.key_game) self.channel.basic_consume(self.reply_request, queue=self.game_queue, no_ack=True) # Spectator queue self.spectator_queue =\ self.channel.queue_declare(exclusive=True).method.queue self.channel.queue_bind(exchange='direct_logs', queue=self.spectator_queue, routing_key=self.spectator_queue) # Control queue for quiting consuming self.control_queue =\ self.channel.queue_declare(exclusive=True).method.queue self.channel.queue_bind(exchange='direct_logs', queue=self.control_queue, routing_key=self.control_queue) self.channel.basic_consume(self.quit_game, queue=self.control_queue, no_ack=True) self.ready_event.set() self.channel.start_consuming() self.game_list.remove_game(self.name)
def add_game(self, name, owner, width, height): '''Add game to the dict of games. @param name: name of game @param owner: owner of game @param width: width of field of game @param height: height of field of game ''' # Create game in new thread game = Game(self, self.server_args, name, owner, width, height) self.games[name] = game game.start() # Send event about added game msg = serverlib.make_e_game_open(name) send_message(self.channel, msg, common.make_key_game_advert(self.server_name)) return game
def on_hide(self): '''Unbind queues, stop consuming. ''' # Unbinding queues self.channel.queue_unbind(exchange='direct_logs', queue=self.game_advertisements, routing_key=common.make_key_games( self.server_name)) self.channel.queue_unbind(exchange='direct_logs', queue=self.game_advertisements, routing_key=common.make_key_game_advert( self.server_name)) self.channel.queue_unbind(exchange='direct_logs', queue=self.client_queue, routing_key=self.client_queue) # Stop consuming if threading.current_thread() == self.listening_thread: self.channel.stop_consuming() else: LOG.error('LobbyWindow.on_hide called from non-listening thread.')