def joinGameInstance(login, password, gameName, instanceName, observationKey=None): if observationKey is None: logging.info("GameServer] " + login + " joining game instance " + gameName + " " + instanceName) else: logging.info("GameServer] " + login + " joining game instance " + gameName + " " + instanceName + " with observation key " + observationKey) # if instance doesn't exist vhost = RabbitMQConfiguration().getRabbitMQProperty( "virtualHostSeparator") + gameName + RabbitMQConfiguration( ).getRabbitMQProperty("virtualHostSeparator") + instanceName if vhost not in gameInstanceManagementDataDict.keys(): logging.warning("GameServer] Game instance " + gameName + " " + instanceName + "doesn't exist, joining canceled ") return False else: gameinstance = gameInstanceManagementDataDict[vhost] set_permissions_vhost_participant(vhost, login, password) if observationKey is None: gameinstance.queueRequest.put(["join", login]) else: gameinstance.queueRequest.put( ["joinWithObservationKey", login, observationKey]) item = gameinstance.queueReturn.get() logging.info("GameServer] " + login + " has joined the game instance " + gameName + " " + instanceName + ".") return item[0]
def handle_delivery_gamelogic(self, channel, method, header, body): seqBody = body.split( RabbitMQConfiguration().getRabbitMQProperty("bodySeparator")) try: key = method.routing_key except: logging.debug("GameLogicServer] message ignored" + body + " " + method) logging.debug("GameLogicServer] Received on /instance " + body + " with key " + key) seqKey = key.split( RabbitMQConfiguration().getRabbitMQProperty("routingKeySeparator")) actionKind = seqKey[2] action = seqKey[3] for i in listofactions.ListOfActions.reverseLookup: if actionKind == listofactions.ListOfActions.whatis(i): for j in listofactions.ListOfActions.whichenuminstance( i).reverseLookup: if action == listofactions.ListOfActions.whichenuminstance( i).whatis(j): listofactions.ListOfActions.whichenuminstance( i).whichfunction(j)(self.state, seqKey, body) return for i in self.state.gamelogiclistofactions.reverseLookup: if actionKind == self.state.gamelogiclistofactions.whatis(i): for j in self.state.gamelogiclistofactions.whichenuminstance( i).reverseLookup: if action == self.state.gamelogiclistofactions.whichenuminstance( i).whatis(j): self.state.gamelogiclistofactions.whichenuminstance( i).whichfunction(j)(self.state, seqKey, body) return
def terminateGameInstance(gameName, instanceName): logging.info("GameServer] Terminating game instance " + gameName + " " + instanceName) # if instance doesn't exist vhost = RabbitMQConfiguration().getRabbitMQProperty( "virtualHostSeparator") + gameName + RabbitMQConfiguration( ).getRabbitMQProperty("virtualHostSeparator") + instanceName if vhost not in gameInstanceManagementDataDict.keys(): logging.warning("GameServer] Game instance " + gameName + " " + instanceName + "doesn't exist, termination canceled ") return False else: # return the game instance, and remove it from the dictionary gameinstance = gameInstanceManagementDataDict.pop(vhost) gameinstance.queueRequest.put(["terminate"]) logging.debug( "GameServer] Wait for the end of game logic server for " + gameName + " " + instanceName) gameinstance.process.join() while not gameinstance.queueRequest.empty(): #print "queue request:", gameinstance.queueRequest.get() logging.debug("GameServer] queue request:" + str(gameinstance.queueRequest.get())) while not gameinstance.queueReturn.empty(): #print "queue return:", gameinstance.queueReturn.get() logging.debug("GameServer] queue return:" + str(gameinstance.queueReturn.get())) return True
def on_channel_open(self, new_channel): logging.debug("GameLogicServer] Channel on /instance opened") self.channel = new_channel self.channel.exchange_declare( exchange=RabbitMQConfiguration().getRabbitMQProperty( "gameLogicServerExchangeName"), type=RabbitMQConfiguration().getRabbitMQProperty( "gameLogicServerExchangeType"), callback=self.on_exchange_declared)
def publish(self, consumer, state, action, message): logging.debug("GameLogicServer] Worker - publish message " + message + " with action " + action + " to consumer " + consumer) routingKey = RabbitMQConfiguration().getRabbitMQProperty( "gameLogicServerUserName") + "." + consumer + "." + action self.channel.basic_publish( exchange=RabbitMQConfiguration().getRabbitMQProperty( "gameLogicServerExchangeName"), routing_key=routingKey, body=message)
def on_queue_declared(self, new_queue): logging.debug("GameLogicServer] Queue on /instance declared") self.queue = new_queue.method.queue gameLogicServerBindingKey = "*." + RabbitMQConfiguration( ).getRabbitMQProperty("gameLogicServerUserName") + ".*.*" self.channel.queue_bind( exchange=RabbitMQConfiguration().getRabbitMQProperty( "gameLogicServerExchangeName"), queue=self.queue, routing_key=gameLogicServerBindingKey, callback=self.on_queue_bind)
def terminate(self): logging.info("GameLogicServer] Terminating") self.state.exiting = True routingKey = RabbitMQConfiguration().getRabbitMQProperty( "gameLogicServerUserName") + ".all.lifecycle.terminate" message = "all the participants must terminate" self.channel.basic_publish( exchange=RabbitMQConfiguration().getRabbitMQProperty( "gameLogicServerExchangeName"), routing_key=routingKey, body=message) logging.debug("GameLogicServer] Stopping gamelogic channel ioloop") self.state.gamelogicchannel.stop() logging.debug("GameLogicServer] Stopping worker channel") self.stop()
def __init__(self, gameName, instanceName): self.gameName = gameName self.instanceName = instanceName self.vhost = RabbitMQConfiguration().getRabbitMQProperty("virtualHostSeparator") + gameName + RabbitMQConfiguration().getRabbitMQProperty("virtualHostSeparator") + instanceName self.semaphore = Semaphore(0) self.queueRequest = Queue() self.queueReturn = Queue()
def __init__(self, state): self.state = state credentials = PlainCredentials( RabbitMQConfiguration().getRabbitMQProperty( "gameLogicServerUserName"), RabbitMQConfiguration().getRabbitMQProperty( "gameLogicServerUserName")) parameters = pika.ConnectionParameters( host=RabbitMQConfiguration().getRabbitMQProperty( "gameLogicServerBrokerHost"), virtual_host=self.state.vhost, credentials=credentials) # instantiate a connection logging.debug("GameLogicServer] WorkerChannel - Starting a connection") self.connection = pika.BlockingConnection(parameters=parameters) logging.debug("GameLogicServer] WorkerChannel - Creating a channel") self.channel = self.connection.channel()
def on_queue_bind(self, new_queue): logging.debug("GameLogicServer] Binding declared") gameLogicServerBindingKey = "*.all.*.*" self.channel.queue_bind( exchange=RabbitMQConfiguration().getRabbitMQProperty( "gameLogicServerExchangeName"), queue=self.queue, routing_key=gameLogicServerBindingKey, callback=self.on_queue_bind_all)
def start(self): # parameters require for the AMQP connection: user name and password... credentials = PlainCredentials( RabbitMQConfiguration().getRabbitMQProperty( "gameLogicServerUserName"), RabbitMQConfiguration().getRabbitMQProperty( "gameLogicServerUserName")) parameters = pika.ConnectionParameters( host=RabbitMQConfiguration().getRabbitMQProperty( "gameLogicServerBrokerHost"), virtual_host=self.state.vhost, credentials=credentials) # instantiate a connection connection = SelectConnection(parameters=parameters, on_open_callback=self.on_connected) # required behavior on close connection.add_on_close_callback(self.on_close) # start the connection connection.ioloop.start()
def set_permissions_vhost_logginserver(vhost): return_code = call("rabbitmqctl add_user " + RabbitMQConfiguration().getRabbitMQProperty("loggingServerUserName") + " " + RabbitMQConfiguration().getRabbitMQProperty("loggingServerUserName"), shell=True) if return_code != 0: if return_code == 2: # User exist logging.info("GameLogicServer] User " + RabbitMQConfiguration().getRabbitMQProperty("loggingServerUserName") + " already exists") pass else: logging.error("GameLogicServer] Problem (" + str(return_code) + ") in adding user "+ RabbitMQConfiguration().getRabbitMQProperty("loggingServerUserName")) exit(1) return_code = call("rabbitmqctl set_permissions -p " + vhost + " " + RabbitMQConfiguration().getRabbitMQProperty("loggingServerUserName") + grant_all_permissions, shell=True) if return_code != 0: logging.error("GameLogicServer] Problem (" + str(return_code) + ") in setting permissions for logging server") exit(1)
def joinWithObservationKey(self, login, observationKey): logging.info("GameLogicServer] Joining of " + login) self.channel.queue_declare(queue=login) logging.debug("GameLogicServer] Queue declared for " + login) logging.debug("GameLogicServer] Observation key is " + observationKey) self.channel.queue_bind( exchange=RabbitMQConfiguration().getRabbitMQProperty( "gameLogicServerExchangeName"), queue=login, routing_key=observationKey) logging.debug("GameLogicServer] First queue bound on for " + login) observationKey = "*.all.*.*" logging.debug("GameLogicServer] Binding key for broadcasts is " + observationKey) self.channel.queue_bind( exchange=RabbitMQConfiguration().getRabbitMQProperty( "gameLogicServerExchangeName"), queue=login, routing_key=observationKey) logging.debug(" GameLogicServer] Second queue bound on for " + login) self.publish(login, self.state, "join.joinOK", login + " has joined")
def setLogger(): loggingLevel = RabbitMQConfiguration(confDir).getRabbitMQProperty("loggingLevel") if(loggingLevel == "DEBUG"): logging.basicConfig(format='[%(levelname)s - %(message)s', level=logging.DEBUG) elif(loggingLevel == "INFO"): logging.basicConfig(format='[%(levelname)s - %(message)s', level=logging.INFO) elif(loggingLevel == "WARNING"): logging.basicConfig(format='[%(levelname)s - %(message)s', level=logging.WARNING) elif(loggingLevel == "ERROR"): logging.basicConfig(format='[%(levelname)s - %(message)s', level=logging.ERROR) elif(loggingLevel == "CRITICAL"): logging.basicConfig(format='[%(levelname)s - %(message)s', level=logging.CRITICAL) else: logging.basicConfig(format='[%(levelname)s - %(message)s', level=logging.INFO)
def createLoggingServer(gameName, instanceName): logging.info("GameLogicServer] Creating Logging server for " + gameName + " " + instanceName) vhost = RabbitMQConfiguration().getRabbitMQProperty( "virtualHostSeparator") + gameName + RabbitMQConfiguration( ).getRabbitMQProperty("virtualHostSeparator") + instanceName credentials = PlainCredentials( RabbitMQConfiguration().getRabbitMQProperty("loggingServerUserName"), RabbitMQConfiguration().getRabbitMQProperty("loggingServerUserName")) parameters = pika.ConnectionParameters( host=RabbitMQConfiguration().getRabbitMQProperty( "gameLogicServerBrokerHost"), virtual_host=vhost, credentials=credentials) logging.debug("LoggingServer] Starting a connection") connection = pika.BlockingConnection(parameters=parameters) logging.debug("LoggingServer] Creating a channel") channel = connection.channel() logging.debug( "LoggingServer] Declaring a queue " + RabbitMQConfiguration().getRabbitMQProperty("loggingServerQueueName")) channel.queue_declare(queue=RabbitMQConfiguration().getRabbitMQProperty( "loggingServerQueueName"), exclusive=False, durable=True) logging.debug( "LoggingServer] Binding the queue with the rooting key " + RabbitMQConfiguration().getRabbitMQProperty("loggingServerBindingKey")) channel.queue_bind(exchange=RabbitMQConfiguration().getRabbitMQProperty( "gameLogicServerExchangeName"), queue=RabbitMQConfiguration().getRabbitMQProperty( "loggingServerQueueName"), routing_key=RabbitMQConfiguration().getRabbitMQProperty( "loggingServerBindingKey")) channel.basic_consume(consumer_callback=handle_delivery_loggingserver, queue=RabbitMQConfiguration().getRabbitMQProperty( "loggingServerQueueName"), no_ack=True) logging.info(" LoggingServer] Waiting for messages on " + vhost) channel.start_consuming() logging.info("LoggingServer] Process terminated")