供master调用的接口描述符

供master调用的接口描述符

供master调用的接口描述符

def masterserviceHandle(target):
    """供master调用的接口描述符
    """
    GlobalObject().masterremote._reference._service.mapTarget(target)

def masterserviceHandle ( target ) : GlobalObject ( ) . masterremote . _reference . _service . mapTarget ( target )


供客户端连接调用的接口描述符

供客户端连接调用的接口描述符

供客户端连接调用的接口描述符

def netserviceHandle(target):
    """供客户端连接调用的接口描述符
    """
    GlobalObject().netfactory.service.mapTarget(target)

def netserviceHandle ( target ) : GlobalObject ( ) . netfactory . service . mapTarget ( target )


作为root节点，供remote节点调用的接口描述符

作为root节点，供remote节点调用的接口描述符

作为root节点，供remote节点调用的接口描述符

def rootserviceHandle(target):
    """作为root节点，供remote节点调用的接口描述符
    """
    GlobalObject().root.service.mapTarget(target)

def rootserviceHandle ( target ) : GlobalObject ( ) . root . service . mapTarget ( target )


def config(self,netfactory=None,root = None,remote=None,db=None):
        """配置存放的对象实例
        """
        self.netfactory = netfactory
        self.root = root
        self.remote = remote
        self.db = db

def config ( self , netfactory = None , root = None , remote = None , db = None ) : self . netfactory = netfactory self . root = root self . remote = remote self . db = db


def __init__(self,url,**kw):
        """
        @param url: str http 访问的路径
        """
        self._url = url
        self.kw = kw

def __init__ ( self , url , * * kw ) : self . _url = url self . kw = kw


def __init__(self,logpath):
        '''配置日志路径
        '''
        self.file = file(logpath, 'w')

def __init__ ( self , logpath ) : self . file = file ( logpath , 'w' )


def __call__(self, eventDict):
        '''日志处理
        '''
        if 'logLevel' in eventDict:
            level = eventDict['logLevel']
        elif eventDict['isError']:
            level = 'ERROR'
        else:
            level = 'INFO'
        text = log.textFromEventDict(eventDict)
        if text is None or level != 'ERROR':
            return
        nowdate = datetime.datetime.now()
        self.file.write('['+str(nowdate)+']\n'+str(level)+ '\n\t' + text + '\r\n')
        self.file.flush()

def __call__ ( self , eventDict ) : if 'logLevel' in eventDict : level = eventDict [ 'logLevel' ] elif eventDict [ 'isError' ] : level = 'ERROR' else : level = 'INFO' text = log . textFromEventDict ( eventDict ) if text is None or level != 'ERROR' : return nowdate = datetime . datetime . now ( ) self . file . write ( '[' + str ( nowdate ) + ']\n' + str ( level ) + '\n\t' + text + '\r\n' ) self . file . flush ( )


def serverStop():
    """停止服务进程
    """
    log.msg('stop')
    if GlobalObject().stophandler:
        GlobalObject().stophandler()
    reactor.callLater(0.5,reactor.stop)
    return True

def serverStop ( ) : log . msg ( 'stop' ) if GlobalObject ( ) . stophandler : GlobalObject ( ) . stophandler ( ) reactor . callLater ( 0.5 , reactor . stop ) return True


def config(self, config, servername=None, dbconfig=None,
                memconfig=None, masterconf=None):
        '''配置服务器
        '''
        GlobalObject().json_config = config
        GlobalObject().remote_connect = self.remote_connect
        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 netport:
            self.netfactory = LiberateFactory()
            netservice = services.CommandService("netservice")
            self.netfactory.addServiceChannel(netservice)
            reactor.listenTCP(netport,self.netfactory)

        if webport:
            self.webroot = Flask("servername")
            GlobalObject().webroot = self.webroot
            reactor.listenWSGI(webport, 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:
            if dbconfig.has_key("user") and dbconfig.has_key("host") and dbconfig.has_key("host"):
                dbpool.initPool({"default":dbconfig})
            else:
                dbpool.initPool(dbconfig)

        if hasmem and memconfig:
            urls = memconfig.get('urls')
#             hostname = str(memconfig.get('hostname'))
            memcached_connect(urls)
            from gfirefly.dbentrust.util import M2DB_PORT,M2DB_HOST,ToDBAddress
            ToDBAddress().setToDBHost(memconfig.get("pubhost",M2DB_HOST))
            ToDBAddress().setToDBPort(memconfig.get("pubport",M2DB_PORT))

        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])

        if masterconf:
            masterport = masterconf.get('rootport')
            masterhost = masterconf.get('roothost')
            self.master_remote = RemoteObject(servername)
            GlobalObject().masterremote = self.master_remote
            import admin
            addr = ('localhost',masterport) if not masterhost else (masterhost,masterport)
            self.master_remote.connect(addr)


def remote_connect(self, rname, rhost):
        """进行rpc的连接
        """
        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 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] started...'%self.servername)
        log.msg('[%s] pid: %s'%(self.servername,os.getpid()))
        reactor.run()

def
start
(
self
)
:
log
.
msg
(
'[%s] started...'
%
self
.
servername
)
log
.
msg
(
'[%s] pid: %s'
%
(
self
.
servername
,
os
.
getpid
(
)
)
)
reactor
.
run
(
)


供master调用的接口：关闭服务器

供master调用的接口：关闭服务器

供master调用的接口：关闭服务器

def serverStop():
    """供master调用的接口：关闭服务器
    """
    log.msg('stop')
    if GlobalObject().stophandler:
        GlobalObject().stophandler()
    reactor.callLater(0.5,reactor.stop)
    return True


供master调用的接口：热更新模块

供master调用的接口：热更新模块

供master调用的接口：热更新模块

def sreload():
    """供master调用的接口：热更新模块
    """
    log.msg('reload')
    if GlobalObject().reloadmodule:
        reload(GlobalObject().reloadmodule)
    return True

def sreload ( ) : log . msg ( 'reload' ) if GlobalObject ( ) . reloadmodule : reload ( GlobalObject ( ) . reloadmodule ) return True


供master调用的接口：进行远程的rpc连接

供master调用的接口：进行远程的rpc连接

供master调用的接口：进行远程的rpc连接

def remote_connect(rname, rhost):
    """供master调用的接口：进行远程的rpc连接
    """
    GlobalObject().remote_connect(rname, rhost)

def remote_connect ( rname , rhost ) : GlobalObject ( ) . remote_connect ( rname , rhost )


def getHeadlength():
        """获取数据包的长度
        """
        pass


def pack():
        '''打包数据包
        '''


Add a target unDisplay when client call it .

def addUnDisplayTarget(self,command):
        '''Add a target unDisplay when client call it.'''
        self.unDisplay.add(command)

def addUnDisplayTarget ( self , command ) : self . unDisplay . add ( command )


def mapTarget(self, target):
        """Add a target to the service."""
        key = target.__name__
        if self._targets.has_key(key):
            exist_target = self._targets.get(key)
            raise "target [%d] Already exists,\
            Conflict between the %s and %s"%(key,exist_target.__name__,target.__name__)
        self._targets[key] = target


def unMapTarget(self, target):
        """Remove a target from the service."""
        key = target.__name__
        if key in self._targets:
            del self._targets[key]

def unMapTarget ( self , target ) : key = target . __name__ if key in self . _targets : del self . _targets [ key ]


def unMapTargetByKey(self,targetKey):
        """Remove a target from the service."""
        del self._targets[targetKey]

def unMapTargetByKey ( self , targetKey ) : del self . _targets [ targetKey ]


def getTarget(self, targetKey):
        """Get a target from the service by name."""
        target = self._targets.get(targetKey, None)
        return target

def getTarget ( self , targetKey ) : target = self . _targets . get ( targetKey , None ) return target


def callTarget(self,targetKey,*args,**kw):
        '''call Target
        @param conn: client connection
        @param targetKey: target ID
        @param data: client data
        '''
        target = self.getTarget(targetKey)
        if not target:
            log.err('the command '+str(targetKey)+' not Found on service')
            return None
        if targetKey not in self.unDisplay:
            log.msg("call method %s on service[single]"%target.__name__)
        response = target(*args,**kw)
        return response

def
callTarget
(
self
,
targetKey
,
*
args
,
*
*
kw
)
:
target
=
self
.
getTarget
(
targetKey
)
if
not
target
:
log
.
err
(
'the command '
+
str
(
targetKey
)
+
' not Found on service'
)
return
if
targetKey
not
in
self
.
unDisplay
:
log
.
msg
(
"call method %s on service[single]"
%
target
.
__name__
)
response
=
target
(
*
args
,
*
*
kw
)
return
response


def mapTarget(self, target):
        """Add a target to the service.
        """
        key = int((target.__name__).split('_')[-1])
        if self._targets.has_key(key):
            exist_target = self._targets.get(key)
            raise "target [%d] Already exists,\
            Conflict between the %s and %s"%(key,exist_target.__name__,target.__name__)
        self._targets[key] = target


def unMapTarget(self, target):
        """Remove a target from the service.
        """
        key = int((target.__name__).split('_')[-1])
        if key in self._targets:
            del self._targets[key]

def unMapTarget ( self , target ) : key = int ( ( target . __name__ ) . split ( '_' ) [ - 1 ] ) if key in self . _targets : del self . _targets [ key ]


Returns a PEP 386 - compliant version number from VERSION .

def get_version(version=None):
    "Returns a PEP 386-compliant version number from VERSION."
    if version is None:
        from gfirefly import VERSION as version
    else:
        assert len(version) == 5
        assert version[3] in ('alpha', 'beta', 'rc', 'final')

    parts = 2 if version[2] == 0 else 3
    main = '.'.join(str(x) for x in version[:parts])

    sub = ''
    if version[3] == 'alpha' and version[4] == 0:
        git_changeset = get_git_changeset()
        if git_changeset:
            sub = '.dev%s' % git_changeset

    elif version[3] != 'final':
        mapping = {'alpha': 'a', 'beta': 'b', 'rc': 'c'}
        sub = mapping[version[3]] + str(version[4])

    return str(main + sub)

def get_version ( version = None ) : if version is None : from gfirefly import VERSION as version else : assert len ( version ) == 5 assert version [ 3 ] in ( 'alpha' , 'beta' , 'rc' , 'final' ) parts = 2 if version [ 2 ] == 0 else 3 main = '.' . join ( str ( x ) for x in version [ : parts ] ) sub = '' if version [ 3 ] == 'alpha' and version [ 4 ] == 0 : git_changeset = get_git_changeset ( ) if git_changeset : sub = '.dev%s' % git_changeset elif version [ 3 ] != 'final' : mapping = { 'alpha' : 'a' , 'beta' : 'b' , 'rc' : 'c' } sub = mapping [ version [ 3 ] ] + str ( version [ 4 ] ) return str ( main + sub )


Returns a numeric identifier of the latest git changeset .

def get_git_changeset():
    """Returns a numeric identifier of the latest git changeset.

    The result is the UTC timestamp of the changeset in YYYYMMDDHHMMSS format.
    This value isn't guaranteed to be unique, but collisions are very unlikely,
    so it's sufficient for generating the development version numbers.
    """
    repo_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    git_log = subprocess.Popen('git log --pretty=format:%ct --quiet -1 HEAD',
            stdout=subprocess.PIPE, stderr=subprocess.PIPE,
            shell=True, cwd=repo_dir, universal_newlines=True)
    timestamp = git_log.communicate()[0]
    try:
        timestamp = datetime.datetime.utcfromtimestamp(int(timestamp))
    except ValueError:
        return None
    return timestamp.strftime('%Y%m%d%H%M%S')


def __init__(self,subcommond,*args):
        '''工具类指令
        '''
        self.subcommond = subcommond
        self.args = args

def __init__ ( self , subcommond , * args ) : self . subcommond = subcommond self . args = args


def FormatCondition(props):
    """生成查询条件字符串
    """
    items = props.items()
    itemstrlist = []
    for _item in items:
        if isinstance(_item[1],Number):
            sqlstr = " `%s`=%s AND"%_item
        else:
            sqlstr = " `%s`='%s' AND "%(_item[0],str(_item[1]).replace("'", "\\'"))
        itemstrlist.append(sqlstr)
    sqlstr = ''.join(itemstrlist)
    return sqlstr[:-4]

def
FormatCondition
(
props
)
:
items
=
props
.
items
(
)
itemstrlist
=
[
]
for
_item
in
items
:
if
isinstance
(
_item
[
1
]
,
Number
)
:
sqlstr
=
" `%s`=%s AND"
%
_item
else
:
sqlstr
=
" `%s`='%s' AND "
%
(
_item
[
0
]
,
str
(
_item
[
1
]
)
.
replace
(
"'"
,
"\\'"
)
)
itemstrlist
.
append
(
sqlstr
)
sqlstr
=
''
.
join
(
itemstrlist
)
return
sqlstr
[
:
4
]


def FormatUpdateStr(props):
    """生成更新语句
    """
    items = props.items()
    itemstrlist = []
    for _item in items:
        if isinstance(_item[1],Number):
            sqlstr = " `%s`=%s,"%_item
        else:
            sqlstr = " `%s`='%s',"%(_item[0],str(_item[1]).replace("'", "\\'"))
        itemstrlist.append(sqlstr)
    sqlstr = ''.join(itemstrlist)
    return sqlstr[:-1]

def
FormatUpdateStr
(
props
)
:
items
=
props
.
items
(
)
itemstrlist
=
[
]
for
_item
in
items
:
if
isinstance
(
_item
[
1
]
,
Number
)
:
sqlstr
=
" `%s`=%s,"
%
_item
else
:
sqlstr
=
" `%s`='%s',"
%
(
_item
[
0
]
,
str
(
_item
[
1
]
)
.
replace
(
"'"
,
"\\'"
)
)
itemstrlist
.
append
(
sqlstr
)
sqlstr
=
''
.
join
(
itemstrlist
)
return
sqlstr
[
:
1
]


遍历所要修改的属性，以生成sql语句

遍历所要修改的属性，以生成sql语句

遍历所要修改的属性，以生成sql语句

def forEachUpdateProps(tablename,props,prere):
    '''遍历所要修改的属性，以生成sql语句'''
    assert isinstance(props, dict)
    pro = FormatUpdateStr(props)
    pre = FormatCondition(prere)
    sqlstr = """UPDATE `%s` SET %s WHERE %s;"""%(tablename,pro,pre)
    return sqlstr

def
forEachUpdateProps
(
tablename
,
props
,
prere
)
:
assert
isinstance
(
props
,
dict
)
pro
=
FormatUpdateStr
(
props
)
pre
=
FormatCondition
(
prere
)
sqlstr
=
"""UPDATE `%s` SET %s WHERE %s;"""
%
(
tablename
,
pro
,
pre
)
return
sqlstr


遍历字段列表生成sql语句

遍历字段列表生成sql语句

遍历字段列表生成sql语句

def EachQueryProps(props):
    '''遍历字段列表生成sql语句
    '''
    sqlstr = ""
    if props == '*':
        return '*'
    elif type(props) == type([0]):
        for prop in props:
            sqlstr = sqlstr + prop +','
        sqlstr = sqlstr[:-1]
        return sqlstr
    else:
        raise Exception('props to query must be dict')
        return

def
EachQueryProps
(
props
)
:
sqlstr
=
""
if
props
==
'*'
:
return
'*'
elif
type
(
props
)
==
type
(
[
0
]
)
:
for
prop
in
props
:
sqlstr
=
sqlstr
+
prop
+
','
sqlstr
=
sqlstr
[
:
1
]
return
sqlstr
else
:
raise
Exception
(
'props to query must be dict'
)
return


遍历所要查询属性，以生成sql语句

遍历所要查询属性，以生成sql语句

遍历所要查询属性，以生成sql语句

def forEachQueryProps(sqlstr, props):
    '''遍历所要查询属性，以生成sql语句'''
    if props == '*':
        sqlstr += ' *'
    elif type(props) == type([0]):
        i = 0
        for prop in props:
            if(i == 0):
                sqlstr += ' ' + prop
            else:
                sqlstr += ', ' + prop
            i += 1
    else:
        raise Exception('props to query must be list')
        return
    return sqlstr

def
forEachQueryProps
(
sqlstr
,
props
)
:
if
props
==
'*'
:
sqlstr
+=
' *'
elif
type
(
props
)
==
type
(
[
0
]
)
:
i
=
0
for
prop
in
props
:
if
(
i
==
0
)
:
sqlstr
+=
' '
+
prop
else
:
sqlstr
+=
', '
+
prop
i
+=
1
else
:
raise
Exception
(
'props to query must be list'
)
return
return
sqlstr


def DeleteFromDB(tablename,props):
    '''从数据库中删除
    '''
    prers = FormatCondition(props)
    sql = """DELETE FROM %s WHERE %s ;"""%(tablename,prers)
    conn = dbpool.connection(write=True,tablename=tablename)
    cursor = conn.cursor()
    count = 0
    try:
        count = cursor.execute(sql)
        conn.commit()
    except Exception,e:
        log.err(e,traceback.format_exc())
        log.err(sql)
    cursor.close()
    conn.close()
    return bool(count)

def
DeleteFromDB
(
tablename
,
props
)
:
prers
=
FormatCondition
(
props
)
sql
=
"""DELETE FROM %s WHERE %s ;"""
%
(
tablename
,
prers
)
conn
=
dbpool
.
connection
(
write
=
True
,
tablename
=
tablename
)
cursor
=
conn
.
cursor
(
)
count
=
0
try
:
count
=
cursor
.
execute
(
sql
)
conn
.
commit
(
)
except
Exception
,
e
:
log
.
err
(
e
,
traceback
.
format_exc
(
)
)
log
.
err
(
sql
)
cursor
.
close
(
)
conn
.
close
(
)
return
bool
(
count
)


def InsertIntoDBAndReturnID(tablename,data):
    """写入数据库,并返回ID
    """
    sql = forEachPlusInsertProps(tablename,data)
    conn = dbpool.connection(write=True,tablename=tablename)
    cursor = conn.cursor()
    cursor.execute(sql)
    conn.commit()
    cursor.execute("SELECT LAST_INSERT_ID();")
    result=cursor.fetchall()[0]
    cursor.close()
    conn.close()
    return result

def
InsertIntoDBAndReturnID
(
tablename
,
data
)
:
sql
=
forEachPlusInsertProps
(
tablename
,
data
)
conn
=
dbpool
.
connection
(
write
=
True
,
tablename
=
tablename
)
cursor
=
conn
.
cursor
(
)
cursor
.
execute
(
sql
)
conn
.
commit
(
)
cursor
.
execute
(
"SELECT LAST_INSERT_ID();"
)
result
=
cursor
.
fetchall
(
)
[
0
]
cursor
.
close
(
)
conn
.
close
(
)
return
result


def InsertIntoDB(tablename,data):
    """写入数据库
    """
    sql = forEachPlusInsertProps(tablename,data)
    conn = dbpool.connection(write=True,tablename=tablename)
    cursor = conn.cursor()
    count = cursor.execute(sql)
    conn.commit()
    cursor.close()
    conn.close()
    return bool(count)

def InsertIntoDB ( tablename , data ) : sql = forEachPlusInsertProps ( tablename , data ) conn = dbpool . connection ( write = True , tablename = tablename ) cursor = conn . cursor ( ) count = cursor . execute ( sql ) conn . commit ( ) cursor . close ( ) conn . close ( ) return bool ( count )


def UpdateWithDict(tablename,props,prere):
    """更新记录
    """
    sql = forEachUpdateProps(tablename, props, prere)
    conn = dbpool.connection(write=True,tablename=tablename)
    cursor = conn.cursor()
    count = 0
    try:
        count = cursor.execute(sql)
        conn.commit()
    except Exception,e:
        log.err(e,traceback.format_exc())
        log.err(sql)
    cursor.close()
    conn.close()
    if(count >= 1):
        return True
    return False

def UpdateWithDict ( tablename , props , prere ) : sql = forEachUpdateProps ( tablename , props , prere ) conn = dbpool . connection ( write = True , tablename = tablename ) cursor = conn . cursor ( ) count = 0 try : count = cursor . execute ( sql ) conn . commit ( ) except Exception , e : log . err ( e , traceback . format_exc ( ) ) log . err ( sql ) cursor . close ( ) conn . close ( ) if ( count >= 1 ) : return True return False


根据所有的外键获取主键ID

根据所有的外键获取主键ID

根据所有的外键获取主键ID

def getAllPkByFkInDB(tablename,pkname,props):
    """根据所有的外键获取主键ID
    """
    props = FormatCondition(props)
    sql = """select `%s` from `%s` where %s;"""%(pkname,tablename,props)
    conn = dbpool.connection(write=False,tablename=tablename)
    cursor = conn.cursor()
    cursor.execute(sql)
    result = cursor.fetchall()
    cursor.close()
    conn.close()
    return [key[0] for key in result]

def
getAllPkByFkInDB
(
tablename
,
pkname
,
props
)
:
props
=
FormatCondition
(
props
)
sql
=
"""select `%s` from `%s` where %s;"""
%
(
pkname
,
tablename
,
props
)
conn
=
dbpool
.
connection
(
write
=
False
,
tablename
=
tablename
)
cursor
=
conn
.
cursor
(
)
cursor
.
execute
(
sql
)
result
=
cursor
.
fetchall
(
)
cursor
.
close
(
)
conn
.
close
(
)
return
[
key
[
0
]
for
key
in
result
]


def GetOneRecordInfo(tablename,props):
    '''获取单条数据的信息
    '''
    props = FormatCondition(props)
    sql = """Select * from `%s` where %s;"""%(tablename,props)
    conn = dbpool.connection(write=False,tablename=tablename)
    cursor = conn.cursor(cursorclass = DictCursor)
    cursor.execute(sql)
    result = cursor.fetchone()
    cursor.close()
    conn.close()
    return result

def
GetOneRecordInfo
(
tablename
,
props
)
:
props
=
FormatCondition
(
props
)
sql
=
"""Select * from `%s` where %s;"""
%
(
tablename
,
props
)
conn
=
dbpool
.
connection
(
write
=
False
,
tablename
=
tablename
)
cursor
=
conn
.
cursor
(
cursorclass
=
DictCursor
)
cursor
.
execute
(
sql
)
result
=
cursor
.
fetchone
(
)
cursor
.
close
(
)
conn
.
close
(
)
return
result


def _run(self):
        """执行协议
        """
        address = 'tcp://%s:%s'%self.to_db_address
        self.sock.connect(address)
        while True:
            try:
                message = self.inbox.get()
                self.sock.send_pyobj(message)
            except Exception as e:
                log.err(_stuff=e,_why=traceback.format_exc())
                log.msg(str(message))

def _run ( self ) : address = 'tcp://%s:%s' % self . to_db_address self . sock . connect ( address ) while True : try : message = self . inbox . get ( ) self . sock . send_pyobj ( message ) except Exception as e : log . err ( _stuff = e , _why = traceback . format_exc ( ) ) log . msg ( str ( message ) )


def getData(self):
        """获取data数据
        """
        data = self.get('data')
        if data:
            return data
        tablename,pk_value = self._name.split(':')
        props = {self._pk:int(pk_value)}
        record = util.GetOneRecordInfo(tablename,props)
        if record:
            self.data = record
            self.insert()
            return self.data
        return None

def getData ( self ) : data = self . get ( 'data' ) if data : return data tablename , pk_value = self . _name . split ( ':' ) props = { self . _pk : int ( pk_value ) } record = util . GetOneRecordInfo ( tablename , props ) if record : self . data = record self . insert ( ) return self . data return None


def delete(self):
        """清理对象
        """
        self.syncDB(state=MMODE_STATE_DEL)
        if self._fk:
            data = self.getData()
            if data:
                fk = data.get(self._fk,"")
                pk = data.get(self._pk)
                self._update_fk(pk, fk, None)
        self.mdelete()

def delete ( self ) : self . syncDB ( state = MMODE_STATE_DEL ) if self . _fk : data = self . getData ( ) if data : fk = data . get ( self . _fk , "" ) pk = data . get ( self . _pk ) self . _update_fk ( pk , fk , None ) self . mdelete ( )


def IsEffective(self):
        '''检测对象是否有效
        '''
        return True


def syncDB(self,state=MMODE_STATE_UPDATE):
        """同步到数据库
        """
        tablename = self._name.split(':')[0]
        if state==MMODE_STATE_NEW:
            props = self.getData()
            pk = self._pk
            result = util.InsertIntoDB(tablename, props)
        elif state==MMODE_STATE_UPDATE:
            props = self.getData()
            pk = self._pk
            prere = {pk:props.get(pk)}
            sql = util.UpdateWithDictSQL(tablename, props, prere)
            DBPub().send((tablename,sql))
            result = True
        else:
            pk = self._pk
            props = self.getData()
            prere = {pk:props.get(pk)}
            result = util.DeleteFromDB(tablename,prere)
        return result

def syncDB ( self , state = MMODE_STATE_UPDATE ) : tablename = self . _name . split ( ':' ) [ 0 ] if state == MMODE_STATE_NEW : props = self . getData ( ) pk = self . _pk result = util . InsertIntoDB ( tablename , props ) elif state == MMODE_STATE_UPDATE : props = self . getData ( ) pk = self . _pk prere = { pk : props . get ( pk ) } sql = util . UpdateWithDictSQL ( tablename , props , prere ) DBPub ( ) . send ( ( tablename , sql ) ) result = True else : pk = self . _pk props = self . getData ( ) prere = { pk : props . get ( pk ) } result = util . DeleteFromDB ( tablename , prere ) return result


def load(self):
        '''读取数据到数据库中
        '''
        mmname = self._name
        recordlist = util.ReadDataFromDB(mmname)
        for record in recordlist:
            pk = record[self.pk]
            mm = MMode(self._name+':%s'%pk,self.pk,data=record,fk=self._fk,timeout=self.timeout)
            mm.insert()

def load ( self ) : mmname = self . _name recordlist = util . ReadDataFromDB ( mmname ) for record in recordlist : pk = record [ self . pk ] mm = MMode ( self . _name + ':%s' % pk , self . pk , data = record , fk = self . _fk , timeout = self . timeout ) mm . insert ( )


def getAllPkByFk(self,fk):
        '''根据外键获取主键列表
        '''
        name = '%s_fk:%s'%(self._name,fk)
        fkmm = MFKMode(name)
        pklist = fkmm.get('pklist')
        if pklist is not None:
            return pklist
        props = {self._fk:fk}
        dbkeylist = util.getAllPkByFkInDB(self._name, self.pk, props)
        name = '%s_fk:%s'%(self._name,fk)
        fkmm = MFKMode(name, pklist = dbkeylist)
        fkmm.insert()
        return dbkeylist

def getAllPkByFk ( self , fk ) : name = '%s_fk:%s' % ( self . _name , fk ) fkmm = MFKMode ( name ) pklist = fkmm . get ( 'pklist' ) if pklist is not None : return pklist props = { self . _fk : fk } dbkeylist = util . getAllPkByFkInDB ( self . _name , self . pk , props ) name = '%s_fk:%s' % ( self . _name , fk ) fkmm = MFKMode ( name , pklist = dbkeylist ) fkmm . insert ( ) return dbkeylist


根据主键，可以获得mmode对象的实例 . \ n >>> m = madmin . getObj ( 1 )

def getObj(self,pk):
        '''根据主键，可以获得mmode对象的实例.\n
        >>> m = madmin.getObj(1)
        '''
        mm = MMode(self._name+':%s'%pk,self.pk,fk=self._fk,timeout=self.timeout)
        if mm.get('data'):
            return mm
        props = {self.pk:pk}
        record = util.GetOneRecordInfo(self._name,props)
        if not record:
            return None
        mm =  MMode(self._name+':%s'%pk,self.pk,data = record,fk=self._fk,timeout=self.timeout)
        mm.insert()
        return mm

def getObj ( self , pk ) : mm = MMode ( self . _name + ':%s' % pk , self . pk , fk = self . _fk , timeout = self . timeout ) if mm . get ( 'data' ) : return mm props = { self . pk : pk } record = util . GetOneRecordInfo ( self . _name , props ) if not record : return None mm = MMode ( self . _name + ':%s' % pk , self . pk , data = record , fk = self . _fk , timeout = self . timeout ) mm . insert ( ) return mm


根据主键，可以获得mmode对象的实例的数据 . \ n >>> m = madmin . getObjData ( 1 )

def getObjData(self,pk):
        '''根据主键，可以获得mmode对象的实例的数据.\n
        >>> m = madmin.getObjData(1)
        '''
        mm = MMode(self._name+':%s'%pk,self.pk,fk=self._fk,timeout=self.timeout)
        if not mm.IsEffective():
            return None
        data = mm.get('data')
        if data:
            return data
        props = {self.pk:pk}
        record = util.GetOneRecordInfo(self._name,props)
        if not record:
            return None
        mm =  MMode(self._name+':%s'%pk,self.pk,data = record,fk=self._fk,timeout=self.timeout)
        mm.insert()
        return record

def getObjData ( self , pk ) : mm = MMode ( self . _name + ':%s' % pk , self . pk , fk = self . _fk , timeout = self . timeout ) if not mm . IsEffective ( ) : return None data = mm . get ( 'data' ) if data : return data props = { self . pk : pk } record = util . GetOneRecordInfo ( self . _name , props ) if not record : return None mm = MMode ( self . _name + ':%s' % pk , self . pk , data = record , fk = self . _fk , timeout = self . timeout ) mm . insert ( ) return record


根据主键列表获取mmode对象的列表 . \ n >>> m = madmin . getObjList ( [ 1 2 3 4 5 ] )

def getObjList(self,pklist):
        '''根据主键列表获取mmode对象的列表.\n
        >>> m = madmin.getObjList([1,2,3,4,5])
        '''
        _pklist = []
        objlist = []
        for pk in pklist:
            mm = MMode(self._name+':%s'%pk,self.pk,fk=self._fk,timeout=self.timeout)
            if not mm.IsEffective():
                continue
            if mm.get('data'):
                objlist.append(mm)
            else:
                _pklist.append(pk)
        if _pklist:
            recordlist = util.GetRecordList(self._name, self.pk,_pklist)
            for record in recordlist:
                pk = record[self.pk]
                mm =  MMode(self._name+':%s'%pk,self.pk,data = record,fk=self._fk,timeout=self.timeout)
                mm.insert()
                objlist.append(mm)
        return objlist

def getObjList ( self , pklist ) : _pklist = [ ] objlist = [ ] for pk in pklist : mm = MMode ( self . _name + ':%s' % pk , self . pk , fk = self . _fk , timeout = self . timeout ) if not mm . IsEffective ( ) : continue if mm . get ( 'data' ) : objlist . append ( mm ) else : _pklist . append ( pk ) if _pklist : recordlist = util . GetRecordList ( self . _name , self . pk , _pklist ) for record in recordlist : pk = record [ self . pk ] mm = MMode ( self . _name + ':%s' % pk , self . pk , data = record , fk = self . _fk , timeout = self . timeout ) mm . insert ( ) objlist . append ( mm ) return objlist


根据主键删除内存中的某条记录信息， \ n这里只是修改内存中的记录状态_state为删除状态 . \ n >>> m = madmin . deleteMode ( 1 )

def deleteMode(self,pk):
        '''根据主键删除内存中的某条记录信息，\n这里只是修改内存中的记录状态_state为删除状态.\n
        >>> m = madmin.deleteMode(1)
        '''
        mm = self.getObj(pk)
        if mm:
#             if self._fk:
#                 data = mm.get('data')
#                 if data:
#                     fk = data.get(self._fk,0)
#                     name = '%s_fk:%s'%(self._name,fk)
#                     fkmm = MFKMode(name)
#                     pklist = fkmm.get('pklist')
#                     if pklist and pk in pklist:
#                         pklist.remove(pk)
#                     fkmm.update('pklist', pklist)
            mm.delete()
        return True

#                     fk = data.get(self._fk,0)

#                     name = '%s_fk:%s'%(self._name,fk)

#                     fkmm = MFKMode(name)

#                     pklist = fkmm.get('pklist')

#                     if pklist and pk in pklist:

#                         pklist.remove(pk)

#                     fkmm.update('pklist', pklist)


def new(self,data):
        """创建一个新的对象
        """
        incrkey = self.incrkey
        tablename = self._name
        if incrkey:
            result = util.InsertIntoDBAndReturnID(tablename, data)
            data[incrkey] = result[0]
            pk = data.get(self.pk)
            if pk is None:
                raise PKValueError(data)
            mm = MMode(self._name+':%s'%pk,self.pk,data=data,fk=self._fk,timeout=self.timeout)
        else:
            pk = data.get(self.pk)
            result = util.InsertIntoDB(tablename, data)
            if not result:
                raise util.SQLError()
            mm = MMode(self._name+':%s'%pk,self.pk,data=data,fk=self._fk,timeout=self.timeout)
        mm.insert()
        if self._fk:
            fk = data.get(self._fk,0)
            name = '%s_fk:%s'%(self._name,fk)
            fkmm = MFKMode(name)
            pklist = fkmm.get('pklist')
            pklist = self.getAllPkByFk(fk)
            pklist.append(pk)
            fkmm.update('pklist', pklist)
        return mm

def new ( self , data ) : incrkey = self . incrkey tablename = self . _name if incrkey : result = util . InsertIntoDBAndReturnID ( tablename , data ) data [ incrkey ] = result [ 0 ] pk = data . get ( self . pk ) if pk is None : raise PKValueError ( data ) mm = MMode ( self . _name + ':%s' % pk , self . pk , data = data , fk = self . _fk , timeout = self . timeout ) else : pk = data . get ( self . pk ) result = util . InsertIntoDB ( tablename , data ) if not result : raise util . SQLError ( ) mm = MMode ( self . _name + ':%s' % pk , self . pk , data = data , fk = self . _fk , timeout = self . timeout ) mm . insert ( ) if self . _fk : fk = data . get ( self . _fk , 0 ) name = '%s_fk:%s' % ( self . _name , fk ) fkmm = MFKMode ( name ) pklist = fkmm . get ( 'pklist' ) pklist = self . getAllPkByFk ( fk ) pklist . append ( pk ) fkmm . update ( 'pklist' , pklist ) return mm


def __init__(self,name,**kw):
        '''
        @param name: str 对象的名称\n
        @param _lock: int 对象锁  为1时表示对象被锁定无法进行修改\n
        '''
        self._name = name
        self._locked = False
        self._timestamp = 0
        self._cas = kw.get("cas",False)
        self._cache = kw.get("cache",True)
        self._timeout = kw.get("timeout",CACHE_TIMEOUT)
        if self._cas:
            self.lock()

def __init__ ( self , name , * * kw ) : self . _name = name self . _locked = False self . _timestamp = 0 self . _cas = kw . get ( "cas" , False ) self . _cache = kw . get ( "cache" , True ) self . _timeout = kw . get ( "timeout" , CACHE_TIMEOUT ) if self . _cas : self . lock ( )


def produceKey(self,keyname):
        '''
        重新生成key
        '''
        if isinstance(keyname, basestring):
            return str(''.join([self._name,':',keyname]))
        else:
            raise MEMKeyError()

def produceKey ( self , keyname ) : if isinstance ( keyname , basestring ) : return str ( '' . join ( [ self . _name , ':' , keyname ] ) ) else : raise MEMKeyError ( )


def lock(self):
        '''锁定对象
        '''
        print "locked"
        if not self.isLocked():
            key = self.produceKey('_lock')
            result = memclient.mclient.add(key,1,LOCK_TIMEOUT)
            if result:
                self._timestamp=time.time()
                self._locked = result
        return self._locked

def lock ( self ) : print "locked" if not self . isLocked ( ) : key = self . produceKey ( '_lock' ) result = memclient . mclient . add ( key , 1 , LOCK_TIMEOUT ) if result : self . _timestamp = time . time ( ) self . _locked = result return self . _locked


def release(self):
        '''释放锁
        '''
        if self.isLocked():
            print "release..."
            key = self.produceKey('_lock')
            memclient.mclient.delete(key)
            self._locked = False

def release ( self ) : if self . isLocked ( ) : print "release..." key = self . produceKey ( '_lock' ) memclient . mclient . delete ( key ) self . _locked = False


def get(self,key):
        '''获取对象值
        '''
        if self._cas and not self.isLocked():
            for _ in xrange(10):
                if not self.lock():
                    gevent.sleep(0.2)
                    continue
                break
            else:
                return None
        key = self.produceKey(key)
        return memclient.mclient.get(key)

def get ( self , key ) : if self . _cas and not self . isLocked ( ) : for _ in xrange ( 10 ) : if not self . lock ( ) : gevent . sleep ( 0.2 ) continue break else : return None key = self . produceKey ( key ) return memclient . mclient . get ( key )


def get_multi(self,keys):
        '''一次获取多个key的值
        @param keys: list(str) key的列表
        '''
        if self._cas and not self.isLocked():
            for _ in xrange(10):
                if not self.lock():
                    gevent.sleep(0.2)
                    continue
                break
            else:
                return None
        keynamelist = [self.produceKey(keyname) for keyname in keys]
        olddict = memclient.mclient.get_multi(keynamelist)
        newdict = dict(zip([keyname.split(':')[-1] for keyname in olddict.keys()],
                              olddict.values()))
        return newdict

def get_multi ( self , keys ) : if self . _cas and not self . isLocked ( ) : for _ in xrange ( 10 ) : if not self . lock ( ) : gevent . sleep ( 0.2 ) continue break else : return None keynamelist = [ self . produceKey ( keyname ) for keyname in keys ] olddict = memclient . mclient . get_multi ( keynamelist ) newdict = dict ( zip ( [ keyname . split ( ':' ) [ - 1 ] for keyname in olddict . keys ( ) ] , olddict . values ( ) ) ) return newdict


def update(self,key,values):
        '''修改对象的值
        '''
        if self._cas and not self.isLocked():
            for _ in xrange(10):
                if not self.lock():
                    gevent.sleep(0.2)
                    continue
                break
            else:
                return None
        key = self.produceKey(key)
        return memclient.mclient.set(key,values,time=self._timeout)

def update ( self , key , values ) : if self . _cas and not self . isLocked ( ) : for _ in xrange ( 10 ) : if not self . lock ( ) : gevent . sleep ( 0.2 ) continue break else : return None key = self . produceKey ( key ) return memclient . mclient . set ( key , values , time = self . _timeout )


def update_multi(self,mapping):
        '''同时修改多个key值
        '''
        if self._cas and not self.isLocked():
            for _ in xrange(10):
                if not self.lock():
                    gevent.sleep(0.2)
                    continue
                break
            else:
                return None
        newmapping = dict(zip([self.produceKey(keyname) for keyname in mapping.keys()],
                              mapping.values()))
        return memclient.mclient.set_multi(newmapping,time=self._timeout)

def update_multi ( self , mapping ) : if self . _cas and not self . isLocked ( ) : for _ in xrange ( 10 ) : if not self . lock ( ) : gevent . sleep ( 0.2 ) continue break else : return None newmapping = dict ( zip ( [ self . produceKey ( keyname ) for keyname in mapping . keys ( ) ] , mapping . values ( ) ) ) return memclient . mclient . set_multi ( newmapping , time = self . _timeout )


def mdelete(self):
        '''删除memcache中的数据
        '''
        nowdict = dict(self.__dict__)
        keys = [keyname for keyname in nowdict.keys() if not keyname.startswith('_')]
        keys = [self.produceKey(key) for key in keys]
        memclient.mclient.delete_multi(keys)

def mdelete ( self ) : nowdict = dict ( self . __dict__ ) keys = [ keyname for keyname in nowdict . keys ( ) if not keyname . startswith ( '_' ) ] keys = [ self . produceKey ( key ) for key in keys ] memclient . mclient . delete_multi ( keys )


def insert(self):
        '''插入对象记录
        '''
        nowdict = dict(self.__dict__)
        newmapping = dict([(self.produceKey(keyname),nowdict[keyname]) for keyname in nowdict.keys() if not keyname.startswith('_')])
        memclient.mclient.set_multi(newmapping,time=self._timeout)

def insert ( self ) : nowdict = dict ( self . __dict__ ) newmapping = dict ( [ ( self . produceKey ( keyname ) , nowdict [ keyname ] ) for keyname in nowdict . keys ( ) if not keyname . startswith ( '_' ) ] ) memclient . mclient . set_multi ( newmapping , time = self . _timeout )


在对象销毁时释放锁，避免在获取memcache数据时死锁

在对象销毁时释放锁，避免在获取memcache数据时死锁

在对象销毁时释放锁，避免在获取memcache数据时死锁

def __del__(self):
        """
        在对象销毁时释放锁，避免在获取memcache数据时死锁
        """
        try:
            if self.isLocked():
                self.release()
        except Exception as e:
            print e

def __del__ ( self ) : try : if self . isLocked ( ) : self . release ( ) except Exception as e : print e


def __init__(self, value=None, cache=True,timeout=CACHE_TIMEOUT):
        """
        @param name: str　字段的名称
        @param cache: bool 是否本地缓存获取到的值
        """
        self.name = ""
        self.value = value
        self.cache = cache
        self.timeout = timeout

def __init__ ( self , value = None , cache = True , timeout = CACHE_TIMEOUT ) : self . name = "" self . value = value self . cache = cache self . timeout = timeout


def getValue(self):
        """
        获取name对应的值
        当cache为True时，并且value值存在时，不再重新从memcache中获取值
        """
        if self.cache and self.value:
            return self.value
        else:
            self.refreshValue()
            return self.value

def getValue ( self ) : if self . cache and self . value : return self . value else : self . refreshValue ( ) return self . value


def refreshValue(self):
        """
        重新从memcached中获取值
        """
        self.value = memclient.mclient.get(self.name)
        return self.value

def refreshValue ( self ) : self . value = memclient . mclient . get ( self . name ) return self . value


def setValue(self,value):
        """
        修改对应的值
        """
        self.value = value
        result = memclient.mclient.set(self.name,value,time=self.timeout)
        return result

def setValue ( self , value ) : self . value = value result = memclient . mclient . set ( self . name , value , time = self . timeout ) return result


def __init__(self, name,**kw):
        """
        @param name: str 表示对象的名称

        """
        self._name = name
        self._locked = False
        self._timestamp = 0
        self._cas = kw.get("cas",False)
        self._cache = kw.get("cache",True)
        self._timeout = kw.get("timeout",CACHE_TIMEOUT)
        if self._cas:
            self.lock()


def initFields(self):
        """
        初始化字段
        """
        nowdict = dict(self.__dict__)
        for item_key,item_value in nowdict.items():
            if isinstance(item_value, MemFields):
                item_value.name = self.produceKey(item_key)
                item_value.timeout = self._timeout
                item_value.cache = self._cache

def initFields ( self ) : nowdict = dict ( self . __dict__ ) for item_key , item_value in nowdict . items ( ) : if isinstance ( item_value , MemFields ) : item_value . name = self . produceKey ( item_key ) item_value . timeout = self . _timeout item_value . cache = self . _cache


def produceKey(self,keyname):
        '''
        重新生成key
        '''
        if isinstance(keyname, basestring):
            return ''.join([self._name,':',keyname])
        else:
            raise MEMKeyError()

def produceKey ( self , keyname ) : if isinstance ( keyname , basestring ) : return '' . join ( [ self . _name , ':' , keyname ] ) else : raise MEMKeyError ( )


def lock(self):
        '''
        检测对象是否被锁定
        '''
        print "lock"
        if not self.isLocked():
            key = self.produceKey('_lock')
            result = memclient.mclient.add(key,1,LOCK_TIMEOUT)
            if result:
                self._timestamp=time.time()
                self._locked = result
        return self._locked

def lock ( self ) : print "lock" if not self . isLocked ( ) : key = self . produceKey ( '_lock' ) result = memclient . mclient . add ( key , 1 , LOCK_TIMEOUT ) if result : self . _timestamp = time . time ( ) self . _locked = result return self . _locked


def release(self):
        '''释放锁
        '''
        if self.isLocked():
            key = self.produceKey('_lock')
            memclient.mclient.delete(key)
            self._locked = False

def release ( self ) : if self . isLocked ( ) : key = self . produceKey ( '_lock' ) memclient . mclient . delete ( key ) self . _locked = False


def insert(self):
        '''
        插入对象记录
        '''
        nowdict = dict(self.__dict__)
        newmapping = dict([(self.produceKey(item_key), item_value.value) for item_key,item_value in\
                            nowdict.items() if isinstance(item_value, MemFields) and item_value.value])
        memclient.mclient.set_multi(newmapping,time=self._timeout)

def insert ( self ) : nowdict = dict ( self . __dict__ ) newmapping = dict ( [ ( self . produceKey ( item_key ) , item_value . value ) for item_key , item_value in nowdict . items ( ) if isinstance ( item_value , MemFields ) and item_value . value ] ) memclient . mclient . set_multi ( newmapping , time = self . _timeout )


def mdelete(self):
        """
        删除缓存中的对象
        """
        nowdict = dict(self.__dict__)
        keys = [self.produceKey(item_key) for item_key,item_value in\
                            nowdict.items() if isinstance(item_value, MemFields)]
        return memclient.mclient.delete_multi(keys)

def mdelete ( self ) : nowdict = dict ( self . __dict__ ) keys = [ self . produceKey ( item_key ) for item_key , item_value in nowdict . items ( ) if isinstance ( item_value , MemFields ) ] return memclient . mclient . delete_multi ( keys )


def __getattribute__(self,attr):
        """
        封装了获取字段值的过程
        """
        value = object.__getattribute__(self,attr)
        if isinstance(value, MemFields):
            print 'locked',self.isLocked()
            if self._cas and not self.isLocked():
                for _ in xrange(10):
                    if not self.lock():
                        time.sleep(0.2)
                        continue
                    break
                else:
                    return None
            return value.getValue()
        return value

def __getattribute__ ( self , attr ) : value = object . __getattribute__ ( self , attr ) if isinstance ( value , MemFields ) : print 'locked' , self . isLocked ( ) if self . _cas and not self . isLocked ( ) : for _ in xrange ( 10 ) : if not self . lock ( ) : time . sleep ( 0.2 ) continue break else : return None return value . getValue ( ) return value


在对象销毁时释放锁，避免在获取memcache数据时死锁

在对象销毁时释放锁，避免在获取memcache数据时死锁

在对象销毁时释放锁，避免在获取memcache数据时死锁


def __init__(self):
        """初始化所有管理的的madmin的集合，放在self.admins中
        """
        self.isStart = False
        self.to_db_port = None

def __init__ ( self ) : self . isStart = False self . to_db_port = None


注册一个madmin对象到管理中 . >>> madmin = MAdmin ( tb_registe characterId incrkey = id ) >>> MAdminManager () . registe ( madmin )

def registe(self,admin):
        """注册一个madmin对象到管理中.
        >>> madmin = MAdmin('tb_registe','characterId',incrkey='id')
        >>> MAdminManager().registe(madmin)
        """
        pass


def _run(self):
        """执行协议
        """
        while True:
#             msg = self.sock.recv()
            try:
                pyobj = self.sock.recv_pyobj()
                tablename,sql = pyobj
                excuteSQL(tablename, sql)
            except Exception,e:
                log.err(_stuff=e,_why=traceback.format_exc())

def
_run
(
self
)
:
while
True
:
#             msg = self.sock.recv()
try
:
pyobj
=
self
.
sock
.
recv_pyobj
(
)
tablename
,
sql
=
pyobj
excuteSQL
(
tablename
,
sql
)
except
Exception
,
e
:
log
.
err
(
_stuff
=
e
,
_why
=
traceback
.
format_exc
(
)
)


遍历所有的madmin，与数据库进行同步。

遍历所有的madmin，与数据库进行同步。

def checkAdmins(self,port=M2DB_PORT):
        """遍历所有的madmin，与数据库进行同步。

        >>>MAdminManager().checkAdmins(port=M2DB_PORT)
        """
        if self.isStart:
            return
        self.isStart=True
        context = zmq.Context()
        self.sock = context.socket(zmq.SUB)

        if port==M2DB_PORT:
            port = ToDBAddress().m2db_port
        address = 'tcp://*:%s'%port
        self.sock.bind(address)
        self.sock.setsockopt(zmq.SUBSCRIBE, "")
        gevent.spawn(self._run)

def checkAdmins ( self , port = M2DB_PORT ) : if self . isStart : return self . isStart = True context = zmq . Context ( ) self . sock = context . socket ( zmq . SUB ) if port == M2DB_PORT : port = ToDBAddress ( ) . m2db_port address = 'tcp://*:%s' % port self . sock . bind ( address ) self . sock . setsockopt ( zmq . SUBSCRIBE , "" ) gevent . spawn ( self . _run )


def memcached_connect(servers,mem_lib="default",dead_retry=_DEAD_RETRY,**kw):
    """
    memcached connect
    @param servers: (list) memcached的地址，例如["127.0.0.1:11211","127.0.0.1:11222",]
    @param mem_lib: 选择性的使用memcached库,默认的使用python-memcached
    {'python-memcached':"memcache","python-ultramemcache":"ultramemcache","default":"memcache"}
    """
    global mclient
    memcache = __import__(MEMCS.get(mem_lib))
    mclient = memcache.Client(servers,dead_retry=dead_retry,**kw)
    server,key = mclient._get_server("ping")
    if not server or not key:
        raise MemConnError()

def memcached_connect ( servers , mem_lib = "default" , dead_retry = _DEAD_RETRY , * * kw ) : global mclient memcache = __import__ ( MEMCS . get ( mem_lib ) ) mclient = memcache . Client ( servers , dead_retry = dead_retry , * * kw ) server , key = mclient . _get_server ( "ping" ) if not server or not key : raise MemConnError ( )


def syncDB(self):
        """同步到数据库
        """
        tablename = self._name.split(':')[0]
        data_item = self.__dict__.get("data")
        data_item.setValue(data_item.value)
        if self._cas:
            self.release()
        props = self.data
        pk = self.pk
        prere = {pk:props.get(pk)}
        result = util.UpdateWithDict(tablename, props, prere)
        return result

def syncDB ( self ) : tablename = self . _name . split ( ':' ) [ 0 ] data_item = self . __dict__ . get ( "data" ) data_item . setValue ( data_item . value ) if self . _cas : self . release ( ) props = self . data pk = self . pk prere = { pk : props . get ( pk ) } result = util . UpdateWithDict ( tablename , props , prere ) return result


def load(self):
        '''读取数据到数据库中
        '''
        mmname = self.name
        recordlist = util.ReadDataFromDB(mmname)
        for record in recordlist:
            pk = record[self.pk]
            mm = MemModel(self.name+':%s'%pk,self.pk,data=record,**self.kw)
            mm.insert()

def load ( self ) : mmname = self . name recordlist = util . ReadDataFromDB ( mmname ) for record in recordlist : pk = record [ self . pk ] mm = MemModel ( self . name + ':%s' % pk , self . pk , data = record , * * self . kw ) mm . insert ( )


def new(self,data):
        """创建一个新的对象
        """
        incrkey = self.incrkey
        tablename = self.name
        if incrkey:
            result = util.InsertIntoDBAndReturnID(tablename, data)
            data[incrkey] = result
            pk = data.get(self.pk)
            if pk is None:
                raise PKValueError(data)
            mm = MemModel(self.name+':%s'%pk,self.pk,data=data,**self.kw)
            setattr(mm,incrkey,pk)
        else:
            pk = data.get(self.pk)
            result = util.InsertIntoDB(tablename, data)
            if not result:
                raise util.SQLError()
            mm = MemModel(self.name+':%s'%pk,self.pk,data=data,**self.kw)
        mm.insert()
        return mm

def new ( self , data ) : incrkey = self . incrkey tablename = self . name if incrkey : result = util . InsertIntoDBAndReturnID ( tablename , data ) data [ incrkey ] = result pk = data . get ( self . pk ) if pk is None : raise PKValueError ( data ) mm = MemModel ( self . name + ':%s' % pk , self . pk , data = data , * * self . kw ) setattr ( mm , incrkey , pk ) else : pk = data . get ( self . pk ) result = util . InsertIntoDB ( tablename , data ) if not result : raise util . SQLError ( ) mm = MemModel ( self . name + ':%s' % pk , self . pk , data = data , * * self . kw ) mm . insert ( ) return mm


根据主键，可以获得mmode对象的实例.

根据主键，可以获得mmode对象的实例.

根据主键，可以获得mmode对象的实例 .

def getObj(self,pk):
        '''根据主键，可以获得mmode对象的实例.
        '''
        mm = MemModel(self.name+':%s'%pk,self.pk,**self.kw)
        if mm.data:
            return mm
        else:
            return None

def getObj ( self , pk ) : mm = MemModel ( self . name + ':%s' % pk , self . pk , * * self . kw ) if mm . data : return mm else : return None


def getObjList(self,pklist):
        '''根据主键列表获取mmode对象的列表.\n
        >>> m = madmin.getObjList([1,2,3,4,5])
        '''
        objlist = []
        for pk in pklist:
            mm = MemModel(self.name+':%s'%pk,self.pk,**self.kw)
            objlist.append(mm)
        return objlist

def getObjList ( self , pklist ) : objlist = [ ] for pk in pklist : mm = MemModel ( self . name + ':%s' % pk , self . pk , * * self . kw ) objlist . append ( mm ) return objlist


def _doChildConnect(name,transport):
    """当server节点连接到master的处理
    """
    server_config = GlobalObject().json_config.get('servers',{}).get(name,{})
    remoteport = server_config.get('remoteport',[])
    child_host = transport.transport.address[0]
    root_list = [rootport.get('rootname') for rootport in remoteport]
    GlobalObject().remote_map[name] = {"host":child_host,"root_list":root_list}
    #通知有需要连的node节点连接到此root节点
    for servername,remote_list in GlobalObject().remote_map.items():
        remote_host = remote_list.get("host","")
        remote_name_host = remote_list.get("root_list","")
        if name in remote_name_host:
            GlobalObject().root.callChild(servername,"remote_connect",name,remote_host)
    #查看当前是否有可供连接的root节点
    master_node_list = GlobalObject().remote_map.keys()
    for root_name in root_list:
        if root_name in master_node_list:
            root_host = GlobalObject().remote_map[root_name]['host']
            GlobalObject().root.callChild(name,"remote_connect",root_name,root_host)

def _doChildConnect ( name , transport ) : server_config = GlobalObject ( ) . json_config . get ( 'servers' , { } ) . get ( name , { } ) remoteport = server_config . get ( 'remoteport' , [ ] ) child_host = transport . transport . address [ 0 ] root_list = [ rootport . get ( 'rootname' ) for rootport in remoteport ] GlobalObject ( ) . remote_map [ name ] = { "host" : child_host , "root_list" : root_list } #通知有需要连的node节点连接到此root节点 for servername , remote_list in GlobalObject ( ) . remote_map . items ( ) : remote_host = remote_list . get ( "host" , "" ) remote_name_host = remote_list . get ( "root_list" , "" ) if name in remote_name_host : GlobalObject ( ) . root . callChild ( servername , "remote_connect" , name , remote_host ) #查看当前是否有可供连接的root节点 master_node_list = GlobalObject ( ) . remote_map . keys ( ) for root_name in root_list : if root_name in master_node_list : root_host = GlobalObject ( ) . remote_map [ root_name ] [ 'host' ] GlobalObject ( ) . root . callChild ( name , "remote_connect" , root_name , root_host )


def stop():
    '''stop service
    '''
    for child in GlobalObject().root.childsmanager._childs.values():
        child.callbackChildNotForResult('serverStop')
    reactor.callLater(0.5,reactor.stop)
    return "stop"

def stop ( ) : for child in GlobalObject ( ) . root . childsmanager . _childs . values ( ) : child . callbackChildNotForResult ( 'serverStop' ) reactor . callLater ( 0.5 , reactor . stop ) return "stop"


def reloadmodule():
    '''reload module
    '''
    for child in GlobalObject().root.childsmanager._childs.values():
        child.callbackChildNotForResult('sreload')
    return "reload"

def reloadmodule ( ) : for child in GlobalObject ( ) . root . childsmanager . _childs . values ( ) : child . callbackChildNotForResult ( 'sreload' ) return "reload"


def __init__(self):
        '''初始化'''
        self._service = Service('proxy')

def __init__ ( self ) : self . _service = Service ( 'proxy' )


def addService(self,service):
        '''添加一条服务通道'''
        self._service = service

def addService ( self , service ) : self . _service = service


def remote_callChild(self, command,*arg,**kw):
        '''代理发送数据
        '''
        return self._service.callTarget(command,*arg,**kw)

def remote_callChild ( self , command , * arg , * * kw ) : return self . _service . callTarget ( command , * arg , * * kw )


def __init__(self,name):
        '''初始化子节点对象
        '''
        self._name = name
        self._transport = None

def __init__ ( self , name ) : self . _name = name self . _transport = None


def getName(self):
        '''获取子节点的名称'''
        return self._name

def getName ( self ) : return self . _name


def setTransport(self,transport):
        '''设置子节点的通道'''
        self._transport = transport

def setTransport ( self , transport ) : self . _transport = transport


回调子节点的接口 \ n return a Defered Object ( recvdata )

def callbackChild(self,*args,**kw):
        '''回调子节点的接口\n
        return a Defered Object (recvdata)
        '''
        return self.callbackChildForResult(*args,**kw)

def callbackChild ( self , * args , * * kw ) : return self . callbackChildForResult ( * args , * * kw )


def callbackChildNotForResult(self,*args,**kw):
        '''回调子节点的接口\n
        return a Defered Object (recvdata)
        '''
        remote = self._transport.getRootObject()
        remote.callRemoteNotForResult('callChild',*args,**kw)

def callbackChildNotForResult ( self , * args , * * kw ) : remote = self . _transport . getRootObject ( ) remote . callRemoteNotForResult ( 'callChild' , * args , * * kw )


def callbackChildForResult(self,*args,**kw):
        '''回调子节点的接口\n
        return a Defered Object (recvdata)
        '''
        remote = self._transport.getRootObject()
        recvdata = remote.callRemoteForResult('callChild',*args,**kw)
        return recvdata

def callbackChildForResult ( self , * args , * * kw ) : remote = self . _transport . getRootObject ( ) recvdata = remote . callRemoteForResult ( 'callChild' , * args , * * kw ) return recvdata


设置代理通道
        @param name: 根节点的名称

def remote_takeProxy(self,name):
        '''设置代理通道
        @param name: 根节点的名称
        '''
        self.factory.root.remote_takeProxy(name,self)

def remote_takeProxy ( self , name ) : self . factory . root . remote_takeProxy ( name , self )


def remote_callTarget(self,command,*args,**kw):
        '''远程调用方法
        @param commandId: int 指令号
        @param data: str 调用参数
        '''
        data = self.factory.root.remote_callTarget(command,*args,**kw)
        return data

def remote_callTarget ( self , command , * args , * * kw ) : data = self . factory . root . remote_callTarget ( command , * args , * * kw ) return data


def __init__(self,dnsmanager = ChildsManager()):
        '''初始化根节点
        '''
        self.service = None
        self.childsmanager = dnsmanager

def __init__ ( self , dnsmanager = ChildsManager ( ) ) : self . service = None self . childsmanager = dnsmanager


def addServiceChannel(self,service):
        '''添加服务通道
        @param service: Service Object(In bilateral.services)
        '''
        self.service = service

def addServiceChannel ( self , service ) : self . service = service


设置代理通道
        @param name: 根节点的名称

def remote_takeProxy(self,name,transport):
        '''设置代理通道
        @param name: 根节点的名称
        '''
        log.msg('node [%s] takeProxy ready'%name)
        child = Child(name)
        self.childsmanager.addChild(child)
        child.setTransport(transport)
        self.doChildConnect(name, transport)

def
remote_takeProxy
(
self
,
name
,
transport
)
:
log
.
msg
(
'node [%s] takeProxy ready'
%
name
)
child
=
Child
(
name
)
self
.
childsmanager
.
addChild
(
child
)
child
.
setTransport
(
transport
)
self
.
doChildConnect
(
name
,
transport
)


当node节点连接时的处理

当node节点连接时的处理

当node节点连接时的处理

def doChildConnect(self,name,transport):
        """当node节点连接时的处理
        """
        pass

def doChildConnect ( self , name , transport ) : pass


def remote_callTarget(self,command,*args,**kw):
        '''远程调用方法
        @param commandId: int 指令号
        @param data: str 调用参数
        '''
        data = self.service.callTarget(command,*args,**kw)
        return data

def remote_callTarget ( self , command , * args , * * kw ) : data = self . service . callTarget ( command , * args , * * kw ) return data


def dropChild(self,*args,**kw):
        '''删除子节点记录'''
        self.childsmanager.dropChild(*args,**kw)

def dropChild ( self , * args , * * kw ) : self . childsmanager . dropChild ( * args , * * kw )


def dropChildByID(self,childId):
        '''删除子节点记录
        '''
        self.doChildLostConnect(childId)
        self.childsmanager.dropChildByID(childId)

def dropChildByID ( self , childId ) : self . doChildLostConnect ( childId ) self . childsmanager . dropChildByID ( childId )


def dropChildSessionId(self, session_id):
        '''删除子节点记录'''
        child = self.childsmanager.getChildBYSessionId(session_id)
        if not child:
            return
        childname = child.getName()
        self.doChildLostConnect(childname)
        self.childsmanager.dropChildByID(childname)

def dropChildSessionId ( self , session_id ) : child = self . childsmanager . getChildBYSessionId ( session_id ) if not child : return childname = child . getName ( ) self . doChildLostConnect ( childname ) self . childsmanager . dropChildByID ( childname )


当node节点连接时的处理

当node节点连接时的处理

当node节点连接时的处理

def doChildLostConnect(self,childname):
        """当node节点连接时的处理
        """
        pass

def doChildLostConnect ( self , childname ) : pass


def callChild(self,key,*args,**kw):
        '''调用子节点的接口
        @param childId: int 子节点的id
        return Defered Object
        '''
        return self.childsmanager.callChild(key,*args,**kw)

def callChild ( self , key , * args , * * kw ) : return self . childsmanager . callChild ( key , * args , * * kw )


def callChildNotForResult(self,childname,*args,**kw):
        '''调用子节点的接口
        @param childId: int 子节点的id
        return Defered Object
        '''
        self.childsmanager.callChildNotForResult(childname,*args,**kw)

def callChildNotForResult ( self , childname , * args , * * kw ) : self . childsmanager . callChildNotForResult ( childname , * args , * * kw )


def __init__(self):
        '''初始化子节点管理器'''
        self._childs = {}

def __init__ ( self ) : self . _childs = { }


根据节点的名称获取节点实例

根据节点的名称获取节点实例

根据节点的名称获取节点实例

def getChild(self,childname):
        '''根据节点的名称获取节点实例'''
        for key,child in self._childs.items():
            if child.getName() == childname:
                return self._childs[key]
        return None

def getChild ( self , childname ) : for key , child in self . _childs . items ( ) : if child . getName ( ) == childname : return self . _childs [ key ] return None


def addChild(self,child):
        '''添加一个child节点\n
        @param child: Child object
        '''
        key = child.getName()
        if self._childs.has_key(key):
            raise "child node %s exists"% key
        self._childs[key] = child

def
addChild
(
self
,
child
)
:
key
=
child
.
getName
(
)
if
self
.
_childs
.
has_key
(
key
)
:
raise
"child node %s exists"
%
key
self
.
_childs
[
key
]
=
child


def dropChild(self,child):
        '''删除一个child 节点\n
        @param child: Child Object
        '''
        key = child.getName()
        try:
            del self._childs[key]
        except Exception,e:
            log.msg(str(e))

def dropChild ( self , child ) : key = child . getName ( ) try : del self . _childs [ key ] except Exception , e : log . msg ( str ( e ) )


删除一个child 节点\n
        @param childId: Child ID

def dropChildByID(self,childId):
        '''删除一个child 节点\n
        @param childId: Child ID
        '''
        try:
            del self._childs[childId]
        except Exception,e:
            log.msg(str(e))

def dropChildByID ( self , childId ) : try : del self . _childs [ childId ] except Exception , e : log . msg ( str ( e ) )


调用子节点的接口\n
        @param childname: str 子节点的名称

调用子节点的接口 \ n

def callChild(self,childname,*args,**kw):
        '''调用子节点的接口\n
        @param childname: str 子节点的名称
        '''
        child = self.getChild(childname)
        if not child:
            log.err("child %s doesn't exists"%childname)
            return
        return child.callbackChild(*args,**kw)

def
callChild
(
self
,
childname
,
*
args
,
*
*
kw
)
:
child
=
self
.
getChild
(
childname
)
if
not
child
:
log
.
err
(
"child %s doesn't exists"
%
childname
)
return
return
child
.
callbackChild
(
*
args
,
*
*
kw
)


调用子节点的接口 \ n

def callChildNotForResult(self,childname,*args,**kw):
        '''调用子节点的接口\n
        @param childname: str 子节点的名称
        '''
        child = self._childs.get(childname,None)
        if not child:
            log.err("child %s doesn't exists"%childname)
            return
        child.callbackChildNotForResult(*args,**kw)

def
callChildNotForResult
(
self
,
childname
,
*
args
,
*
*
kw
)
:
child
=
self
.
_childs
.
get
(
childname
,
)
if
not
child
:
log
.
err
(
"child %s doesn't exists"
%
childname
)
return
child
.
callbackChildNotForResult
(
*
args
,
*
*
kw
)


def getChildBYSessionId(self, session_id):
        """根据sessionID获取child节点信息
        """
        for child in self._childs.values():
            if child._transport.transport.sessionno == session_id:
                return child
        return None

def getChildBYSessionId ( self , session_id ) : for child in self . _childs . values ( ) : if child . _transport . transport . sessionno == session_id : return child return None


def setProxyReference(self,pr):
        """设置代理接口提供对象
        """
        self.reference = pr

def setProxyReference ( self , pr ) : self . reference = pr


重写获取接口对象的方法，从代理接口提供对象中获取

重写获取接口对象的方法，从代理接口提供对象中获取

重写获取接口对象的方法，从代理接口提供对象中获取

def getRemoteMethod(self, _name):
        """重写获取接口对象的方法，从代理接口提供对象中获取
        """
        method = getattr(self.reference, "remote_%s"%_name)
        return method

def getRemoteMethod ( self , _name ) : method = getattr ( self . reference , "remote_%s" % _name ) return method


node节点端开后的处理

node节点端开后的处理

node节点端开后的处理

def doconnectionLost(self):
        """node节点端开后的处理
        """
        if self.ro:
            self.ro.reconnect()

def doconnectionLost ( self ) : if self . ro : self . ro . reconnect ( )


def __init__(self,name,timeout=600):
        '''初始化远程调用对象
        @param port: int 远程分布服的端口号
        @param rootaddr: 根节点服务器地址
        '''
        self._name = name
        self._factory = BilateralClientFactory(self)
        self._reference = ProxyReference()
        self._addr = None
        self._timeout = timeout

def __init__ ( self , name , timeout = 600 ) : self . _name = name self . _factory = BilateralClientFactory ( self ) self . _reference = ProxyReference ( ) self . _addr = None self . _timeout = timeout


def setName(self,name):
        '''设置节点的名称'''
        self._name = name

def setName ( self , name ) : self . _name = name


def getName(self):
        '''获取节点的名称'''
        return self._name

def getName ( self ) : return self . _name


def connect(self,addr):
        '''初始化远程调用对象'''
        self._addr = addr
        reactor.connectTCP(addr[0], addr[1], self._factory)
        self.takeProxy()

def connect ( self , addr ) : self . _addr = addr reactor . connectTCP ( addr [ 0 ] , addr [ 1 ] , self . _factory ) self . takeProxy ( )


def reconnect(self,addr=()):
        '''重新连接'''
        if addr:
            self.connect(addr)
        else:
            self.connect(self._addr)

def reconnect ( self , addr = ( ) ) : if addr : self . connect ( addr ) else : self . connect ( self . _addr )


def addServiceChannel(self,service):
        '''设置引用对象'''
        self._reference.addService(service)

def addServiceChannel ( self , service ) : self . _reference . addService ( service )


像远程服务端发送代理通道对象

像远程服务端发送代理通道对象

像远程服务端发送代理通道对象

def takeProxy(self):
        '''像远程服务端发送代理通道对象
        '''
        self._factory._protocol.setProxyReference(self._reference)
        deferedRemote = self._factory.getRootObject(timeout=self._timeout)
        deferedRemote.callRemoteNotForResult('takeProxy',self._name)

def takeProxy ( self ) : self . _factory . _protocol . setProxyReference ( self . _reference ) deferedRemote = self . _factory . getRootObject ( timeout = self . _timeout ) deferedRemote . callRemoteNotForResult ( 'takeProxy' , self . _name )


默认远程调用，等待结果放回

默认远程调用，等待结果放回

默认远程调用，等待结果放回

def callRemote(self,commandId,*args,**kw):
        """默认远程调用，等待结果放回
        """
        deferedRemote = self._factory.getRootObject(timeout=self._timeout)
        return deferedRemote.callRemoteForResult('callTarget',commandId,*args,**kw)

def callRemote ( self , commandId , * args , * * kw ) : deferedRemote = self . _factory . getRootObject ( timeout = self . _timeout ) return deferedRemote . callRemoteForResult ( 'callTarget' , commandId , * args , * * kw )


远程调用，并等待结果放回

远程调用，并等待结果放回

远程调用，并等待结果放回

def callRemoteForResult(self,commandId,*args,**kw):
        '''远程调用，并等待结果放回
        '''
        deferedRemote = self._factory.getRootObject(timeout=self._timeout)
        return deferedRemote.callRemoteForResult('callTarget',commandId,*args,**kw)

def callRemoteForResult ( self , commandId , * args , * * kw ) : deferedRemote = self . _factory . getRootObject ( timeout = self . _timeout ) return deferedRemote . callRemoteForResult ( 'callTarget' , commandId , * args , * * kw )


远程调用,不需要结果放回

远程调用,不需要结果放回

远程调用 不需要结果放回

def callRemoteNotForResult(self,commandId,*args,**kw):
        '''远程调用,不需要结果放回
        '''
        deferedRemote = self._factory.getRootObject(timeout=self._timeout)
        return deferedRemote.callRemoteNotForResult('callTarget',commandId,*args,**kw)

def callRemoteNotForResult ( self , commandId , * args , * * kw ) : deferedRemote = self . _factory . getRootObject ( timeout = self . _timeout ) return deferedRemote . callRemoteNotForResult ( 'callTarget' , commandId , * args , * * kw )


def __init__(self, _conn):
        '''
        id 连接的ID
        transport 连接的通道
        '''
        self.id = _conn.transport.sessionno
        self.instance = _conn

def __init__ ( self , _conn ) : self . id = _conn . transport . sessionno self . instance = _conn


def loseConnection(self):
        '''断开与客户端的连接
        '''
        self.instance.transport.close()

def loseConnection ( self ) : self . instance . transport . close ( )


def safeToWriteData(self,topicID,msg):
        """发送消息
        """
        self.instance.safeToWriteData(msg,topicID)

def safeToWriteData ( self , topicID , msg ) : self . instance . safeToWriteData ( msg , topicID )


def __init__(self,HEAD_0 = 0,HEAD_1=0,HEAD_2=0,HEAD_3=0,protoVersion= 0,serverVersion=0):
        '''初始化
        @param HEAD_0: int 协议头0
        @param HEAD_1: int 协议头1
        @param HEAD_2: int 协议头2
        @param HEAD_3: int 协议头3
        @param protoVersion: int 协议头版本号
        @param serverVersion: int 服务版本号
        '''
        self.HEAD_0 = HEAD_0
        self.HEAD_1 = HEAD_1
        self.HEAD_2 = HEAD_2
        self.HEAD_3 = HEAD_3
        self.protoVersion = protoVersion
        self.serverVersion = serverVersion

def __init__ ( self , HEAD_0 = 0 , HEAD_1 = 0 , HEAD_2 = 0 , HEAD_3 = 0 , protoVersion = 0 , serverVersion = 0 ) : self . HEAD_0 = HEAD_0 self . HEAD_1 = HEAD_1 self . HEAD_2 = HEAD_2 self . HEAD_3 = HEAD_3 self . protoVersion = protoVersion self . serverVersion = serverVersion


def getHeadlength(self):
        """获取数据包的长度
        """
        return 17


def unpack(self,dpack):
        '''解包
        '''
        try:
            ud = struct.unpack('!sssss3I',dpack)
        except struct.error,de:
            log.err(de,traceback.format_exc())
            return {'result':False,'command':0,'length':0}
        HEAD_0 = ord(ud[0])
        HEAD_1 = ord(ud[1])
        HEAD_2 = ord(ud[2])
        HEAD_3 = ord(ud[3])
        protoVersion = ord(ud[4])
        serverVersion = ud[5]
        length = ud[6]-4
        command = ud[7]
        if HEAD_0 <>self.HEAD_0 or HEAD_1<>self.HEAD_1 or\
             HEAD_2<>self.HEAD_2 or HEAD_3<>self.HEAD_3 or\
              protoVersion<>self.protoVersion or serverVersion<>self.serverVersion:
            return {'result':False,'command':0,'length':0}
        return {'result':True,'command':command,'length':length}

def unpack ( self , dpack ) : try : ud = struct . unpack ( '!sssss3I' , dpack ) except struct . error , de : log . err ( de , traceback . format_exc ( ) ) return { 'result' : False , 'command' : 0 , 'length' : 0 } HEAD_0 = ord ( ud [ 0 ] ) HEAD_1 = ord ( ud [ 1 ] ) HEAD_2 = ord ( ud [ 2 ] ) HEAD_3 = ord ( ud [ 3 ] ) protoVersion = ord ( ud [ 4 ] ) serverVersion = ud [ 5 ] length = ud [ 6 ] - 4 command = ud [ 7 ] if HEAD_0 <> self . HEAD_0 or HEAD_1 <> self . HEAD_1 or HEAD_2 <> self . HEAD_2 or HEAD_3 <> self . HEAD_3 or protoVersion <> self . protoVersion or serverVersion <> self . serverVersion : return { 'result' : False , 'command' : 0 , 'length' : 0 } return { 'result' : True , 'command' : command , 'length' : length }


def pack(self,command,response):
        '''打包数据包
        '''
        HEAD_0 = chr(self.HEAD_0)
        HEAD_1 = chr(self.HEAD_1)
        HEAD_2 = chr(self.HEAD_2)
        HEAD_3 = chr(self.HEAD_3)
        protoVersion = chr(self.protoVersion)
        serverVersion = self.serverVersion
        length = response.__len__()+4
        commandID = command
        data = struct.pack('!sssss3I',HEAD_0,HEAD_1,HEAD_2,HEAD_3,\
                           protoVersion,serverVersion,length,commandID)
        data = data + response
        return data

def pack ( self , command , response ) : HEAD_0 = chr ( self . HEAD_0 ) HEAD_1 = chr ( self . HEAD_1 ) HEAD_2 = chr ( self . HEAD_2 ) HEAD_3 = chr ( self . HEAD_3 ) protoVersion = chr ( self . protoVersion ) serverVersion = self . serverVersion length = response . __len__ ( ) + 4 commandID = command data = struct . pack ( '!sssss3I' , HEAD_0 , HEAD_1 , HEAD_2 , HEAD_3 , protoVersion , serverVersion , length , commandID ) data = data + response return data


def __init__(self):
        '''初始化
        @param _connections: dict {connID:conn Object}
        '''
        self._connections = {}

def __init__ ( self ) : self . _connections = { }


def getNowConnCnt(self):
        '''获取当前连接数量'''
        return len(self._connections.items())

def getNowConnCnt ( self ) : return len ( self . _connections . items ( ) )


加入一条连接
        @param _conn: Conn object

def addConnection(self, conn):
        '''加入一条连接
        @param _conn: Conn object
        '''
        _conn = Connection(conn)
        if self._connections.has_key(_conn.id):
            raise Exception("系统记录冲突")
        self._connections[_conn.id] = _conn

def addConnection ( self , conn ) : _conn = Connection ( conn ) if self . _connections . has_key ( _conn . id ) : raise Exception ( "系统记录冲突") self . _connections [ _conn . id ] = _conn


更加连接的id删除连接实例
        @param connID: int 连接的id

更加连接的id删除连接实例

更加连接的id删除连接实例

def dropConnectionByID(self, connID):
        '''更加连接的id删除连接实例
        @param connID: int 连接的id
        '''
        try:
            del self._connections[connID]
        except Exception as e:
            log.msg(str(e))

def dropConnectionByID ( self , connID ) : try : del self . _connections [ connID ] except Exception as e : log . msg ( str ( e ) )


根据ID获取一条连接
        @param connID: int 连接的id

def getConnectionByID(self, connID):
        """根据ID获取一条连接
        @param connID: int 连接的id
        """
        return self._connections.get(connID,None)

def getConnectionByID ( self , connID ) : return self . _connections . get ( connID , None )


根据连接ID主动端口与客户端的连接

根据连接ID主动端口与客户端的连接

根据连接ID主动端口与客户端的连接

def loseConnection(self,connID):
        """根据连接ID主动端口与客户端的连接
        """
        conn = self.getConnectionByID(connID)
        if conn:
            conn.loseConnection()
            self.dropConnectionByID(connID)

def loseConnection ( self , connID ) : conn = self . getConnectionByID ( connID ) if conn : conn . loseConnection ( ) self . dropConnectionByID ( connID )


def pushObject(self,topicID , msg, sendList):
        """主动推送消息
        """
        for target in sendList:
            try:
                conn = self.getConnectionByID(target)
                if conn:
                    conn.safeToWriteData(topicID,msg)
            except Exception,e:
                log.err(e,traceback.format_exc())

def pushObject ( self , topicID , msg , sendList ) : for target in sendList : try : conn = self . getConnectionByID ( target ) if conn : conn . safeToWriteData ( topicID , msg ) except Exception , e : log . err ( e , traceback . format_exc ( ) )


def connectionMade(self):
        '''连接建立处理
        '''
        address = self.transport.getAddress()
        log.msg('Client %d login in.[%s,%d]'%(self.transport.sessionno,\
                address[0],address[1]))
        self.factory.connmanager.addConnection(self)
        self.factory.doConnectionMade(self)

def
connectionMade
(
self
)
:
address
=
self
.
transport
.
getAddress
(
)
log
.
msg
(
'Client %d login in.[%s,%d]'
%
(
self
.
transport
.
sessionno
,
address
[
0
]
,
address
[
1
]
)
)
self
.
factory
.
connmanager
.
addConnection
(
self
)
self
.
factory
.
doConnectionMade
(
self
)


def connectionLost(self,reason):
        '''连接断开处理
        '''
        log.msg('Client %d login out.'%(self.transport.sessionno))
        self.factory.doConnectionLost(self)
        self.factory.connmanager.dropConnectionByID(self.transport.sessionno)

def
connectionLost
(
self
,
reason
)
:
log
.
msg
(
'Client %d login out.'
%
(
self
.
transport
.
sessionno
)
)
self
.
factory
.
doConnectionLost
(
self
)
self
.
factory
.
connmanager
.
dropConnectionByID
(
self
.
transport
.
sessionno
)


线程安全的向客户端发送数据

线程安全的向客户端发送数据

def safeToWriteData(self,data,command):
        '''线程安全的向客户端发送数据
        @param data: str 要向客户端写的数据
        '''
        if data is None:
            return
        senddata = self.factory.produceResult(command,data)
        self.transport.sendall(senddata)

def safeToWriteData ( self , data , command ) : if data is None : return senddata = self . factory . produceResult ( command , data ) self . transport . sendall ( senddata )


数据到达处理
        @param data: str 客户端传送过来的数据

def dataReceived(self, data):

        '''数据到达处理
        @param data: str 客户端传送过来的数据
        '''
        length = self.factory.dataprotocl.getHeadlength()#获取协议头的长度
        self.buff += data
        while self.buff.__len__() >= length:
            unpackdata = self.factory.dataprotocl.unpack(self.buff[:length])
            if not unpackdata.get('result'):
                log.msg('illegal data package --')
                self.factory.connmanager.loseConnection(self.transport.sessionno)
                break
            command = unpackdata.get('command')
            rlength = unpackdata.get('length')
            request = self.buff[length:length+rlength]
            if request.__len__()< rlength:
                log.msg('some data lose')
                break
            self.buff = self.buff[length+rlength:]
            response = self.factory.doDataReceived(self,command,request)
            if not response:
                continue
            self.safeToWriteData(response, command)

def
dataReceived
(
self
,
data
)
:
length
=
self
.
factory
.
dataprotocl
.
getHeadlength
(
)
#获取协议头的长度
self
.
buff
+=
data
while
self
.
buff
.
__len__
(
)
>=
length
:
unpackdata
=
self
.
factory
.
dataprotocl
.
unpack
(
self
.
buff
[
:
length
]
)
if
not
unpackdata
.
get
(
'result'
)
:
log
.
msg
(
'illegal data package --'
)
self
.
factory
.
connmanager
.
loseConnection
(
self
.
transport
.
sessionno
)
break
command
=
unpackdata
.
get
(
'command'
)
rlength
=
unpackdata
.
get
(
'length'
)
request
=
self
.
buff
[
length
:
length
+
rlength
]
if
request
.
__len__
(
)
<
rlength
:
log
.
msg
(
'some data lose'
)
break
self
.
buff
=
self
.
buff
[
length
+
rlength
:
]
response
=
self
.
factory
.
doDataReceived
(
self
,
command
,
request
)
if
not
response
:
continue
self
.
safeToWriteData
(
response
,
command
)


def __init__(self,dataprotocl=DataPackProtoc()):
        '''初始化
        '''
        protocols.ServerFactory.__init__(self)
        self.service = None
        self.connmanager = ConnectionManager()
        self.dataprotocl = dataprotocl

def __init__ ( self , dataprotocl = DataPackProtoc ( ) ) : protocols . ServerFactory . __init__ ( self ) self . service = None self . connmanager = ConnectionManager ( ) self . dataprotocl = dataprotocl


def doConnectionMade(self,conn):
        '''当连接建立时的处理'''
        pass

def doConnectionMade ( self , conn ) : pass


def doConnectionLost(self,conn):
        '''连接断开时的处理'''
        pass

def doConnectionLost ( self , conn ) : pass


def addServiceChannel(self,service):
        '''添加服务通道'''
        self.service = service

def addServiceChannel ( self , service ) : self . service = service


def doDataReceived(self,conn,commandID,data):
        '''数据到达时的处理'''
        response = self.service.callTarget(commandID,conn,data)
        return response

def doDataReceived ( self , conn , commandID , data ) : response = self . service . callTarget ( commandID , conn , data ) return response


产生客户端需要的最终结果

产生客户端需要的最终结果

def produceResult(self,command,response):
        '''产生客户端需要的最终结果
        @param response: str 分布式客户端获取的结果
        '''
        return self.dataprotocl.pack(command,response)

def produceResult ( self , command , response ) : return self . dataprotocl . pack ( command , response )


def loseConnection(self,connID):
        """主动端口与客户端的连接
        """
        self.connmanager.loseConnection(connID)

def loseConnection ( self , connID ) : self . connmanager . loseConnection ( connID )


def pushObject(self,topicID , msg, sendList):
        '''服务端向客户端推消息
        @param topicID: int 消息的主题id号
        @param msg: 消息的类容，protobuf结构类型
        @param sendList: 推向的目标列表(客户端id 列表)
        '''
        self.connmanager.pushObject(topicID, msg, sendList)

def pushObject ( self , topicID , msg , sendList ) : self . connmanager . pushObject ( topicID , msg , sendList )