From: <Blu...@us...> - 2010-09-04 20:55:44
|
Revision: 389 http://virtplayground.svn.sourceforge.net/virtplayground/?rev=389&view=rev Author: BlueWolf_ Date: 2010-09-04 20:55:38 +0000 (Sat, 04 Sep 2010) Log Message: ----------- Spreading some code across multiple files to make them more findable Modified Paths: -------------- trunk/client/VP.py trunk/client/functions.py trunk/client/playground.py Added Paths: ----------- trunk/client/callback.py trunk/client/timer.py Modified: trunk/client/VP.py =================================================================== --- trunk/client/VP.py 2010-09-01 21:01:21 UTC (rev 388) +++ trunk/client/VP.py 2010-09-04 20:55:38 UTC (rev 389) @@ -25,6 +25,8 @@ from layout import Layout from windows import Windows from downloader import Downloader +from timer import Timer +from callback import Callback class Main(): @@ -147,164 +149,5 @@ sh['layout'].call(name, data) sh['windows'].call(name, data) -class Timer(): - """ - This timer will update every frame as defined in the config - """ - def __init__(self): - self.speed = 1/float(sh['config']['graphics']['fps']) - self.callers = {} - self.timer = None - - def start(self, name, call, *extra): - # Start a timer for this call - - self.callers[name] = (call, extra) - - if self.timer == None: # Start a timer - self.timer = threading.Timer(self.speed, self.update) - self.timer.start() - - def stop(self, name): - try: del self.callers[name] - except: pass - - if self.callers == {} and self.timer: - self.timer.cancel() - self.timer = None - - def update(self): - # Stop blitting to the screen - sh['main'].updaterect = [] - sh['main'].updatewaiting = True - - # Do stuff - for name, info in self.callers.items(): - if info[0](name, *info[1]): - del self.callers[name] - - # Force one big update - sh['main'].updatewaiting = False - if sh['main'].updaterect != []: - sh['main'].update(sh['main'].updaterect) - sh['main'].updaterect = [] - - - # Start the next timer - if self.callers != {}: - self.timer = threading.Timer(self.speed, self.update) - self.timer.start() - else: - self.timer = None - -class Callback(core.Callback): - - def data_received(self, data): - print " --> \t" + repr(data) - - def data_send(self, data): - print " <-- \t" + repr(data) - - def connection_ready(self, public_rsa, reconnecting): - # We are connected - - if reconnecting: - sh['main'].call("status", { - "status": "login", - "where": "logging in" - }) - else: - # Do we need to log in automatically? - if sh['config']['user']['username'] != "": - sh['main'].call("status", { - "status": "login", - "where": "logging in" - }) - sh['client'].login(sh['config']['user']['username'], \ - sh['config']['user']['password']) - - else: - sh['main'].call("status", { - "status": "login", - "where": "waiting" - }) - - def logged_in(self, username, cid, owner): - sh['main'].call("status", { - "status": "login", - "where": "downloading" - }) - - # Save the login - if sh['login_info'] != None: - sh['config']['user']['username'] = sh['login_info'][0] - sh['config']['user']['password'] = sh['login_info'][1] - sh['login_info'] = None - sh['config'].write() - - def failed_logging_in(self, reason): - # [TODO] Send the reason in some way - - sh['main'].call("status", { - "status": "login", - "where": "waiting" - }) - - sh['config']['user']['username'] = "" - sh['config']['user']['password'] = "" - sh['login_info'] = None - sh['config'].write() - - - def disconnected(self, reason): - print " !!! \tConnection closed: " + reason - - sh['login_info'] = None - - if reason == "manual": return - - # [TODO] Send the reason in some way - if reason in ["closed", "gone offline"]: - # Reconnect while logging in - sh['main'].call("status", { - "status": "login", - "where": "connecting" - }) - return True - - elif reason in ["duplicate", "crash"]: - # Reconnecting while not logging in - sh['main'].call("status", { - "status": "login", - "where": "connecting" - }) - - # But don't reconnect when we where a duplicate - if reason == "duplicate": - sh['config']['user']['username'] = "" - sh['config']['user']['password'] = "" - sh['config'].write() - - sh['client'].connect(sh['config']['host']['host'], \ - sh['config']['host']['port']) - - else: - # Not doing anything at all - # [TODO] Show popup or something, without reconnecting? - sh['main'].call("status", { - "status": "login", - "where": "connecting" - }) - - def custom_received(self, header, body): - if header == "filetable": # List of all the downloadable objects - sh['filetable'] = body - - sh['main'].call("filetable", { - "action": "update" - }) - - - if __name__ == "__main__": Main() Added: trunk/client/callback.py =================================================================== --- trunk/client/callback.py (rev 0) +++ trunk/client/callback.py 2010-09-04 20:55:38 UTC (rev 389) @@ -0,0 +1,128 @@ +#!/usr/bin/env python + +## This file is part of Virtual Playground +## Copyright (c) 2010 Jos Ratsma + Koen Koning + +## This program is free software; you can redistribute it and/or +## modify it under the terms of the GNU General Public License +## as published by the Free Software Foundation; either +## version 2 of the License, or (at your option) any later version. + +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. + +## You should have received a copy of the GNU General Public License +## along with this program; if not, write to the Free Software +## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +from functions import * +import core + +class Callback(core.Callback): + + def data_received(self, data): + print " --> \t" + repr(data) + + def data_send(self, data): + print " <-- \t" + repr(data) + + def connection_ready(self, public_rsa, reconnecting): + # We are connected + + if reconnecting: + sh['main'].call("status", { + "status": "login", + "where": "logging in" + }) + else: + # Do we need to log in automatically? + if sh['config']['user']['username'] != "": + sh['main'].call("status", { + "status": "login", + "where": "logging in" + }) + sh['client'].login(sh['config']['user']['username'], \ + sh['config']['user']['password']) + + else: + sh['main'].call("status", { + "status": "login", + "where": "waiting" + }) + + def logged_in(self, username, cid, owner): + sh['main'].call("status", { + "status": "login", + "where": "downloading" + }) + + # Save the login + if sh['login_info'] != None: + sh['config']['user']['username'] = sh['login_info'][0] + sh['config']['user']['password'] = sh['login_info'][1] + sh['login_info'] = None + sh['config'].write() + + def failed_logging_in(self, reason): + # [TODO] Send the reason in some way + + sh['main'].call("status", { + "status": "login", + "where": "waiting" + }) + + sh['config']['user']['username'] = "" + sh['config']['user']['password'] = "" + sh['login_info'] = None + sh['config'].write() + + + def disconnected(self, reason): + print " !!! \tConnection closed: " + reason + + sh['login_info'] = None + + if reason == "manual": return + + # [TODO] Send the reason in some way + if reason in ["closed", "gone offline"]: + # Reconnect while logging in + sh['main'].call("status", { + "status": "login", + "where": "connecting" + }) + return True + + elif reason in ["duplicate", "crash"]: + # Reconnecting while not logging in + sh['main'].call("status", { + "status": "login", + "where": "connecting" + }) + + # But don't reconnect when we where a duplicate + if reason == "duplicate": + sh['config']['user']['username'] = "" + sh['config']['user']['password'] = "" + sh['config'].write() + + sh['client'].connect(sh['config']['host']['host'], \ + sh['config']['host']['port']) + + else: + # Not doing anything at all + # [TODO] Show popup or something, without reconnecting? + sh['main'].call("status", { + "status": "login", + "where": "connecting" + }) + + def custom_received(self, header, body): + if header == "filetable": # List of all the downloadable objects + sh['filetable'] = body + + sh['main'].call("filetable", { + "action": "update" + }) Modified: trunk/client/functions.py =================================================================== --- trunk/client/functions.py 2010-09-01 21:01:21 UTC (rev 388) +++ trunk/client/functions.py 2010-09-04 20:55:38 UTC (rev 389) @@ -87,9 +87,3 @@ print "Config: The '%s' section is missing" % section_list[0] sys.exit() - - - -# GUI needs the functions again. That's why it has to be imported after all -# functions have been defined -import gui Modified: trunk/client/playground.py =================================================================== --- trunk/client/playground.py 2010-09-01 21:01:21 UTC (rev 388) +++ trunk/client/playground.py 2010-09-04 20:55:38 UTC (rev 389) @@ -16,6 +16,7 @@ ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. from functions import * +import gui class Playground(): Added: trunk/client/timer.py =================================================================== --- trunk/client/timer.py (rev 0) +++ trunk/client/timer.py 2010-09-04 20:55:38 UTC (rev 389) @@ -0,0 +1,70 @@ +#!/usr/bin/env python + +## This file is part of Virtual Playground +## Copyright (c) 2010 Jos Ratsma + Koen Koning + +## This program is free software; you can redistribute it and/or +## modify it under the terms of the GNU General Public License +## as published by the Free Software Foundation; either +## version 2 of the License, or (at your option) any later version. + +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. + +## You should have received a copy of the GNU General Public License +## along with this program; if not, write to the Free Software +## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +from functions import * + +class Timer(): + """ + This timer will update every frame as defined in the config + """ + def __init__(self): + self.speed = 1/float(sh['config']['graphics']['fps']) + self.callers = {} + self.timer = None + + def start(self, name, call, *extra): + # Start a timer for this call + + self.callers[name] = (call, extra) + + if self.timer == None: # Start a timer + self.timer = threading.Timer(self.speed, self.update) + self.timer.start() + + def stop(self, name): + try: del self.callers[name] + except: pass + + if self.callers == {} and self.timer: + self.timer.cancel() + self.timer = None + + def update(self): + # Stop blitting to the screen + sh['main'].updaterect = [] + sh['main'].updatewaiting = True + + # Do stuff + for name, info in self.callers.items(): + if info[0](name, *info[1]): + del self.callers[name] + + # Force one big update + sh['main'].updatewaiting = False + if sh['main'].updaterect != []: + sh['main'].update(sh['main'].updaterect) + sh['main'].updaterect = [] + + + # Start the next timer + if self.callers != {}: + self.timer = threading.Timer(self.speed, self.update) + self.timer.start() + else: + self.timer = None This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |