def signal(self, controller, request, done): """ Perform an RPC call to signal all clients 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 client :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 client: %s...', str(request)[:40]) # send signal to all registered clients # TODO: verify signal auth if request.event in registered_clients: for port in registered_clients[request.event]: def callback(req, resp): logger.info("Signal received by " + str(port)) service = RpcService(proto.EventsClientService_Stub, port, 'localhost') service.signal(request, callback=callback) # send response back to client response = proto.EventResponse() response.status = proto.EventResponse.OK done.run(response)
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 test_RpcService(self): client = RpcService(proto.SimpleService_Stub, self.service_port, "localhost") test_msg = proto.Simple() test_msg.val = 1 response = client.TestRpc(test_msg, timeout=5000) self.assertEqual(response.val, test_msg.val + 1, "Rpc test failed")
class StorageService(object): def __init__(self, host, port): self.service = RpcService(StorageService_Stub, port, host) def storeSensorData(self, sensordata, callback): try: self.service.storeSensorData(sensordata, callback=callback) except Exception, ex: log.exception(ex)
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 test_server_replies_ping(self): """ Ensure server replies to a ping. """ request = PingRequest() service = RpcService(EventsServerService_Stub, port, 'localhost') response = service.ping(request, timeout=1000) self.assertIsNotNone(response) self.assertEqual(EventResponse.OK, response.status, 'Wrong response status.')
def test_client_replies_ping(self): """ Ensure clients reply to a ping. """ daemon = client.ensure_client_daemon() port = daemon.get_port() request = PingRequest() service = RpcService(EventsClientService_Stub, port, 'localhost') response = service.ping(request, timeout=1000) self.assertEqual(EventResponse.OK, response.status, 'Wrong response status.')
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 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.')
def __init__(self): # Server details hostname = "localhost" port = 8090 # Create a new service instance self.service = RpcService(server_pb2.CommandService_Stub, port, hostname)
class ControllerProtobuf: def __init__(self): # Server details hostname = "localhost" port = 8090 # Create a new service instance self.service = RpcService(server_pb2.CommandService_Stub, port, hostname) ########################################################################## ################################ CLIENT ################################## ########################################################################## def is_connected(self): print("Try connection") request = server_pb2.IsConnectedRequest() # Make an synchronous call response = None try: response = self.service.is_connected(request, timeout=10000) is not None if response: print("Connection sucessful") except Exception, ex: log.exception(ex) return response
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 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 test_client_receives_signal(self): """ Ensure clients can receive signals. """ sig = 7 flag = Mock() events.register(sig, lambda req: flag(req.event)) 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) flag.assert_called_once_with(sig)
def test_events_server_service_register(self): """ Ensure the server can register clients 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_clients 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 client port.')
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_signal_executes_callback(self): """ Ensure callback is executed upon receiving signal. """ sig = CLIENT_UID 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') # register a callback flag = Mock() events.register(sig, lambda req: flag(req.event)) # signal response = service.signal(request) self.assertEqual(EventResponse.OK, response.status, 'Wrong response status.') time.sleep(1) # wait for signal to arrive flag.assert_called_once_with(sig)
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 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")
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 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(proto.SignalRequest, proto.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 ping(port, reqcbk=None, timeout=1000): """ Ping a client running in C{port}. :param port: the port in which the client should be listening :type port: int :param reqcbk: a callback to be called when a response from client is received :type reqcbk: function(proto.PingRequest, proto.EventResponse) :param timeout: the timeout for synch calls :type timeout: int :return: the response from client for synch calls or nothing for asynch calls. :rtype: leap.common.events.events_pb2.EventsResponse or None """ request = proto.PingRequest() service = RpcService( proto.EventsClientService_Stub, port, 'localhost') logger.debug("Pinging a client in port %d..." % port) return service.ping(request, callback=reqcbk, timeout=timeout)
def ping(port=SERVER_PORT, reqcbk=None, timeout=1000): """ Ping the server. :param port: the port in which server should be listening :type port: int :param reqcbk: a callback to be called when a response from server is received :type reqcbk: function(proto.PingRequest, proto.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.PingRequest() service = RpcService( proto.EventsServerService_Stub, port, 'localhost') logger.info("Pinging server in port %d..." % port) return service.ping(request, callback=reqcbk, timeout=timeout)
def main_pb(): logging.basicConfig(level = logging.DEBUG, format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s') logging.info("Logging in with team %s and password %s" % (team, password)) service = RpcService(codebots_pb2.CodebotsService_Stub, 12345, "localhost") res = service.login(codebots_pb2.LoginRequest(team=team, password=password)) session = res.session logging.info("Initiated session %s" % (session)) try: while True: res = service.getStatus(codebots_pb2.StatusRequest(session=session)) print "TURN %d" % (res.turnNum) status = dict([(tank.id, Tank(team = tank.team, position = (tank.posx, tank.posy))) for tank in res.fieldStatus.tanks]) simulator = Simulator(status) simulator.print_field() res = service.waitForSimulation(codebots_pb2.WaitForSimulationRequest(session = session)) print except KeyboardInterrupt: logging.info("Shutting down") finally: logging.info("Logging out") res = service.logout(codebots_pb2.LogoutRequest(session = session)) logging.info("Finished!")
def main(): logging.basicConfig(level = logging.DEBUG, format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s') team = int(sys.argv[1]) password = sys.argv[2] logging.info("Logging in with team %s and password %s" % (team, password)) service = RpcService(codebots_pb2.CodebotsService_Stub, 12345, "localhost") res = service.login(codebots_pb2.LoginRequest(team = team, password = password)) session = res.session logging.info("Initiated session %s" % (session)) try: while True: # Getting the status (a bit stupid, but I need testing) res = service.getShortStatus(codebots_pb2.ShortStatusRequest(session = session)) turn = res.turnNum res = service.getStatus(codebots_pb2.StatusRequest(session = session)) fieldStatus = res.fieldStatus print "TURN %d" % (turn) # Getting last differential res = service.getDifferential(codebots_pb2.GetDifferentialRequest(session = session, turn = turn-1)) print Differential.from_protobuf(res) # Performing a random request req = codebots_pb2.AddRequestsRequest(session = session, turn = turn) for tank in fieldStatus.tanks: tankReq = req.requests.add() tankReq.id = tank.id tankReq.move = random.randint(0, 3) res = service.addRequests(req) print res # Waiting for next step res = service.waitForSimulation(codebots_pb2.WaitForSimulationRequest(session = session)) print except KeyboardInterrupt: logging.info("Shutting down") finally: logging.info("Logging out") res = service.logout(codebots_pb2.LogoutRequest(session = session)) logging.info("Finished!")
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')
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)
class Device(models.Model): name = models.CharField(max_length=30) device_class = models.ForeignKey(DeviceClass) server_name = models.CharField(max_length=30, blank=True) argument = models.CharField(max_length=30, blank=True) depends_on = models.ManyToManyField('Device', related_name='depends', symmetrical=False, blank=True) location = models.ForeignKey(Location) hidden = models.BooleanField(default=False) power_state = models.ForeignKey(State) def __unicode__(self): return self.name 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 send_get(self, feature_type): request = control_pb2.GetRequest() request.device.CopyFrom(self.pb) request.type = feature_type response = self.service.Get(request, timeout=10000) if not response.result: log.error('Invalide answer: ' + response.error) return response def send_set(self, feature_type, value): request = control_pb2.SetRequest() request.device.CopyFrom(self.pb) request.type = feature_type request.value = value response = self.service.Set(request, timeout=1000) if not response.result: log.error('Invalide answer: ' + response.error) return response def get_pwr(self): for d in self.depends_on.all(): response = d.get_pwr() if response.value != control_pb2.POWER_ON: return response return self.send_get(control_pb2.FEAT_PWR) def send_pwr(self, value): # no protection for recursive dependencies. if value == control_pb2.POWER_ON: # power other device first for d in self.depends_on.all(): d.send_pwr(value) response = self.send_set(control_pb2.FEAT_PWR, value) if response.result: self.power_state = State.objects.get(value=value) self.save() if value == control_pb2.POWER_OFF: for d in self.depends_on.all(): d.send_pwr(value)
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.
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 = EventsClientDaemon.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 unregister(signal, uid=None, reqcbk=None, timeout=1000): """ Unregister a callback. If C{uid} is specified, unregisters only the callback identified by that unique id. Otherwise, unregisters all callbacks :param signal: the signal that causes the callback to be launched :type signal: int (see the `events.proto` file)
# 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
def __init__(self, host, port): self.service = RpcService(StorageService_Stub, port, host)
class Client(object): ''' Client handles the write and read requests to the shared log. It communicates with the sequencer and server using protobuf. ''' 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 append(self, data): ''' string -> (int list * float list) Checking length of data and for every piece of the data, try guessing a server to write to and waiting for response. If a guess fails, retry writing. Every time some feedback helps to guess better next time. Return the list of tokens storing the data. Failure Handling: Sequencer returns specific token ID when the guessing server is already full. Exception Handling: NONE ''' data_len = len(data) if data_len == 0: return [] number_of_tokens = (data_len - 1) // self.config.FLASH_PAGE_SIZE + 1 token_list = [] # try send every piece of data by guessing for i in xrange(number_of_tokens): piece_beg = i * self.config.FLASH_PAGE_SIZE piece_end = (i + 1) * self.config.FLASH_PAGE_SIZE if i == number_of_tokens - 1: piece_end = data_len piece_data = data[piece_beg:piece_end] while True: server_w = self.guess_server() piece_id = self.client_id self.send_to_sequencer(server_w, piece_id) request_timestamp = utils.nanotime() response_w = self.write_to_flash(server_w, piece_data, piece_id) self.update_guess_info(response_w, request_timestamp, server_w) if response_w.status == flash_service_pb2.WriteResponse.SUCCESS: token_list.append(response_w.measure.token) break return token_list def update_guess_info(self, response, request_timestamp, server): ''' update information about guessing after the response from flash ''' ips_measure = response.measure if response.status == flash_service_pb2.WriteResponse.SUCCESS: if self.largest_token < ips_measure.token: self.largest_token = ips_measure.token self.largest_timestamp = ips_measure.token_timestamp self.latest_ips = ips_measure.ips self.delay = ips_measure.request_timestamp - request_timestamp assert (self.delay >= 0) self.last_state = SUCCESS elif response.status == flash_service_pb2.WriteResponse.ERROR_NO_CAPACITY: self.latest_ips = ips_measure.ips self.last_server = server self.last_state = FULL else: self.latest_ips = ips_measure.ips self.last_state = FAIL def guess_server(self): ''' None -> int Guess the next server that should be written to. ''' if self.last_state == INITIAL: return random.randint(0, self.config.FLASH_PER_GROUP - 1) elif self.last_state == FULL: return self.last_server + 1 else: # SUCCESS or FAIL here guess_inc = ( utils.nanos_to_sec(self.delay + utils.nanotime() - self.largest_timestamp) * self.latest_ips + 1 + self.config.CLIENT_GUESS_OVERESTIMATION) guess_token = int(math.ceil(self.largest_token + guess_inc)) guess_token += random.randint(1, 2) (guess_server, _, _, _) = self.projection.translate(guess_token) return guess_server def send_to_sequencer(self, flash_unit_number, data_id): ''' int * int -> SequencerServiceResponse Asyncronously send server and data id to the sequencer, ignores the response. ''' request_seq = sequencer_service_pb2.SequencerServiceRequest() request_seq.flash_unit_number = flash_unit_number request_seq.data_id = data_id response_seq = self.service.Write(request_seq, timeout=10000, callback=Callback()) return response_seq 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") def reset_guess_info(self): 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 def trim(self, token): # we don't need trim in this project pass def fill(self, token): # holes will be filled by servers pass
request = proto.CommandRequest() if(args.command == "POWER_OFF"): command = proto.POWER_OFF elif(args.command == "POWER_ON"): command = proto.POWER_ON else: raise NameError('Command Not in Dictionary') request.credentials = socket.gethostbyname(socket.gethostname()) request.command = command except Exception, ex: print(ex) except NameError: print('Command supplied does not exist in dictionary %s', args.command) raise service = RpcService(proto.PoshService_Stub, int(args.port), args.host) try: request.IsInitialized() response = service.getCommandResponse(request, timeout=1000) if response is None: raise Exception('service not responding') print(response) except Exception, ex: print(ex)