-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathserver.py
309 lines (249 loc) · 14.5 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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import socket
import pickle
import threading
import time
import numpy
import pygad.nn
import pygad.gann
import kivy.app
import kivy.uix.button
import kivy.uix.label
import kivy.uix.textinput
import kivy.uix.boxlayout
class ServerApp(kivy.app.App):
def __init__(self):
super().__init__()
def create_socket(self, *args):
self.soc = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
self.label.text = "Socket Created"
self.create_socket_btn.disabled = True
self.bind_btn.disabled = False
self.close_socket_btn.disabled = False
def bind_socket(self, *args):
ipv4_address = self.server_ip.text
port_number = self.server_port.text
self.soc.bind((ipv4_address, int(port_number)))
self.label.text = "Socket Bound to IPv4 & Port Number"
self.bind_btn.disabled = True
self.listen_btn.disabled = False
def listen_accept(self, *args):
self.soc.listen(1)
self.label.text = "Socket is Listening for Connections"
self.listen_btn.disabled = True
self.listenThread = ListenThread(kivy_app=self)
self.listenThread.start()
def close_socket(self, *args):
self.soc.close()
self.label.text = "Socket Closed"
self.create_socket_btn.disabled = False
self.bind_btn.disabled = True
self.listen_btn.disabled = True
self.close_socket_btn.disabled = True
def build(self):
self.create_socket_btn = kivy.uix.button.Button(text="Create Socket", disabled=False)
self.create_socket_btn.bind(on_press=self.create_socket)
self.server_ip = kivy.uix.textinput.TextInput(hint_text="IPv4 Address", text="localhost")
self.server_port = kivy.uix.textinput.TextInput(hint_text="Port Number", text="10000")
self.server_socket_box_layout = kivy.uix.boxlayout.BoxLayout(orientation="horizontal")
self.server_socket_box_layout.add_widget(self.server_ip)
self.server_socket_box_layout.add_widget(self.server_port)
self.bind_btn = kivy.uix.button.Button(text="Bind Socket", disabled=True)
self.bind_btn.bind(on_press=self.bind_socket)
self.listen_btn = kivy.uix.button.Button(text="Listen to Connections", disabled=True)
self.listen_btn.bind(on_press=self.listen_accept)
self.close_socket_btn = kivy.uix.button.Button(text="Close Socket", disabled=True)
self.close_socket_btn.bind(on_press=self.close_socket)
self.label = kivy.uix.label.Label(text="Socket Status")
self.box_layout = kivy.uix.boxlayout.BoxLayout(orientation="vertical")
self.box_layout.add_widget(self.create_socket_btn)
self.box_layout.add_widget(self.server_socket_box_layout)
self.box_layout.add_widget(self.bind_btn)
self.box_layout.add_widget(self.listen_btn)
self.box_layout.add_widget(self.close_socket_btn)
self.box_layout.add_widget(self.label)
return self.box_layout
model = None
# Preparing the NumPy array of the inputs.
data_inputs = numpy.array([[1, 1],
[1, 0],
[0, 1],
[0, 0]])
# Preparing the NumPy array of the outputs.
data_outputs = numpy.array([0,
1,
1,
0])
num_classes = 2
num_inputs = 2
num_solutions = 6
GANN_instance = pygad.gann.GANN(num_solutions=num_solutions,
num_neurons_input=num_inputs,
num_neurons_hidden_layers=[2],
num_neurons_output=num_classes,
hidden_activations=["relu"],
output_activation="softmax")
class SocketThread(threading.Thread):
def __init__(self, connection, client_info, kivy_app, buffer_size=1024, recv_timeout=5):
threading.Thread.__init__(self)
self.connection = connection
self.client_info = client_info
self.buffer_size = buffer_size
self.recv_timeout = recv_timeout
self.kivy_app = kivy_app
def recv(self):
all_data_received_flag = False
received_data = b""
while True:
try:
data = self.connection.recv(self.buffer_size)
received_data += data
try:
pickle.loads(received_data)
# If the previous pickle.loads() statement is passed, this means all the data is received.
# Thus, no need to continue the loop. The flag all_data_received_flag is set to True to signal all data is received.
all_data_received_flag = True
except BaseException:
# An exception is expected when the data is not 100% received.
pass
if data == b'': # Nothing received from the client.
received_data = b""
# If still nothing received for a number of seconds specified by the recv_timeout attribute, return with status 0 to close the connection.
if (time.time() - self.recv_start_time) > self.recv_timeout:
return None, 0 # 0 means the connection is no longer active and it should be closed.
elif all_data_received_flag:
print("All data ({data_len} bytes) Received from {client_info}.".format(client_info=self.client_info, data_len=len(received_data)))
self.kivy_app.label.text = "All data ({data_len} bytes) Received from {client_info}.".format(client_info=self.client_info, data_len=len(received_data))
if len(received_data) > 0:
try:
# Decoding the data (bytes).
received_data = pickle.loads(received_data)
# Returning the decoded data.
return received_data, 1
except BaseException as e:
print("Error Decoding the Client's Data: {msg}.\n".format(msg=e))
self.kivy_app.label.text = "Error Decoding the Client's Data"
return None, 0
else:
# In case data are received from the client, update the recv_start_time to the current time to reset the timeout counter.
self.recv_start_time = time.time()
except BaseException as e:
print("Error Receiving Data from the Client: {msg}.\n".format(msg=e))
self.kivy_app.label.text = "Error Receiving Data from the Client"
return None, 0
def model_averaging(self, model, other_model):
model_weights = pygad.nn.layers_weights(last_layer=model, initial=False)
other_model_weights = pygad.nn.layers_weights(last_layer=other_model, initial=False)
new_weights = numpy.array(model_weights + other_model_weights)/2
pygad.nn.update_layers_trained_weights(last_layer=model, final_weights=new_weights)
def reply(self, received_data):
global GANN_instance, data_inputs, data_outputs, model
if (type(received_data) is dict):
if (("data" in received_data.keys()) and ("subject" in received_data.keys())):
subject = received_data["subject"]
print("Client's Message Subject is {subject}.".format(subject=subject))
self.kivy_app.label.text = "Client's Message Subject is {subject}".format(subject=subject)
print("Replying to the Client.")
self.kivy_app.label.text = "Replying to the Client"
if subject == "echo":
if model is None:
data = {"subject": "model", "data": GANN_instance}
else:
predictions = pygad.nn.predict(last_layer=model, data_inputs=data_inputs)
error = numpy.sum(numpy.abs(predictions - data_outputs))
# In case a client sent a model to the server despite that the model error is 0.0. In this case, no need to make changes in the model.
if error == 0:
data = {"subject": "done", "data": None}
else:
data = {"subject": "model", "data": GANN_instance}
try:
response = pickle.dumps(data)
except BaseException as e:
print("Error Encoding the Message: {msg}.\n".format(msg=e))
self.kivy_app.label.text = "Error Encoding the Message"
elif subject == "model":
try:
GANN_instance = received_data["data"]
best_model_idx = received_data["best_solution_idx"]
best_model = GANN_instance.population_networks[best_model_idx]
if model is None:
model = best_model
else:
predictions = pygad.nn.predict(last_layer=model, data_inputs=data_inputs)
error = numpy.sum(numpy.abs(predictions - data_outputs))
# In case a client sent a model to the server despite that the model error is 0.0. In this case, no need to make changes in the model.
if error == 0:
data = {"subject": "done", "data": None}
response = pickle.dumps(data)
return
self.model_averaging(model, best_model)
# print(best_model.trained_weights)
# print(model.trained_weights)
predictions = pygad.nn.predict(last_layer=model, data_inputs=data_inputs)
print("Model Predictions: {predictions}".format(predictions=predictions))
error = numpy.sum(numpy.abs(predictions - data_outputs))
print("Prediction Error = {error}".format(error=error))
self.kivy_app.label.text = "Prediction Error = {error}".format(error=error)
if error != 0:
data = {"subject": "model", "data": GANN_instance}
response = pickle.dumps(data)
else:
data = {"subject": "done", "data": None}
response = pickle.dumps(data)
except BaseException as e:
print("Error Decoding the Client's Data: {msg}.\n".format(msg=e))
self.kivy_app.label.text = "Error Decoding the Client's Data"
else:
response = pickle.dumps("Response from the Server")
try:
self.connection.sendall(response)
except BaseException as e:
print("Error Sending Data to the Client: {msg}.\n".format(msg=e))
self.kivy_app.label.text = "Error Sending Data to the Client: {msg}".format(msg=e)
else:
print("The received dictionary from the client must have the 'subject' and 'data' keys available. The existing keys are {d_keys}.".format(d_keys=received_data.keys()))
self.kivy_app.label.text = "Error Parsing Received Dictionary"
else:
print("A dictionary is expected to be received from the client but {d_type} received.".format(d_type=type(received_data)))
self.kivy_app.label.text = "A dictionary is expected but {d_type} received.".format(d_type=type(received_data))
def run(self):
print("Running a Thread for the Connection with {client_info}.".format(client_info=self.client_info))
self.kivy_app.label.text = "Running a Thread for the Connection with {client_info}.".format(client_info=self.client_info)
# This while loop allows the server to wait for the client to send data more than once within the same connection.
while True:
self.recv_start_time = time.time()
time_struct = time.gmtime()
date_time = "Waiting to Receive Data Starting from {day}/{month}/{year} {hour}:{minute}:{second} GMT".format(year=time_struct.tm_year, month=time_struct.tm_mon, day=time_struct.tm_mday, hour=time_struct.tm_hour, minute=time_struct.tm_min, second=time_struct.tm_sec)
print(date_time)
received_data, status = self.recv()
if status == 0:
self.connection.close()
self.kivy_app.label.text = "Connection Closed with {client_info}".format(client_info=self.client_info)
print("Connection Closed with {client_info} either due to inactivity for {recv_timeout} seconds or due to an error.".format(client_info=self.client_info, recv_timeout=self.recv_timeout), end="\n\n")
break
# print(received_data)
self.reply(received_data)
class ListenThread(threading.Thread):
def __init__(self, kivy_app):
threading.Thread.__init__(self)
self.kivy_app = kivy_app
def run(self):
while True:
try:
connection, client_info = self.kivy_app.soc.accept()
self.kivy_app.label.text = "New Connection from {client_info}".format(client_info=client_info)
socket_thread = SocketThread(connection=connection,
client_info=client_info,
kivy_app=self.kivy_app,
buffer_size=1024,
recv_timeout=10)
socket_thread.start()
except BaseException as e:
self.kivy_app.soc.close()
print("Error in the run() of the ListenThread class: {msg}.\n".format(msg=e))
self.kivy_app.label.text = "Socket is No Longer Accepting Connections"
self.kivy_app.create_socket_btn.disabled = False
self.kivy_app.close_socket_btn.disabled = True
break
serverApp = ServerApp()
serverApp.title="Server App"
serverApp.run()