-
Notifications
You must be signed in to change notification settings - Fork 3
/
session.py
93 lines (79 loc) · 2.95 KB
/
session.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
92
93
from net import *
import time
import json
from random import randint
RETRY = 0.5
WORRY = 8
TIMEOUT = 10
class Session(object):
def __init__(self, IP, PORT, ID, manager):
self.locator = (IP, PORT, ID)
self.manager = manager
self.sender = SendPipe(IP, PORT)
self.txTime = 0
self.rxTime = 0
def poll(self):
Time = time.time()
rxTime = Time - self.rxTime
txTime = Time - self.txTime
if (rxTime > WORRY) and (txTime > RETRY):
self.send("ping")
def event(self):
self.rxTime = time.time()
def send(self, data):
self.txTime = time.time()
packet = json.dumps((self.manager.locator, data))
print "TX: ", packet
self.sender.send(packet)
class SessionManager(object):
def __init__(self, IP, PORT, processQ):
self.locator = (IP, PORT, randint(0,9999))
self.receiver = RecvPipe(IP, PORT)
self.receiver.start()
self.sessions = {}
self.processQ = processQ
self.nextSessionID = 1
def poll(self):
self.process()
for loc in self.sessions.keys():
self.sessions[loc].poll()
# Kill old sessions
for loc in self.sessions.keys():
if time.time() - self.sessions[loc].rxTime > TIMEOUT:
self.sessions.pop(loc)
def process(self):
while not self.receiver.empty():
packet = self.receiver.recv()[0]
locator, data = json.loads(packet)
locator = tuple(locator)
if locator not in self.serverList():
self.sessions[locator] = Session(locator[0],
locator[1],
locator[2],
self)
self.sessions[locator].event()
if data == "ping":
print "RX: ", locator, data
self.sessions[locator].send("pong")
elif data == "pong":
print "RX: ", locator, data
else:
print "RX: ", locator, data
self.processQ.put((locator, data))
def send(self, locator, data):
if locator in self.sessions.keys():
self.sessions[locator].send(data)
def serverList(self):
return self.sessions.keys()
class MasterSessionManager(SessionManager):
pass
class WorkerSessionManager(SessionManager):
def __init__(self, IP, PORT, mIP, mPORT, processQ):
SessionManager.__init__(self, IP, PORT, processQ)
self.defaultMasterSession = Session(mIP, mPORT, 0, self)
def poll(self):
if len(self.sessions) == 0:
self.sessions[self.defaultMasterSession.locator] = self.defaultMasterSession
elif len(self.sessions) > 1 and self.defaultMasterSession.locator in self.sessions:
del self.sessions[self.defaultMasterSession.locator]
SessionManager.poll(self)