def CallMethod(self, method, controller, request, response_pb, done): """Call a method. Don't use this directly, use the Stub object. Args: method: The MethodDescriptor to call controller: An RpcController object request: The request message response: The response class done: A closure to call once complete. """ message = Rpc_pb2.RpcMessage() message.type = Rpc_pb2.REQUEST message.id = self._sequence message.name = method.name message.buffer = request.SerializeToString() self._SendMessage(message) self._sequence += 1 if message.id in self._outstanding_responses: # fail any outstanding response with the same id, not the best approach # but it'll do for now. logging.warning('Response %d already pending, failing now', message.id) response = self._outstanding_responses[message.id] response.controller.SetFailed('Duplicate request found') self._InvokeCallback(response) response = OutstandingResponse(message.id, controller, done, response_pb) self._outstanding_responses[message.id] = response
def _SendRequestFailed(self, request): """Send a response indiciating this request failed. Args: request: An OutstandingRequest object. """ message = Rpc_pb2.RpcMessage() message.type = Rpc_pb2.RESPONSE_FAILED message.id = request.id message.buffer = request.controller.ErrorText() self._SendMessage(message) del self._outstanding_requests[request.id]
def _HandleNewMessage(self, data): """Handle a new Message. Args: data: A chunk of data representing a RpcMessage """ message = Rpc_pb2.RpcMessage() message.ParseFromString(data) if message.type in self.MESSAGE_HANDLERS: self.MESSAGE_HANDLERS[message.type](self, message) else: logging.warning('Not sure of message type %d', message.type())
def RequestComplete(self, request, response): """This is called on the server side when a request has completed. Args: request: the OutstandingRequest object that has completed. response: the response to send to the client """ message = Rpc_pb2.RpcMessage() if request.controller.Failed(): self._SendRequestFailed(request) return message.type = Rpc_pb2.RESPONSE message.id = request.id message.buffer = response.SerializeToString() self._SendMessage(message) del self._outstanding_requests[request.id]