#!/usr/bin/env python from twisted.internet import reactor import warnings warnings.filterwarnings("ignore") from nevow import appserver from pyscrabble.net.server import ScrabbleServerFactory from pyscrabble.net.site import ScrabbleSite from pyscrabble import constants from pyscrabble import dist from pyscrabble import logger from pyscrabble import manager import os import ConfigParser class ServerConsole: def __init__(self): basePath = self.getBasePath() os.chdir( basePath ) self.logger = logger.getLogger("pyscrabble_server.log") resources = self.getResources() dist.ensure_config_dir(constants.CONFIG_DIR) self.readConfig(resources) self.gameFactory = ScrabbleServerFactory(resources, logger=self.logger) self.site = appserver.NevowSite(ScrabbleSite(basePath, self.gameFactory)) def startConsole(self, disableSignals = False): reactor.listenTCP(self.g_port, self.gameFactory) self.logger.info('Game server listening on %d' % self.g_port) reactor.listenTCP(self.w_port, self.site) self.logger.info('Web server listening on %d' % self.w_port) if disableSignals: reactor.run(installSignalHandlers=0) else: reactor.run() def stopConsole(self): self.gameFactory.stopFactory() reactor.stop() def readConfig(self, resources): config = resources["config"][constants.SERVER_CONSOLE_CONFIG] if not os.path.exists(config): raise IOError, "server_console.cfg must exist in %s" % resources["config"].path parser = ConfigParser.ConfigParser() parser.read( config ) self.w_port = int(parser.get("pyscrabble","web_port")) self.g_port = int(parser.get("pyscrabble","game_port")) def getBasePath(self): return os.curdir def getResources(self): ''' Get Resource Manager ''' return manager.ResourceManager() if __name__ == '__main__': x = ServerConsole() x.startConsole()