Exemplo n.º 1
0
    def unsubscribe(contract, reqId=None, sync=False):
        if not reqId:
            reqId = str(uuid.uuid4())
        operatorId = Config.operatorId
        sourceNodeId = Config.nodeId

        commonReq = CommonReqField()
        commonReq.sourceNodeId = sourceNodeId
        commonReq.targetNodeId = 0
        commonReq.operatorId = operatorId
        commonReq.reqId = reqId

        rpcUnsubscribeReq = RpcUnsubscribeReq()

        rpcUnsubscribeReq.commonReq.CopyFrom(commonReq)
        rpcUnsubscribeReq.contract.CopyFrom(contract)

        if sync:
            RpcClientRspHandler.registerWaitReqId(reqId)

        sendResult = RpcClientProcessService.sendCoreRpc(0, rpcUnsubscribeReq.SerializeToString(), reqId,
                                                         RpcId.UNSUBSCRIBE_REQ)

        if sync and not sendResult:
            RpcClientRspHandler.unregisterWaitReqId(reqId)
            return False

        if sync:
            startTime = time.time()
            while True:
                if time.time() - startTime < Config.rpcTimeOut:
                    rpcUnsubscribeRsp = RpcClientRspHandler.getAndRemoveRpcUnsubscribeRsp(reqId)
                    if not rpcUnsubscribeRsp:
                        rpcExceptionRsp = RpcClientRspHandler.getAndRemoveRpcExceptionRsp(reqId);
                        if rpcExceptionRsp:
                            logger.error("取消订阅错误,请求ID: %s, 远程错误回报 %s", reqId, rpcExceptionRsp.info)
                            return False
                        time.sleep(0.02)
                    else:
                        commonRsp = rpcUnsubscribeRsp.commonRsp
                        errorId = commonRsp.errorId
                        if errorId == 0:
                            return True
                        else:
                            logger.error("取消订阅错误,请求ID:%s,错误ID:%s,远程错误回报:%s", reqId, errorId, commonRsp.errorMsg)
                            return False
                else:
                    RpcClientRspHandler.unregisterWaitReqId(reqId)
                    logger.error("取消订阅错误,请求ID: %s,等待回报超时", reqId)
                    return False
    def unsubscribe(contract, gatewayId=None, reqId=None, sync=False):
        if not reqId:
            reqId = str(uuid.uuid4())
        operatorId = Config.operatorId
        sourceNodeId = Config.nodeId

        commonReq = CommonReqField()
        commonReq.sourceNodeId = sourceNodeId
        commonReq.targetNodeId = 0
        commonReq.operatorId = operatorId
        commonReq.reqId = reqId

        rpcUnsubscribeReq = RpcUnsubscribeReq()

        rpcUnsubscribeReq.commonReq.CopyFrom(commonReq)
        rpcUnsubscribeReq.contract.CopyFrom(contract)

        if gatewayId:
            rpcUnsubscribeReq.gatewayId = gatewayId

        if sync:
            RpcClientRspHandler.registerWaitReqId(reqId)

        sendResult = RpcClientProcessService.sendRoutineCoreRpc(
            0, rpcUnsubscribeReq.SerializeToString(), reqId,
            RpcId.UNSUBSCRIBE_REQ)

        if sync and not sendResult:
            RpcClientRspHandler.unregisterWaitReqId(reqId)
            return False

        if sync:
            startTime = time.time()
            while True:
                if time.time() - startTime < Config.rpcTimeOut:
                    rpcUnsubscribeRsp = RpcClientRspHandler.getAndRemoveRpcUnsubscribeRsp(
                        reqId)
                    if not rpcUnsubscribeRsp:
                        rpcExceptionRsp = RpcClientRspHandler.getAndRemoveRpcExceptionRsp(
                            reqId)
                        if rpcExceptionRsp:
                            logger.error("取消订阅错误,请求ID: %s, 远程错误回报 %s", reqId,
                                         rpcExceptionRsp.info)
                            return False
                        time.sleep(0.02)
                    else:
                        commonRsp = rpcUnsubscribeRsp.commonRsp
                        requestStatus = commonRsp.requestStatus
                        if requestStatus == CommonStatusEnum.SUCCESS:
                            return True
                        elif requestStatus == CommonStatusEnum.INFO:
                            logger.info("取消订阅错误,请求ID:%s,远程信息回报:%s", reqId,
                                        commonRsp.info)
                            return True
                        elif requestStatus == CommonStatusEnum.WARN:
                            logger.warning("取消订阅错误,请求ID:%s,远程警告回报:%s", reqId,
                                           commonRsp.info)
                            return True
                        elif requestStatus == CommonStatusEnum.ERROR:
                            logger.error("取消订阅错误,请求ID:%s,远程错误回报:%s", reqId,
                                         commonRsp.info)
                            return False
                        else:
                            logger.error("取消订阅错误,请求ID:%s,未知的请求状态", reqId)
                            return False
                else:
                    RpcClientRspHandler.unregisterWaitReqId(reqId)
                    logger.error("取消订阅错误,请求ID: %s,等待回报超时", reqId)
                    return False