def Finish(self, status, resp_body=None): resp = self.GetResp() resp.command_status = status if resp_body: resp.body = resp_body if self.protocol.role == BaseCommand.PV_ROLE_HUMAN: with self.protocol.lock_dictWaitResp: me = self.protocol.dictWaitResp.pop(self.command_seq, None) if me is None: return #this request has been feedbacked or timeout resp.Send() elif self.protocol.role == BaseCommand.PV_ROLE_INTERNAL: sb_protocol = None with self.protocol.factory.lockDict: sb_protocol = self.protocol.factory.dictRelayer.get( self.internalMessage.destId) if sb_protocol: with sb_protocol.lock_dictWaitResp: me = sb_protocol.dictWaitResp.pop(self.command_seq, None) if me is None: return #this request has been feedbacked or timeout interMessage = InternalMessage.CInternalMessage() interMessage.SetParam( self.internalMessage.fromType, self.internalMessage.fromId, self.internalMessage.fromSock, InternalMessage.OPER_RESPONSE, "", self.internalMessage.destType, self.internalMessage.destId, self.internalMessage.destSock) resp.Send(interMessage)
def Run(self): if self.protocol.role == BaseCommand.PV_ROLE_INTERNAL: account_protocol = self.protocol.factory.GetAccountProtocol( relayer_id=self.internalMessage.fromId, client_id=self.internalMessage.destId) if account_protocol is None: return with account_protocol.lock_dictWaitResp: request = account_protocol.dictWaitResp.pop( self.command_seq, None) if request is None: return #this request has been feedbacked or timeout self.Send() else: with self.protocol.lock_dictWaitResp: request = self.protocol.dictWaitResp.pop( self.command_seq, None) if request is None: return #this request has been feedbacked or timeout interMessage = InternalMessage.CInternalMessage() interMessage.SetParam(request.internalMessage.fromType, request.internalMessage.fromId, request.internalMessage.fromSock, InternalMessage.OPER_RESPONSE, "", request.internalMessage.destType, request.internalMessage.destId, request.internalMessage.destSock) self.Send(interMessage)
def Feedback(self): if self.bFinished: return respond = self.GetResp() # if self.protocol is not None and self.protocol.role ==BaseCommand.PV_ROLE_HUMAN: if self.protocol is not None: result = -1 # if one control command fail, the total command fail for relayer_id in self.dictFinishedRelayerControls.keys(): for dc in self.dictFinishedRelayerControls[relayer_id]: if result <= 0: result = dc.result respond.body = dc.body if result == -1: respond.SetErrorCode(BaseCommand.CS_RELAYEROFFLINE) else: respond.SetErrorCode(result) interMessage = None if self.protocol.role == BaseCommand.PV_ROLE_INTERNAL: interMessage = InternalMessage.CInternalMessage() interMessage.SetParam(self.internalMessage.fromType, self.internalMessage.fromId, self.internalMessage.fromSock, InternalMessage.OPER_RESPONSE, "", self.internalMessage.destType, self.internalMessage.destId, self.internalMessage.destSock) respond.Send(interMessage) self.bFinished = True return respond
def Run(self): if self.protocol.role == BaseCommand.PV_ROLE_INTERNAL: sb_protocol = None with self.protocol.factory.lockDict: sb_protocol = self.protocol.factory.dictRelayer.get( self.internalMessage.destId) if sb_protocol is None: self.Finish(False) return with sb_protocol.lock_dictWaitResp: sb_protocol.dictWaitResp[self.command_seq] = self self.Execute() return with self.protocol.lockCmd: if self.protocol.role == BaseCommand.PV_ROLE_HUMAN: if not self.Authorized(): self.SendUnauthorizedResp() return CBaseCommand.Run(self) #check whether contain parameter relayer_id relayer_id = self.body.get(BaseCommand.PN_RELAYERID, None) if relayer_id is None: respond = self.GetResp() respond.SetErrorCode(BaseCommand.CS_PARAMLACK) respond.Send() return with self.protocol.lock_dictWaitResp: self.protocol.dictWaitResp[self.command_seq] = self interMessage = InternalMessage.CInternalMessage() interMessage.SetParam(InternalMessage.TTYPE_GATEWAY, relayer_id, 0, InternalMessage.OPER_REQUEST, "", InternalMessage.TTYPE_HUMAN, self.protocol.client_id, id(self.protocol.transport)) self.Send(interMessage) #threading.Timer(Config.timeout_relayer_control,self.timeout).start() reactor.callLater(Config.timeout_relayer_control, self.timeout)
def Send(self): maxcmd_flow_control = 1000 if self.protocol.role == BaseCommand.PV_ROLE_RELAYER: maxcmd_flow_control = Config.maxcmd_relayer_control while True: with self.protocol.cond_dictControlling: if len(self.protocol.dictControlling.keys() ) > maxcmd_flow_control: logging.debug( "call self.protocol.cond_dictControlling.wait() due to reach maxcmd in protocol %d", id(self.protocol.transport)) self.protocol.cond_dictControlling.wait() elif self.command_seq in self.protocol.dictControlling: logging.debug( "call self.protocol.cond_dictControlling.wait() due to same command_seq in protocol %d", id(self.protocol.transport)) self.protocol.cond_dictControlling.wait() else: interMessage = None self.protocol.dictControlling[self.command_seq] = self self.timer = reactor.callLater( Config.timeout_relayer_control, self.timeout) # if isinstance(self.requireCommand,BaseCommand.CBaseCommand): if self.requireCommand is not None and self.requireCommand.internalMessage is None: interMessage = InternalMessage.CInternalMessage() interMessage.SetParam( InternalMessage.TTYPE_GATEWAY, self.relayer_id, 0, InternalMessage.OPER_REQUEST, "", InternalMessage.TTYPE_HUMAN, self.requireCommand.protocol.client_id, id(self.requireCommand.protocol.transport)) CBaseCommand.Send(self, interMessage) break