Esempio n. 1
0
    def _sendRPC(self, service, method, request, response):
        try:
            request_wrapper = rpc_pb2.Request()
            request_wrapper.rpc_id = self._next_rpc_id
            self._next_rpc_id += 1
            request_wrapper.service = service
            request_wrapper.method = method
            request_wrapper.body = request.Encode()
            self._writePB(request_wrapper)

            response_wrapper = rpc_pb2.Response()
            self._readPB(response_wrapper)
        except:
            # Any exception here should cause us to close the socket to make sure we
            # don't leave it in an unknown state.
            self.closeSession()
            raise

        assert response_wrapper.rpc_id == request_wrapper.rpc_id

        if response_wrapper.status == APPLICATION_ERROR:
            raise apiproxy_errors.ApplicationError(
                response_wrapper.application_error,
                response_wrapper.error_detail)
        elif response_wrapper.status in _ExceptionsMap:
            ex, message = _ExceptionsMap[response_wrapper.status]
            raise ex(message % (service, method))
        else:
            response.ParseFromString(response_wrapper.body)
Esempio n. 2
0
def _make_request(type, id="", config=LoadConfig()):
    req = rpc_proto.Request()
    req.type = type
    req.id = id
    req.config.guarantee = config.guarantee
    req.config.limit = config.limit
    req.config.swap_limit = config.swap_limit
    return req
Esempio n. 3
0
    def createRpcRequest(self, method, request):
        '''Wrap the user's request in an RPC protocol message.'''
        rpcRequest = rpc_pb.Request()
        rpcRequest.request_proto = request.SerializeToString()
        rpcRequest.service_name = method.containing_service.full_name
        rpcRequest.method_name = method.name

        return rpcRequest
Esempio n. 4
0
    def parseServiceRequest(self, bytestream_from_client):
        '''Validate the data stream received from the client.'''

        # Convert the client request into a PB Request object
        request = rpc_pb.Request()

        # Catch anything which isn't a valid PB bytestream
        try:
            request.MergeFromString(bytestream_from_client)
        except Exception, e:
            raise error.BadRequestDataError("Invalid request from \
                                            client (decodeError): " + str(e))
Esempio n. 5
0
def get_request():
    input = rpc_pb2.Request()
    service = UserServiceImpl()
    input.service_name = service.GetDescriptor().full_name
    input.method_name = 'UserLogin'
    data = user_pb2.UserLoginRequest()
    data.user_name = '*****@*****.**'
    data.user_pass = '******'
    input.request_proto = data.SerializeToString()
    request = input.SerializeToString()
    protocol = RPCProtocol()
    return protocol.build_request(request)
Esempio n. 6
0
def request(server, method, params):
    socket = context.socket(zmq.REQ)
    socket.connect(server)

    request = rpc_pb2.Request()
    request.method = method
    request.param = params

    socket.send(request.SerializeToString())
    reply = socket.recv()
    response = rpc_pb2.Response.FromString(reply)

    return response.data
Esempio n. 7
0
 def test_handle(self):
     input = rpc_pb2.Request()
     input.service_name = self.service.GetDescriptor().full_name
     input.method_name = 'HelloWorld'
     data = hello_world_pb2.HelloRequest()
     data.my_name = 'Zach'
     input.request_proto = data.SerializeToString()
     ret = self.handler.handle(input.SerializeToString())
     response = rpc_pb2.Response()
     response.ParseFromString(ret)
     service_res = hello_world_pb2.HelloResponse()
     service_res.ParseFromString(response.response_proto)
     self.assertEqual(service_res.hello_world, 'Hello Zach')
def get_request():
    input = rpc_pb2.Request()
    service = ServerStatus()
    input.service_name = service.GetDescriptor().full_name
    input.method_name = 'RPCStatus'
    data = status_pb2.TCPRequest()
    data.client_ip = 'Zach'
    data.user_name = 'Zach'
    data.user_pass = '******'
    input.request_proto = data.SerializeToString()
    request = input.SerializeToString()
    #print binascii.hexlify(request)
    protocol = RPCProtocol()
    return protocol.build_request(request)
Esempio n. 9
0
    def handle(self):
        logger = self.server.logger

        while True:
            try:
                req_raw = self.request.recv(MAX_MSG_SIZE)
            except socket.error as err:
                logger.warning("Failed to receive request: %s" % err)
                break

            if not req_raw:
                break

            req = rpc_proto.Request()
            try:
                req.ParseFromString(req_raw)
            except:
                logger.warning("Invalid request")
                break

            logger.debug("Received request:\n" + "-" * 8 + "\n%s" % req +
                         "-" * 8)

            handler = self.__handlers.get(req.type)

            try:
                if not handler:
                    raise Error(errno.EINVAL, "Invalid request type")
                resp = handler(self, req)
                if not resp:
                    resp = rpc_proto.Response()
                    resp.type = rpc_proto.RESP_EMPTY
            except Error as err:
                resp = rpc_proto.Response()
                resp.type = rpc_proto.RESP_ERROR
                resp.errcode = err.errcode
                resp.errmsg = err.errmsg

            resp_list = resp if isinstance(resp, list) else [resp]

            try:
                for resp in resp_list:
                    logger.debug("Sending response:\n" + "-" * 8 +
                                 "\n%s" % resp + "-" * 8)
                    self.request.send(resp.SerializeToString())
            except socket.error as err:
                logger.warning("Failed to send response: %s" % err)
                break