-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
138 lines (106 loc) · 4.12 KB
/
Copy pathserver.py
File metadata and controls
138 lines (106 loc) · 4.12 KB
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
import argparse
import socket
from multiprocessing import Process, Pipe
from queue import Queue
import select
from colorama import Fore, Style
from quicksort_parallel import quicksort_parallel
def quicksort_helper(input_list, proc_count):
p_pipe, c_pipe = Pipe(duplex=False)
proc = Process(
target=quicksort_parallel,
args=(
input_list,
c_pipe,
proc_count,
1
)
)
proc.start()
output = p_pipe.recv()
proc.join()
proc.close()
return output
def create_server(host, port):
# Create socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Unset blocking
server.setblocking(0)
# Bind to host and port
server.bind((host, port))
# Listen
server.listen()
print(Fore.BLUE + f'Listening on {host}:{port}' + Style.RESET_ALL)
return server
def serve(server):
# Wait until ready for reading list
reads = [server]
# Wait until ready for writing list
writes = []
# Wait for exceptional condition list
errors = []
# Message queue
messages_per_client = {}
while True:
read_sel, write_sel, except_sel = select.select(reads, writes, errors)
for obj in read_sel:
if obj is server:
# Accept the client
connection, client = obj.accept()
print(Fore.GREEN + f'New client found: {client}' + Style.RESET_ALL)
# Unset blocking
connection.setblocking(0)
# Add client to reads
reads.append(connection)
# Create message queue for current connection
messages_per_client[connection] = Queue()
else:
# Receive message
message = obj.recv(1024)
if message:
if obj not in messages_per_client.keys():
print(Fore.RED + 'Client not registered' + Style.RESET_ALL)
continue
try:
message_eval = eval(message)
if not isinstance(message_eval, tuple) or not isinstance(message_eval[1], list):
print(Fore.YELLOW + 'Please provide input in the following format:')
print(Fore.YELLOW + '(<number of processes>, <list to sort>)')
print(Style.RESET_ALL)
else:
# Sort the list
sorted_list = quicksort_helper(message_eval[1], message_eval[0])
# Put message into message queue
messages_per_client[obj].put('[' + ','.join([str(i) for i in sorted_list]) + ']')
except (ValueError, SyntaxError):
print(Fore.YELLOW + 'Please provide input in the following format:')
print(Fore.YELLOW + '(<number of processes>, <list to sort>)')
print(Style.RESET_ALL)
# Add client to writes
if obj not in writes:
writes.append(obj)
for obj in write_sel:
try:
# Get message for current client
message = messages_per_client[obj].get_nowait()
except:
# If there are no massage for that client remove from writes
writes.remove(obj)
else:
# If there are messages waiting send them
obj.send(message.encode())
for obj in except_sel:
if obj in reads:
reads.remove(obj)
if obj in writes:
writes.remove(obj)
obj.close()
def main():
argParser = argparse.ArgumentParser()
argParser.add_argument("-sh", "--server_host", type=str, default='localhost', help="Host name")
argParser.add_argument("-sp", "--server_port", type=int, default='8008', help="Port number")
args = argParser.parse_args()
server = create_server(args.server_host, args.server_port)
serve(server)
if __name__ == "__main__":
main()