-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwsService.py
67 lines (55 loc) · 1.75 KB
/
wsService.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
#!/usr/bin/python
import serviceSkeleton
import json
import threading
import sys
from Tree import *
PORT = 5000
HOST = "127.0.0.1"
USERSONLINE = set([])
USERSONLINELOCK = threading.Lock()
treeLock = threading.Lock()
# if integer argument passed, use that as port
if len(sys.argv) > 1:
try:
PORT=int(sys.argv[1])
except:
pass
def serviceFunc(conn, addr):
global USERSONLINELOCK,USERSONLINE,treeLock
print "Connection :D"
with USERSONLINELOCK:
USERSONLINE.add(conn)
with treeLock:
nodes = [nodeToDict(i) for i in Tree.values()]
edges = [[p.ID, c] for p in Tree.values() for c in p.getChildren()]
conn.sendall(json.dumps({'command': 'tree', 'nodes': nodes, 'edges': edges}))
while True:
try:
message = json.loads(conn.recv(1048576))
except:
USERSONLINE.remove(conn)
conn.close()
return
print message
# Do stuff with the message
with treeLock:
changes = {
'addNode': addNode,
'deleteNode': deleteNode,
'addEdge': addEdge,
'deleteEdge': deleteEdge,
'editContent': editContent,
'searchNode': searchNode,
}[message['command']](*message['args'])
print changes
if changes is not None:
if 'command' in changes and changes['command'] == 'found':
with USERSONLINELOCK:
conn.sendall(json.dumps(changes))
continue
with USERSONLINELOCK:
print USERSONLINE
for c in USERSONLINE:
c.sendall(json.dumps(changes))
serviceSkeleton.startService(PORT,serviceFunc,host=HOST)