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 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 __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
class Master: def __init__(self): """ """ self.configpath = None self.mainpath = None self.root = None self.webroot = None def config(self,configpath,mainpath): """ """ self.configpath = configpath self.mainpath = mainpath self.masterapp() def masterapp(self): config = json.load(open(self.configpath,'r')) 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 webapp import rootapp reactor.listenTCP(webport, Site(self.webroot)) reactor.listenTCP(rootport, BilateralFactory(self.root)) def start(self): ''' ''' config = json.load(open(self.configpath,'r')) sersconf = config.get('servers') for sername in sersconf.keys(): cmds = 'python %s %s %s'%(self.mainpath,sername,self.configpath) subprocess.Popen(cmds,shell=True) reactor.run()
class Master: def __init__(self): """ """ self.configpath = None self.mainpath = None self.root = None self.webroot = None def config(self, configpath, mainpath): """ """ self.configpath = configpath self.mainpath = mainpath self.masterapp() def masterapp(self): config = json.load(open(self.configpath, 'r')) 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 webapp import rootapp reactor.listenTCP(webport, Site(self.webroot)) reactor.listenTCP(rootport, BilateralFactory(self.root)) def start(self): ''' ''' config = json.load(open(self.configpath, 'r')) sersconf = config.get('servers') for sername in sersconf.keys(): cmds = 'python %s %s %s' % (self.mainpath, sername, self.configpath) subprocess.Popen(cmds, shell=True) reactor.run()
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 masterapp(self): config = json.load(open(self.configpath,'r')) 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 webapp import rootapp reactor.listenTCP(webport, Site(self.webroot)) reactor.listenTCP(rootport, BilateralFactory(self.root))
def masterapp(self): # get config 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') webroot = mastercnf.get('webroot') # create root and webroot self.root = PBRoot() rootservice = services.Service("rootservice") self.root.addServiceChannel(rootservice) self.webroot = vhost.NameVirtualHost() # save root and webroot to global GlobalObject().root = self.root GlobalObject().webroot = self.webroot # add root resource, the webroot can be customized if webroot: parts = webroot.split('.') cls = parts[-1] pkg = '.'.join(parts[0:-1]) module = __import__(pkg, fromlist = [cls]) clsObj = getattr(module, cls) self.webroot.addHost('localhost', clsObj()) else: self.webroot.addHost('localhost', DefaultMasterDirectory()) if masterlog: log.addObserver(loogoo(masterlog))#日志处理 log.startLogging(sys.stdout) import rootapp reactor.listenTCP(webport, DelaySite(self.webroot)) reactor.listenTCP(rootport, BilateralFactory(self.root))
class FFServer: def __init__(self): ''' ''' self.netfactory = None#net前端 self.root = None#分布式root节点 self.webroot = None#http服务 self.remote = {}#remote节点 self.master_remote = None self.db = None self.mem = None self.servername = None self.remoteportlist = [] self.isssl = False; self.sslkey = ''; self.sslcrt = ''; self.sslcontext = None; 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.isssl = config.get('ssl'); self.sslkey = config.get('sslkey'); self.sslcrt = config.get('sslcrt'); if self.isssl: self.sslcontext = ssl.DefaultOpenSSLContextFactory(self.sslkey,self.sslcrt); self.servername = servername 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 if self.isssl: reactor.listenSSL(webport, DelaySite(self.webroot), self.sslcontext) else: 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) 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 if masterconf: masterport = masterconf.get('rootport') masterhost = masterconf.get('roothost') self.master_remote = RemoteObject(servername) addr = ('localhost',masterport) if not masterhost else (masterhost,masterport) self.master_remote.connect(addr) GlobalObject().masterremote = self.master_remote import admin def remote_connect(self, rname, rhost): """ """ for cnf in self.remoteportlist: _rname = cnf.get('rootname') if rname == _rname: rport = cnf.get('rootport') if not rhost: addr = ('localhost',rport) else: addr = (rhost,rport) self.remote[rname].connect(addr) break def start(self): '''启动服务器 ''' log.msg('%s start...'%self.servername) log.msg('%s pid: %s'%(self.servername,os.getpid())) reactor.run()
class FFGServer(FFServer): 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) 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) if hasdb and dbconfig: log.msg(str(dbconfig)) db_pool.init_db_pool(**dbconfig) if hasmem and memconfig: urls = memconfig.get('urls') hostname = str(memconfig.get('hostname')) mclient.connect(urls, hostname) log.startLogging(DailyLogFile('master.log', 'logs/')) # 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 firefly.server.admin def start_after(self): func.log_info('{} has started, the pid is {}.'.format(self.servername, os.getpid())) def stop_before(self): func.log_info('{} is stoped.'.format(self.servername)) if GlobalObject().stophandler: GlobalObject().stophandler() signal.alarm(1) def start(self): reactor.addSystemEventTrigger('after', 'startup', self.start_after) reactor.addSystemEventTrigger('before', 'shutdown', self.stop_before) GlobalObject().server = self FFServer.start(self)
#coding:utf8 ''' Created on 2011-10-17 @author: lan (www.9miao.com) ''' from firefly.utils import services from firefly.distributed.root import PBRoot, BilateralFactory from twisted.internet import reactor from twisted.python import log import sys reactor = reactor log.startLogging(sys.stdout) root = PBRoot() ser = services.Service('test') root.addServiceChannel(ser) 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测试')
class Master: """ """ def __init__(self): """ """ self.configpath = None self.mainpath = None self.root = None self.webroot = None def config(self, configpath, mainpath): """ """ self.configpath = configpath self.mainpath = mainpath 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 start(self): """ """ sys_args = sys.argv if len(sys_args) > 2 and sys_args[1] == "single": server_name = sys_args[2] if server_name == "master": mode = MASTER_SERVER_MODE else: mode = SINGLE_SERVER_MODE else: mode = MULTI_SERVER_MODE server_name = "" if mode == MULTI_SERVER_MODE: self.masterapp() config = json.load(open(self.configpath, "r")) sersconf = config.get("servers") for sername in sersconf.keys(): cmds = "python %s %s %s" % (self.mainpath, sername, self.configpath) subprocess.Popen(cmds, shell=True) reactor.run() elif mode == SINGLE_SERVER_MODE: config = json.load(open(self.configpath, "r")) sername = server_name cmds = "python %s %s %s" % (self.mainpath, sername, self.configpath) subprocess.Popen(cmds, shell=True) else: self.masterapp() reactor.run()
class FFServer: def __init__(self): self.netfactory = None # net前端 self.root = None # 分布式root节点 self.webroot = None # http服务 self.ws = None self.remote = {} # remote节点 self.master_remote = None self.db = None self.mem = None self.servername = None self.remoteportlist = [] def set_name(self, name): """ 设置当前节点服务器名称 :param name: :return: """ self.servername = name 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 remote_connect(self, rname, rhost): """ 连接远程RRoot节点 :param rname: :param rhost: :return: """ for cnf in self.remoteportlist: _rname = cnf.get('rootname') if rname == _rname: rport = cnf.get('rootport') if not rhost: addr = ('localhost', rport) else: addr = (rhost, rport) self.remote[rname].connect(addr) break def startAfter(self): """ 启动之后 :return: """ log.msg('%s has started, the pid is %s ...' % (self.servername, os.getpid())) def stopBefore(self): """ 停止之前 :return: """ log.msg('[%s] server is stopped ...' % self.servername) if GlobalObject().stophandler: GlobalObject().stophandler() signal.alarm(1) def start(self): """ 启动服务器 :return: """ reactor.addSystemEventTrigger('after', 'startup', self.startAfter) reactor.addSystemEventTrigger('before', 'shutdown', self.stopBefore) 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
class FFServer: def __init__(self): ''' ''' self.netfactory = None #net前端 self.root = None #分布式root节点 self.remote = {} #remote节点 self.master_remote = None self.db = None self.mem = None self.servername = None def config(self, config, dbconfig=None, memconfig=None, masterconf=None): '''配置服务器 ''' netport = config.get('netport') #客户端连接 rootport = config.get('rootport') #root节点配置 remoteportlist = config.get('remoteport', []) #remote节点配置列表 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') self.master_remote = RemoteObject(servername) addr = ('localhost', 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 rootport: self.root = PBRoot() rootservice = services.Service("rootservice") self.root.addServiceChannel(rootservice) reactor.listenTCP(rootport, BilateralFactory(self.root)) for cnf in remoteportlist: rname = cnf.get('rootname') rport = cnf.get('rootport') self.remote[rname] = RemoteObject(servername) addr = ('localhost', rport) self.remote[rname].connect(addr) 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: GlobalObject().reloadmodule = __import__(mreload) import admin def start(self): '''启动服务器 ''' log.msg('%s start...' % self.servername) log.msg('%s pid: %s' % (self.servername, os.getpid())) reactor.run()
class FFServer: def __init__(self): ''' ''' self.netfactory = None#net前端 self.root = None#分布式root节点 self.webroot = None#http服务 self.remote = None#remote节点 self.master_remote = None self.db = None self.servername = None self.remoteportlist = [] def config(self, server_id, version, config, servername=None, dbconfig=None, cacheconfig=None, masterconf=None): '''配置服务器 ''' GlobalObject().server_id = server_id GlobalObject().version = version 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')#数据库连接 app = config.get('app')#入口模块名称 cpuid = config.get('cpu')#绑定cpu mreload = config.get('reload')#重新加载模块名称 self.servername = servername self.remote = RemoteManager(self.servername) 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.addRemote(rname) if hasdb and dbconfig: log.msg(str(dbconfig)) dbpool.initPool(**dbconfig) if cacheconfig: GlobalObject().cache_config = cacheconfig 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 if masterconf: masterport = masterconf.get('rootport') masterhost = masterconf.get('roothost') self.master_remote = RemoteObject(servername) addr = ('localhost',masterport) if not masterhost else (masterhost,masterport) self.master_remote.connect(addr) GlobalObject().masterremote = self.master_remote import admin def remote_connect(self, rname, rhost): """ """ for cnf in self.remoteportlist: _rname = cnf.get('rootname') if rname == _rname: rport = cnf.get('rootport') if not rhost: addr = ('localhost',rport) else: addr = (rhost,rport) self.remote.connectRemote(rname, addr) break def start(self): '''启动服务器 ''' log.msg('%s start...'%self.servername) log.msg('%s pid: %s'%(self.servername,os.getpid())) reactor.run()
class FFGServer(FFServer): 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) 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) if hasdb and dbconfig: log.msg(str(dbconfig)) db_pool.init_db_pool(**dbconfig) if hasmem and memconfig: urls = memconfig.get('urls') hostname = str(memconfig.get('hostname')) mclient.connect(urls, hostname) log_filename = "%s%s" % (self.servername, ".log") log.startLogging(DailyLogFile(log_filename, 'logs/')) #log.startLogging(DailyLogFile('master.log', 'logs/')) # 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 firefly.server.admin def start_after(self): func.log_info('{} has started, the pid is {}.'.format( self.servername, os.getpid())) def stop_before(self): func.log_info('{} is stoped.'.format(self.servername)) if GlobalObject().stophandler: GlobalObject().stophandler() signal.alarm(1) def start(self): reactor.addSystemEventTrigger('after', 'startup', self.start_after) reactor.addSystemEventTrigger('before', 'shutdown', self.stop_before) GlobalObject().server = self FFServer.start(self)
class FFServer: def __init__(self): ''' ''' self.netfactory = None#net前端 self.root = None#分布式root节点 self.webroot = None#http服务 self.remote = {}#remote节点 self.master_remote = None self.db = None self.mem = None self.servername = None self.remoteportlist = [] 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 logpath: log.addObserver(loogoo(logpath))#日志处理 log.startLogging(sys.stdout) if masterconf: log.msg("start in masterconf") masterport = masterconf.get('rootport') masterhost = masterconf.get('roothost') self.master_remote = RemoteObject(servername) addr = ('localhost',masterport) if not masterhost else (masterhost,masterport) self.master_remote.connect(addr) GlobalObject().masterremote = self.master_remote print "end in masterconf" if netport: print "start in netport" self.netfactory = LiberateFactory() netservice = services.CommandService("netservice") self.netfactory.addServiceChannel(netservice) reactor.listenTCP(netport,self.netfactory) print "end in netport" if webport: print "start in webport" self.webroot = vhost.NameVirtualHost() GlobalObject().webroot = self.webroot reactor.listenTCP(webport, DelaySite(self.webroot)) print "end in webport" if rootport: print "start in rootport" self.root = PBRoot() rootservice = services.Service("rootservice") self.root.addServiceChannel(rootservice) reactor.listenTCP(rootport, BilateralFactory(self.root)) print "end in rootport" for cnf in self.remoteportlist: rname = cnf.get('rootname') print "start in remoteportlist"+rname self.remote[rname] = RemoteObject(self.servername) print "end in remoteportlist"+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 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 remote_connect(self, rname, rhost): """ """ print "in remote_connect:" + rname+"|" + rhost for cnf in self.remoteportlist: _rname = cnf.get('rootname') if rname == _rname: rport = cnf.get('rootport') if not rhost: addr = ('localhost',rport) else: addr = (rhost,rport) self.remote[rname].connect(addr) break print "end in remote_connect" def start(self): '''启动服务器 ''' log.msg('%s start...'%self.servername) log.msg('%s pid: %s'%(self.servername,os.getpid())) reactor.run()
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
class Master: """ """ def __init__(self): """ """ self.configpath = None self.mainpath = None self.root = None self.webroot = None def config(self,configpath,mainpath): """ """ self.configpath = configpath self.mainpath = mainpath def masterapp(self): # get config 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') webroot = mastercnf.get('webroot') # create root and webroot self.root = PBRoot() rootservice = services.Service("rootservice") self.root.addServiceChannel(rootservice) self.webroot = vhost.NameVirtualHost() # save root and webroot to global GlobalObject().root = self.root GlobalObject().webroot = self.webroot # add root resource, the webroot can be customized if webroot: parts = webroot.split('.') cls = parts[-1] pkg = '.'.join(parts[0:-1]) module = __import__(pkg, fromlist = [cls]) clsObj = getattr(module, cls) self.webroot.addHost('localhost', clsObj()) else: self.webroot.addHost('localhost', DefaultMasterDirectory()) if masterlog: log.addObserver(loogoo(masterlog))#日志处理 log.startLogging(sys.stdout) import rootapp reactor.listenTCP(webport, DelaySite(self.webroot)) reactor.listenTCP(rootport, BilateralFactory(self.root)) def start(self): ''' ''' sys_args = sys.argv if len(sys_args)>2 and sys_args[1] == "single": server_name = sys_args[2] if server_name == "master": mode = MASTER_SERVER_MODE else: mode = SINGLE_SERVER_MODE else: mode = MULTI_SERVER_MODE server_name = "" if mode == MULTI_SERVER_MODE: self.masterapp() config = json.load(open(self.configpath,'r')) sersconf = config.get('servers') for sername in sersconf.keys(): cmds = 'python %s %s %s'%(self.mainpath,sername,self.configpath) subprocess.Popen(cmds,shell=True) reactor.run() elif mode == SINGLE_SERVER_MODE: config = json.load(open(self.configpath,'r')) sername = server_name cmds = 'python %s %s %s'%(self.mainpath,sername,self.configpath) subprocess.Popen(cmds,shell=True) else: self.masterapp() reactor.run()
class FFServer: def __init__(self): ''' ''' self.netfactory = None#net前端 self.root = None#分布式root节点 self.webroot = None#http服务 self.remote = {}#remote节点 self.master_remote = None self.db = None self.mem = None self.servername = None def config(self,config,dbconfig = None,memconfig = None,masterconf=None): '''配置服务器 ''' netport = config.get('netport')#客户端连接 webport = config.get('webport')#http连接 rootport = config.get('rootport')#root节点配置 remoteportlist = config.get('remoteport',[])#remote节点配置列表 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') self.master_remote = RemoteObject(servername) addr = ('localhost',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 remoteportlist: rname = cnf.get('rootname') rport = cnf.get('rootport') self.remote[rname] = RemoteObject(servername) addr = ('localhost',rport) self.remote[rname].connect(addr) 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: reactor.callLater(0.1,__import__,app) if mreload: GlobalObject().reloadmodule = __import__(mreload) import admin def start(self): '''启动服务器 ''' log.msg('%s start...'%self.servername) log.msg('%s pid: %s'%(self.servername,os.getpid())) reactor.run()
class Master: def __init__(self): self.root = None self.web = None self.mode = MULTI_SERVER_MODE self.node = None self.script = "appmain.py" self.process = ProcessMonitor() self.service = service.MultiService() self.start_time = 0 def set_mode(self, mode): self.mode = mode def set_node(self, node): self.node = node def set_script(self, script): self.script = script 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 create_node(self, name): """ 创建节点服务 :param name: :return: """ args = ["python", self.script, name] self.process.addProcess(name, args, env=os.environ) def start(self, app): """ 启动APP :param app: :return: """ self.start_time = reactor.seconds() if self.mode == MULTI_SERVER_MODE: self.create_master() servers = Config().servers for name in servers.keys(): self.create_node(name) elif self.mode == SINGLE_SERVER_MODE: self.create_node(self.node) else: self.create_master() reactor.addSystemEventTrigger('after', 'startup', self.startAfter) reactor.addSystemEventTrigger('before', 'shutdown', self.stopBefore) if "-y" in sys.argv and "-n" not in sys.argv: app.setComponent( log.ILogObserver, log.FileLogObserver(DailyLogFile("logs/master.log", "")).emit) self.service.setServiceParent(app) GlobalObject().server = self def startAfter(self): """ 启动之后 :return: """ log.msg("*** The master in the %s launch ***" % time.strftime( '%Y-%m-%d %H:%M:%S', time.localtime(self.start_time))) # 在根目录写入启动进程信息 process = [(os.getpid(), 'master')] for name, proc in self.process.protocols.items(): process.append((proc.transport.pid, name)) with open("status.json", "w+") as f: f.write(Jsonify(process)) f.close() def stopBefore(self): """ 关闭之前 :return: """ log.msg("*** Wait for the child process to exit ***") wait = dict([(proc.transport.pid, name) for name, proc in self.process.protocols.items()]) while True: try: pid = os.wait()[0] log.msg("[%s] child node has quit, pid: %s" % (wait.get(pid), pid)) except: break signal.alarm(1)
#coding:utf8 ''' Created on 2011-10-17 @author: lan (www.9miao.com) ''' from firefly.utils import services from firefly.distributed.root import PBRoot,BilateralFactory from twisted.internet import reactor from twisted.python import log import sys reactor = reactor log.startLogging(sys.stdout) root = PBRoot() ser = services.Service('test') root.addServiceChannel(ser) 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
class Master: """ """ def __init__(self): """ """ self.configpath = None self.mainpath = None self.root = None self.webroot = None def config(self,configpath,mainpath): """ """ self.configpath = configpath self.mainpath = mainpath 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 webapp import rootapp reactor.listenTCP(webport, DelaySite(self.webroot)) reactor.listenTCP(rootport, BilateralFactory(self.root)) def start(self): ''' ''' sys_args = sys.argv if len(sys_args)>2 and sys_args[1] == "single": server_name = sys_args[2] if server_name == "master": mode = MASTER_SERVER_MODE else: mode = SINGLE_SERVER_MODE else: mode = MULTI_SERVER_MODE server_name = "" if mode == MULTI_SERVER_MODE: self.masterapp() config = json.load(open(self.configpath,'r')) sersconf = config.get('servers') for sername in sersconf.keys(): cmds = 'python %s %s %s'%(self.mainpath,sername,self.configpath) subprocess.Popen(cmds,shell=True) reactor.run() elif mode == SINGLE_SERVER_MODE: config = json.load(open(self.configpath,'r')) sername = server_name cmds = 'python %s %s %s'%(self.mainpath,sername,self.configpath) subprocess.Popen(cmds,shell=True) else: self.masterapp() reactor.run()
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
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) 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) 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: GlobalObject().reloadmodule = __import__(mreload) GlobalObject().remote_connect = self.remote_connect import admin
class Master: """ """ def __init__(self): """ """ self.configpath = None self.mainpath = None self.root = None self.webroot = None # 谁调用了??? def config(self, configpath, mainpath): """ """ self.configpath = configpath self.mainpath = mainpath def masterapp(self): """ """ config = json.load(open(self.configpath, 'r')) GlobalObject().json_config = config mastercnf = config.get('master') # 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 webapp import rootapp reactor.listenTCP(webport, DelaySite(self.webroot)) # webport开始监听 reactor.listenTCP(rootport, BilateralFactory(self.root)) # rootport开始监听 def start(self): ''' ''' sys_args = sys.argv if len(sys_args) > 2 and sys_args[1] == "single": server_name = sys_args[2] if server_name == "master": mode = MASTER_SERVER_MODE # 主服务器模式 else: mode = SINGLE_SERVER_MODE # 单服务器模式 else: mode = MULTI_SERVER_MODE # 多服务器模式 server_name = "" if mode == MULTI_SERVER_MODE: # 多服务器模式 self.masterapp() config = json.load(open(self.configpath, 'r')) sersconf = config.get('servers') # servers for sername in sersconf.keys(): # sername cmds = 'python %s %s %s' % (self.mainpath, sername, self.configpath) subprocess.Popen(cmds, shell=True) reactor.run() elif mode == SINGLE_SERVER_MODE: # 单服务器模式 config = json.load(open(self.configpath, 'r')) sername = server_name # sername cmds = 'python %s %s %s' % (self.mainpath, sername, self.configpath) subprocess.Popen(cmds, shell=True) else: # 主服务器模式 self.masterapp() reactor.run()