-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpathServer.py
52 lines (43 loc) · 1.21 KB
/
pathServer.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
"""
This serves paths to the web side. It allows all of the path data to be kept in memory,
without loading it into Python types from files or network, which is very slow
Protocol:
- exaple in pathClient.py
- 1. Connect, send comma-separated two nodes ie: nodeid1,nodeid2
- 2. Receive JSON array back. Array contains the nodes, in order, for the path.
"""
from socket import *
import thread
from calc import pathFinder
import redis
import json
BUFF = 1024
HOST = '127.0.0.1'
PORT = 7284 # PATH :)
cache=redis.Redis()
pF = pathFinder(None)
pF.load()
def read_socket(client):
return client.recv(1)
def handler(clientsock,addr):
global cache
global pF
data = clientsock.recv(1024)
print data.decode("ascii")
data=data.decode("ascii").rstrip()
if not data:
clientsock.close()
return
nodes=data.split(",")
clientsock.send(json.dumps(pF.findPath(nodes[0],nodes[1])))
clientsock.close()
if __name__=='__main__':
serversock = socket(AF_INET, SOCK_STREAM)
serversock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
serversock.bind((HOST, PORT))
serversock.listen(20)
while 1:
print "Started"
clientsock, addr = serversock.accept()
print '...connected from:', addr
thread.start_new_thread(handler, (clientsock, addr))