From: <ch...@us...> - 2010-08-25 19:59:00
|
Revision: 373 http://virtplayground.svn.sourceforge.net/virtplayground/?rev=373&view=rev Author: chozone Date: 2010-08-25 19:58:54 +0000 (Wed, 25 Aug 2010) Log Message: ----------- [server] * Added list with files (name, url and checksum) that the client will receive upon logging in, so it knows where to download stuff. [server-core] * Sending/receiving custom messages Modified Paths: -------------- trunk/server/VPS.py trunk/server/callback.py trunk/server/core/callback.py trunk/server/core/parser.py trunk/server/core/server.py trunk/server/database trunk/server/functions.py Added Paths: ----------- trunk/server/filetable.py Modified: trunk/server/VPS.py =================================================================== --- trunk/server/VPS.py 2010-08-25 12:45:22 UTC (rev 372) +++ trunk/server/VPS.py 2010-08-25 19:58:54 UTC (rev 373) @@ -18,6 +18,7 @@ ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. import core + from functions import * from callback import Callback @@ -78,6 +79,8 @@ check_database() + +file_table.update_from_database() server = core.Server(config, Callback()) server.start_server() Modified: trunk/server/callback.py =================================================================== --- trunk/server/callback.py 2010-08-25 12:45:22 UTC (rev 372) +++ trunk/server/callback.py 2010-08-25 19:58:54 UTC (rev 373) @@ -36,6 +36,11 @@ def enters_vp(self, cid): print " > %s: %s"%(cid, self.clients[cid]['con'].ip) + + # Send locations and checksums of files to client app, so they can + # download what they need. + self.clients[cid]['con'].custom_send("filetable", file_table.table) + def leaves_vp(self, cid): print " < %s: %s"%(cid, self.clients[cid]['con'].ip) Modified: trunk/server/core/callback.py =================================================================== --- trunk/server/core/callback.py 2010-08-25 12:45:22 UTC (rev 372) +++ trunk/server/core/callback.py 2010-08-25 19:58:54 UTC (rev 373) @@ -138,6 +138,21 @@ in your own callback class. """ pass + + def custom_received(self, cid, header, body): + """ + This is called when the core received custom data from the app of a + client. This can be used to send custom (non-core) stuff, eg money. + + header: + What type the body is. (string) + body: + The body of the message. (can basically be any kind of object) + + This is a placeholder. If you want to catch this event, overwrite this + in your own callback class. + """ + pass def debug_crash(self, traceback): Modified: trunk/server/core/parser.py =================================================================== --- trunk/server/core/parser.py 2010-08-25 12:45:22 UTC (rev 372) +++ trunk/server/core/parser.py 2010-08-25 19:58:54 UTC (rev 373) @@ -274,4 +274,16 @@ connection.send("error", {"reason":result}) else: connection.send("signup") + + def custom(self, cid, msg): + """ + A custom command send from the client app. + + * header: + The type of the message + * body: + The message itself + """ + + self.call.custom(cid, msg['header'], msg['body']) \ No newline at end of file Modified: trunk/server/core/server.py =================================================================== --- trunk/server/core/server.py 2010-08-25 12:45:22 UTC (rev 372) +++ trunk/server/core/server.py 2010-08-25 19:58:54 UTC (rev 373) @@ -377,6 +377,16 @@ del self.__clients[self.__cid] + def custom_send(self, data_header, data_body = {}): + """ + Sends custom data 'data_body' of type 'data_header' directly to the + custom_received of the client app. This can be used to implement + non-core stuff like money. + """ + + self.send("custom", {"header": data_header, "body": data_body}) + + def send(self, data_header, data_body = {}): """ Sends `data_body` of type `data_header` to the client. It will Modified: trunk/server/database =================================================================== --- trunk/server/database 2010-08-25 12:45:22 UTC (rev 372) +++ trunk/server/database 2010-08-25 19:58:54 UTC (rev 373) @@ -16,5 +16,13 @@ `lastonline` timestamp NOT NULL, `lastip` varchar(255) NOT NULL, PRIMARY KEY (`id`) -) ENGINE=MyISAM DEFAULT CHARSET=latin1;''' +) ENGINE=MyISAM DEFAULT CHARSET=latin1;''' +files = '''CREATE TABLE IF NOT EXISTS `files` ( + `id` int(11) NOT NULL auto_increment, + `filename` varchar(255) NOT NULL, + `url` varchar(255) NOT NULL, + `checksum` varchar(32) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1;''' + Added: trunk/server/filetable.py =================================================================== --- trunk/server/filetable.py (rev 0) +++ trunk/server/filetable.py 2010-08-25 19:58:54 UTC (rev 373) @@ -0,0 +1,55 @@ +## This file is part of Virtual Playground +## Copyright (c) 2009-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. + +class FileTable(): + def __init__(self, db): + self.table = {} + self.db = db + + def update_from_database(self): + self.table = {} + + self.db.execute("SELECT `filename`, `url`, `checksum` FROM `files`") + table = self.db.fetchall() + for file in table: + fullurl = file['url'].strip('/') + fullurl = "%s/%s" % (fullurl, file['filename']) + self.table[file['filename']] = {'url': fullurl, + 'checksum': file['checksum']} + + return self.table + + def add(self, filename, url, checksum): + filename = filename.strip('/') + url = url.strip('/') + if filename in self.table.keys(): + raise FileTableError("File already exists in table!") + + db.execute("INSERT INTO `files` (`id`, `filename`, `url`, `checksum`) \ + VALUES (NULL, %s, %s, %s)", (filename, url, checksum)) + + fullurl = "%s/%s" % (url, filename) + self.table[filename] = {'url': fullurl, + 'checksum': checksum} + + + + class FileTableError(Exception): + def __init__(self, value): + self.value = value + def __str__(self): + return repr(self.value) \ No newline at end of file Modified: trunk/server/functions.py =================================================================== --- trunk/server/functions.py 2010-08-25 12:45:22 UTC (rev 372) +++ trunk/server/functions.py 2010-08-25 19:58:54 UTC (rev 373) @@ -19,6 +19,7 @@ from getpass import getpass import database +from filetable import FileTable try: from configobj import ConfigObj except: sys.exit("ERROR: You need the configobj-module for this to work!") @@ -67,6 +68,8 @@ config['database']['password']) print "Connected to database `%s` on `%s`" % (config['database']['database'], config['database']['host']) + +file_table = FileTable(db) \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |