class RemoteObject(object): '''远程调用对象 一个实例 ''' def __init__(self, name, index=1, ref=None): '''初始化远程调用对象 @param port: int 远程分布服的端口号 @param rootaddr: 根节点服务器地址 ''' self.setName(name) self.index = index self._factory = pb.PBClientFactory() if ref is None: self._reference = ProxyReference() else: self._reference = ref self._addr = None def setName(self, name): '''设置节点的名称''' self._name = 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 reconnect(self): '''重新连接''' self.connect(self._addr) def addServiceChannel(self, service): '''设置引用对象''' self._reference.addService(service) def takeProxy(self): '''像远程服务端发送代理通道对象 ''' deferedRemote = self._factory.getRootObject() deferedRemote.addCallback(callRemote, 'takeProxy', self._name, self._reference) def callRemote(self, commandId, *args, **kw): '''远程调用''' deferedRemote = self._factory.getRootObject() return deferedRemote.addCallback(callRemote, 'callTarget', commandId, *args, **kw)
class RemoteObject(object): '''远程调用对象''' instance = None @classmethod def getInstance(cls, name=None): if cls.instance is None: cls.instance = RemoteObject(name) return cls.instance def __init__(self, name=None): self._name = name self._factory = pb.PBClientFactory() self._reference = ProxyReference() self._addr = None def setName(self, name): self._name = 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 reconnect(self): self.connect(self._addr) def addServiceChannel(self, service): self._reference.addService(service) def getServiceChannel(self): return self._reference.getService() def disconnectCallback(self, callback): self._factory.stopFactory = callback def takeProxy(self): deferedRemote = self._factory.getRootObject() deferedRemote.addCallback(callRemote, 'takeProxy', self._name, self._reference) def callRemote(self, commandId, *args, **kw): '''回调root的接口''' deferedRemote = self._factory.getRootObject() return deferedRemote.addCallback(callRemote, 'callTarget', commandId, *args, **kw)
class RemoteObject(object): '''远程调用对象''' def __init__(self,name): '''初始化远程调用对象 @param port: int 远程分布服的端口号 @param rootaddr: 根节点服务器地址 ''' self._name = name self._factory = pb.PBClientFactory() self._reference = ProxyReference() self._addr = None def setName(self,name): '''设置节点的名称''' self._name = 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 reconnect(self): '''重新连接''' self.connect(self._addr) def addServiceChannel(self,service): '''设置引用对象''' self._reference.addService(service) def takeProxy(self): '''像远程服务端发送代理通道对象 ''' deferedRemote = self._factory.getRootObject() deferedRemote.addCallback(callRemote,'takeProxy',self._name,self._reference) def callRemote(self,commandId,*args,**kw): '''远程调用''' deferedRemote = self._factory.getRootObject() return deferedRemote.addCallback(callRemote,'callTarget',commandId,*args,**kw)
class RemoteObject(object): """远程调用对象""" def __init__(self, name): """初始化远程调用对象 @param port: int 远程分布服的端口号 @param rootaddr: 根节点服务器地址 """ self._name = name self._factory = pb.PBClientFactory() self._reference = ProxyReference() def setName(self, name): """设置节点的名称""" self._name = name def getName(self): """获取节点的名称""" return self._name def connect(self, addr): """初始化远程调用对象""" reactor.connectTCP(addr[0], addr[1], self._factory) self.takeProxy() def addServiceChannel(self, service): """设置引用对象""" self._reference.addService(service) def takeProxy(self): """像远程服务端发送代理通道对象 """ deferedRemote = self._factory.getRootObject() deferedRemote.addCallback(callRemote, "takeProxy", self._name, self._reference) def callRemote(self, commandId, *args, **kw): """远程调用""" deferedRemote = self._factory.getRootObject() return deferedRemote.addCallback(callRemote, "callTarget", commandId, *args, **kw)
class RemoteObject(object): '''远程调用对象''' def __init__(self, name, timeout=600): '''初始化远程调用对象 @param port: int 远程分布服的端口号 @param rootaddr: 根节点服务器地址 ''' self._name = name self._factory = BilateralClientFactory() self._reference = ProxyReference() self._addr = None self._timeout = timeout def setName(self, name): '''设置节点的名称''' self._name = 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 reconnect(self, addr=()): '''重新连接''' if addr: self.connect(addr) else: self.connect(self._addr) 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 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 callRemoteNotForResult(self, commandId, *args, **kw): '''远程调用,不需要结果放回 ''' deferedRemote = self._factory.getRootObject(timeout=self._timeout) return deferedRemote.callRemoteNotForResult('callTarget', commandId, *args, **kw)
class RemoteObject(object): """远程调用对象""" def __init__(self, name, timeout=600): """初始化远程调用对象 @param port: int 远程分布服的端口号 @param rootaddr: 根节点服务器地址 """ self._name = name self._factory = BilateralClientFactory() self._reference = ProxyReference() self._addr = None self._timeout = timeout def __getattr__(self, fun): class RpcFunctionWrapper: def __init__(self, fun, handle): self._fun = fun self._handle = handle def __call__(self, *arg, **kw): if not self._handle: return False return self._handle('callTarget', self._fun, *arg, **kw) deferedRemote = self._factory.getRootObject(timeout=self._timeout) if not deferedRemote: return RpcFunctionWrapper(fun, None) if fun.endswith('remote'): return RpcFunctionWrapper(fun, deferedRemote.callRemoteForResult) if fun.endswith('remote_noresult'): return RpcFunctionWrapper(fun, deferedRemote.callRemoteNotForResult) raise Exception('error rpc name, must endwith <remote>:%s' % fun) return None def __getitem__(self, node): class NodeWrapper: def __init__(self, node, handle): self._handle = handle self._node = node def __getattr__(self, fun): class RpcFunctionWrapper: def __init__(self, node, fun, handle): self._fun = fun self._node = node self._handle = handle def __call__(self, *arg, **kw): if not self._handle: return False return self._handle('transit', self._node, self._fun, *arg, **kw) if fun.endswith('remote'): return RpcFunctionWrapper(self._node, fun, self._handle.callRemoteForResult) if fun.endswith('remote_noresult'): return RpcFunctionWrapper( self._node, fun, self._handle.callRemoteNotForResult) deferedRemote = self._factory.getRootObject(timeout=self._timeout) return NodeWrapper(node, deferedRemote) def setName(self, name): """设置节点的名称""" self._name = 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 reconnect(self, addr=()): """重新连接""" if addr: self.connect(addr) else: self.connect(self._addr) 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)
class RemoteObject(object): '''远程调用对象''' def __init__(self, name, rname): '''初始化远程调用对象 @param port: int 远程分布服的端口号 @param rootaddr: 根节点服务器地址 ''' self._name = name self._rname = rname self._factory = pb.PBClientFactory() self._reference = ProxyReference() self._addr = None self._factory.clientConnectionLost = self.clientConnectionLost self._factory.clientConnectionFailed = self.clientConnectionFailed self.srclientConnectionMade = self._factory.clientConnectionMade self._factory.clientConnectionMade = self.clientConnectionMade def clientConnectionLost(self, connector, reason, reconnecting=1): log.err("clientConnectionLost for %s: %s, %s, %s" % (self._rname, str(reason), self._addr, reconnecting)) self.reconnect() def clientConnectionFailed(self, connector, reason): log.err("clientConnectionFailed for %s :%s, %s" % (self._rname, str(reason), self._addr)) self.reconnect() def clientConnectionMade(self, broker): log.msg("clientConnectionMade for ", self._rname) self.srclientConnectionMade(broker) self.takeProxy() def setName(self, name): '''设置节点的名称''' self._name = name def getName(self): '''获取节点的名称''' return self._name def connect(self, addr): '''初始化远程调用对象''' self._addr = addr reactor.connectTCP(addr[0], addr[1], self._factory) def reconnect(self): '''重新连接''' self.connect(self._addr) def addServiceChannel(self, service): '''设置引用对象''' self._reference.addService(service) def takeProxy(self): '''像远程服务端发送代理通道对象 ''' log.msg("takeProxy ....") deferedRemote = self._factory.getRootObject() deferedRemote.addCallback(callRemote, 'takeProxy', self._name, self._reference) def callRemote(self, commandId, *args, **kw): '''远程调用''' deferedRemote = self._factory.getRootObject() return deferedRemote.addCallback(callRemote, 'callTarget', commandId, *args, **kw)
class RemoteObject(object): '''远程调用对象''' 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 setName(self,name): '''设置节点的名称''' self._name = 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 reconnect(self,addr=()): '''重新连接''' if addr: self.connect(addr) else: self.connect(self._addr) 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 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 callRemoteNotForResult(self,commandId,*args,**kw): '''远程调用,不需要结果放回 ''' deferedRemote = self._factory.getRootObject(timeout=self._timeout) return deferedRemote.callRemoteNotForResult('callTarget',commandId,*args,**kw)
class RemoteObject(object): """远程调用对象""" def __init__(self, name, timeout=600): """初始化远程调用对象 @param port: int 远程分布服的端口号 @param rootaddr: 根节点服务器地址 """ self._name = name self._factory = BilateralClientFactory() self._reference = ProxyReference() self._addr = None self._timeout = timeout def __getattr__(self, fun): class RpcFunctionWrapper: def __init__(self, fun, handle): self._fun = fun self._handle = handle def __call__(self, *arg): if not self._handle: return False return self._handle('callTarget', self._fun, *arg) deferedRemote = self._factory.getRootObject(timeout=self._timeout) if not deferedRemote: return RpcFunctionWrapper(fun, None) if fun.endswith('remote'): return RpcFunctionWrapper(fun, deferedRemote.callRemoteForResult) if fun.endswith('remote_noresult'): return RpcFunctionWrapper(fun, deferedRemote.callRemoteNotForResult) raise Exception('error rpc name, must endwith <remote>:%s' % fun) return None def __getitem__(self, node): class NodeWrapper: def __init__(self, node, handle): self._handle = handle self._node = node def __getattr__(self, fun): class RpcFunctionWrapper: def __init__(self, node, fun, handle): self._fun = fun self._node = node self._handle = handle def __call__(self, *arg): if not self._handle: return False return self._handle('transit', self._node, self._fun, *arg) if fun.endswith('remote'): return RpcFunctionWrapper(self._node, fun, self._handle.callRemoteForResult) if fun.endswith('remote_noresult'): return RpcFunctionWrapper(self._node, fun, self._handle.callRemoteNotForResult) deferedRemote = self._factory.getRootObject(timeout=self._timeout) return NodeWrapper(node, deferedRemote) def setName(self, name): """设置节点的名称""" self._name = 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 reconnect(self, addr=()): """重新连接""" if addr: self.connect(addr) else: self.connect(self._addr) 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 is_connected(self): deferedRemote = self._factory.getRootObject(timeout=self._timeout) if deferedRemote.broker: return True else: return False