# add modules folder to interpreter path
import sys
import os
modules_path = '../../modules/'
if os.path.exists(modules_path):
    sys.path.insert(1, modules_path)

import pika
import network_constants

EXCHANGE_NAME = 'inhub'
EXCHANGE_TYPE = 'direct'
ROUTING_KEY = 'trace-retrieval'

HOST = network_constants.HOST_IP

connection = pika.BlockingConnection(pika.ConnectionParameters(HOST, 30302, heartbeat=60, blocked_connection_timeout=30))
channel = connection.channel()

channel.exchange_declare(exchange=EXCHANGE_NAME, exchange_type=EXCHANGE_TYPE)
result = channel.queue_declare(queue=ROUTING_KEY, exclusive=False, auto_delete=False)
queue_name = result.method.queue
channel.queue_bind(exchange=EXCHANGE_NAME, queue=queue_name, routing_key=ROUTING_KEY)

def callback(ch, method, properties, body):
    print(f"### Received: {body}")
    ch.basic_ack(delivery_tag=method.delivery_tag)

channel.basic_consume(queue=queue_name,
                        auto_ack=False,
                        on_message_callback=callback)

channel.start_consuming()
