-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathecho_server.py
76 lines (71 loc) · 2.38 KB
/
echo_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
#!/usr/bin/env python
import socket
import sys
import pickle
map_output={}
reduced_output={}
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the port
server_address = ('127.0.0.1', 5000)
print ('starting up on %s port %s' % server_address)
sock.bind(server_address)
# Listen for incoming connections
sock.listen(1)
print ('waiting for a connection\n')
connection, client_address = sock.accept()
while True:
data = connection.recv(4096)
data=data.decode()
print ('received "%s"' % data)
if data=='Ready':
print ('Sending Start Init Command\n')
message1='Run_Init'
connection.send(message1.encode())
elif data=='Init_OK':
print ('Init OK, sending run map command\n')
message2='Run_Map'
connection.sendall(message2.encode())
elif data=='Map_OK':
print ('Map OK, sending run shuffle command\n')
message3='Run_Shuffle'
#stelnei to mhnma
connection.sendall(message3.encode())
#dexetai ta dedomena
try:
map_output=pickle.loads(connection.recv(4096))
print('map output received')
print(map_output,'\n')
except socket.error:
print ('Reiveiving Data failed')
elif data=='Shuffle_OK':
print ('Shuffle OK, sending run reduce command\n')
message4='Run_PrepareReduceInput'
#stelnei to mhnuma
connection.sendall(message4.encode())
#stelnei dedomena
try:
connection.sendall(pickle.dumps(map_output))
print('merged data sent')
except socket.error:
#Send failed
print ('Sending Message Shuffle_OK failed')
sys.exit()
elif data=='PrepareReduce_OK':
print ('PrepareReduce OK, sending run Reduce command\n')
message5='Run_Reduce'
connection.sendall(message5.encode())
#dexetai dedomena
try:
reduced_output=pickle.loads(connection.recv(4096))
print('reduced output received')
except socket.error:
print ('Reiveiving Data failed')
elif data=='Reduce_OK':
print ('Reduced Data:')
print(reduced_output,'\n')
break
else:
print ('no more data from' %client_address)
break
connection.close()