From: <Blu...@us...> - 2009-11-15 16:19:27
|
Revision: 309 http://virtplayground.svn.sourceforge.net/virtplayground/?rev=309&view=rev Author: BlueWolf_ Date: 2009-11-15 16:19:20 +0000 (Sun, 15 Nov 2009) Log Message: ----------- Added reason in callback.disconnected and the core can now properly reconnect Modified Paths: -------------- trunk/client/core/callback.py trunk/client/core/client.py Modified: trunk/client/core/callback.py =================================================================== --- trunk/client/core/callback.py 2009-11-14 23:08:44 UTC (rev 308) +++ trunk/client/core/callback.py 2009-11-15 16:19:20 UTC (rev 309) @@ -23,18 +23,29 @@ send stuff. This is placeholder. If you want to catch this event, - overwrite this in your own placeholder + overwrite this in your own callback """ pass - def disconnected(self): + def disconnected(self, reason): """ This is called when the connection dies. For example when you close the connection manually, the server kicks you, your modem explodes or your cat eates the LAN cable. + reason: + A string, representing the reason why it disconnected. + It currently only has two options: + * "server" - Server dropped the connection + * "manual" - Client (you) closed the connection + with .close() + + + Return True if you want it to reconnect. Be warned that this + event also will be raised when you do .close()! + This is placeholder. If you want to catch this event, - overwrite this in your own placeholder + overwrite this in your own callback """ pass @@ -48,7 +59,7 @@ This is placeholder. If you want to catch this event, - overwrite this in your own placeholder + overwrite this in your own callback """ pass @@ -62,7 +73,7 @@ This is placeholder. If you want to catch this event, - overwrite this in your own placeholder + overwrite this in your own callback """ pass Modified: trunk/client/core/client.py =================================================================== --- trunk/client/core/client.py 2009-11-14 23:08:44 UTC (rev 308) +++ trunk/client/core/client.py 2009-11-15 16:19:20 UTC (rev 309) @@ -18,7 +18,6 @@ import simplejson, socket, threading, time from parser import Parser - class Client(threading.Thread): """ This is the client-core for Virtual Playground. This will handle the @@ -55,12 +54,18 @@ # Create all default settings self.__config_default(config) self.__config = config - self.__parse = Parser(self.__call, self) - self.is_online = True + self.__is_online = False + self.__disconnect_reason = None + self.__do_connect = threading.Event() + self.__do_connect.clear() + threading.Thread.__init__(self) + + self.setDaemon(True) + self.start() def __config_default(self, config): @@ -81,51 +86,65 @@ if self.__sock: raise ConnectionError("You are already connected!") - self.__sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.__do_connect.set() # Release the hounds - self.start() def run(self): """ Used by threading, not for external usage. """ - - #TODO: reconnect loop thingy here - while 1: - try: - self.__sock.connect((self.__host, self.__port)) - break - except: - time.sleep(1) + self.__do_connect.wait() # Wait to connect + + self.__is_online = True + self.__disconnect_reason = None + + self.__sock = socket.socket(socket.AF_INET, + socket.SOCK_STREAM) + + while 1: + try: + self.__sock.connect((self.__host, + self.__port)) + break + except: + time.sleep(1) - self.__call.connected() + self.__call.connected() - self.__pinger = self.__Pinger(self.send) + self.__pinger = self.__Pinger(self.send) - #Infinite loop that receives data - buffer = '' - while 1: - try: - data = self.__sock.recv(1024) - except: - if self.is_online: self.close() - return - if not data: - if self.is_online: self.close() - return + #Infinite loop that receives data + buffer = '' + while 1: + try: + data = self.__sock.recv(1024) + except: + print 'except' + self.__disconnect_reason = "close" + break + if not data: + self.__disconnect_reason = "close" + break - buffer += data - #Each dataset must end with a delimiter: chr(1) - if chr(1) in buffer: + buffer += data + + #Each dataset must end with a delimiter: chr(1) + if chr(1) not in buffer: continue + data = buffer.split(chr(1)) buffer = data[-1] data = data[:-1] for msg in data: self.__parse(simplejson.loads(msg)) - + + if self.__is_online: + self.close() + + + def send(self, data_header, data_body = {}): """ Sends `data_body` of type `data_header` to the server. It will @@ -149,16 +168,32 @@ callback. If not, you can reuse this class by calling `connect`. """ - if not self.is_online: - raise ConnectionError("The connection is already \ - closed!") + if not self.__is_online: + raise ConnectionError("The connection is already " \ + "closed!") + # What's the reason, the meaning of this? + if self.__disconnect_reason == "close": # 42 + reason = "server" + else: + reason = "manual" + + + self.__do_connect.clear() # Block the thread-loop + + self.__is_online = False + self.__pinger.cancel() + self.__pinger = None + + self.__sock.shutdown(0) self.__sock.close() - self.is_online = False - if self.__call.disconnected(): - self.__sock = None - self.connect(self.__host, self.__port) + self.__sock = None + + if self.__call.disconnected(reason): + # Reconnect + self.__do_connect.set() + class __Pinger(): def __init__(self, send): """ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |