From 41adade9fc827e2a6dc6ff41267dc15d2dce19b8 Mon Sep 17 00:00:00 2001 From: Malachy Byrne Date: Thu, 13 Apr 2023 04:28:10 +0100 Subject: [PATCH] Added withdrawal capabilities. TODO: improve logging, have researchers make use of withdrawal --- classes.py | 1 - project_server.py | 1 + university.py | 57 ++++++++++++++++++++++++++++------------------- 3 files changed, 35 insertions(+), 24 deletions(-) diff --git a/classes.py b/classes.py index 36dd166..ea7a40f 100644 --- a/classes.py +++ b/classes.py @@ -6,7 +6,6 @@ from typing import List class Transaction: person: str value: int - description: str @dataclass diff --git a/project_server.py b/project_server.py index a8462b0..d21ff14 100644 --- a/project_server.py +++ b/project_server.py @@ -24,6 +24,7 @@ def get_project(ch, method, properties, body): routing_key=properties.reply_to, properties=pika.BasicProperties(correlation_id=properties.correlation_id), body=project) + log(f"Replying to {properties.reply_to}") log(f"got project {project_id}") diff --git a/university.py b/university.py index a4739b5..2eb335c 100644 --- a/university.py +++ b/university.py @@ -1,35 +1,46 @@ import pika import os +import pickle -from functions import log +from functions import log, save_project +from classes import * -if not os.path.exists("approved_projects"): - os.makedirs("approved_projects") +class University: + def __init__(self): + self.connection = pika.BlockingConnection(pika.ConnectionParameters('localhost', 5672, '/', pika.PlainCredentials("user", "password"))) + self.channel = self.connection.channel() -connection = pika.BlockingConnection(pika.ConnectionParameters('localhost', 5672, '/', pika.PlainCredentials("user", "password"))) -channel = connection.channel() + self.channel.queue_declare(queue='university_project') + self.channel.queue_declare(queue='withdraw_funds') + self.channel.basic_consume(queue="university_project", on_message_callback=self.receive_response, auto_ack=True) + self.channel.basic_consume(queue="withdraw_funds", on_message_callback=self.withdraw_funds, auto_ack=True) + self.response = None + self.corr_id = None -channel.queue_declare(queue='approved_projects') + def receive_response(self, ch, method, properties, body): + self.response = body + project = pickle.loads(self.response) + transaction = Transaction(self.name, int(self.amount)) + project.history.append(transaction) + project.amount -= int(self.amount) + if project.amount >= 0: + save_project(project) + log(f"{self.name} withdrew {self.amount} from {project.acronym}. {project.amount} remaining") + else: + log(f"{self.name} attempter to withdraw {self.amount} from {project.acronym} but there was only {project.amount + int(self.amount)} remaining") -def process_approved_project(ch, method, properties, body): - proposal = body.decode() - acronym, title, description, amount = proposal.split(',') + def withdraw_funds(self, ch, method, properties, body): + body = body.decode() + proj_id, self.name, self.amount = body.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') - - log(f'Approved project received: {body}') - -channel.basic_consume(queue='approved_projects', on_message_callback=process_approved_project, auto_ack=True) + self.channel.basic_publish(exchange='', routing_key='get_project', + properties=pika.BasicProperties(reply_to="university_project"), + body=proj_id) try: - log('University waiting for approved projects...') - channel.start_consuming() + university = University() + university.channel.start_consuming() except KeyboardInterrupt: - channel.stop_consuming() - connection.close() + university.channel.stop_consuming() + university.connection.close()