Example #1
0
    def __init__(self,
                 host='localhost',
                 max_size=200000000,
                 json_name='Output.json',
                 exchangeName='eventRoute',
                 bindKey='stampede.job_inst.#'):
        """
        A constructor for our Receiver class responsible for:
        1) Setting maximum queue size at receiving end during parsing.
        2) Setting up JSON output file.
        3) Setting up connections with the server, creation of queues and exchange.
        4) Setting up the database table titled "JSON" to store needed statistics.

        Arguments:
            host: The hostname.
            maxSize: Maximum size of the priority queue that we use in parsing.
            json_name: Name of JSON file we'll output to.
            exchangeName: The desired name for the exchange.
        """
        threading.Thread.__init__(self)

        self.init_map = {'stampede.job_inst.#': self.init_job_inst}

        self.recalculate_map = {
            'stampede.job_inst.#': self.recalculate_job_inst
        }
        #functions should be added incrementally if we are implementing more event handlers

        #Declaring queue size, output file name, host name and the queue we'll
        #listen to with this receiver.
        self.Q = Queue.PriorityQueue(max_size)
        self.JsonObj = JsonOutput(json_name)
        self.host = host

        #Taghrid.start
        self.exchange = exchangeName
        self.bindKey = bindKey
        self.init_map[self.bindKey]()
        #Taghrid.end

        #Establishing connection with the server.
        self.connection = pika.BlockingConnection(
            pika.ConnectionParameters(host=self.host))
        self.channel = self.connection.channel()

        #Creating a topic exchange.
        self.channel.exchange_declare(exchange=exchangeName, type='topic')

        #Getting a random queue name.
        randomize = self.channel.queue_declare(exclusive=True)
        self.random_name = randomize.method.queue

        #Binding the queue to the routing key.
        self.channel.queue_bind(exchange=exchangeName,
                                queue=self.random_name,
                                routing_key=bindKey)