def do_POST(self): print("Recv req.....reading body") #rfile: Contains an input stream, positioned at the start of the optional input data. req_str = self.rfile.read(int(self.headers["content-length"])); convertcase_req = convertcase_pb2.ConvertcaseRequest() convertcase_req.ParseFromString(req_str) print("Recv service_name: "+ convertcase_req.header.service_name +" method_name: "+ convertcase_req.header.method_name) service = self.server.service_map[convertcase_req.header.service_name] method = service.GetDescriptor().FindMethodByName(convertcase_req.header.method_name) req = convertcase_pb2.ConvertcaseService.GetRequestClass(method)() resp = convertcase_pb2.ConvertcaseService.GetResponseClass(method)() rpc = ConvertcaseController() def done_callback(resp): """ Sends @resp back to the client over HTTP. """ resp_str = resp.SerializeToString() print("Sending "+ resp_str) self.send_response(200) self.send_header("Content-length", str(len(resp_str))) #Sends a blank line, indicating the end of the HTTP headers in the response. self.end_headers() self.wfile.write(resp_str) req.CopyFrom(convertcase_req) #Calls ConvertcaseImpl.ToLowercase() or ToUppercase() depending on method descriptor. #Once finished call done_callback() to notify. service.CallMethod(method, rpc, req, done_callback)
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 run(self): with self._conn: block_chan = BlockChannel(self._conn) while True: meta_buffer, data_buffer = block_chan.next_block() status, meta = self._protocol.unpack(meta_buffer) if not status: logger.error("fail to unpack buffer for %s" % meta.reason) continue full_class, method = self.split_method(meta.method) if not full_class: logger.error("class name is None,the method is %s" % meta.method) continue service = self._service_router.get(full_class, None) if not service: logger.error("no service for class name %s" % full_class) continue method_func = service.GetDescriptor().FindMethodByName(method) if not method_func: logger.error("no matched method %s in class %s" % (method, full_class)) continue req_class = service.GetRequestClass(method_func) if not req_class: logger.error("no matched req class ") continue req = req_class() req.ParseFromString(data_buffer) response = service.CallMethod(method_func, None, req, None) self.do_response(response, meta.sequence_id, block_chan)
def stringReceived(self, data): rpc = Rpc() rpc.ParseFromString(data) for serializedRequest in rpc.request: service = self._services[serializedRequest.method.split('.')[0]] if not service: self.sendError(serializedRequest.id, RpcErrors.SERVICE_NOT_FOUND) return method = service.GetDescriptor().FindMethodByName( serializedRequest.method.split('.')[1]) if not method: self.sendError(serializedRequest.id, RpcErrors.METHOD_NOT_FOUND) return request = service.GetRequestClass(method)() request.ParseFromString(serializedRequest.serialized_request) controller = Controller(peer=self.transport.getPeer()) d = Deferred() d.addCallback(self.serialize_response, serializedRequest, controller) d.addCallback(self.serialize_rpc) d.addCallback(lambda rpc: self.sendString(rpc.SerializeToString())) service.CallMethod(method, controller, request, d.callback) for serializedResponse in rpc.response: _id = serializedResponse.id if _id in self._pending: self._pending[_id].callback(serializedResponse)
def serveOneClient(self, service): (clientsocket, address) = self.serversocket.accept() print "got connection from", address while True: message = decode(clientsocket) if not message: clientsocket.close() break assert message.type == rpc_pb2.REQUEST assert message.service == service.GetDescriptor().name method = service.GetDescriptor().FindMethodByName(message.method) request_class = service.GetRequestClass(method) request = request_class() request.ParseFromString(message.request) response = service.CallMethod(method, None, request, None) responseMessage = rpc_pb2.RpcMessage() responseMessage.type = rpc_pb2.RESPONSE responseMessage.id = message.id responseMessage.response = response.SerializeToString() wire = encode(responseMessage) clientsocket.sendall(wire) print "connection is down", address
class UdpChannel(BaseChannel, DatagramProtocol): def __init__(self, host=None, port=None): self._host = host self._port = port self.connected = False BaseChannel.__init__(self) def startProtocol(self): if self._host and self._port: self.transport.connect(self._host, self._port) self.connected = True def datagramReceived(self, data, (host, port)): rpc = Rpc() rpc.ParseFromString(data) for serializedRequest in rpc.request: service = self._services[serializedRequest.method.split('.')[0]] if not service: self.sendError(serializedRequest.id, RpcErrors.SERVICE_NOT_FOUND, host, port) return method = service.GetDescriptor().FindMethodByName( serializedRequest.method.split('.')[1]) if not method: self.sendError(serializedRequest.id, RpcErrors.METHOD_NOT_FOUND, host, port) return request = service.GetRequestClass(method)() request.ParseFromString(serializedRequest.serialized_request) controller = Controller() d = Deferred() d.addCallback(self.serialize_response, serializedRequest) d.addCallback(self.serialize_rpc) d.addCallback(lambda rpc: self.send_string(rpc.SerializeToString(), host, port)) service.CallMethod(method, controller, request, d.callback) for serializedResponse in rpc.response: id = serializedResponse.id if self._pending.has_key(id): self._pending[id].callback(serializedResponse)
async def call(self, rpc): service = self.manager.get_service(rpc.request.service) method = service.GetDescriptor().FindMethodByName(rpc.request.method) request_args = service.GetRequestClass(method)() request_args.ParseFromString(rpc.request.args) controller = None callback = self.ResponseCallback(rpc.sid) # synchronously call method co = service.CallMethod(method, controller, request_args, callback) # await if method is a coroutine, i.e. asynchronous method if asyncio.iscoroutine(co): await co # send response if callback.response: await self.send_response(callback.sid, callback.response)
def register_service(self, service): self.service_map[service.GetDescriptor().name] = service
str = request.SerializeToString() #Send given @request over HTTP POST to remote HTTP server who is #hosting method identified by @method_desc. #Caller have already populated with necessary info so just send it across. self.http_conn.request("POST", "rpc/", str) print("Sending " + str) #Wait for response from HTTP request and then pass it to the caller using #@done() callback. resp_obj = self.http_conn.getresponse() #print(resp_obj.read()) str = resp_obj.read(resp_obj.getheader("content-length")) response = response_class() response.ParseFromString(str) done(response) def done(resp): print("Got response " + resp.word + "\n") channel = ConvertcaseChannel("localhost:8000") service = convertcase_pb2.ConvertcaseService_Stub(channel) method = service.GetDescriptor().FindMethodByName("ToUppercase") req = convertcase_pb2.ConvertcaseService.GetRequestClass(method)() rpc = ConvertcaseController() req.header.service_name = "ConvertcaseService" req.header.method_name = "ToUppercase" req.word = "HELLo ! WORLd" service.ToLowercase(rpc, req, done)
def add_service(self, service): self._service_router[service.GetDescriptor().full_name] = service
def add_service(self, service): self._services[service.GetDescriptor().name] = service