예제 #1
0
파일: protocol.py 프로젝트: zhaozw/hall37
    def onMsg(self):
        pack = stackless.getcurrent()._fttask.pack
        ftlog.debug('A2AProtocol->', FTTasklet.concurrent_task_count, '[' + pack + ']')
        src, dst, queryid, userheader1, userheader2, message = agentmsg.unpack(pack)
        if src == None or dst == None:
            ftlog.info("ERROR, recive a error format message")
            return

        if self.peer_id == 0:
            self.peer_id = src
            ftcon.serverid_protocol_map[self.peer_id] = self
            ftlog.info("Receive other agent register, agentid=", self.peer_id)
            return

        # 处理agent服务自身的命令
        if dst == ftcon.global_config["server_id"] and A2AProtocol.onCommand != None:
            #             ftlog.debug('A2AProtocol-> it self command !!')
            A2AProtocol.onCommand(self, src, queryid, userheader1, userheader2, message)
            return

        try:
            # 从其他agent过来的数据,转给连接的dst service
            #             ftlog.debug('A2AProtocol->send to target->', dst)
            protocol = ftcon.serverid_protocol_map[dst]
            if performance.PERFORMANCE_NET:
                pack = performance.linkMsgTime('LW', pack)
            protocol.transport.write(pack + '\r\n')
        except:
            ftlog.error('msg route error, dst_server_id=', dst)
예제 #2
0
    def onMsg(self):
        pack = stackless.getcurrent()._fttask.pack
        ftlog.debug('A2AProtocol->', FTTasklet.concurrent_task_count,
                    '[' + pack + ']')
        src, dst, queryid, userheader1, userheader2, message = agentmsg.unpack(
            pack)
        if src == None or dst == None:
            ftlog.info("ERROR, recive a error format message")
            return

        if self.peer_id == 0:
            self.peer_id = src
            ftcon.serverid_protocol_map[self.peer_id] = self
            ftlog.info("Receive other agent register, agentid=", self.peer_id)
            return

        # 处理agent服务自身的命令
        if dst == ftcon.global_config[
                "server_id"] and A2AProtocol.onCommand != None:
            #             ftlog.debug('A2AProtocol-> it self command !!')
            A2AProtocol.onCommand(self, src, queryid, userheader1, userheader2,
                                  message)
            return

        try:
            # 从其他agent过来的数据,转给连接的dst service
            #             ftlog.debug('A2AProtocol->send to target->', dst)
            protocol = ftcon.serverid_protocol_map[dst]
            if performance.PERFORMANCE_NET:
                pack = performance.linkMsgTime('LW', pack)
            protocol.transport.write(pack + '\r\n')
        except:
            ftlog.error('msg route error, dst_server_id=', dst)
예제 #3
0
 def query(self,
           src,
           dst,
           userheader1,
           userheader2,
           data,
           timeout,
           notimeoutex=0):
     try:
         if self.transport:
             global _QUERY_ID
             resultDeferred = defer.Deferred()
             _QUERY_ID += 1
             query_id = src + '.' + str(_QUERY_ID)
             msg = agentmsg.packstr(src, dst, query_id, userheader1,
                                    userheader2, data)
             cancelCall = reactor.callLater(timeout, self._clearFailed,
                                            resultDeferred, query_id, msg,
                                            notimeoutex)
             _LIVE_MESSAGES[query_id] = (resultDeferred, cancelCall, time())
             if performance.PERFORMANCE_NET:
                 msg = performance.linkMsgTime('LW', msg)
             ftlog.debug('transport.write', msg)
             self.transport.write(msg)
             return resultDeferred
         else:
             ftlog.error('tcpquery : not connected !! ', self)
     except FTMsgPackException, e:
         raise e
예제 #4
0
def query(dst, data, userheader1='', userheader2='', timeout=2, notimeoutex=0, returnDeffer=0):
    src = ftcon.global_config["server_id"]
    server_conf = ftcon.getServerConf(src)
    agent_id = server_conf['agent']
    protocol = ftcon.serverid_protocol_map[agent_id]
    if ide_debug():
        ide_print_pack("Query[%s]" % dst.split("-")[0], json.loads(data))
    d = protocol.query(src, dst, userheader1, userheader2, data, timeout, notimeoutex=notimeoutex)
    if returnDeffer == 1:
        return d
    res = stackless.getcurrent()._fttask.waitDefer(d)
    global _QUERY_SLOW_TIME
    if isinstance(res, tuple):
        msg = res[0]
        query_write_time = res[1]
        query_recive_time = res[2]
        ct = time()
        if ct - query_write_time > _QUERY_SLOW_TIME:
            if performance.PERFORMANCE_NET:
                msg = performance.linkMsgTime('DO', msg)
            ftlog.warn('QUERY REPLAY SLOW ! reply=%0.6f' % (query_recive_time - query_write_time),
                       'schedule=%0.6f' % (ct - query_recive_time), 'total=%0.6f' % (ct - query_write_time),
                       'dst=', dst, 'request=', data, 'response=', msg)
        return msg
    return res
예제 #5
0
파일: protocol.py 프로젝트: zhaozw/hall37
    def lineReceived(self, data):
        if performance.PERFORMANCE_NET:
            data = performance.linkMsgTime('LR', data)
        ftlog.debug('S2AProtocol->lineReceived', FTTasklet.concurrent_task_count, time(), data)
        src, dst, query_id, userheader1, userheader2, msg = agentmsg.unpack(data)
        if ide_debug():
            ide_print_pack("S2AP Recv [%5s]" % src.split("-")[0], json.loads(msg))
        if src is None or dst is None:
            ftlog.info("ERROR, recive a error format message")
            return

        if self.peer_id == 0:
            self.peer_id = src
            ftcon.serverid_protocol_map[self.peer_id] = self
            ftlog.info('receive register, agentid=', self.peer_id)
            return

        _countProtocolPack(1, self)

        # send过来的数据
        if query_id == '':
            self._runTasklet(data=msg, src=src, dst=dst, userheader1=userheader1, userheader2=userheader2,
                             time_recv=time())
        else:
            querysrc, _ = query_id.split('.')
            server_id = ftcon.global_config["server_id"]
            # query本服务的请求
            if querysrc != server_id:
                self._runTasklet(data=msg, src=src, dst=dst, query_id=query_id, userheader1=userheader1,
                                 userheader2=userheader2, time_recv=time())
            # response回来的请求
            else:
                if userheader1 == 'RQ':  # 本进程内, 异步查询本进程的其他消息接口
                    self._runTasklet(data=msg, src=src, dst=dst, query_id=query_id, userheader1=userheader1,
                                     userheader2=userheader2, time_recv=time())
                else:
                    d, c, t = None, None, 0
                    #                     ftlog.debug('lineReceived', query_id, id(_LIVE_MESSAGES), id(self))
                    if query_id in _LIVE_MESSAGES:
                        d, c, t = _LIVE_MESSAGES[query_id]
                        del _LIVE_MESSAGES[query_id]
                    else:
                        if query_id in _FAILED_MESSAGES:
                            del _FAILED_MESSAGES[query_id]
                            ftlog.warn('QUERY TOO SLOW !!', query_id, msg)
                            if len(_FAILED_MESSAGES) > 100:
                                _FAILED_MESSAGES.clear()
                        else:
                            ftlog.warn('NOT KNOW of query_id->', query_id, msg)
                    if d and c:
                        try:
                            c.cancel()
                            d.callback((msg, t, time()))
                        except:
                            ftlog.error(msg)
예제 #6
0
    def onMsg(self):
        pack = stackless.getcurrent()._fttask.pack
        ftlog.debug('A2SProtocol->', FTTasklet.concurrent_task_count, '[',
                    pack, ']')
        src, dst, queryid, userheader1, userheader2, message = agentmsg.unpack(
            pack)
        #zxdebug:测试query时,AG直接返回的性能
        #self.transport.write("GA01|CO01|"+queryid+"|||"+message+"\r\n")
        #return
        #zxdebug
        if src == None or dst == None:
            ftlog.info("ERROR, recive a error format message")
            return
        if self.peer_id == 0:
            self.peer_id = src
            ftcon.serverid_protocol_map[self.peer_id] = self
            ftlog.info("Receive service register, serverid=", self.peer_id,
                       self)
            return

        # 处理agent服务自身的命令
        if dst == ftcon.global_config[
                "server_id"] and A2SProtocol.onCommand != None:
            #             ftlog.debug('A2SProtocol-> it self command !!')
            A2SProtocol.onCommand(self, src, queryid, userheader1, userheader2,
                                  message)
            return

        if dst.find('AG') == 0:  # 如果是直接发送给另外的一个AGENT, 那器agent就是其本身
            agent_id = dst
        else:
            server_conf = ftcon.getServerConf(dst)
            agent_id = server_conf['agent']
        try:
            # 从src发过来的数据,转给dst所在的agent
            # 如果是agent所属内部进程过来的数据, 直接转发至dst进程
            if agent_id == ftcon.global_config["server_id"]:
                agent_id = dst


#             ftlog.debug('A2SProtocol->send to agentid->', agent_id, 'dst=', dst)
            protocol = ftcon.serverid_protocol_map[agent_id]
            if performance.PERFORMANCE_NET:
                pack = performance.linkMsgTime('LW', pack)
            protocol.transport.write(pack + '\r\n')
        except:
            ftlog.error(
                'msg route error, not found agent protocol, dst_server_id=',
                dst, 'agend=', agent_id)
예제 #7
0
파일: protocol.py 프로젝트: zhaozw/hall37
 def query(self, src, dst, userheader1, userheader2, data, timeout, notimeoutex=0):
     try:
         if self.transport:
             global _QUERY_ID
             resultDeferred = defer.Deferred()
             _QUERY_ID += 1
             query_id = src + '.' + str(_QUERY_ID)
             msg = agentmsg.packstr(src, dst, query_id, userheader1, userheader2, data)
             cancelCall = reactor.callLater(timeout, self._clearFailed, resultDeferred, query_id, msg, notimeoutex)
             _LIVE_MESSAGES[query_id] = (resultDeferred, cancelCall, time())
             if performance.PERFORMANCE_NET:
                 msg = performance.linkMsgTime('LW', msg)
             ftlog.debug('transport.write', msg)
             self.transport.write(msg)
             return resultDeferred
         else:
             ftlog.error('tcpquery : not connected !! ', self)
     except FTMsgPackException, e:
         raise e
예제 #8
0
def _response(dst, data, queryid, userheader1, userheader2):
    src = ftcon.global_config["server_id"]
    server_conf = ftcon.getServerConf(src)
    if 'agent' in server_conf :
        agent_id = server_conf['agent']
    else:
        agent_id = dst
        if dst in ftcon.serverid_protocol_map :
            agent_id = dst
        else:
            server_conf = ftcon.getServerConf(dst)
            if 'agent' in server_conf :
                agent_id = server_conf['agent']
            
    message = agentmsg.packstr(src, dst, queryid, userheader1, userheader2, data)
    protocol = ftcon.serverid_protocol_map[agent_id]
    if performance.PERFORMANCE_NET :
        message = performance.linkMsgTime('LW', message)
    ftlog.debug('transport.write', message)
    protocol.transport.write(message)
예제 #9
0
파일: protocol.py 프로젝트: zhaozw/hall37
    def onMsg(self):
        pack = stackless.getcurrent()._fttask.pack
        ftlog.debug('A2SProtocol->', FTTasklet.concurrent_task_count, '[', pack, ']')
        src, dst, queryid, userheader1, userheader2, message = agentmsg.unpack(pack)
        # zxdebug:测试query时,AG直接返回的性能
        # self.transport.write("GA01|CO01|"+queryid+"|||"+message+"\r\n")
        # return
        # zxdebug
        if src == None or dst == None:
            ftlog.info("ERROR, recive a error format message")
            return
        if self.peer_id == 0:
            self.peer_id = src
            ftcon.serverid_protocol_map[self.peer_id] = self
            ftlog.info("Receive service register, serverid=", self.peer_id, self)
            return

        # 处理agent服务自身的命令
        if dst == ftcon.global_config["server_id"] and A2SProtocol.onCommand != None:
            #             ftlog.debug('A2SProtocol-> it self command !!')
            A2SProtocol.onCommand(self, src, queryid, userheader1, userheader2, message)
            return

        if dst.find('AG') == 0:  # 如果是直接发送给另外的一个AGENT, 那器agent就是其本身
            agent_id = dst
        else:
            server_conf = ftcon.getServerConf(dst)
            agent_id = server_conf['agent']
        try:
            # 从src发过来的数据,转给dst所在的agent
            # 如果是agent所属内部进程过来的数据, 直接转发至dst进程
            if agent_id == ftcon.global_config["server_id"]:
                agent_id = dst
            # ftlog.debug('A2SProtocol->send to agentid->', agent_id, 'dst=', dst)
            protocol = ftcon.serverid_protocol_map[agent_id]
            if performance.PERFORMANCE_NET:
                pack = performance.linkMsgTime('LW', pack)
            protocol.transport.write(pack + '\r\n')
        except:
            ftlog.error('msg route error, not found agent protocol, dst_server_id=', dst, 'agend=', agent_id)
예제 #10
0
파일: wrapper.py 프로젝트: zhaozw/hall37
def _response(dst, data, queryid, userheader1, userheader2):
    src = ftcon.global_config["server_id"]
    server_conf = ftcon.getServerConf(src)
    if 'agent' in server_conf:
        agent_id = server_conf['agent']
    else:
        agent_id = dst
        if dst in ftcon.serverid_protocol_map:
            agent_id = dst
        else:
            server_conf = ftcon.getServerConf(dst)
            if 'agent' in server_conf:
                agent_id = server_conf['agent']

    message = agentmsg.packstr(src, dst, queryid, userheader1, userheader2, data)
    protocol = ftcon.serverid_protocol_map[agent_id]
    if performance.PERFORMANCE_NET:
        message = performance.linkMsgTime('LW', message)
    ftlog.debug('transport.write', message)
    if ide_debug():
        ide_print_pack("Resp [%5s]" % dst.split("-")[0], data)
    protocol.transport.write(message)
예제 #11
0
 def lineReceived(self, data):
     if performance.PERFORMANCE_NET:
         data = performance.linkMsgTime('LR', data)
     _countProtocolPack(1, self)
     self._runTasklet(data=data, time_recv=time())
예제 #12
0
    def lineReceived(self, data):
        if performance.PERFORMANCE_NET:
            data = performance.linkMsgTime('LR', data)
        ftlog.debug('S2AProtocol->lineReceived',
                    FTTasklet.concurrent_task_count, time(), data)
        src, dst, query_id, userheader1, userheader2, msg = agentmsg.unpack(
            data)
        if src == None or dst == None:
            ftlog.info("ERROR, recive a error format message")
            return

        if self.peer_id == 0:
            self.peer_id = src
            ftcon.serverid_protocol_map[self.peer_id] = self
            ftlog.info('receive register, agentid=', self.peer_id)
            return

        _countProtocolPack(1, self)

        # send过来的数据
        if query_id == '':
            self._runTasklet(data=msg,
                             src=src,
                             dst=dst,
                             userheader1=userheader1,
                             userheader2=userheader2,
                             time_recv=time())
        else:
            querysrc, _ = query_id.split('.')
            server_id = ftcon.global_config["server_id"]
            # query本服务的请求
            if querysrc != server_id:
                self._runTasklet(data=msg,
                                 src=src,
                                 dst=dst,
                                 query_id=query_id,
                                 userheader1=userheader1,
                                 userheader2=userheader2,
                                 time_recv=time())
            # response回来的请求
            else:
                if userheader1 == 'RQ':  # 本进程内, 异步查询本进程的其他消息接口
                    self._runTasklet(data=msg,
                                     src=src,
                                     dst=dst,
                                     query_id=query_id,
                                     userheader1=userheader1,
                                     userheader2=userheader2,
                                     time_recv=time())
                else:
                    d, c, t = None, None, 0
                    #                     ftlog.debug('lineReceived', query_id, id(_LIVE_MESSAGES), id(self))
                    if query_id in _LIVE_MESSAGES:
                        d, c, t = _LIVE_MESSAGES[query_id]
                        del _LIVE_MESSAGES[query_id]
                    else:
                        if query_id in _FAILED_MESSAGES:
                            del _FAILED_MESSAGES[query_id]
                            ftlog.warn('QUERY TOO SLOW !!', query_id, msg)
                            if len(_FAILED_MESSAGES) > 100:
                                _FAILED_MESSAGES.clear()
                        else:
                            ftlog.warn('NOT KNOW of query_id->', query_id, msg)
                    if d and c:
                        try:
                            c.cancel()
                            d.callback((msg, t, time()))
                        except:
                            ftlog.error(msg)
예제 #13
0
파일: protocol.py 프로젝트: zhaozw/hall37
 def lineReceived(self, data):
     if performance.PERFORMANCE_NET:
         data = performance.linkMsgTime('LR', data)
     _countProtocolPack(1, self)
     self._runTasklet(data=data, time_recv=time())