def test_n_services(self): client1 = RpcService(proto.SimpleService_Stub, self.service_port, "localhost") client2 = RpcService(proto.SimpleService2_Stub, self.service_port, "localhost") test_msg = proto.Simple() test_msg.val = 1 response = client1.TestRpc(test_msg, timeout=5000) self.assertEqual(response.val, test_msg.val + 1, "Rpc test failed") response = client2.TestRpc(test_msg, timeout=5000) self.assertEqual(response.val, test_msg.val + 1, "Rpc test failed")
def _start_flash_server(cls, server_index): host, port = config.SERVER_ADDR_LIST[server_index] server_thread = test.helper.ServerThread( port, host, flash_service.FlashServiceImpl.from_index(server_index, config)) server_thread.start_server() return RpcService(flash_service_pb2.FlashService_Stub, port, host)
def signal(self, controller, request, done): """ Perform an RPC call to signal all components registered to receive a specific signal. @param controller: used to mediate a single method call @type controller: protobuf.socketrpc.controller.SocketRpcController @param request: the request received from the component @type request: leap.common.events.events_pb2.SignalRequest @param done: callback to be called when done @type done: protobuf.socketrpc.server.Callback """ logger.info('Received signal from component: %s', str(request)) # send signal to all registered components # TODO: verify signal auth if request.event in registered_components: for port in registered_components[request.event]: def callback(req, resp): logger.info("Signal received by " + str(port)) service = RpcService(proto.EventsComponentService_Stub, port, 'localhost') service.signal(request, callback=callback) # send response back to component response = proto.EventResponse() response.status = proto.EventResponse.OK done.run(response)
def test_signal_response_status(self): """ Ensure there's an appropriate response from server when signaling. """ sig = 4 request = SignalRequest() request.event = sig request.content = 'my signal contents' request.mac_method = mac_auth.MacMethod.MAC_NONE request.mac = "" service = RpcService(EventsServerService_Stub, port, 'localhost') # test synch response = service.signal(request, timeout=1000) self.assertEqual(EventResponse.OK, response.status, 'Wrong response status.') # test asynch def local_callback(request, response): global local_callback_executed local_callback_executed = True events.register(sig, local_callback) service.signal(request, callback=local_callback) time.sleep(0.1) self.assertTrue(local_callback_executed, 'Local callback did not execute.')
def setUpClass(cls): # start server thread cls.server_thread = test.helper.ServerThread( FLASH_SERVER_PORT, FLASH_SERVER_HOST, flash_service.FlashServiceImpl.from_index(0, config)) cls.server_thread.start_server() cls.service = RpcService(flash_service_pb2.FlashService_Stub, FLASH_SERVER_PORT, FLASH_SERVER_HOST) cls._reset_flash_server()
def fill_hole_flash(self, token): """Creates a hole at the token by sending a FillHole request.""" _, host, port, offset = self.projection.translate(token) request_f = flash_service_pb2.FillHoleRequest() request_f.offset = offset # TODO: initialize RpcService in init service_f = RpcService(flash_service_pb2.FlashService_Stub, port, host) service_f.FillHole(request_f, callback=self.callback)
def __init__(self, config=global_config): self.projection = projection.Projection(config) self.service = RpcService(sequencer_service_pb2.SequencerService_Stub, config.SEQUENCER_PORT, config.SEQUENCER_HOST) self.client_id = str(uuid4()) # guessing information self.largest_token = -1 self.largest_timestamp = 0.0 self.last_state = INITIAL self.last_server = -1 self.latest_ips = 0.0 self.delay = 0.0 self.config = config
def signal(signal, content="", mac_method="", mac="", reqcbk=None, timeout=1000): """ Send `signal` event to events server. Will timeout after timeout ms if response has not been received. The timeout arg is only used for asynch requests. If a reqcbk callback has been supplied the timeout arg is not used. The response value will be returned for a synch request but nothing will be returned for an asynch request. :param signal: the signal that causes the callback to be launched :type signal: int (see the `events.proto` file) :param content: the contents of the event signal :type content: str :param mac_method: the method used for auth mac :type mac_method: str :param mac: the content of the auth mac :type mac: str :param reqcbk: a callback to be called when a response from server is received :type reqcbk: function callback(leap.common.events.events_pb2.EventResponse) :param timeout: the timeout for synch calls :type timeout: int :return: the response from server for synch calls or nothing for asynch calls :rtype: leap.common.events.events_pb2.EventsResponse or None """ request = proto.SignalRequest() request.event = signal request.content = content request.mac_method = mac_method request.mac = mac service = RpcService(proto.EventsServerService_Stub, server.SERVER_PORT, 'localhost') logger.info("Sending signal to server: %s", str(request)[:40]) return service.signal(request, callback=reqcbk, timeout=timeout)
def test_events_server_service_register(self): """ Ensure the server can register components to be signaled. """ sig = 5 request = RegisterRequest() request.event = sig request.port = 8091 request.mac_method = mac_auth.MacMethod.MAC_NONE request.mac = "" service = RpcService(EventsServerService_Stub, port, 'localhost') complist = server.registered_components self.assertEqual({}, complist, 'There should be no registered_ports when ' 'server has just been created.') response = service.register(request, timeout=1000) self.assertTrue(sig in complist, "Signal not registered succesfully.") self.assertTrue(8091 in complist[sig], 'Failed registering component port.')
def __init__(self, *args, **kwargs): super(Device, self).__init__(*args, **kwargs) if not hasattr(self, 'device_class'): return c = self.device_class if c.device_type.value in [ control_pb2.APC, control_pb2.PC_WIN, control_pb2.PC_LINUX, control_pb2.UFO, control_pb2.IPPOWER, control_pb2.IPMI ]: server = 'localhost' else: server = self.server_name self.service = RpcService(control_pb2.ControlService_Stub, 10021, server) d_pb = control_pb2.Device() d_pb.type = c.device_type.value d_pb.server = self.server_name d_pb.port = str(self.argument) self.pb = control_pb2.Device() self.pb.CopyFrom(d_pb)
def test_component_receives_signal(self): """ Ensure components can receive signals. """ sig = 7 def getsig(param=None): global received received = True events.register(sig, getsig) request = SignalRequest() request.event = sig request.content = "" request.mac_method = mac_auth.MacMethod.MAC_NONE request.mac = "" service = RpcService(EventsServerService_Stub, port, 'localhost') response = service.signal(request, timeout=1000) self.assertTrue(response is not None, 'Did not receive response.') time.sleep(0.5) self.assertTrue(received, 'Did not receive signal back.')
def send_to_flash(self, request, token, is_full=False): """If is_full is True, token does not have any meaning, should be ignored. """ (host, port) = self.config.SERVER_ADDR_LIST[request.flash_unit_index] # TODO: initialize RpcService in init service_f = RpcService(flash_service_pb2.FlashService_Stub, port, host) request_f = flash_service_pb2.WriteOffsetRequest() request_f.data_id = request.data_id (_, _, _, request_f.offset) = self.projection.translate(token) request_f.is_full = is_full request_f.measure.token = token request_f.measure.request_timestamp = request.request_timestamp # timestamp is the timestamp for ips token_timestamp = utils.nanotime() request_f.measure.token_timestamp = token_timestamp request_f.measure.ips = self.ips_thread.get_ips() service_f.WriteOffset(request_f, callback=self.callback) sequencing_overhead = token_timestamp - request.request_timestamp self.logger.debug("sequencing overhead for {0}: {1}ns".format( request.data_id, sequencing_overhead))
def write_to_flash(self, server, data, data_id): ''' int * string * int -> WriteResponse.Status Send data and its id to the server, and wait until the response is back. Failure Handling: NONE Exception Handling: NONE ''' # data must be fit in a page right now (dest_host, dest_port) = self.config.SERVER_ADDR_LIST[server] service_w = RpcService(flash_service_pb2.FlashService_Stub, dest_port, dest_host) request_w = flash_service_pb2.WriteRequest() request_w.data_id = data_id request_w.data = data response_w = service_w.Write(request_w, timeout=10000) return response_w
def read(self, token): ''' int -> string Read the data stored in the given token of the shared log. Failure Handling: Raise an exception up to the client. Exception Handling: NONE ''' (_, dest_host, dest_port, dest_page) = self.projection.translate(token) service_r = RpcService(flash_service_pb2.FlashService_Stub, dest_port, dest_host) request_r = flash_service_pb2.ReadRequest() request_r.offset = dest_page response_r = service_r.Read(request_r, timeout=10000) if response_r.status == flash_service_pb2.ReadResponse.SUCCESS: return response_r.data else: raise Exception("server read error")
registered_callbacks[signal] = [] cbklist = registered_callbacks[signal] if uid and filter(lambda (x, y): x == uid, cbklist): if not replace: raise CallbackAlreadyRegisteredException() else: registered_callbacks[signal] = filter(lambda (x, y): x != uid, cbklist) registered_callbacks[signal].append((uid, callback)) # register callback on server request = proto.RegisterRequest() request.event = signal request.port = EventsComponentDaemon.get_instance().get_port() request.mac_method = mac_auth.MacMethod.MAC_NONE request.mac = "" service = RpcService(proto.EventsServerService_Stub, server.SERVER_PORT, 'localhost') logger.info("Sending registration request to server on port %s: %s", server.SERVER_PORT, str(request)[:40]) return service.register(request, callback=reqcbk, timeout=timeout) def signal(signal, content="", mac_method="", mac="", reqcbk=None, timeout=1000): """ Send `signal` event to events server.
def __init__(self, host, port): self.service = RpcService(StorageService_Stub, port, host)
# Configure logging import logging log = logging.getLogger(__name__) logging.basicConfig(level=logging.DEBUG) # Server details hostname = 'localhost' port = 8090 # Create a request request = hello_world_pb2.HelloRequest() request.my_name = 'Zach' # Create a new service instance service = RpcService(hello_world_pb2.HelloWorldService_Stub, port, hostname) def callback(request, response): """Define a simple async callback.""" log.info('Asynchronous response :' + response.__str__()) # Make an asynchronous call try: log.info('Making asynchronous call') response = service.HelloWorld(request, callback=callback) except Exception, ex: log.exception(ex) # Make a synchronous call
May 2009, Nov 2010 ''' # Add main protobuf module to classpath import sys sys.path.append('../../main') import time_pb2 as proto from protobuf.socketrpc import RpcService import logging log = logging.getLogger(__name__) hostname = 'localhost' port = 8090 if __name__ == '__main__': logging.basicConfig(level=logging.DEBUG) log.debug("test") # Create request message request = proto.TimeRequest() service = RpcService(proto.TimeService_Stub, port, hostname) try: response = service.getTime(request, timeout=1000) log.info(response) except Exception, ex: log.exception(ex)
log = logging.getLogger(__name__) logging.basicConfig(level=logging.DEBUG) # Server details hostname = 'localhost' port = 8090 # Create a request request = chunks_pb2.Chunk() request.id = 'Id del chunk' request.owner = 'Owner del chunk' request.chunk = 'El contenido del chunk' # Create a new service instance service = RpcService(chunks_pb2.ReplicantSrvr_Stub, port, hostname) def callback(request, response): """Define a simple async callback.""" log.info('Asynchronous response :' + response.__str__()) # Make an asynchronous call try: log.info('Making asynchronous call') response = service.SendChunk(request, callback=callback) except Exception, ex: log.exception(ex) # Make a synchronous call try: log.info('Making synchronous call')