-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoctor.py
More file actions
53 lines (42 loc) · 1.76 KB
/
doctor.py
File metadata and controls
53 lines (42 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import sys
import pika
import json
from threading import Thread
from utils import Injuries, HOST, INJURY_REQUEST_EXCHANGE_NAME, INJURY_RESPONSE_EXCHANGE_NAME
name = sys.argv[1]
response_queue = f"{name}_queue"
pika_parameters = pika.ConnectionParameters(host=HOST)
def send_message():
sender_connection = pika.BlockingConnection(pika_parameters)
sender_channel = sender_connection.channel()
while True:
try:
patient_name = input("Enter patient name:")
injury_name = input("Enter injury name (hip, knee, elbow):")
injury = Injuries(injury_name)
msg = json.dumps({
"patient_name": patient_name,
"injury": injury.value,
"doctor_name": name
})
sender_channel.basic_publish(exchange=INJURY_REQUEST_EXCHANGE_NAME, routing_key=injury, body=msg)
except ValueError:
print("Invalid injury")
continue
sender_connection.close()
def callback(_ch, _method, _properties, body):
print(f" [x] Received {body.decode()}")
if __name__ == '__main__':
send_message_thread = Thread(target=send_message)
send_message_thread.start()
receiver_connection = None
try:
receiver_connection = pika.BlockingConnection(pika_parameters)
receive_channel = receiver_connection.channel()
receive_channel.queue_declare(queue=response_queue)
receive_channel.queue_bind(queue=response_queue, exchange=INJURY_RESPONSE_EXCHANGE_NAME, routing_key=name)
receive_channel.basic_consume(queue=response_queue, on_message_callback=callback, auto_ack=True)
receive_channel.start_consuming()
except KeyboardInterrupt:
if receiver_connection:
receiver_connection.close()