-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathChannel.py
More file actions
58 lines (46 loc) · 1.29 KB
/
Channel.py
File metadata and controls
58 lines (46 loc) · 1.29 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
import asynchat
import sys, traceback
from rencode import loads, dumps
class Channel(asynchat.async_chat):
endchars = '\0---\0'
def __init__(self, conn=None, addr=(), server=None):
asynchat.async_chat.__init__(self, conn)
self.addr = addr
self._server = server
self._ibuffer = ""
self.set_terminator(self.endchars)
self.sendqueue = []
def collect_incoming_data(self, data):
self._ibuffer += data
def found_terminator(self):
data = loads(self._ibuffer)
self._ibuffer = ""
if type(dict()) == type(data) and data.has_key('action'):
[getattr(self, n)(data) for n in ('Network_' + data['action'], 'Network') if hasattr(self, n)]
else:
print "OOB data:", data
def Pump(self):
[asynchat.async_chat.push(self, d) for d in self.sendqueue]
self.sendqueue = []
def Send(self, data):
self.sendqueue.append(dumps(data) + self.endchars)
def handle_connect(self):
if hasattr(self, "Connected"):
self.Connected()
else:
print "Unhandled Connected()"
def handle_error(self):
try:
self.close()
except:
pass
if hasattr(self, "Error"):
self.Error(sys.exc_info()[1])
else:
asynchat.async_chat.handle_error(self)
def handle_expt(self):
pass
def handle_close(self):
if hasattr(self, "Close"):
self.Close()
asynchat.async_chat.handle_close(self)