-
Notifications
You must be signed in to change notification settings - Fork 0
/
logviewer.py
93 lines (73 loc) · 2.38 KB
/
logviewer.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
from os import path as op
import sys
import tornado.web
import tornadio
import tornadio.router
import tornadio.server
import thread
import time
global connections
def readLog(threadName, delay):
global connections
file = open(sys.argv[1], 'r')
while True:
where = file.tell()
line = file.readline()
if not line:
time.sleep(delay)
file.seek(where)
else:
print threadName, 'read: ', line,
for connection in connections:
for p in connection.participants:
p.send(line)
break
ROOT = op.normpath(op.dirname(__file__))
class IndexHandler(tornado.web.RequestHandler):
"""Regular HTTP handler to serve the chatroom page"""
def get(self):
self.render("index.html")
class ChatConnection(tornadio.SocketConnection):
# Class level variable
participants = set()
def __init__(self, protocol, io_loop, heartbeat_interval):
tornadio.SocketConnection.__init__(self, protocol, io_loop, heartbeat_interval)
def on_open(self, *args, **kwargs):
self.participants.add(self)
self.send("CONNECTED")
global connections
connections.add(self)
def on_message(self, message):
for p in self.participants:
p.send(message)
def on_close(self):
self.participants.remove(self)
self.send("DISCONNECTED")
global connections
connections.remove(self)
#use the routes classmethod to build the correct resource
ChatRouter = tornadio.get_router(ChatConnection)
#configure the Tornado application
application = tornado.web.Application(
[(r"/", IndexHandler, None), ChatRouter.route()],
enabled_protocols = ['websocket',
'flashsocket',
'xhr-multipart',
'xhr-polling'],
flash_policy_port = 843,
flash_policy_file = op.join(ROOT, 'flashpolicy.xml'),
socket_io_port = 8001
)
if __name__ == "__main__":
# initialize login
import logging
logging.getLogger().setLevel(logging.DEBUG)
# initliaze connections instance
global connections
connections = set()
try:
thread.start_new_thread(readLog, ("LogReader", 1,) )
except:
print "Error: unable to start thread"
# start server
tornadio.server.SocketServer(application)