示例#1
0
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]
示例#2
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
示例#3
0
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
示例#4
0
 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)
示例#5
0
 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)
示例#6
0
 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)
示例#7
0
 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()
示例#8
0
	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()
示例#9
0
 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()
示例#10
0
 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)
示例#11
0
 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()
示例#12
0
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)
示例#13
0
 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")
示例#14
0
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)
示例#15
0
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")