-
Notifications
You must be signed in to change notification settings - Fork 3
/
app_server.py
executable file
·76 lines (53 loc) · 1.7 KB
/
app_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/python3
import sys
import time
import zmq
from datetime import datetime, timedelta
from zmq.auth.ioloop import IOLoopAuthenticator
from zmq.eventloop.ioloop import IOLoop, PeriodicCallback
from zmq.eventloop.zmqstream import ZMQStream
from zmq_msg_helo import *
class AppServer(object):
listen = "127.0.0.1"
port = 5556
def __init__( self ):
self.ctx = zmq.Context()
self.loop = IOLoop.instance()
self.client_identities = {}
self.server = self.ctx.socket(zmq.ROUTER)
bind_addr = "tcp://%s:%s" % ( self.listen, self.port )
self.server.bind( bind_addr )
print("Server listening for new client connections at", bind_addr)
self.server = ZMQStream(self.server)
self.server.on_recv(self.on_recv)
self.periodic = PeriodicCallback(self.periodictask, 1000)
def start(self):
self.periodic.start()
try:
self.loop.start()
except KeyboardInterrupt:
pass
def periodictask(self):
stale_clients = []
for client_id, last_seen in self.client_identities.items():
if last_seen + timedelta(seconds=10) < datetime.utcnow():
stale_clients.append(client_id)
else:
msg = HelloMessage()
msg.send(self.server, client_id)
for client_id in stale_clients:
print("Haven't received a HELO from cliient %s recently. Dropping from list of connected clients." % client_id)
del self.client_identities[client_id]
sys.stdout.write(".")
sys.stdout.flush()
def on_recv(self, msg):
identity = msg[0]
self.client_identities[identity] = datetime.utcnow()
msg_type = msg[1]
print("Received message of type %s from client ID %s!" % (msg_type, identity))
def main():
my_server = AppServer()
my_server.start()
if __name__ == '__main__':
main()
# vim: ts=4 sw=4 noet