Exemplo n.º 1
0
 def setUp(self):
     self.channel = SocketRpcChannel(host=host, port=success_port)
     self.controller = self.channel.newController()
     self.service = test_pb2.TestService_Stub(self.channel)
     self.request = test_pb2.Request()
     self.request.str_data = 'I like cheese'
     self.callback = lambda request, response: response
     self.thread = service.RpcThread(
         test_pb2.TestService_Stub.TestMethod,
         self.service, self.controller, self.request, self.callback)
     ServerThread.start_server(
         success_port, fail_port, Exception('YOU FAIL!'))
Exemplo n.º 2
0
    def __init__(self, service_stub_class, port, host):
        '''
        Contruct a new ProtoBufRpcRequest and return it.

        Accepted Arguments:
        service_stub_class -- (Service_Stub) The client side RPC
                              stub class produced by protoc from
                              the .proto file
        port -- (Integer) The port on which the service is running
                on the RPC server.
        host -- (String) The hostname or IP address of the server
                running the RPC service.
        '''
        self.service_stub_class = service_stub_class
        self.port = port
        self.host = host

        # Setup the RPC channel
        self.channel = SocketRpcChannel(host=self.host, port=self.port)
        self.service = self.service_stub_class(self.channel)

        # go through service_stub methods and add a wrapper function to
        # this object that will call the method
        for method in service_stub_class.GetDescriptor().methods:
            # Add service methods to the this object
            rpc = lambda request, timeout=None, callback=None, service=self, \
                method=method.name: \
                service.call(service_stub_class.__dict__[method], request,
                             timeout, callback)
            rpc.__doc__ = method.name + ' method of the ' + \
                service_stub_class.DESCRIPTOR.name + ' from the ' + \
                service_stub_class.__module__ + ' module generated by the ' + \
                'protoc compiler.  This method can be called ' + \
                'synchronously by setting timeout -> ms or ' + \
                'asynchrounously by setting callback -> ' + \
                'function(request,response)\n\nSynchronous Example:\n' + \
                '\trequest = ' + method.input_type.name + '()\n' + \
                '\ttry:\n' + \
                '\t#Wait 1000ms for a response\n' + \
                '\t\tresponse = ' + method.name + \
                '(request, timeout=1000)\n' + \
                '\texcept: RpcException\n' + \
                '\t\t#Handle exception\n\n' + \
                'Asynchronous Example:\n' + \
                '\tdef callback(request,response):\n' + \
                '\t\t#Do some stuff\n' + \
                '\trequest = ' + method.input_type.name + '()\n' + \
                '\ttry:\n' + \
                '\t\t' + method.name + '(request, callback=callback)\n' + \
                '\texcept: RpcException\n' + \
                '\t\t#Handle exception\n\n'
            self.__dict__[method.name] = rpc