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. |
From: <ch...@us...> - 2010-08-28 13:36:17
|
Revision: 377 http://virtplayground.svn.sourceforge.net/virtplayground/?rev=377&view=rev Author: chozone Date: 2010-08-28 13:36:11 +0000 (Sat, 28 Aug 2010) Log Message: ----------- Added landscapes to server (retrieving them from database + sending them to client upon login) Modified Paths: -------------- trunk/server/VPS.py trunk/server/callback.py trunk/server/database trunk/server/filetable.py trunk/server/functions.py Added Paths: ----------- trunk/server/landscapes.py Modified: trunk/server/VPS.py =================================================================== --- trunk/server/VPS.py 2010-08-27 21:40:53 UTC (rev 376) +++ trunk/server/VPS.py 2010-08-28 13:36:11 UTC (rev 377) @@ -43,7 +43,7 @@ # Users table has no admins create_account_setup() - print "Database check done..." + print "Database check done." def create_account_setup(): # Ask user if he wants to create an account (1st one, admin) @@ -81,6 +81,8 @@ check_database() file_table.update_from_database() +landscapes.update_from_database() + server = core.Server(config, Callback()) server.start_server() Modified: trunk/server/callback.py =================================================================== --- trunk/server/callback.py 2010-08-27 21:40:53 UTC (rev 376) +++ trunk/server/callback.py 2010-08-28 13:36:11 UTC (rev 377) @@ -41,6 +41,8 @@ # download what they need. self.clients[cid]['con'].custom_send("filetable", file_table.table) + self.clients[cid]['con'].custom_send("landscapes", landscapes.list) + def leaves_vp(self, cid): print " < %s: %s"%(cid, self.clients[cid]['con'].ip) Modified: trunk/server/database =================================================================== --- trunk/server/database 2010-08-27 21:40:53 UTC (rev 376) +++ trunk/server/database 2010-08-28 13:36:11 UTC (rev 377) @@ -26,3 +26,12 @@ PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;''' +landscapes = '''CREATE TABLE IF NOT EXISTS `landscapes` ( + `id` int(11) NOT NULL auto_increment, + `type` varchar(255) NOT NULL, + `direction` varchar(255) NOT NULL, + `position` int(11) NOT NULL, + `transition` int(11) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1;''' + Modified: trunk/server/filetable.py =================================================================== --- trunk/server/filetable.py 2010-08-27 21:40:53 UTC (rev 376) +++ trunk/server/filetable.py 2010-08-28 13:36:11 UTC (rev 377) @@ -21,6 +21,8 @@ self.db = db def update_from_database(self): + print "Retrieving file table from database..." + self.table = {} self.db.execute("SELECT `filename`, `url`, `checksum` FROM `files`") @@ -31,6 +33,8 @@ self.table[file['filename']] = {'url': fullurl, 'checksum': file['checksum']} + + print "Done, found %s entries in file table." % len(self.table) return self.table def add(self, filename, url, checksum): Modified: trunk/server/functions.py =================================================================== --- trunk/server/functions.py 2010-08-27 21:40:53 UTC (rev 376) +++ trunk/server/functions.py 2010-08-28 13:36:11 UTC (rev 377) @@ -20,6 +20,7 @@ import database from filetable import FileTable +from landscapes import LandscapeManager try: from configobj import ConfigObj except: sys.exit("ERROR: You need the configobj-module for this to work!") @@ -70,6 +71,7 @@ config['database']['host']) file_table = FileTable(db) +landscapes = LandscapeManager(db) \ No newline at end of file Added: trunk/server/landscapes.py =================================================================== --- trunk/server/landscapes.py (rev 0) +++ trunk/server/landscapes.py 2010-08-28 13:36:11 UTC (rev 377) @@ -0,0 +1,34 @@ +## 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 LandscapeManager(): + def __init__(self, db): + self.list = () + self.db = db + + def update_from_database(self): + print "Retrieving landscapes from database..." + + self.list = {} + + self.db.execute("SELECT `type`, `direction`, `position`, 'transition' \ + FROM `landscapes` ORDER BY `id` ASC") # Order in DB = order in world + self.list = self.db.fetchall() + + print "Done, found %s landscapes." % len(self.list) + + return self.list \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ch...@us...> - 2010-09-12 01:01:38
|
Revision: 402 http://virtplayground.svn.sourceforge.net/virtplayground/?rev=402&view=rev Author: chozone Date: 2010-09-12 01:01:32 +0000 (Sun, 12 Sep 2010) Log Message: ----------- Implemented the log system of the client in the server. Some messages should probably be changed and better categorized, but it's a start. Modified Paths: -------------- trunk/server/VPS.py trunk/server/callback.py trunk/server/filetable.py trunk/server/functions.py trunk/server/landscapes.py Modified: trunk/server/VPS.py =================================================================== --- trunk/server/VPS.py 2010-09-11 23:32:12 UTC (rev 401) +++ trunk/server/VPS.py 2010-09-12 01:01:32 UTC (rev 402) @@ -24,7 +24,7 @@ def check_database(): - print "Checking database..." + log('debug', "Checking database...") db.execute("SHOW TABLES") ret = db.fetchall() db_tables = [i.values()[0] for i in ret] @@ -34,7 +34,7 @@ for t in req_tables: if t not in db_tables: - print "Creating table `%s`..."%t + log('debug', "Creating table `%s`..."%t) db.execute(db_dump[t]) @@ -43,7 +43,7 @@ # Users table has no admins create_account_setup() - print "Database check done." + log('debug', "Database check done.") def create_account_setup(): # Ask user if he wants to create an account (1st one, admin) @@ -75,28 +75,37 @@ VALUES (NULL, %s, %s, 'admin', 0, '', '', '', NOW(), '127.0.0.1')", (username, password)) - print "Created user '%s'!"%username + log('succeed', "Created user '%s'!"%username) -check_database() +def main(): + check_database() + + log('debug', "Retrieving file table from database...") + file_table.update_from_database() + log('debug', "Done, %s entries in file table." % len(file_table.table)) + + log('debug', "Retrieving landscapes from database...") + landscapes.update_from_database() + log('debug', "Done, found %s landscapes." % len(landscapes.list)) -file_table.update_from_database() -landscapes.update_from_database() + + server = core.Server(config, Callback()) + server.start_server() - -server = core.Server(config, Callback()) -server.start_server() + # Now we sit back and let the server do its stuff. Only stop at + # KeyboardInterrupts (^C). + # TODO: *some* interactivity here? (so you can type in commands) + try: + while 1: + time.sleep(100) -# Now we sit back and let the server do its stuff. Only stop at -# KeyboardInterrupts (^C). -# TODO: *some* interactivity here? (so you can type in commands) -try: - while 1: - time.sleep(100) + except KeyboardInterrupt: + pass -except KeyboardInterrupt: - print + + server.exit() + log('succeed', "Server down") + sys.exit() - -server.exit() -sys.exit() +if __name__ == '__main__': main() Modified: trunk/server/callback.py =================================================================== --- trunk/server/callback.py 2010-09-11 23:32:12 UTC (rev 401) +++ trunk/server/callback.py 2010-09-12 01:01:32 UTC (rev 402) @@ -23,19 +23,19 @@ def __init__(self): self.clients = {} def data_received(self, cid, data): - print " --> " + cid + ": " + repr(data) + log('sendin', cid + ": " + repr(data)) def data_send(self, cid, data): - print " <-- " + cid + ": " + repr(data) + log('sendout', cid + ": " + repr(data)) def debug_crash(self, traceback): - print "\n*** Crash ***\n\n%s\n" % traceback + log('error', "\n*** Crash ***\n\n%s\n" % traceback) def server_online(self, clients): self.clients = clients - print "*** SERVER ONLINE ***" + log('succeed', "*** SERVER ONLINE ***") def enters_vp(self, cid): - print " > %s: %s"%(cid, self.clients[cid]['con'].ip) + log('debug', " > %s: %s"%(cid, self.clients[cid]['con'].ip)) # Send locations and checksums of files to client app, so they can # download what they need. @@ -44,13 +44,13 @@ self.clients[cid]['con'].custom_send("landscapes", landscapes.list) def leaves_vp(self, cid): - print " < %s: %s"%(cid, self.clients[cid]['con'].ip) + log('debug', " < %s: %s"%(cid, self.clients[cid]['con'].ip)) def check_login(self, cid, usr, pwd): db.execute("SELECT `username` FROM `users` WHERE \ `username`=%s AND \ `password`=%s", (usr, pwd)) - print "* Login %s %s" % (usr, pwd) + log('debug', "* Login %s %s" % (usr, pwd)) result = db.fetchone() if result is None: return False @@ -71,6 +71,6 @@ (usr, pwd, extra['email'], extra['realname'], self.clients[cid]['con'].ip)) - print "* Signup %s" % usr + log('debug', "* Signup %s" % usr) return False # All ok, no errors Modified: trunk/server/filetable.py =================================================================== --- trunk/server/filetable.py 2010-09-11 23:32:12 UTC (rev 401) +++ trunk/server/filetable.py 2010-09-12 01:01:32 UTC (rev 402) @@ -21,8 +21,6 @@ self.db = db def update_from_database(self): - print "Retrieving file table from database..." - self.table = {} self.db.execute("SELECT `filename`, `url`, `checksum` FROM `files`") @@ -33,8 +31,6 @@ self.table[file['filename']] = {'url': fullurl, 'checksum': file['checksum']} - - print "Done, found %s entries in file table." % len(self.table) return self.table def add(self, filename, url, checksum): Modified: trunk/server/functions.py =================================================================== --- trunk/server/functions.py 2010-09-11 23:32:12 UTC (rev 401) +++ trunk/server/functions.py 2010-09-12 01:01:32 UTC (rev 402) @@ -26,9 +26,23 @@ except: sys.exit("ERROR: You need the configobj-module for this to work!") def config_defaults(config): + + ## TODO: This should (partly) be replaced by same system that client uses + ## (with the Validator, but keeping the interactive stuff) + + + if 'debug' not in config: + config['debug'] = {} + + if not 'level' in config['debug']: + config['debug']['level'] = 2 + if not 'use_colors' in config['debug']: + config['debug']['use_colors'] = True + + if 'database' not in config: config['database'] = {} - + intro_text = """ Some settings are missing from the config. Because these settings are required, We will now ask for them in this console. Please type in the correct value, and @@ -57,18 +71,68 @@ config['database']['password'] = getpass("Database password: ") +def log(t, *m): + def level(i): + return config['debug']['level'] >= i + use_colors = config['debug']['use_colors'] + write = sys.stdout.write + def color(c): + write("\033[" + c + "m") + + message = "\t".join([str(part) for part in m]) + + if t == "error" and level(1): + icon = "!!!" + icon_color = "1;31;7" + text_color = "1;91" + + elif t == "warning" and level(2): + icon = "[!]" + icon_color = "1;33;7" + text_color = "93" + + elif t == "succeed" and level(3): + icon = "OK " + icon_color = "1;32;7" + text_color = "32" + + elif t == "debug" and level(4): + icon = " ~ " + icon_color = "1;7" + text_color = "" + + elif t == "sendin" and level(5): + icon = "|<-" + icon_color = "1;100;2" + text_color = "2" + + elif t == "sendout" and level(5): + icon = "|->" + icon_color = "1;100;2" + text_color = "2" + + else: + return + + if use_colors: color(icon_color) + write(" " + icon + " ") + if use_colors: color("0;" + text_color) + write("\t" + message) + if use_colors: color("0") + write("\n") + config = ConfigObj(os.path.join(sys.path[0], 'config')) config_defaults(config) config.write() -print "Connecting to database..." +log('debug', "Connecting to database...") db = database.DB( config['database']['host'], config['database']['database'], config['database']['user'], config['database']['password']) -print "Connected to database `%s` on `%s`" % (config['database']['database'], - config['database']['host']) +log('succeed', "Connected to database `%s` on `%s`" % + (config['database']['database'], config['database']['host'])) file_table = FileTable(db) landscapes = LandscapeManager(db) Modified: trunk/server/landscapes.py =================================================================== --- trunk/server/landscapes.py 2010-09-11 23:32:12 UTC (rev 401) +++ trunk/server/landscapes.py 2010-09-12 01:01:32 UTC (rev 402) @@ -21,14 +21,10 @@ self.db = db def update_from_database(self): - print "Retrieving landscapes from database..." - self.list = {} self.db.execute("SELECT `type`, `direction`, `position`, `transition` \ FROM `landscapes` ORDER BY `id` ASC") # Order in DB = order in world self.list = self.db.fetchall() - print "Done, found %s landscapes." % len(self.list) - return self.list \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <Blu...@us...> - 2010-09-14 21:23:33
|
Revision: 405 http://virtplayground.svn.sourceforge.net/virtplayground/?rev=405&view=rev Author: BlueWolf_ Date: 2010-09-14 21:23:27 +0000 (Tue, 14 Sep 2010) Log Message: ----------- Better colors with screen (the program) now Modified Paths: -------------- trunk/server/callback.py trunk/server/functions.py Modified: trunk/server/callback.py =================================================================== --- trunk/server/callback.py 2010-09-14 19:28:33 UTC (rev 404) +++ trunk/server/callback.py 2010-09-14 21:23:27 UTC (rev 405) @@ -23,9 +23,9 @@ def __init__(self): self.clients = {} def data_received(self, cid, data): - log('sendin', cid + ": " + repr(data)) + log('recv', cid + ": " + repr(data)) def data_send(self, cid, data): - log('sendout', cid + ": " + repr(data)) + log('send', cid + ": " + repr(data)) def debug_crash(self, traceback): log('error', "\n*** Crash ***\n\n%s\n" % traceback) Modified: trunk/server/functions.py =================================================================== --- trunk/server/functions.py 2010-09-14 19:28:33 UTC (rev 404) +++ trunk/server/functions.py 2010-09-14 21:23:27 UTC (rev 405) @@ -24,6 +24,13 @@ try: from configobj import ConfigObj except: sys.exit("ERROR: You need the configobj-module for this to work!") + +# To get colors working in Windows. Though it works partially +if os.name == "nt": + try: import colarama + except: pass + else: + colarama.init() def config_defaults(config): @@ -71,54 +78,54 @@ config['database']['password'] = getpass("Database password: ") -def log(t, *m): - def level(i): - return config['debug']['level'] >= i - use_colors = config['debug']['use_colors'] - write = sys.stdout.write - def color(c): - write("\033[" + c + "m") - - message = "\t".join([str(part) for part in m]) - - if t == "error" and level(1): - icon = "!!!" - icon_color = "1;31;7" - text_color = "1;91" - - elif t == "warning" and level(2): - icon = "[!]" - icon_color = "1;33;7" - text_color = "93" - - elif t == "succeed" and level(3): - icon = "OK " - icon_color = "1;32;7" - text_color = "32" - - elif t == "debug" and level(4): - icon = " ~ " - icon_color = "1;7" - text_color = "" - - elif t == "sendin" and level(5): - icon = "|<-" - icon_color = "1;100;2" - text_color = "2" - - elif t == "sendout" and level(5): - icon = "|->" - icon_color = "1;100;2" - text_color = "2" - - else: - return - - if use_colors: color(icon_color) - write(" " + icon + " ") - if use_colors: color("0;" + text_color) - write("\t" + message) - if use_colors: color("0") +def log(t, *m): + def level(i): + return config['debug']['level'] >= i + use_colors = config['debug']['use_colors'] + write = sys.stdout.write + def color(c): + write("\033[" + c + "m") + + message = "\t".join([str(part) for part in m]) + + if t == "error" and level(1): + icon = "!!!" + icon_color = "1;31;7" + text_color = "1;91" + + elif t == "warning" and level(2): + icon = "[!]" + icon_color = "1;33;7" + text_color = "93" + + elif t == "succeed" and level(3): + icon = "OK " + icon_color = "1;32;7" + text_color = "32" + + elif t == "debug" and level(4): + icon = " ~ " + icon_color = "1;7" + text_color = "" + + elif t == "recv" and level(5): + icon = "|<-" + icon_color = "1;90;7" + text_color = "90" + + elif t == "send" and level(5): + icon = "|->" + icon_color = "1;90;7" + text_color = "90" + + else: + return + + if use_colors: color(icon_color) + write(" " + icon + " ") + if use_colors: color("0;" + text_color) + write("\t" + message) + if use_colors: color("0") write("\n") @@ -138,4 +145,4 @@ landscapes = LandscapeManager(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. |
From: <Blu...@us...> - 2010-09-15 17:45:30
|
Revision: 406 http://virtplayground.svn.sourceforge.net/virtplayground/?rev=406&view=rev Author: BlueWolf_ Date: 2010-09-15 17:45:24 +0000 (Wed, 15 Sep 2010) Log Message: ----------- Added a proper config for the core and made a lock for the database Modified Paths: -------------- trunk/server/VPS.py trunk/server/core/server.py trunk/server/database.py trunk/server/functions.py Modified: trunk/server/VPS.py =================================================================== --- trunk/server/VPS.py 2010-09-14 21:23:27 UTC (rev 405) +++ trunk/server/VPS.py 2010-09-15 17:45:24 UTC (rev 406) @@ -90,7 +90,9 @@ log('debug', "Done, found %s landscapes." % len(landscapes.list)) - server = core.Server(config, Callback()) + server = core.Server({ + "max-connections": 100 + }, Callback()) server.start_server() # Now we sit back and let the server do its stuff. Only stop at Modified: trunk/server/core/server.py =================================================================== --- trunk/server/core/server.py 2010-09-14 21:23:27 UTC (rev 405) +++ trunk/server/core/server.py 2010-09-15 17:45:24 UTC (rev 406) @@ -54,7 +54,7 @@ The port the server should listen on. Should be int between 1 and 65535. Note that everything lower than 100 usually needs to be run as root under Unix/Linux. - Default is 5162. + Default is 6653. -max_connections: Maximum amount of clients that can be connected simultaneously. Modified: trunk/server/database.py =================================================================== --- trunk/server/database.py 2010-09-14 21:23:27 UTC (rev 405) +++ trunk/server/database.py 2010-09-15 17:45:24 UTC (rev 406) @@ -15,7 +15,7 @@ ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -import sys +import sys, threading try: import MySQLdb except: sys.exit("ERROR: You need the MySQLdb-module for this to work!") @@ -26,7 +26,8 @@ self.host = host self.database = database self.user = user - self.passwd = passwd + self.passwd = passwd + self.lock = threading.Lock() self._connect() @@ -56,6 +57,8 @@ # Uncomment for mysql-debugging! #print '\tMySQLdb.' + attr + repr(arg) + + self.lock.acquire() func = getattr(self.db, attr) try: @@ -67,7 +70,10 @@ dbfunc = func(*arg) else: # Some other error we don't care about raise MySQLdb.OperationalError, message - + + finally: + self.lock.release() + return dbfunc return exc Modified: trunk/server/functions.py =================================================================== --- trunk/server/functions.py 2010-09-14 21:23:27 UTC (rev 405) +++ trunk/server/functions.py 2010-09-15 17:45:24 UTC (rev 406) @@ -16,7 +16,7 @@ ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. import os, sys, time, hashlib -from getpass import getpass +from getpass import getpass import database from filetable import FileTable This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <Blu...@us...> - 2010-09-15 18:41:32
|
Revision: 407 http://virtplayground.svn.sourceforge.net/virtplayground/?rev=407&view=rev Author: BlueWolf_ Date: 2010-09-15 18:41:24 +0000 (Wed, 15 Sep 2010) Log Message: ----------- Added a better handling for rsa en json-parse errors. These aren't really errors but warnings and are known to happen. App can optionally catch these warnings Modified Paths: -------------- trunk/server/callback.py trunk/server/core/callback.py trunk/server/core/parser.py trunk/server/core/server.py Modified: trunk/server/callback.py =================================================================== --- trunk/server/callback.py 2010-09-15 17:45:24 UTC (rev 406) +++ trunk/server/callback.py 2010-09-15 18:41:24 UTC (rev 407) @@ -56,7 +56,10 @@ return False else: return result['username'] - + + def error(self, name, reason): + log("warning", "Error '" + name + "' occured: " + reason) + def signup(self, cid, usr, pwd, extra): db.execute("SELECT `id` FROM `users` WHERE `username`=%s", usr) Modified: trunk/server/core/callback.py =================================================================== --- trunk/server/core/callback.py 2010-09-15 17:45:24 UTC (rev 406) +++ trunk/server/core/callback.py 2010-09-15 18:41:24 UTC (rev 407) @@ -171,6 +171,25 @@ pass + def error(self, name, reason): + """ + When something within the code goes wrong or unexpected (like a + json or rsa-parse error, which don't need a complete traceback, they + will be dumped into this function. You can safely ignore this message, + the server should continue working normally + + name: + Which error this is. + * rsa + * json + + reason: + String with the reason of this error + + This is a placeholder. If you want to catch this event, overwrite this + in your own callback class. + """ + def check_login(self, cid, usr, pwd): """ This is used to verify the user's login. Return the real username* when Modified: trunk/server/core/parser.py =================================================================== --- trunk/server/core/parser.py 2010-09-15 17:45:24 UTC (rev 406) +++ trunk/server/core/parser.py 2010-09-15 18:41:24 UTC (rev 407) @@ -39,7 +39,7 @@ func = getattr(self, str(head), None) if (func): - func(cid, body) + return func(cid, body) def _check_double_user(self, isbot, screenname, data): """ @@ -112,7 +112,13 @@ # Decrypt the password - pwd = rsa.decrypt(msg['pwd'], client['rsa']) + try: + pwd = rsa.decrypt(msg['pwd'], client['rsa']) + except Exception, error: + self.call.error("rsa", error.__class__.__name__ + " - " + \ + error.message) + return True + # Double sha, so no one can insert "raw" sha pwd = hashlib.sha1(pwd).hexdigest() @@ -261,7 +267,13 @@ connection = client['con'] # Decrypt the password - pwd = rsa.decrypt(msg['pwd'], client['rsa']) + try: + pwd = rsa.decrypt(msg['pwd'], client['rsa']) + except Exception, error: + self.call.error("rsa", error.__class__.__name__ + " - " + \ + error.message) + return True + # Double sha, so no one can insert "raw" sha pwd = hashlib.sha1(pwd).hexdigest() @@ -286,4 +298,4 @@ """ 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-09-15 17:45:24 UTC (rev 406) +++ trunk/server/core/server.py 2010-09-15 18:41:24 UTC (rev 407) @@ -326,12 +326,25 @@ for msg in data: try: - self.__parser(self.__cid, simplejson.loads(msg)) - except Exception, Er: + try: + parsed = simplejson.loads(msg) + except Exception, error: + self.__call.error("json", error.__class__.__name__ \ + + " - " + error.message + " - " + repr(msg)) + self.close_msg("crash") + return + + if self.__parser(self.__cid, parsed): + # Force to close the connection due to an error + self.close_msg("crash") + return + + except Exception, error: self.__parser_crash() # Kick connection self.close_msg("crash") + return def close(self, reason = "manual"): """ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |