def __init__(self): log.startLogging(sys.stdout) reload(sys) sys.setdefaultencoding('UTF-8') #数据库 import database GlobalManager.db = database.db GlobalManager.Session = database.Session #服务器配置 from server import Server from services import Service server = Server(8100, "game") GlobalManager.tcpserver = server GlobalManager.netservice = Service('NetService') GlobalManager.tcpserver.serverFactory.addServiceChannel( GlobalManager.netservice) #开启进程监视 GlobalManager.pbroot = PBRoot() GlobalManager.pbroot.addServiceChannel(GlobalManager.netservice) reactor.listenTCP(10000, BilateralFactory(GlobalManager.pbroot)) #加入服务器逻辑函数 import handle
def create_master(self): """ 创建Master服务 :return: """ config = Config().config GlobalObject().json_config = config mastercnf = config.get('master') rootport = mastercnf.get('rootport') webport = mastercnf.get('webport') masterlog = mastercnf.get('log') self.root = PBRoot() rootservice = services.Service("rootservice") self.root.addServiceChannel(rootservice) self.web = vhost.NameVirtualHost() self.web.addHost('0.0.0.0', './') GlobalObject().root = self.root GlobalObject().webroot = self.web import webapp import rootapp internet.TCPServer(webport, DelaySite(self.web)).setServiceParent(self.service) internet.TCPServer(rootport, BilateralFactory( self.root)).setServiceParent(self.service) self.process.setServiceParent(self.service)
def masterapp(self): """ """ config = json.load(open(self.configpath, 'r')) GlobalObject().json_config = config mastercnf = config.get('master') rootport = mastercnf.get('rootport') webport = mastercnf.get('webport') masterlog = mastercnf.get('log') self.root = PBRoot() rootservice = services.Service("rootservice") self.root.addServiceChannel(rootservice) self.webroot = vhost.NameVirtualHost() self.webroot.addHost('0.0.0.0', './') GlobalObject().root = self.root GlobalObject().webroot = self.webroot if masterlog: log.addObserver(loogoo(masterlog)) #日志处理 log.startLogging(sys.stdout) import firefly.master.webapp import firefly.master.rootapp reactor.listenTCP(webport, DelaySite(self.webroot), interface='127.0.0.1') reactor.listenTCP(rootport, BilateralFactory(self.root))
def masterapp(self): """ """ config = json.load(open(self.configpath, "r")) GlobalObject().json_config = config mastercnf = config.get("master") rootport = mastercnf.get("rootport") webport = mastercnf.get("webport") masterlog = mastercnf.get("log") self.root = PBRoot() rootservice = services.Service("rootservice") self.root.addServiceChannel(rootservice) self.webroot = vhost.NameVirtualHost() self.webroot.addHost("0.0.0.0", "./") GlobalObject().root = self.root GlobalObject().webroot = self.webroot if masterlog: log.addObserver(loogoo(masterlog)) # 日志处理 log.startLogging(sys.stdout) from . import webapp from . import rootapp reactor.listenTCP(webport, DelaySite(self.webroot)) reactor.listenTCP(rootport, BilateralFactory(self.root))
def config(self, config, servername=None, dbconfig=None, memconfig=None, masterconf=None): '''配置服务器 ''' GlobalObject().json_config = config netport = config.get('netport') #客户端连接 webport = config.get('webport') #http连接 rootport = config.get('rootport') #root节点配置 self.remoteportlist = config.get('remoteport', []) #remote节点配置列表 if not servername: servername = config.get('name') #服务器名称 logpath = config.get('log') #日志 hasdb = config.get('db') #数据库连接 hasmem = config.get('mem') #memcached连接 app = config.get('app') #入口模块名称 cpuid = config.get('cpu') #绑定cpu mreload = config.get('reload') #重新加载模块名称 self.servername = servername if masterconf: masterport = masterconf.get('rootport') masterhost = masterconf.get('roothost') self.master_remote = RemoteObject(servername, "master") addr = ('localhost', masterport) if not masterhost else (masterhost, masterport) self.master_remote.connect(addr) GlobalObject().masterremote = self.master_remote if netport: self.netfactory = LiberateFactory() netservice = services.CommandService("netservice") self.netfactory.addServiceChannel(netservice) reactor.listenTCP(netport, self.netfactory) if webport: self.webroot = vhost.NameVirtualHost() GlobalObject().webroot = self.webroot reactor.listenTCP(webport, DelaySite(self.webroot)) if rootport: self.root = PBRoot() rootservice = services.Service("rootservice") self.root.addServiceChannel(rootservice) reactor.listenTCP(rootport, BilateralFactory(self.root)) for cnf in self.remoteportlist: rname = cnf.get('rootname') self.remote[rname] = RemoteObject(self.servername, rname) if hasdb and dbconfig: log.msg(str(dbconfig)) dbpool.initPool(**dbconfig) if hasmem and memconfig: urls = memconfig.get('urls') hostname = str(memconfig.get('hostname')) mclient.connect(urls, hostname) if logpath: log.addObserver(loogoo(logpath)) #日志处理 log.startLogging(sys.stdout) if cpuid: affinity.set_process_affinity_mask(os.getpid(), cpuid) GlobalObject().config(netfactory=self.netfactory, root=self.root, remote=self.remote) if app: __import__(app) if mreload: _path_list = mreload.split(".") GlobalObject().reloadmodule = __import__(mreload, fromlist=_path_list[:1]) GlobalObject().remote_connect = self.remote_connect import admin
def serviceHandle(target): '''服务处理 @param target: func Object ''' ser.mapTarget(target) @serviceHandle def printData1(data, data1): print data, data1 print "############################" # d = root.callChildByName("test_node",1,u'Root测试') return data @serviceHandle def printData2(data, data1): print data, data1 print "############################" # d = root.callChildByName("test_node",1,u'Root测试') return data if __name__ == '__main__': reactor.listenTCP(1000, BilateralFactory(root)) reactor.callLater(5, root.callChildByName, 'test_node', 'printOK', 'asdfawefasdf') reactor.run()
def set_config(self): """ 初始化节点服务配置 :return: """ config = Config().config ser_cfg = config.get("servers", {}).get(self.servername) if not ser_cfg: raise ValueError mem_cfg = config.get("cache") master_cfg = config.get("master") db_cfg = config.get("db") GlobalObject().json_config = ser_cfg netport = ser_cfg.get('netport') # 客户端连接 webport = ser_cfg.get('webport') # http连接 rootport = ser_cfg.get('rootport') # root节点配置 wsport = ser_cfg.get("wsport") # WebSocket端口 self.remoteportlist = ser_cfg.get('remoteport', []) # remote节点配置列表 logpath = ser_cfg.get('log') # 日志 hasdb = ser_cfg.get('db') # 数据库连接 hasmem = ser_cfg.get('mem') # memcached连接 app = ser_cfg.get('app') # 入口模块名称 cpuid = ser_cfg.get('cpu') # 绑定cpu mreload = ser_cfg.get('reload') # 重新加载模块名称 if master_cfg: masterport = master_cfg.get('rootport') masterhost = master_cfg.get('roothost') self.master_remote = RemoteObject(self.servername) addr = ('localhost', masterport) if not masterhost else (masterhost, masterport) self.master_remote.connect(addr) GlobalObject().masterremote = self.master_remote if netport: self.netfactory = LiberateFactory() netservice = services.CommandService("netservice") self.netfactory.addServiceChannel(netservice) reactor.listenTCP(netport, self.netfactory) if webport: self.webroot = vhost.NameVirtualHost() GlobalObject().webroot = self.webroot reactor.listenTCP(webport, DelaySite(self.webroot)) if rootport: self.root = PBRoot() rootservice = services.Service("rootservice") self.root.addServiceChannel(rootservice) reactor.listenTCP(rootport, BilateralFactory(self.root)) if wsport: self.ws = WsFactory(wsport) wsservice = services.CommandService("wsservice") self.ws.addServiceChannel(wsservice) reactor.listenTCP(wsport, self.ws) for cnf in self.remoteportlist: rname = cnf.get('rootname') self.remote[rname] = RemoteObject(self.servername) if hasdb and db_cfg: log.msg(str(db_cfg)) dbpool.initPool(**db_cfg) if hasmem and mem_cfg: urls = mem_cfg.get('urls') hostname = str(mem_cfg.get('hostname')) mclient.connect(urls, hostname) if logpath: log.addObserver(loogoo(logpath)) #日志处理 log.startLogging(sys.stdout) if cpuid: affinity.set_process_affinity_mask(os.getpid(), cpuid) GlobalObject().config(netfactory=self.netfactory, root=self.root, remote=self.remote) GlobalObject().server = self if app: __import__(app) if mreload: _path_list = mreload.split(".") GlobalObject().reloadmodule = __import__(mreload, fromlist=_path_list[:1]) GlobalObject().remote_connect = self.remote_connect import admin
def config(self, config, servername=None, dbconfig=None, memconfig=None, masterconf=None): """配置服务器 """ GlobalObject().json_config = config netport = config.get("netport") # 客户端连接 webport = config.get("webport") # http连接 rootport = config.get("rootport") # root节点配置 self.remoteportlist = config.get("remoteport", []) # remote节点配置列表 if not servername: servername = config.get("name") # 服务器名称 logpath = config.get("log") # 日志 hasdb = config.get("db") # 数据库连接 hasmem = config.get("mem") # memcached连接 app = config.get("app") # 入口模块名称 cpuid = config.get("cpu") # 绑定cpu mreload = config.get("reload") # 重新加载模块名称 self.servername = servername if masterconf: masterport = masterconf.get("rootport") masterhost = masterconf.get("roothost") self.master_remote = RemoteObject(servername, "master") addr = ("localhost", masterport) if not masterhost else (masterhost, masterport) self.master_remote.connect(addr) GlobalObject().masterremote = self.master_remote if netport: self.netfactory = LiberateFactory() netservice = services.CommandService("netservice") self.netfactory.addServiceChannel(netservice) reactor.listenTCP(netport, self.netfactory) if webport: self.webroot = vhost.NameVirtualHost() GlobalObject().webroot = self.webroot reactor.listenTCP(webport, DelaySite(self.webroot)) if rootport: self.root = PBRoot() rootservice = services.Service("rootservice") self.root.addServiceChannel(rootservice) reactor.listenTCP(rootport, BilateralFactory(self.root)) for cnf in self.remoteportlist: rname = cnf.get("rootname") self.remote[rname] = RemoteObject(self.servername, rname) if hasdb and dbconfig: log.msg(str(dbconfig)) dbpool.initPool(**dbconfig) if logpath: log.addObserver(loogoo(logpath)) # 日志处理 log.startLogging(sys.stdout) GlobalObject().config(netfactory=self.netfactory, root=self.root, remote=self.remote) if app: __import__(app) if mreload: _path_list = mreload.split(".") GlobalObject().reloadmodule = __import__(mreload, fromlist=_path_list[:1]) GlobalObject().remote_connect = self.remote_connect from . import admin