/
server.py
272 lines (233 loc) · 9.81 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
import sys
import signal
#from gevent_zeromq import zmq
import gevent
from gevent.monkey import patch_all; patch_all()
from zmq import green as zmq
from gevent import server
from gevent.server import StreamServer
from gevent.server import _tcp_listener
from multiprocessing import Process, current_process, cpu_count
import pprint
import string
from collections import defaultdict
from gevent.queue import Queue
import re
import time
from gevent.pool import Pool as GPool
from gevent.queue import Queue
def note(format, *args):
sys.stderr.write('[%s]\t%s\n' % (current_process().name, format%args))
word_split = re.compile(r'[ ]')
listener = _tcp_listener(('0.0.0.0', 8891))
def send_bye(socket):
socket.send('BYE 0000')
def send_msg(socket, msg):
try:
str_to_send = 'RCV ' + string.zfill(len(msg) + 1, 4) + ' ' + msg
# print "Sending: ", str_to_send
socket.send(str_to_send)
except:
pass
def send_msg_to_subscriber(publisher, to, msg):
try:
# print "Sending to PUB:" + to + '-- message is: ' + msg
publisher.socket.send_multipart([to, msg])
except:
# exc_info = sys.exc_info()
# pprint.pprint(exc_info)
# import pdb
# pdb.set_trace()
pass
global_sockets = defaultdict()
global_users = defaultdict(str)
BASE_PUBLISHER = 9000
class NothingInSocket(Exception):
pass
class Publisher(object):
def __init__(self, process_num, zmq_context):
self.context = zmq_context
self.socket = zmq_context.socket(zmq.PUB)
print "Publishing on: ", (BASE_PUBLISHER + process_num)
# self.socket.bind("tcp://*:" + str(BASE_PUBLISHER + process_num))
self.socket.bind("ipc:///tmp/" + str(BASE_PUBLISHER + process_num))
return
def close(self):
try:
self.socket.close()
except:
pass
class Subscriber(object):
def __init__(self, num_of_processes, zmq_context):
self.context = zmq_context
self.num_of_processes = num_of_processes + 1
self.sockets = {}
for x in xrange(self.num_of_processes):
socket = zmq_context.socket(zmq.SUB)
self.sockets[x] = socket
print "Subscrbing on: ", (BASE_PUBLISHER + x)
# socket.connect("tcp://localhost:" + str(BASE_PUBLISHER + x))
socket.connect("ipc:///tmp/" + str(BASE_PUBLISHER + x))
self.start_subscribers()
def start_subscribers(self):
processing_queue = gevent.queue.Queue(None)
subscribers_pool = GPool(self.num_of_processes)
subscriber_jobs = [subscribers_pool.spawn(self.start_listening_subscriber, self.sockets[x], processing_queue) for x in xrange(self.num_of_processes)]
processing_pool = GPool(5000)
processing_jobs = [processing_pool.spawn(self.send_msg_to_user_socket, processing_queue) for x in xrange(5000)]
def send_msg_to_user_socket(self, processing_queue):
while True:
job = processing_queue.get()
if job == StopIteration:
processing_queue.put(StopIteration)
print "Exiting one thread"
return
if job[0] in global_sockets.keys():
send_msg(global_sockets[job[0]], job[1])
def start_listening_subscriber(self, socket, processing_queue):
while True:
# print "Waiting on the SUB port"
try:
to_whom, content = socket.recv_multipart()
# print "Got Something"
# print "Got Message into subscriber: ", to_whom, content
processing_queue.put((to_whom, content))
# send_msg(global_sockets[to_whom], content)
except:
# exc_info = sys.exc_info()
# pprint.pprint(exc_info)
# import pdb
# pdb.set_trace()
pass
def set_subscriber(self, to_whom):
for x in xrange(self.num_of_processes):
# self.sockets[x].setsockopt(zmq.SUBSCRIBE, '')
self.sockets[x].setsockopt(zmq.SUBSCRIBE, to_whom.lower().strip())
def close(self):
for x in xrange(self.num_of_processes):
try:
self.sockets[x].close()
except:
pass
class ListeningServer(StreamServer):
def __init__(self, listener, publisher, subscriber):
StreamServer.__init__(self, listener, self.proto)
self.publisher = publisher
self.subscriber = subscriber
def wait_till_as_many_bytes(self, socket, as_many_bytes):
command_str = ''
command_size = as_many_bytes
while True:
try:
command_str += socket.recv(command_size - len(command_str))
except Exception as e:
raise e
if command_str == '':
# print "Socket Returned nothing"
raise NothingInSocket("Socket Returned nothing")
if len(command_str) == as_many_bytes:
return command_str
def proto(self, socket, address):
while True:
try:
command_str = self.wait_till_as_many_bytes(socket, 8)
# print "Command String is: ", current_process().name, command_str
(command, length_of_rest) = command_str.lower().split(' ')
length_of_rest = int(length_of_rest)
try:
rest_str = self.wait_till_as_many_bytes(socket, length_of_rest)
rest_str = rest_str.strip()
except:
exc_info = sys.exc_info()
pprint.pprint(exc_info)
socket.close()
break
# print "REST is: ", rest_str
# if command[0] == 'r' and command == 'reg':
if command[0] == 'r':
rest_str = rest_str.lower()
global_sockets[rest_str] = socket
global_users[socket] = rest_str
self.subscriber.set_subscriber(rest_str)
# elif command[0] == 's' and command == 'snd':
elif command[0] == 's':
try:
msg_parts = rest_str.split(' ')
# msg_parts = re.split(r'\)?[, .]\(?', rest_str)
# msg_parts = word_split.split(rest_str)
to_whom = msg_parts[0].lower()
the_msg = ' '.join(msg_parts[1:])
if to_whom in global_sockets.keys():
send_msg(global_sockets[to_whom], global_users[socket] + ' ' + string.zfill(len(msg_parts) - 1, 4) + ' ' + the_msg)
else:
send_msg_to_subscriber(self.publisher, to_whom, global_users[socket] + ' ' + string.zfill(len(msg_parts) - 1, 4) + ' ' + the_msg)
# send_msg_to_subscriber(self.publisher, to_whom, global_users[socket] + ' ' + string.zfill(len(msg_parts) - 1, 4) + ' ' + the_msg)
# send_msg(global_sockets[to_whom], global_users[socket] + ' ' + string.zfill(len(msg_parts) - 1, 4) + ' ' + the_msg)
# gevent.spawn(send_msg, global_sockets[to_whom], global_users[socket] + ' ' + string.zfill(len(msg_parts) - 1, 4) + ' ' + the_msg)
except:
pass
else:
socket.close()
break
except NothingInSocket as e:
# print e
socket.close()
return
except:
# exc_info = sys.exc_info()
# pprint.pprint(exc_info)
socket.close()
return
def echo(self, socket, address):
print 'New connection from %s:%s' % address
fileobj = socket.makefile()
# fileobj.write('Welcome to the echo server! Type quit to exit.\r\n')
# fileobj.write('In %s\r\n' % current_process().name)
# fileobj.flush()
while True:
try:
line = fileobj.readline()
except:
exc_info = sys.exc_info()
pprint.pprint(exc_info)
if not line:
print "client disconnected"
break
if line.strip().lower() == 'quit':
print "client quit"
break
# fileobj.write(current_process().name + '\t' + line)
# fileobj.flush()
print "echoed", repr(line)
def close(self):
if self.closed:
sys.exit('Multiple exit signals received - aborting.')
else:
note('Closing socket')
send_bye(self.socket)
StreamServer.close()
number_of_processes = 13
def handle_signals(listening_server, publisher, subscriber):
print "Closing Everything"
listening_server.close()
publisher.close()
subscriber.close()
def serve_forever(listener, process_num):
note('starting server')
zmq_context = zmq.Context()
publisher = Publisher(process_num, zmq_context)
time.sleep(1)
subscriber = Subscriber(number_of_processes, zmq_context)
print "Sarting the listening server"
listening_server = ListeningServer(listener, publisher, subscriber)
gevent.signal(signal.SIGTERM, handle_signals, listening_server, publisher, subscriber)
gevent.signal(signal.SIGINT, handle_signals, listening_server, publisher, subscriber)
# gevent.signal(signal.SIGTERM, listening_server.close)
# gevent.signal(signal.SIGINT, listening_server.close)
listening_server.start()
gevent.wait()
print 'Starting %s processes' % number_of_processes
for i in range(number_of_processes):
Process(target=serve_forever, args=(listener,i + 1,)).start()
serve_forever(listener, 0)
#go run ./test.go -clients=10000 -burst_interval=100 -burst=100 -port="8891" -duration=1000 -wait=10000