예제 #1
0
    def _rpcexec_b(self, payload):
        """ Execute a call by sending the payload

            :param json payload: Payload data
            :raises ValueError: if the server does not respond in proper JSON format
            :raises RPCError: if the server returns an error
        """
        #log.debug(json.dumps(payload))
        log.debug("RPC-exec blocking %d for %s %s", payload['id'],
                  payload['params'][1], payload['params'][2])

        if not self.needed:
            raise TimedOut()

        self.ws.send(json.dumps(payload, ensure_ascii=False).encode('utf8'))
        reply = self.ws.recv()

        ret = {}
        try:
            ret = json.loads(reply, strict=False)
        except ValueError:
            raise ValueError("Client returned invalid format. Expected JSON!")

        #log.debug(json.dumps(reply))

        self.last_reply += 1

        if 'error' in ret:
            if 'detail' in ret['error']:
                raise exceptions.RPCError(ret['error']['detail'])
            else:
                raise exceptions.RPCError(ret['error']['message'])
        else:
            return ret["result"]
예제 #2
0
    def _rpcexec(self, payload):
        """ Execute a call by sending the payload

            :param json payload: Payload data
            :raises ValueError: if the server does not respond in proper JSON format
            :raises RPCError: if the server returns an error
        """
        if not (self.needed):
            raise exceptions.NumRetriesReached()
        #log.debug(json.dumps(payload))
        call_id = payload['id']
        log.debug("RPC-exec request %d %s %s", call_id, payload['params'][1],
                  payload['params'][2])
        #if payload['params'][1] == "lookup_account_names":
        #    raise Exception("NO")
        try:
            #if self.connected:
            #    self.wssend(payload)
            #else:
            self.requests.put(payload, block=True)
        except KeyboardInterrupt:
            raise
        except:
            raise Exception("Unable to queue request")

        sleepbase = 120 * 3 if self.proxy_type else 1
        sleeptime = sleepbase

        preid = int(self.connection_id)
        seen_reply = int(self.last_reply)
        error = None
        attempts = 0
        while True:
            if self.initialized and not (self.connected) and not (
                    self.connecting):
                raise TimedOut()  #exceptions.NumRetriesReached
            #print("RPC-exec waiting for", call_id, payload['params'][1], payload['params'][2], "[", "%.03f" % sleeptime, "]","/",attempts)
            try:
                error = self.errors.get(block=False)
            except KeyboardInterrupt:
                raise
            except:
                pass

            if "_connection_id" in payload:  # request sent on differen conn?
                if self.connection_id > payload["_connection_id"]:
                    if error:  # reply will never come, then
                        raise error
                    raise TimedOut()

            attempts = self.connection_id - preid
            if ((self.num_retries >= 0 and attempts >= self.num_retries)
                    and not (self.connected)):
                self.connecting = False
                if error:
                    raise error
                raise exceptions.NumRetriesReached()

            if (sleeptime <= 0) or not (self.needed):
                self.connecting = False
                if error:
                    raise error
                raise TimedOut()

            with self.replylock:
                ret = self.replies.pop(call_id, None)
            #sleeptime = (cnt - 1) * 2 if cnt < 10 else 10

            if not ret:
                # other requests are fine
                new_reply = int(self.last_reply)
                if new_reply != seen_reply:
                    seen_reply = new_reply
                    # let's sleep some more, then!
                    sleeptime = sleepbase
                    continue
                sleeptime -= self.rate_limit * 2
                time.sleep(self.rate_limit * 2)
                continue

            self.last_reply += 1

            if 'error' in ret:
                from pprint import pprint
                pprint(ret)
                if 'detail' in ret['error']:
                    raise exceptions.RPCError(ret['error']['detail'])
                else:
                    raise exceptions.RPCError(ret['error']['message'])
            else:
                return ret["result"]
예제 #3
0
    def _rpcexec(self, payload):
        """ Execute a call by sending the payload

            :param json payload: Payload data
            :raises ValueError: if the server does not respond in proper JSON format
            :raises RPCError: if the server returns an error
        """
        if not (self.needed):
            raise exceptions.NumRetriesReached()
        log.debug(json.dumps(payload))
        call_id = payload['id']
        print("RPC-exec request", call_id, payload['params'][1],
              payload['params'][2])
        #if payload['params'][1] == "lookup_account_names":
        #    raise Exception("NO")
        try:
            #if self.connected:
            #    self.wssend(payload)
            #else:
            self.requests.put(payload, block=True)
        except KeyboardInterrupt:
            raise
        except:
            raise Exception("Unable to queue request")

        sleeptime = (self.num_retries - 1) * 3  # if cnt < 10 else 10
        sleeptime *= 3 if self.proxy_type else 1

        cnt = 1
        preid = None
        while True:
            if self.initialized and not (self.connected) and not (
                    self.connecting):
                raise TimedOut()  #exceptions.NumRetriesReached
            #print("RPC-exec waiting for", call_id, payload['params'][1], payload['params'][2], "[", sleeptime, "]")
            if (sleeptime <= 0) or not (self.needed):
                raise TimedOut()

            try:
                error = self.errors.get(block=False)
                if error:
                    raise error
            except KeyboardInterrupt:
                raise
            except:
                pass

            #if self.connected and preid and self._preid != preid:
            #    raise NumRetriesReached()

            with self.replylock:
                ret = self.replies.pop(call_id, None)
            #sleeptime = (cnt - 1) * 2 if cnt < 10 else 10

            if not ret:
                sleeptime -= self.rate_limit * 2
                time.sleep(self.rate_limit * 2)
                continue

            if 'error' in ret:
                from pprint import pprint
                pprint(ret)
                if 'detail' in ret['error']:
                    raise exceptions.RPCError(ret['error']['detail'])
                else:
                    raise exceptions.RPCError(ret['error']['message'])
            else:
                return ret["result"]