def string_received(self, data): rpc = Rpc() rpc.ParseFromString(data) for serializedRequest in rpc.request: service = self.server.services[serializedRequest.method.split('.') [0]] method = service.GetDescriptor().FindMethodByName( serializedRequest.method.split('.')[1]) if method: request = service.GetRequestClass(method)() request.ParseFromString(serializedRequest.serialized_request) controller = Controller() class callbackClass(object): def __init__(self): self.response = None def __call__(self, response): self.response = response callback = callbackClass() service.CallMethod(method, controller, request, callback) responseRpc = self.serialize_rpc( self.serialize_response(callback.response, serializedRequest)) self.send_string(responseRpc.SerializeToString())
def string_received(self, data): rpc = Rpc() rpc.ParseFromString(data) for serializedResponse in rpc.response: id = serializedResponse.id if self._pending.has_key(id) and self._pending[id] != None: responseClass = self._pending[id][0] done = self._pending[id][1] done( self.unserialize_response(serializedResponse, responseClass))
def CallMethod(self, methodDescriptor, rpcController, request, responseClass, done=None): self.id += 1 self._pending[self.id] = (responseClass, done) rpc = Rpc() rpcRequest = rpc.request.add() rpcRequest.method = methodDescriptor.containing_service.name + '.' + methodDescriptor.name rpcRequest.serialized_request = request.SerializeToString() rpcRequest.id = self.id self.send_string(rpc.SerializeToString()) buffer = self._tcpSocket.recv(struct.calcsize("!I")) bufferLen = int(struct.unpack("!I", buffer)[0]) buffer = self._tcpSocket.recv(bufferLen) r = self.string_received(buffer) return r
def serialize_rpc(self, serializedResponse): rpc = Rpc() rpcResponse = rpc.response.add() rpcResponse.serialized_response = serializedResponse.serialized_response rpcResponse.id = serializedResponse.id return rpc