-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
executable file
·179 lines (144 loc) · 6 KB
/
server.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
import socket
import threading
from time import *
from typing import List
from enums.TypeMessage import TypeMessage
from computer import Computer
from match import Match
class MyServer:
def __init__(self, nbr_listener) -> None:
self.message_separator = "|"
self.match_to_play : List[Match] = []
self.result : List[Match] = []
self.registered_result : List[Match] = []
self.nbr_parties = 0
self.connected_computers : List[Computer]= []
self.threads : List[threading.Thread] = []
self.running = True
# get the hostname
self.host = socket.gethostname()
self.port = 49300 # initiate port no above 1024
self.server_socket = socket.socket() # get instance
# look closely. The bind() function takes tuple as argument
self.server_socket.bind((self.host, self.port)) # bind host address and port together
# configure how many client the server can listen simultaneously
self.server_socket.listen(nbr_listener)
self.server_program()
def get_current_matches(self):
result = []
for c in self.connected_computers:
for m in c.actual_games:
result.append(m)
return result
def command(self):
while self.running:
mes = input("->")
if mes == "show":
print("--- Connected computer ---")
for c in self.connected_computers:
print(c)
print("--------------------------")
if mes == "matches" or mes == "m":
print("------ Match to play ------")
for m in self.match_to_play:
print(m)
print("----- Playing matches -----")
for c in self.connected_computers:
c.print_match()
print("--------- Result ---------")
for m in self.result:
print(m)
print("--------------------------")
if mes == "shutdown":
self.stop_server()
pass
def stop_server(self):
if self.running == False:
return
self.running = False
#self.server_socket.sendall(TypeMessage.encode_package(TypeMessage.END_CONNECTION, "")) ne fonctionne pas pour des raisons inconnues
for c in self.connected_computers:
c.conn.send(TypeMessage.encode_package(TypeMessage.END_CONNECTION, ""))
#réveille le serv, thread qui attend des entrées
client_socket = socket.socket() # instantiate
client_socket.connect((self.host, self.port)) # connect to the server
client_socket.close() #juste pour actualiser
sleep(1)
self.server_socket.close()
print("Shutdown...")
def give_match_to(self, c:Computer):
if len(self.match_to_play)==0:
return
match = self.match_to_play[0]
match.time_start = time()
self.match_to_play.remove(match)
c.add_match(match)
def on_new_client(self, conn:socket, addr):
print("Connection from: " + str(addr))
computer = None
try:
encoded_data = ""
get_info = False
while self.running:
add_encoded_data = conn.recv(1024).decode()
if not add_encoded_data:
# if data is not received break
break
encoded_data = encoded_data + add_encoded_data
decoded_data, encoded_data = TypeMessage.decode_package(encoded_data)
for (type_data, data) in decoded_data:
if type_data == TypeMessage.CONNECTION:
#print("recv data", data)
system, node_name, cores_number = data.split(",")
computer = Computer(conn, addr)
computer.set_stat(system, node_name, cores_number)
self.connected_computers.append(computer)
get_info = True
print("Connected:", str(computer))
continue
if not get_info:
print("Not connected when getting information !")
raise Exception()
if type_data == TypeMessage.MATCH:
print("Get match")
match = Match.from_string(data)
if match in computer.actual_games:
computer.actual_games.remove(match)
computer.nbr_game_done = computer.nbr_game_done + 1
self.result.append(match)
finally:
print("close connection")
if computer in self.connected_computers:
computer.is_connected = False
else:
print("Last computer disconnected")
conn.close() # close the connection
def wait_client(self):
while self.running:
conn, addr = self.server_socket.accept()
t = threading.Thread(target = self.on_new_client, args = [conn, addr])
t.start()
self.threads.append(t)
def server_program(self):
#Permet d'effectuer des commandes
#Ce thread ne s'arrête pas seul, il faut obligatoirement mettre "s" dans la console
t = threading.Thread(target = self.command)
t.start()
self.threads.append(t)
#Permet d'attendre des clients
clients = threading.Thread(target= self.wait_client)
clients.start()
self.threads.append(clients)
print("Finishing")
return
while True:
pass
for c in self.connected_computers:
c.conn.close()
for t in self.threads:
t.join()
print("Finished !")
if __name__ == '__main__':
MyServer(2)
while True:
sleep(10)