class RemoteNetCodingReader(InputStreamReader): def __init__(self, node_addr, block_id, coding_id, stream_id, nodes, **kwargs): nodes = ';'.join(map(str, nodes)) self.client = Client(*node_addr) self.header = DataNodeHeader.generate(DataNodeHeader.OP_CODING, block_id, coding_id, stream_id, nodes) self.client.send(self.header) super(RemoteNetCodingReader, self).__init__(self.client.recv_stream(), async=True, **kwargs) def finalize(self, kill=False): if not kill: self.client.assert_ack() super(RemoteNetCodingReader, self).finalize(kill)
class SnakeClient(object): def __init__(self, stupid=False): self.uuid = None self.ui = None self.net_cli = Client() if not stupid: self.ui = ui.SnakeUI() def create(self): self.uuid = str(uuid.uuid1()) self.net_cli.set_uuid(self.uuid) self.net_cli.send(NEW_CLIENT) def get_world(self): res = self.net_cli.send(GET_WORLD) try: return json.loads(res) except json.decoder.JSONDecodeError: with open("dump", "w") as fl: print("Broken world recieved.") fl.write(res) return self.get_world() def commands_handler(self): cmd = self.ui.get_event() if cmd in [GO_UP, GO_DOWN, GO_RIGHT, GO_LEFT]: self.net_cli.send(cmd) if cmd == CLIENT_RESET: self.create() def display_map(self): w = self.get_world() self.ui.draw(w) def run(self): while True: self.commands_handler() self.display_map()
def handle_message(data): cli = Client() client_uuid, command = data.split("|") cli.set_uuid(client_uuid) cli.send(NEW_CLIENT) if command == "up": cli.send(GO_UP) elif command == "down": cli.send(GO_DOWN) elif command == "left": cli.send(GO_LEFT) elif command == "right": cli.send(GO_RIGHT) elif command == "r": cli.send(NEW_CLIENT)
def handle_message(user_uuid): cli = Client() cli.set_uuid(user_uuid) resp = cli.send(GET_WORLD) emit("map", resp)
class SimNode(object): START_BYTE = 0xFE TIME_OUT_S = 1 def __init__(self): self.client = Client() self.tubes = Tubes(10) #set the LED color to the default color self.led_color = (255, 255, 255) #create a counter for the HB time self.last_hb_time = 0 #create an action handler for acting on incoming messages self.actions = utils.ActionHandler() self.actions.add_action("REQUEST_REPORT\0", self._request_report) self.actions.add_action("SET_LED\0", self._set_led) self.actions.add_action("FIRE_TUBE\0", self._fire_tube) self.actions.add_action("HEARTBEAT\0", self._heartbeat) self.actions.add_action("RESPONSE\0", self._response) self.actions.add_action("REPORT\0", self._report) #extra trackers for internal use only self._last_fire_time = 0 self._LOAD_DELAY_S = 1 self._TIME_BETWEEN_LOADS_S = 0.1 self._last_load_time = time.time() def time_since_fire(self): return time.time() - self._last_fire_time def time_since_HB(self): actualTime = time.time() - self.last_hb_time return actualTime if actualTime <= 4294967295 else 4294967295 #what do we have to do? def main(self): #we need to check the state of the tubes and update tube_state #for the simulation we are going to just assume that the tubes get #reloaded at some rate some time after the last fire command. self._update_tubes() #we need to check for and handle new messages if self.client.is_connected(): try: incoming = self.client.receive(message.parser) if incoming is not None: self.actions.do_action(incoming.id, incoming) else: pass except Exception as e: print "Exception occured while trying to process messages" print e self.client.close() else: time.sleep(0.01) """ Each of the "handler methods" below are for internal use only and are used to orginize the behivors that are linked to each incoming message type. """ def _fire_tube(self, incoming): tube_number = incoming.tube_number if self.time_since_HB() < self.TIME_OUT_S: if tube_number <= self.tubes.get_num_tubes(): success = self.tubes.fire_tube(tube_number) if success: #print "firing tube number", tube_number print "firing tube:", tube_number self._last_fire_time = time.time() response = message.MsgResponse(success, 1) else: #print "Tube is not loaded", tube_number response = message.MsgResponse(success, 4) else: #print "Tube number does not exist" response = message.MsgResponse(0, 2) else: #print "Heatbeat has expired" response = message.MsgResponse(0, 0) self.client.send(response) def _response(self, incoming): print "in response, code is", incoming.isAck, incoming.flags def _request_report(self, incoming): print "in request report" self.client.send(message.MsgResponse(1, 0)) report_msg = message.MsgReport(self.tubes.get_num_tubes(), self.tubes.get_tubes(), self.led_color, self.time_since_HB()) self.client.send(report_msg) def _set_led(self, incoming): print "in set led", incoming.red, incoming.green, incoming.blue self.client.send(message.MsgResponse(1,1)) self.led_color = (incoming.red, incoming.green, incoming.blue) def _heartbeat(self, incoming): print "in heartbeat at time", time.time() self.last_hb_time = time.time() self.client.send(message.MsgResponse(1, 0)) def _report(self, incoming): print "wtf is the master sending report messages?" """ Update the state of the tubes. For the simulation all this will do is check for unloaded tubes and load a random tube. """ def _update_tubes(self): if self.time_since_fire() > self._LOAD_DELAY_S: if time.time() - self._last_load_time > self._TIME_BETWEEN_LOADS_S: if self.tubes.get_num_empty() > 0: tube_num = random.choice(self.tubes.get_empty_tubes()) self.tubes.load_tube(tube_num) self._last_load_time = time.time() print self.tubes.get_num_tubes() - self.tubes.get_num_empty(), "+", tube_num def close(self): self.client.close()