-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.py
144 lines (126 loc) · 4.95 KB
/
client.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import queue
import sys
import threading
from time import sleep
from flask import Flask, request, Response
import json
import const
import requests
import os
import logging
# ----------------- Coordinator functions -----------------
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
app = Flask(__name__)
# import of coordinator functions/routes
from coordinator_functions import coordinator, get_user
app.register_blueprint(coordinator, url_prefix="/")
# ---------------------------------------------------------
# ------------------- Client ------------------------------
permission = False # global variable to control my permission
coordinator = False # define if I am the coordinator
coordnator_name = "" # coordinator name, to get this IP and PORT to request permissions
# ------------------ mutual exclusion functions ---------------------
@app.route("/give_permission", methods=['POST']) # this route receive a response from coordinator giving permission (like a webhook)
def give_permission():
global permission
permission_from_coordinator = request.json['permission']
if permission_from_coordinator:
permission = True
print("Permission granted")
return {}
def wait_for_permission(): # this function will ask for permission to coordinator and wait for it
global permission
if not permission:
requests.post(f"{get_user(coordnator_name)}/get_permission", json={"user": i_am})
while permission == False:
sleep(1)
print("Waiting for permission...")
return True
def release_permission(): # this function will release permission to coordinator after finishing use the resource (score)
global permission
permission = False
requests.post(f"{get_user(coordnator_name)}/release_permission", json={"user": i_am})
# ------------------ end of mutual exclusion functions ---------------------
# ------------------ election functions ---------------------
def request_election():
election_response = []
for user in const.registry:
if user != i_am:
(ip, port, id) = const.registry[user]
if id > const.registry[i_am][2]:
try:
response = requests.post(f"http://{ip}:{port}/election", json={"user": i_am})
election_response.append(response.json()['response'])
except:
pass
if len(election_response) == 0:
print("I am the new coordinator")
global coordinator
coordinator = True
global coordnator_name
coordnator_name = i_am
for user in const.registry:
if user != i_am:
(ip, port, id) = const.registry[user]
try:
response = requests.post(f"http://{ip}:{port}/election_result", json={"coordinator": i_am})
except:
pass
@app.route("/election", methods=['POST'])
def election():
user = request.json['user']
threading.Thread(target=request_election).start()
return json.dumps({"response": "ok"})
@app.route("/election_result", methods=['POST'])
def election_result():
global coordnator_name
new_coordinator = request.json['coordinator']
print(f"New coordinator: {new_coordinator}")
coordnator_name = new_coordinator
return json.dumps({"response": "ok"})
def request_score():
response = requests.get(f"http://{const.CHAT_SERVER_HOST}:{const.CHAT_SERVER_PORT}/get_score")
print(f"current score: {response.json()['score']}")
def start_server(i_am):
(ip, port, id) = const.registry[i_am]
app.run(host=ip, port=port)
def update_score():
request_score()
new_score = input("Enter new score: ")
try:
response = requests.post(f"http://{const.CHAT_SERVER_HOST}:{const.CHAT_SERVER_PORT}/update_score", json={"score": new_score})
print(f"new score: {response.json()['score']}")
print("Score updated successfully")
except:
print(response.json()['error'])
options = {
"1": request_score,
"2": update_score,
"3": exit
}
if __name__ == "__main__":
i_am = str(sys.argv[1])
coordnator_name = str(sys.argv[2])
if i_am == coordnator_name:
coordinator = True
print("I am coordinator")
threading.Thread(target=start_server, args=(i_am,), daemon=True).start()
while True:
print("--------------------------------------------------------------------------------")
print("Insert the number of the option you want to execute:")
print("1 - Get Score")
print("2 - Update Score")
print("3 - Close")
option = input("Choose an option: ")
print("--------------------------------------------------------------------------------")
if option not in options:
print("Invalid option")
continue
os.system('cls||clear')
try:
wait_for_permission()
options[option]()
release_permission()
except:
request_election()