-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathconduits.py
40 lines (28 loc) · 1009 Bytes
/
conduits.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
import protocols
class Conduit:
downlink = None
def __init__(self):
self.handlers = {}
self.setup()
def setup(self):
pass
def setDownlink(self, downlink):
self.downlink = downlink
def packetReceived(self, packet, fullPacket):
self.dispatch(packet, fullPacket)
def dispatch(self, packet, fullPacket):
target = self.handlers.get(packet.payload.name, self.handlers["default"])
target.packetReceived(packet.payload, fullPacket)
def sendPacket(self, packet):
print "sendPacket", repr(packet)
self.downlink.sendPacket(packet)
def registerHandler(self, name, protocol):
self.handlers[name] = protocol
class Protocol(Conduit):
def sendPacket(self, packet):
protocols["driver"].sendPacket(packet)
class Scream(Protocol):
def __init__(self, name):
self.name = name
def packetReceived(self, packet, fullPacket):
print "scream", self.name, repr(fullPacket)