-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py.2
More file actions
145 lines (116 loc) · 3.26 KB
/
server.py.2
File metadata and controls
145 lines (116 loc) · 3.26 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
import threading
import tornado.ioloop
import tornado.web
import tornado.websocket
import tornado.template
import time
import websocket
from random import randint
global running
global ws
clients = []
netbook = []
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...'
def msg(self, url):
self.write_message(u"controlling" + url)
def msg2(self):
self.write_message(u"asdf")
#Code for handling the data sent from the webpage
class WSHandler(tornado.websocket.WebSocketHandler):
active=False
count=0
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
ws.send(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"Message: " + str(self.count)+ " " + str(clients))
def activate(self):
if self.active == False:
self.active = True
s = str(randint(3333,4444));
self.write_message(u"controlling" + s)
netbook[0].msg(s)
application = tornado.web.Application([
(r'/ws', WSHandler),
(r'/netbook', WSHandlerNetbook),
(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 == 11:
clients[0].close()
clients.remove(clients[0]);
if len(netbook) != 0:
netbook[0].write_message(u"closed")
for c in clients:
c.msg()
#print ws.connected
#print ws._tunnel
#print ws.recv
if len(netbook) == 0:
print 'netbook not connected'
def stuff(self):
l = dir(ws)
print ws.l
def on_message(ws, message):
print message
def on_error(ws, error):
print error
def on_close(ws):
print "### closed ###"
def on_open(ws):
print "ws opened"
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")
ws = websocket.WebSocketApp("ws://192.168.1.101:9094/ws", on_error = on_error, on_close = on_close)
ws.on_open = on_open
ws.run_forever()
# ws.run_forever()
tornado.ioloop.IOLoop.instance().start()