-
Notifications
You must be signed in to change notification settings - Fork 0
/
chessP2P.py
48 lines (40 loc) · 1.37 KB
/
chessP2P.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
from menu import Menu,ChallengeFriend,ViewChallenges,ExitApp,Client
from chessboard import ChessBoard
from threading import Thread
import time
def create_menu():
options=[ChallengeFriend(),ViewChallenges(),ExitApp()]
return Menu(options=options)
shutdown_received=False
def handle_shutdown():
shutdown_received=True
Client.shutdown_server()
while Client.server_up:
time.sleep(0.5)
print("Process exited gracefully")
if __name__=="__main__":
#Starting background server to listen for challenges
challenge_listener = Thread(target = Client.start_server,daemon=True)
challenge_listener.start()
MENU=create_menu()
MAX_CONSECUTIVE_LOOP_ERRORS=15
should_exit=False
error_count=0
while(not MENU.exit_requested and not shutdown_received):
try:
MENU.display()
print(f"Your IP: {Client.ip}")
MENU.select(input("Enter an option: "))
error_count=0
except KeyboardInterrupt:
print("\nTerminating process...")
break
except:
error_count+=1
if(error_count>=MAX_CONSECUTIVE_LOOP_ERRORS):
print("Application errored too many times. Attempting to clean up and exit..,")
break
Client.shutdown_server()
challenge_listener.join()
#Shutdown server
print("Exiting...")