From: <Blu...@us...> - 2009-10-24 19:35:20
|
Revision: 307 http://virtplayground.svn.sourceforge.net/virtplayground/?rev=307&view=rev Author: BlueWolf_ Date: 2009-10-24 19:35:13 +0000 (Sat, 24 Oct 2009) Log Message: ----------- * Some changes in de Callback * Added config in the core * Callback is now outside the try, in send Modified Paths: -------------- trunk/client/core/__init__.py trunk/client/core/callback.py trunk/client/core/client.py Modified: trunk/client/core/__init__.py =================================================================== --- trunk/client/core/__init__.py 2009-10-24 18:56:45 UTC (rev 306) +++ trunk/client/core/__init__.py 2009-10-24 19:35:13 UTC (rev 307) @@ -1,3 +1,8 @@ +""" +This is the client core for Virtual Playground. +You can use this by importing Client and Callback +""" + ## This file is part of Virtual Playground ## Copyright (c) 2009 Jos Ratsma + Koen Koning @@ -19,3 +24,4 @@ Client = client.Client Callback = callback.Callback +__all__ = ['Client', 'Callback'] Modified: trunk/client/core/callback.py =================================================================== --- trunk/client/core/callback.py 2009-10-24 18:56:45 UTC (rev 306) +++ trunk/client/core/callback.py 2009-10-24 19:35:13 UTC (rev 307) @@ -16,47 +16,53 @@ ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. class Callback: - def connected(self, con): + def connected(self): """ - connected(self, con) This will be called when a connection with the server is made. `con` contains the core's `Client` class, so you can use it to send stuff. - This is a placeholder. - If you want to catch this event, overwrite it. + This is placeholder. If you want to catch this event, + overwrite this in your own placeholder """ pass def disconnected(self): """ - connected(self) 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. - This is a placeholder. - If you want to catch this event, overwrite it. + This is placeholder. If you want to catch this event, + overwrite this in your own placeholder """ pass def data_received(self, data): """ - data_received(self, data) - Called when we received data from the server. + Called when we received data from the server. Normally, you + don't need this. - This is a placeholder. - If you want to catch this event, overwrite it. + data: + Dict with the data that has been received + + + This is placeholder. If you want to catch this event, + overwrite this in your own placeholder """ pass def data_send(self, data): """ - data_send(self, data) - Called when we send data to the server. + Called when we send data to the server. Normally, you + don't need this. - This is a placeholder. - If you want to catch this event, overwrite it. + data: + Dict with the data that will be send. + + + This is placeholder. If you want to catch this event, + overwrite this in your own placeholder """ pass Modified: trunk/client/core/client.py =================================================================== --- trunk/client/core/client.py 2009-10-24 18:56:45 UTC (rev 306) +++ trunk/client/core/client.py 2009-10-24 19:35:13 UTC (rev 307) @@ -20,19 +20,54 @@ class Client(threading.Thread): - def __init__(self, callback_class): - """ - This class will handle all of the connection to the server. - """ + """ + This is the client-core for Virtual Playground. This will handle the + connections and data to and from the server. + + config: + This should be a dict with settings for the client. See the + bottom of this documentation for a list of all possible + settings. + + callback_class: + The callback class will be used to notify you for certain events + that happens in the core. It has to look something like this: + + class Callback(core.Callback): + ... + + See the doc in core.Callback for more information about this. + + -------- + + The settings: + Currently, the config is a bit lonely and quiet. Do you want to + fill it? :-( - self.__call = callback_class() + """ + + def __init__(self, config, callback_class): + + self.__call = callback_class self.__sock = None self.__pinger = None + + # Create all default settings + self.__config_default(config) + self.__config = config + + self.__parse = Parser(self.__call, self) self.is_online = True threading.Thread.__init__(self) - + + + def __config_default(self, config): + #config.setdefault('whatever', 'duh') + pass + + def connect(self, host, port): """ Will (try to) connect to the server on `host`:`port`. @@ -64,7 +99,7 @@ except: time.sleep(1) - self.__call.connected(self) + self.__call.connected() self.__pinger = self.__Pinger(self.send) @@ -93,22 +128,21 @@ def send(self, data_header, data_body = {}): """ - send(data_header, data_body = {}) => None Sends `data_body` of type `data_header` to the server. It will automatically be encoded as JSON, and the delimeter character (chr(1)) will be send automatically too. `Data_header` is a string, `data_body` a dict. """ + + self.__call.data_send({data_header:data_body}) + data = simplejson.dumps({data_header:data_body}) try: - self.__call.data_send({data_header:data_body}) - data = simplejson.dumps({data_header:data_body}) self.__sock.send(data + chr(1)) except: pass def close(self): """ - close(self) => None Will close the connection to the server. If the connection in already down, it will raise an exception. This will trigger the callback `disconnected`. It may reconnect, depending on the This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |