-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
71 lines (56 loc) · 2.68 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
import socket
import json
import threading
from process_data import ProcessData
# Create Socket options
local_host = socket.gethostname()
local_port = 1234
UDPServerPort = (local_host, local_port)
buffer_size = 1024
socket_lock = threading.Lock()
# Create a UDP socket
UDPServerSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Bind to port
UDPServerSocket.bind(UDPServerPort)
welcome_message = "SERVER IS NOW LISTENING AT UDP SOCKET"
print(welcome_message)
# Create empty dictionary to store messages
storage = {}
def handle_socket(data, client_address, local_storage):
# print('The client at {} says {!r}'.format(client_address, data))
# Load received data into JSON
data_receive = json.loads(data.decode())
# Split JSON data into specific parts
process_data = ProcessData(data_receive)
# Verify if the client_code is valid
client_code = process_data.client_code
# Verify client code
if client_code == "T":
print("MESSAGE RECEIVED: T To: {} From: {} Text: {}".format(process_data.dest_id, process_data.client_id,
process_data.send_text))
if process_data.dest_id not in local_storage:
local_storage[process_data.dest_id] = [] # Create a queue to store data with the same key
local_storage[process_data.dest_id].append(process_data)
result = json.dumps({"message": "messages stored"})
elif client_code == "C":
if process_data.client_id in local_storage.keys():
obj = local_storage[process_data.client_id].pop(0) # Pop out relative object
if obj.send_text is not None:
result = json.dumps({"message": obj.send_text, "sender": obj.client_id}) # Convert text and id into json
print("MESSAGE TO SEND: T To: {} From: {} Text: {}".format(obj.dest_id, obj.client_id, obj.send_text))
else:
result = json.dumps({"message": "No Text", "sender": obj.client_id})
print("MESSAGE TO SEND: T To: {} Text: No Text".format(obj.client_id))
else:
result = json.dumps({"message": "No Text"})
print("MESSAGE TO SEND: T To: {} Text: No Text".format(process_data.client_id))
else:
result = json.dumps({"message": "Invalid <code> format. "})
with socket_lock:
UDPServerSocket.sendto(result.encode(), client_address)
while True:
processing_data, processing_client_address = UDPServerSocket.recvfrom(buffer_size * 2)
# Create threads to handle multiple clients
thread = threading.Thread(target=handle_socket, args=(processing_data, processing_client_address, storage))
thread.daemon = True
thread.start()