-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
184 lines (134 loc) · 4.59 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
#!/usr/bin/env python
#
# Copyright (c) 2013-2016, ETH Zurich.
# All rights reserved.
#
# This file is distributed under the terms in the attached LICENSE file.
# If you do not find this file, copies can be found by writing to:
# ETH Zurich D-INFK, Universitaetstr. 6, CH-8092 Zurich. Attn: Systems Group.
import socket
import sys
import json
import helpers
import config
import threading
import logging
from io import StringIO
PORT=25041
class SimArgs:
"""This is for passing arguments to the simulate function
In none-server-mode, this is extracted from the arguments
parsed with argparse"""
multicast = True
hybrid = False
hybrid_cluster = 'NUMA'
machine = None
overlay = None
group = []
multimessage = False
reverserecv = False
class ClientThread(threading.Thread):
def __init__(self, address, socket):
threading.Thread.__init__(self)
self.socket = socket
print ('connection from', address)
def run(self):
try:
# Receive the data in small chunks and retransmit it
buf = StringIO()
# XXX properly detect the length of the message
# here and receive ALL of it.
data = self.socket.recv(10240)
print(data.decode('ascii'))
buf.write(data.decode('ascii'))
# There is still no guarantee that this is
# correct, but if not, the json parser will fail.
assert len(data)<10240 # Otherwise, the message is
# longer than 10240 and we
# need to properly
# implement sockets
res = handle_request(json.loads(buf.getvalue()))
if len(res)>0:
self.socket.sendall(res.encode('ascii'))
finally:
# Clean up the connection
self.socket.close()
print ("Client disconnected...")
def handle_request(r):
"""Handle the Simulator request given by the r dictionary
"""
print ("handle_request executed .. ")
print (r)
# Parse request ..
config = SimArgs()
config.machine = r[u'machine']
config.overlay = [r[u'topology']] # List of topologies - just one
config.group = r[u'cores']
overlay = r[u'topology'].split('-')
overlay_name = overlay[0]
overlay_args = overlay[1:]
if overlay_name == 'hybrid':
overlay_name = 'cluster'
config.hybrid = True;
config.hybrid_cluster = overlay_args[0];
config.overlay = [u'cluster']
if overlay_args == 'mm' :
config.multimessage = True
elif overlay_args == 'rev' :
config.reverserecv = True
c = config
from simulator import simulate
(last_nodes, leaf_nodes, root) = simulate(config)
# Generate response to be sent back to client
import config
assert len(config.models)==1 # Exactly one model has been generated
res = {}
res['root'] = root
res['model'] = config.models[0]
res['last_node'] = last_nodes[0]
res['leaf_nodes'] = leaf_nodes[0]
res['git-version'] = helpers.git_version().decode('ascii')
print(res)
logging.info(('Responding with >>>'))
logging.info((json.dumps(res)))
logging.info(('<<<'))
write_statistics(c.machine)
return json.dumps(res)
STAT_FILE = 'statistics.json'
def write_statistics(machine):
# Read
try:
with open(STAT_FILE, 'r') as f:
stat = json.loads(f.read())
f.close()
except IOError:
stat = {}
# Update
stat['num_served'] = stat.get('num_served', 0) + 1
stat['num_served_%s' % machine] = stat.get('num_served_%s' % machine, 0) + 1
# Write
f = open(STAT_FILE, 'w')
json.dump(stat, f)
def server_loop():
config.running_as_server = True
print ('Starting server')
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_address = ('', PORT) # empty string = accept from all addresses
print ('starting up on %s port %s' % server_address)
sock.bind(server_address)
sock.listen(1)
try:
while True:
# Wait for a connection
print ('waiting for a connection')
connection, client_address = sock.accept()
# Handle client connection in a separate thread
t = ClientThread(client_address, connection)
t.start()
finally:
# Cleanup sockets
print ("Closing socket .. ")
# sock.shutdown(1)
# sock.close()