-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatserver.py
More file actions
165 lines (137 loc) · 4.08 KB
/
statserver.py
File metadata and controls
165 lines (137 loc) · 4.08 KB
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
import threading
import tornado.ioloop
import tornado.web
import tornado.websocket
import tornado.template
import time
import websocket
from random import randint
CONTROL_TIME = 10
global running
global ws
clients = []
netbook = []
bbb = []
c=0
#Initialize TOrnado to use 'GET' and load index.html
class MainHandler(tornado.web.RequestHandler):
def get(self):
loader = tornado.template.Loader(".")
self.write(loader.load("index.html").generate())
#Code for handling the data sent from the webpage
class WSHandlerNetbook(tornado.websocket.WebSocketHandler):
def check_origin(self, origin):
return True
def open(self):
print 'Netbook connection opened...'
netbook.append(self)
def on_message(self, message):
print 'Netbook Message: ', message
def on_close(self):
print 'Netbook connection closed...'
netbook.remove(self)
def msg(self, url):
self.write_message(u"controlling" + url)
def msg2(self):
self.write_message(u"asdf")
class WSHandlerBBB(tornado.websocket.WebSocketHandler):
def check_origin(self, origin):
return True
def open(self):
print 'BBB connection opened...'
bbb.append(self)
def on_message(self, message):
print 'BBB Message: ', message
def on_close(self):
print 'BBB connection closed...'
bbb.remove(self)
def send_message(self, message):
self.write_message(message)
def sendBBBMessage(message):
if len(bbb) == 0:
print 'BBB has not connected yet'
else:
bbb[0].send_message(message)
#Code for handling the data sent from the webpage
class WSHandler(tornado.websocket.WebSocketHandler):
active=False
count=CONTROL_TIME
def check_origin(self, origin):
return True
def open(self):
clients.append(self)
print 'connection opened...'
print clients
def on_message(self, message):
if clients.index(self) == 0:
print 'received:', message
sendBBBMessage(message)
def on_close(self):
clients.remove(self)
print 'connection closed...'
print clients
netbook[0].write_message(u"closed")
def msg(self):
self.write_message(u"Controlling... Time Left: " + str(self.count)+ "sec | Queue Size: " + str(len(clients)))
def waitmsg(self):
self.write_message(u"Waiting... " + str(len(clients)) + " In queue | Your location is " + str(clients.index(self)) + " | Approx wait time: " + str(clients.index(self) * CONTROL_TIME))
def activate(self):
if self.active == False:
self.active = True
s = str(randint(3333,4444));
self.write_message(u"controlling" + s)
if len(netbook) != 0:
netbook[0].msg(s)
else:
print "netbook not connected"
application = tornado.web.Application([
(r'/ws', WSHandler),
(r'/netbook', WSHandlerNetbook),
(r'/bbb', WSHandlerBBB),
(r'/', MainHandler),
(r"/(.*)", tornado.web.StaticFileHandler, {"path": "./resources"}),
])
class myThread (threading.Thread):
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
print "Ready"
while running:
time.sleep(1) # sleep for 200 ms
if len(clients) != 0:
clients[0].count = clients[0].count - 1
clients[0].msg()
clients[0].activate()
if clients[0].count == 0:
clients[0].close()
clients.remove(clients[0]);
if len(netbook) != 0:
netbook[0].write_message(u"closed")
for i in range(1, len(clients)):
clients[i].waitmsg()
#print ws.connected
#print ws._tunnel
#print ws.recv
if len(netbook) == 0:
print 'netbook not connected'
if len(bbb) == 0:
print 'bbb not connected'
def stuff(self):
l = dir(ws)
print ws.l
if __name__ == "__main__":
running = True
thread1 = myThread(1, "Thread-1", 1)
thread1.setDaemon(True)
thread1.start()
application.listen(9093) #9093 #starts the websockets connection
#websocket.enableTrace(True)
#ws = websocket.create_connection("ws://192.168.1.101:9094/ws")
#connectToBBB()
#ws.on_error = on_error2
# ws = websocket.WebSocketApp("ws://192.168.1.101:9094/ws", on_error = on_error2, on_close = on_close2)
# ws.run_forever()
tornado.ioloop.IOLoop.instance().start()