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. |