-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaunch.py
268 lines (214 loc) · 8.04 KB
/
launch.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import threading
import time
import enquiries
import os
import sys
import re
import math
import multiprocessing
from src.components.server.server import Server
from src.components.client.client import Client
from src.core.utils.configuration import Configuration
from src.protocol.base import Message
from src.protocol.client.write.text_message import TextMessage
from src.protocol.client.write.initial import InitMessage
processes = {}
def clear():
os.system("clear")
def print_banner():
columns, rows = os.get_terminal_size(0)
print("*" * columns)
print("__ _____ _____ ___ _ _ ".center(columns, " "))
print("\ \ / / _ \/ __ \ / _ \ | | | | ".center(columns, " "))
print(" \ V / /_\ \ / \// /_\ \ | | __ _ _ _ _ __ ___| |__ ___ _ __ ".center(columns, " "))
print(" \ /| _ | | | _ | | | / _` | | | | '_ \ / __| '_ \ / _ \ '__|".center(columns, " "))
print(" | || | | | \__/\| | | | | |___| (_| | |_| | | | | (__| | | | __/ | ".center(columns, " "))
print(" \_/\_| |_/\____/\_| |_/ \_____/\__,_|\__,_|_| |_|\___|_| |_|\___|_| ".center(columns, " "))
print()
print("*" * columns)
def print_menu_banner(menu_text):
clear()
columns, rows = os.get_terminal_size(0)
print_banner()
print(menu_text.center(columns))
print("*" * columns)
def main_menu():
exiting = False
while not exiting:
print_menu_banner("Main Menu")
options = [
"1. Spawn Processes",
"2. Kill Processes",
"3. Run Tests",
"4. Exit (Ctrl+C)",
]
choice = enquiries.choose("Choose one of these options: ", options)
if choice == options[0]:
spawn_processes_menu()
elif choice == options[1]:
kill_processes_menu()
elif choice == options[3]:
return
def kill_processes_menu():
exiting = False
while not exiting:
print_menu_banner("Kill Processes")
options = []
counter = 1
for component in processes:
options.append(str(counter) + ". Kill " + component)
counter += 1
options.append(str(counter) + ". Return to Main Menu")
choice = enquiries.choose("Choose one of these options: ", options)
if choice == options[-1]:
return
counter = 1
for component in processes:
if choice == str(counter) + ". Kill " + component:
kill_processes_component_menu(component)
counter += 1
def kill_processes_component_menu(component):
print_menu_banner(
"Kill Processes (Selection: "
+ component
+ ") - Suggestion: Upper limit for Byzantine faults: "
+ str(math.ceil(len(processes[component]) / 4) - 1)
)
options = []
counter = 1
for p in processes[component]:
options.append(str(counter) + str(p))
counter += 1
options.append(str(counter) + ". Return")
choice = enquiries.choose("Choose multiple of these options (space to select): ", options, multi=True)
if options[-1] in choice:
return
terminating_procs = []
counter = 1
for p in processes[component]:
if str(counter) + str(p) in choice:
p.terminate()
terminating_procs.append(p)
counter += 1
while len(terminating_procs) > 0:
for p in terminating_procs:
if not p.is_alive():
p.join()
terminating_procs.remove(p)
processes[component].remove(p)
def spawn_processes_menu():
while True:
print_menu_banner("Spawn Processes")
options = [
"1. Launch Server",
"2. Launch Client",
"3. Return to Main Menu",
]
choice = enquiries.choose("Choose one of these options: ", options)
if choice == options[-1]:
return False
print("Number of Instances: ", end="")
instances = int(input())
if choice == options[0]:
for i in range(instances):
p = multiprocessing.Process(target=launch_server)
p.start()
processes["server"].append(p)
elif choice == options[1]:
for _ in range(instances):
p = multiprocessing.Process(target=launch_client)
p.start()
processes["client"].append(p)
def launch_server(initial=False, i=0):
sys.stdout = open("logs/server-" + str(os.getpid()) + ".out", "a", buffering=1)
sys.stderr = open("logs/server-" + str(os.getpid()) + ".out", "a", buffering=1)
server = Server(initial, i, verbose=True)
server.start()
def inc_string(string):
if string == "z" * len(string):
return "a" * (len(string) + 1)
new_string = ""
for i in range(len(string) - 1, -1, -1):
if string[i] == "z":
new_string = "a" + new_string
else:
new_string = string[:i] + chr(ord(string[i]) + 1) + new_string
return new_string
def max_string(val1 : str, val2 : str):
if len(val1) != len(val2):
return val1 if len(val1) > len(val2) else val2
for i in range(len(val1) - 1, -1, -1):
if val1[i] != val2[i]:
return val1 if val1[i] > val2[i] else val2
return val1 # val1 = val2
def launch_client():
sys.stdout = open("logs/client-" + str(os.getpid()) + ".out", "a", buffering=1)
sys.stderr = open("logs/client-" + str(os.getpid()) + ".out", "a", buffering=1)
client = Client()
client.start()
channel = client._delivery_channel
old_ts = 0
last_seen_text = ""
text = "a"
def timeout_handler():
channel.produce(None)
while True:
ts = time.time_ns() / 10**9
if ts - old_ts > 5:
old_ts = ts
client.send("(Reply to {}) {}".format(last_seen_text, text) if last_seen_text != "" else text)
text = inc_string(text)
timer = threading.Timer(5, timeout_handler)
timer.start()
data = channel.consume()
timer.cancel()
if data is not None:
msg = Message.initFromJSON(data)
msg.decode()
if msg.header == "Write: Initial":
msg = InitMessage.initFromJSON(data)
msg.decode()
print("{} joined the chat".format(msg.identifier))
if msg.header == "Write: TextMessage":
msg = TextMessage.initFromJSON(data)
msg.decode()
sender_id = msg.get_signature()[0]
print("{}: {}".format(sender_id, msg.text))
if sender_id != client._identifier:
last_seen_text = '{}: "{}"'.format(sender_id, msg.text.split(" ")[-1] if " " in msg.text else msg.text)
text = max_string(text, msg.text.split(" ")[-1] if " " in msg.text else msg.text)
def main():
if "-c" in sys.argv:
for f in os.listdir("logs/"):
os.remove(os.path.join("logs/", f))
processes["server"] = []
processes["client"] = []
if len(sys.argv) > 1 and "-i" in sys.argv[1]:
config = Configuration()
for i in range(config.data["initial"]["instances"]):
p = multiprocessing.Process(
target=launch_server,
args=(
True,
i,
),
)
p.start()
processes["server"].append(p)
main_menu()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("exiting...")
finally:
for component in processes:
for p in processes[component]:
p.terminate()
print("Waiting for processes to terminate...")
while sum([len(processes[component]) for component in processes]) > 0:
for component in processes:
for p in processes[component]:
if not p.is_alive():
p.join()
processes[component].remove(p)