def callback_findObjectById4All(self, ret, activeEp, inactiveEp): eplist = [ EndPointInfo(x.host, x.port, x.timeout, x.weight, x.weightType) for x in activeEp if ret == 0 and x.istcp ] ieplist = [ EndPointInfo(x.host, x.port, x.timeout, x.weight, x.weightType) for x in inactiveEp if ret == 0 and x.istcp ] self.__adpManager.setEndpoints(eplist, ieplist)
def refreshEndpoints(self): ''' @brief: 刷新服务器列表 @return: 新的服务列表 @rtype: EndPointInfo列表 ''' tarsLogger.debug('AdapterProxyManager:refreshEndpoints') if self.__isDirectProxy: return interval = self.__comm.getProperty('refresh-endpoint-interval', float) / 1000 locator = self.__comm.getProperty('locator') if '@' not in locator: raise TarsRegistryException('locator is not valid: ' + locator) now = time.time() last = self.__lastFreshTime epSize = len(self.__adps) if last + interval < now or (epSize <= 0 and last + 2 < now): queryFPrx = self.__comm.stringToProxy(QueryFProxy, locator) if epSize == 0 or last == 0: ret, activeEps, inactiveEps = (queryFPrx.findObjectById4All( self.__object.name())) # 目前只支持TCP eplist = [ EndPointInfo(x.host, x.port) for x in activeEps if ret == 0 and x.istcp ] self.setEndpoints(eplist) else: queryFPrx.async_findObjectById4All( self.__queryRegisterCallback, self.__object.name()) self.__lastFreshTime = now
def parseConnAddr(self, connAddr): ''' @brief: 解析connAddr字符串 @param connAddr: 连接地址 @type connAddr: str @return: 解析结果 @rtype: dict, key是str,val里name是str, timeout是float,endpoint是EndPointInfo的list ''' tarsLogger.debug('Communicator:parseConnAddr') connAddr = connAddr.strip() connInfo = {'name': '', 'timeout': -1, 'endpoint': []} if '@' not in connAddr: connInfo['name'] = connAddr return connInfo try: tks = connAddr.split('@') connInfo['name'] = tks[0] tks = tks[1].lower().split(':') parser = argparse.ArgumentParser(add_help=False) parser.add_argument('-h') parser.add_argument('-p') parser.add_argument('-t') for tk in tks: argv = tk.split() if argv[0] != 'tcp': raise TarsException( 'unsupport transmission protocal : %s' % connInfo['name']) mes = parser.parse_args(argv[1:]) try: ip = mes.h if mes.h is not None else '' port = int(mes.p) if mes.p is not None else '-1' timeout = int(mes.t) if mes.t is not None else '-1' connInfo['endpoint'].append(EndPointInfo( ip, port, timeout)) except Exception: raise TarsException('Unrecognized option : %s' % mes) except TarsException: raise except Exception, exp: raise TarsException(exp)
def parseConnAddr(self, connAddr): ''' @brief: 解析connAddr字符串 @param connAddr: 连接地址 @type connAddr: str @return: 解析结果 @rtype: dict, key是str,val里name是str, timeout是float,endpoint是EndPointInfo的list ''' tarsLogger.debug('Communicator:parseConnAddr') connAddr = connAddr.strip() connInfo = {'name': '', 'timeout': -1, 'endpoint': []} if '@' not in connAddr: connInfo['name'] = connAddr return connInfo try: tks = connAddr.split('@') connInfo['name'] = tks[0] tks = tks[1].lower().split(':') for tk in tks: argv = tk.split() if argv[0] != 'tcp': raise TarsException( 'unsupport transmission protocal : %s' % args[0]) opts, args = getopt.getopt(argv[1:], "h:p:t:") ip, port = '', -1 for opt, arg in opts: if opt == '-h': ip = arg elif opt == '-p': port = int(arg) elif opt == '-t': connInfo['timeout'] = float(arg) / 1000.0 else: raise TarsException('unkown option : %s' % opt) connInfo['endpoint'].append(EndPointInfo(ip, port)) except TarsException: raise except Exception, exp: raise TarsException(exp)
def callback_findObjectById4All(self, ret, activeEp, inactiveEp): eplist = [ EndPointInfo(x.host, x.port) for x in activeEps if ret == 0 and x.istcp ] self.__adpManager.setEndpoints(eplist)