From 54e3154876753b514901de4ae839e7ccdd6985d2 Mon Sep 17 00:00:00 2001 From: Gaz Date: Mon, 10 Apr 2023 17:21:07 +0100 Subject: [PATCH] unversity stores approved projects in files --- funding_agency.py | 8 ++++++-- researcher.py | 19 +++++++++++++++---- university.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 university.py diff --git a/funding_agency.py b/funding_agency.py index 50d2ed3..2e0619e 100644 --- a/funding_agency.py +++ b/funding_agency.py @@ -14,12 +14,16 @@ def process_proposal(ch, method, properties, body): if 200000 <= int(amount) <= 500000: channel.basic_publish(exchange='', - routing_key=str(properties.reply_to), + routing_key=properties.reply_to, + properties=pika.BasicProperties(correlation_id=properties.correlation_id), body=f"{acronym} approved") + project_info=f"{acronym},{title},{description},{amount}" + channel.basic_publish(exchange='', routing_key='approved_projects', body=project_info) print(f"{acronym} approved") else: channel.basic_publish(exchange='', - routing_key=str(properties.reply_to), + routing_key=properties.reply_to, + properties=pika.BasicProperties(correlation_id=properties.correlation_id), body=f"{acronym} rejected") print(f"{acronym} rejected") diff --git a/researcher.py b/researcher.py index d93748c..34beda5 100644 --- a/researcher.py +++ b/researcher.py @@ -1,10 +1,18 @@ import pika +import uuid connection = pika.BlockingConnection(pika.ConnectionParameters('localhost', 5672, '/', pika.PlainCredentials("user", "password"))) channel = connection.channel() channel.queue_declare(queue='research_proposals') + +def process_response(ch, method, properties, body): + if corr_id == properties.correlation_id: + print(body) + else: + print("response failed") + proposals = [ {"acronym": "PRT1", "title": "Phantom Blood", "description": "Johnathen", "amount": "300000"}, {"acronym": "PRT2", "title": "Battle Tendency", "description": "Joseph", "amount": "400000"}, @@ -14,17 +22,20 @@ proposals = [ {"acronym": "PRT6", "title": "Stone Ocean", "description": "Jolyne", "amount": "420000"}, {"acronym": "PRT7", "title": "Steel Ball Run", "description": "Johnny", "amount": "100000"}, {"acronym": "PRT8", "title": "Jojolion", "description": "Josuke", "amount": "900000"}, - {"acronym": "PRT9", "title": "Jojolands", "description": "Jodio", "amount": "420000"} + {"acronym": "PRT9", "title": "Jojolands", "description": "Jodio", "amount": "450000"} ] +corr_id = str(uuid.uuid4()) + for proposal in proposals: - result = channel.queue_declare(queue='', exclusive=True) - reply_to = result.method.queue + + response_queue = channel.queue_declare(queue='', exclusive=True).method.queue channel.basic_publish(exchange='', routing_key='research_proposals', - properties=pika.BasicProperties(reply_to=reply_to), + properties=pika.BasicProperties(reply_to=response_queue, correlation_id=corr_id), body=f"{proposal['acronym']},{proposal['title']},{proposal['description']},{proposal['amount']}") print(f"Proposal {proposal['acronym']} sent") + channel.basic_consume(queue=response_queue, on_message_callback=process_response) connection.close() \ No newline at end of file diff --git a/university.py b/university.py new file mode 100644 index 0000000..e773593 --- /dev/null +++ b/university.py @@ -0,0 +1,29 @@ +import os +import pika + +connection = pika.BlockingConnection(pika.ConnectionParameters('localhost', 5672, '/', pika.PlainCredentials("user", "password"))) +channel = connection.channel() + +channel.queue_declare(queue='approved_projects') + +def process_approved_project(ch, method, properties, body): + proposal = body.decode() + acronym, title, description, amount = proposal.split(',') + + + filename = os.path.join(os.getcwd(), 'approved_projects', f'{acronym}.txt') + with open(filename, 'w') as f: + f.write(f'Acronym: {acronym}\n') + f.write(f'Researcher: {description}\n') + f.write(f'Budget: {amount}\n') + + print(f'Approved project received: {body}') + +channel.basic_consume(queue='approved_projects', on_message_callback=process_approved_project, auto_ack=True) + +try: + print('University waiting for approved projects...') + channel.start_consuming() +except KeyboardInterrupt: + channel.stop_consuming() + connection.close() \ No newline at end of file