def send_packet(self, packet, src_id, dst_id=None, callback=None, callback_args=tuple() ): self._log("----------------------- SEND PACKET -----------------------") if not self.frame_loss.check(len(packet)): self._log("----------------------- OK -----------------------") self._log("send-packet {}->{} {}".format(src_id, dst_id, packet)) if enable_statsct: Statsct.log("send-packet {}->{} {}".format(src_id, dst_id, packet)) Statsct.add_packet_info(packet, src_id, dst_id, True) # if dst_id == None, it is a broadcast link_list = self.get_link_by_id(src_id, dst_id) count = 0 for link in link_list: count += self.send_packet_on_link(link, packet) else: self._log("----------------------- KO -----------------------") self._log("packet was lost {}->{}".format(src_id, dst_id)) if enable_statsct: Statsct.log("packet was lost {}->{} {}".format(src_id, dst_id, packet)) Statsct.add_packet_info(packet,src_id,dst_id, False) count = 0 # if callback != None: args = callback_args+(count,) # XXX need to check. - CA: # [CA] the 'count' is now passed as 'status' in: # SimulLayer2._event_sent_callback(self, transmit_callback, status # the idea is that is transmission fails, at least you can pass # count == 0 (status == 0), and you can do something there. # (in general case, some meta_information need to be sent) #args = callback_args callback(*args) return count
def init_stat(self): # Statistic module radio_config = self.simul_config["radio"] Statsct.initialize(init_time=0) Statsct.log("Statsct test") Statsct.set_packet_size(radio_config["data_size"]) Statsct.set_SF(radio_config["SF"])
def send_packetX(self, packet, src_id, dst_id=None, callback=None, callback_args=tuple()): """send a message to another device in a client - server Simulation""" self._log("----------------------- SEND PACKET -----------------------") if not self.frame_loss.check(packet): self._log("----------------------- OK -----------------------") self._log("send-packet {}->{} {}".format(src_id, dst_id, packet)) if enable_statsct: Statsct.log("send-packet {}->{} {}".format(src_id, dst_id, packet)) Statsct.add_packet_info(packet,src_id,dst_id, True) # if dst_id == None, it is a broadcast # link_list = self.get_link_by_id(src_id, dst_id) count = 1 # for link in link_list: # count += self.send_packet_on_link(link, packet) note_table_list = list(self.node_table.items())[-1][1] #self.node_table[0].protocol.layer2.clientSend.send(packet) note_table_list.protocol.layer2.roleSend.send(packet) try: number_tiles_send = \ note_table_list.protocol.fragment_session.session_list[0]["session"].current_number_tiles_sent() state = note_table_list.protocol.fragment_session.session_list[0]["session"].state print("STATE : ", state) print("Lenght queue", len(self.scheduler.queue)) if (state == self.SEND_ALL_1 or state == self.ACK_FAILURE or state == self.ACK_TIMEOUT) \ and number_tiles_send == 0: print("------------------------------- RECEIVE PACKET ------------------------------") message = note_table_list.protocol.layer2.roleSend.Receive() print("Message from Server", message) note_table_list.protocol.layer2.event_receive_packet(note_table_list.id, message) # note_table_list.protocol.fragment_session.session_list[0]["session"].state = 'START' except: print("Not fragment state") else: self._log("----------------------- KO -----------------------") self._log("packet was lost {}->{}".format(src_id, dst_id)) if enable_statsct: Statsct.log("packet was lost {}->{} {}".format(src_id, dst_id, packet)) Statsct.add_packet_info(packet,src_id,dst_id, False) count = 0 # if callback != None: args = callback_args + (count,) # XXX need to check. - CA: # [CA] the 'count' is now passed as 'status' in: # SimulLayer2._event_sent_callback(self, transmit_callback, status # the idea is that is transmission fails, at least you can pass # count == 0 (status == 0), and you can do something there. # (in general case, some meta_information need to be sent) #args = callback_args callback(*args) return count
def deliver_packet(self, packet, src_id, dst_id=None, callback=None, callback_args=tuple(), with_hack=False): self._log("----------------------- SEND PACKET -----------------------") lost = self.frame_loss.is_lost(len(packet)) if not lost: self._log("----------------------- OK -----------------------") self._log("send-packet {}->{} {}".format(src_id, dst_id, packet)) if enable_statsct: Statsct.log("send-packet {}->{} {}".format(src_id, dst_id, packet)) clock = self.scheduler.get_clock() Statsct.add_packet_info(clock, packet, src_id, dst_id, True) # if dst_id == None, it is a broadcast link_list = self.get_link_by_id(src_id, dst_id) if not with_hack: count = 0 for link in link_list: count += self.send_packet_on_link(link, packet) else: count = 1 self._send_packetX_hack() # [CA] should be removed else: self._log("----------------------- KO -----------------------") self._log("packet was lost {}->{}".format(src_id, dst_id)) if enable_statsct: Statsct.log("packet was lost {}->{} {}".format(src_id, dst_id, packet)) clock = self.scheduler.get_clock() Statsct.add_packet_info(clock, packet, src_id, dst_id, False) count = 0 # if self.observer is not None: info = {"src":src_id, "dst":dst_id, "packet":packet, "clock": clock, "count":count, "lost":lost, "event-id": self.scheduler._get_event_id() } self.observer.record_packet(info) if callback != None: #XXX: should called by channel after delay args = callback_args+(count,) # XXX need to check. - CA: # [CA] the 'count' is now passed as 'status' in: # SimulLayer2._event_sent_callback(self, transmit_callback, status # the idea is that is transmission fails, at least you can pass # count == 0 (status == 0), and you can do something there. # (in general case, some meta_information need to be sent) #args = callback_args callback(*args) return count
max_packet_size = 1290 #bytes #packet_sizes = [80,160,320,640,1280] #packet_sizes = [320] #packet_sizes = [320,640,1280] packet_sizes = [80, 160, 320, 640] packet_sizes = [80] tile_size = 49 #tile_size = 240 payload_ack_always = tile_size * 2 ack_on_error = True #--------------------------------------------------------------------------- """ Init stastct module """ Statsct.initialize() Statsct.log("Statsct test") Statsct.set_packet_size(data_size) Statsct.set_SF(SF) #--------------------------------------------------------------------------- #no-ack rm0 = RuleManager() rm0.add_context(rule_context, compress_rule, frag_rule3, frag_rule4) rm1 = RuleManager() rm1.add_context(rule_context, compress_rule, frag_rule4, frag_rule3) #ack-on-error if ack_on_error: rm0 = RuleManager() rm0.add_context(rule_context, compress_rule, frag_rule1, frag_rule2)
def frag_generic(rules_filename, packet_loss): # -------------------------------------------------- # General configuration l2_mtu = 72 # bits data_size = 14 # bytes SF = 12 simul_config = { "log": True, } # --------------------------------------------------------------------------- # Configuration packets loss if packet_loss: # Configuration with packet loss in noAck and ack-on-error loss_rate = 15 # in % collision_lambda = 0.1 background_frag_size = 54 loss_config = {"mode": "rate", "cycle": loss_rate} # loss_config = {"mode":"collision", "G":collision_lambda, "background_frag_size":background_frag_size} else: # Configuration without packet loss in noAck and ack-on-error loss_rate = None loss_config = None # --------------------------------------------------------------------------- # Init packet loss if loss_config is not None: simul_config["loss"] = loss_config # --------------------------------------------------------------------------- def make_node(sim, rule_manager, devaddr=None, extra_config={}, role=None): extra_config["unique-peer"] = True node = net_sim_core.SimulSCHCNode(sim, extra_config, role) node.protocol.set_rulemanager(rule_manager) if devaddr is None: devaddr = node.id node.layer2.set_devaddr(devaddr) return node # --------------------------------------------------------------------------- # Statistic module Statsct.initialize() Statsct.log("Statsct test") Statsct.set_packet_size(data_size) Statsct.set_SF(SF) # --------------------------------------------------------------------------- devaddr1 = b"\xaa\xbb\xcc\xdd" devaddr2 = b"\xaa\xbb\xcc\xee" dprint("---------Rules Device -----------") rm0 = RuleManager() # rm0.add_context(rule_context, compress_rule1, frag_rule3, frag_rule4) rm0.Add(device=devaddr1, file=rules_filename) rm0.Print() dprint("---------Rules gw -----------") rm1 = RuleManager() # rm1.add_context(rule_context, compress_rule1, frag_rule4, frag_rule3) rm1.Add(device=devaddr2, file=rules_filename) rm1.Print() # --------------------------------------------------------------------------- # Configuration of the simulation Statsct.get_results() sim = net_sim_core.Simul(simul_config) node0 = make_node(sim, rm0, devaddr1, role="device") # SCHC device node1 = make_node(sim, rm1, devaddr2, role="core-server") # SCHC gw sim.add_sym_link(node0, node1) node0.layer2.set_mtu(l2_mtu) node1.layer2.set_mtu(l2_mtu) # --------------------------------------------------------------------------- # Information about the devices dprint( "-------------------------------- SCHC device------------------------") dprint("SCHC device L3={} L2={} RM={}".format(node0.layer3.L3addr, node0.id, rm0.__dict__)) dprint( "-------------------------------- SCHC gw ---------------------------") dprint("SCHC gw L3={} L2={} RM={}".format(node1.layer3.L3addr, node1.id, rm1.__dict__)) dprint( "-------------------------------- Rules -----------------------------") dprint("rules -> {}, {}".format(rm0.__dict__, rm1.__dict__)) dprint("") # --------------------------------------------------------------------------- # Statistic configuration Statsct.setSourceAddress(node0.id) Statsct.setDestinationAddress(node1.id) # -------------------------------------------------- # Message coap = bytearray(b"`\x12\x34\x56\x00\x1e\x11\x1e\xfe\x80\x00" + b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + b"\x00\x00\x01\xfe\x80\x00\x00\x00\x00\x00" + b"\x00\x00\x00\x00\x00\x00\x00\x00\x02\x16" + b"2\x163\x00\x1e\x00\x00A\x02\x00\x01\n\xb3" + b"foo\x03bar\x06ABCD==Fk=eth0\xff\x84\x01" + b"\x82 &Ehello") # --------------------------------------------------------------------------- # Simnulation node0.protocol.layer3.send_later(1, None, node1.layer3.L3addr, coap) old_stdout = sys.stdout set_debug_output(True) sys.stdout = io.StringIO() sim.run() simulation_output = sys.stdout.getvalue() sys.stdout = old_stdout set_debug_output(False) print(simulation_output) return simulation_output