Handler and thread cleanup on client disconnect

develop
Sven Slootweg 13 years ago
parent 185af55f84
commit ca9d12bb9c

@ -13,17 +13,23 @@ def remove_from_list(ls, val):
return [value for value in ls if value is not val] return [value for value in ls if value is not val]
class PingThread(threading.Thread): class PingThread(threading.Thread):
aborted = False
def __init__(self, channel): def __init__(self, channel):
self.channel = channel self.channel = channel
threading.Thread.__init__(self) threading.Thread.__init__(self)
self.daemon = True self.daemon = True
def run(self): def run(self):
while True: while self.aborted is False:
self.pingkey = ''.join(random.choice(string.letters + string.digits) for i in xrange(5)) self.pingkey = ''.join(random.choice(string.letters + string.digits) for i in xrange(5))
self.pingtime = datetime.time(datetime.now()) self.pingtime = datetime.time(datetime.now())
self.channel.send("PING %s" % self.pingkey) self.channel.send("PING %s" % self.pingkey)
time.sleep(10) time.sleep(10)
def abort(self):
self.aborted = True
print "Aborted thread."
class Client: class Client:
buff = "" buff = ""
@ -59,6 +65,11 @@ class Client:
self.channel_map[channel_numeric].process_chunk(data) self.channel_map[channel_numeric].process_chunk(data)
else: else:
print "WARNING: Received data on non-existent channel %d" % channel_numeric print "WARNING: Received data on non-existent channel %d" % channel_numeric
def end(self):
for key, chan in self.channel_map.iteritems():
print "Destructing handler for channel %d" % chan.numeric
chan.handler.destruct()
class Channel: class Channel:
numeric = 0 numeric = 0
@ -86,6 +97,9 @@ class Handler:
def process(self, chunk): def process(self, chunk):
pass pass
def destruct(self):
pass
class ControlHandler(Handler): class ControlHandler(Handler):
pingthread = None pingthread = None
@ -134,6 +148,9 @@ class ControlHandler(Handler):
seconds_end = current_time.second + microseconds seconds_end = current_time.second + microseconds
print "Latency: %f seconds" % (seconds_end - seconds_start) print "Latency: %f seconds" % (seconds_end - seconds_start)
def destruct(self):
self.pingthread.abort()
class EchoHandler(Handler): class EchoHandler(Handler):
def process(self, chunk): def process(self, chunk):
@ -196,11 +213,12 @@ class SSLDaemon:
self.client_list.append(new_client) self.client_list.append(new_client)
else: else:
data = sock.recv(1024) data = sock.recv(1024)
cur_client = self.client_map[sock.fileno()]
if data: if data:
cur_client = self.client_map[sock.fileno()]
cur_client.process_data(data) cur_client.process_data(data)
else: else:
cur_client.end()
self.select_inputs = remove_from_list(self.select_inputs, sock) self.select_inputs = remove_from_list(self.select_inputs, sock)
print "NOTICE: Client disconnected" print "NOTICE: Client disconnected"
except ssl.SSLError, err: except ssl.SSLError, err:

Loading…
Cancel
Save