/
server.py
115 lines (92 loc) · 2.95 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
#!/usr/bin/env python
from os import path as op
import os
import tornado.web
import tornadio
import tornadio.router
import tornadio.server
import simplejson as json
import tornado.ioloop
ioloop = tornado.ioloop.IOLoop.instance()
ROOT = op.normpath(op.dirname(__file__))
next_id = 1
next_node = 1
class IndexHandler(tornado.web.RequestHandler):
"""Regular HTTP handler to serve the front page"""
def get(self):
self.render("static/index.html")
class Server:
clients = set()
def __init__(self):
pass
def send(self, client, packet):
client.send(packet)
def broadcast(self, packet):
print 'broadcast: ' + json.dumps(packet)
for c in self.clients:
c.send(packet)
server = Server()
class Client(tornadio.SocketConnection):
def on_open(self, *args, **kwargs):
global next_id
server.clients.add(self)
self.id = next_id;
print 'Created client ' + str(self.id)
next_id += 1;
packet = {'init': {'id': self.id}}
server.send(self, packet)
def on_message(self, message):
pass
def on_close(self):
server.clients.remove(self)
packet = {'left': {'id': self.id}}
server.broadcast(packet)
settings = {
'static_path': op.join(op.dirname(__file__), 'static'),
}
#use the routes classmethod to build the correct resource
Router = tornadio.get_router(Client)
#configure the Tornado application
application = tornado.web.Application(
[(r"/static/(.*)", tornado.web.StaticFileHandler, dict(path=settings['static_path'])),
(r"/", IndexHandler),
Router.route()],
enabled_protocols = ['websocket',
'flashsocket',
'xhr-multipart',
'xhr-polling'],
flash_policy_port = 843,
flash_policy_file = op.join(ROOT, 'flashpolicy.xml'),
socket_io_port = 11001
)
import zmq
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect("tcp://localhost:11100")
socket.setsockopt(zmq.SUBSCRIBE, "")
poller = zmq.Poller()
poller.register(socket, zmq.POLLIN)
def idle():
socks = dict(poller.poll(0))
if socket in socks and socks[socket] == zmq.POLLIN:
jmsg = socket.recv()
print 'received '+jmsg
msg = json.loads(jmsg)
server.broadcast(msg)
# Use this instead of idle() to send some fake nodes out
def test_idle():
print 'idle'
global next_node
msg = socket.recv()
msg = {'newNode': {'name': 'node' + str(next_node) }}
server.broadcast(msg)
msg = {'newEdge': {'from': 'node' + str(next_node - 1),
'to': 'node' + str(next_node)}}
server.broadcast(msg)
next_node += 1
if __name__ == "__main__":
import logging
logging.getLogger().setLevel(logging.DEBUG)
io_loop = tornado.ioloop.IOLoop.instance()
tornado.ioloop.PeriodicCallback(idle, 100).start()
tornadio.server.SocketServer(application, io_loop=io_loop)