def send_message(): #read parameters if len(sys.argv) == 2: ruleId = str(sys.argv[1]) fileToSend = "testfile.txt" elif len(sys.argv) == 3: ruleId = str(sys.argv[1]) fileToSend = str(sys.argv[2]) else: ruleId = "1" fileToSend = "testFile.txt" ruleId = int(ruleId) print("ruleId: ", ruleId, "fileToSend: ", fileToSend) #create protocol instance scheduler = Scheduler() schc = SCHC_server(scheduler) '''Message payload generation''' '''payload = bytearray(range(1, 20+1)) print("Payload") print(binascii.hexlify(payload)) print("Payload size:", len(payload)) print("")''' #read file for payload file = open(fileToSend, 'r') print('file: ', file) payload = file.read().encode() print("Payload") print(binascii.hexlify(payload)) print("Payload size:", len(payload)) print("") #send message schc.sendMessage(payload, ruleId) scheduler.run()
def __init__(self, simul_config={}): self.simul_config = simul_config self.node_table = {} self.link_set = set() self.event_id = 0 self.scheduler = Scheduler() self.log_file = None self.frame_loss = ConditionalTrue( **self.simul_config.get("loss", {"mode": "cycle"}))
def __init__(self, simul_config = {}): self.ACK_SUCCESS = "ACK_SUCCESS" self.ACK_FAILURE = "ACK_FAILURE" self.RECEIVER_ABORT = "RECEIVER_ABORT" self.SEND_ALL_1 = "SEND_ALL_1" self.WAITING_FOR_ACK = "WAITING_FOR_ACK" self.ACK_TIMEOUT = "ACK_TIMEOUT" self.simul_config = simul_config self.node_table = {} self.link_set = set() self.event_id = 0 self.scheduler = Scheduler() self.log_file = None self.frame_loss = ConditionalTrue( **self.simul_config.get("loss", {"mode":"cycle"}))
class Simul: def __init__(self, simul_config={}): self.simul_config = simul_config self.node_table = {} self.link_set = set() self.event_id = 0 self.scheduler = Scheduler() self.log_file = None self.frame_loss = ConditionalTrue( **self.simul_config.get("loss", {"mode": "cycle"})) def set_log_file(self, filename): self.log_file = open(filename, "w") def log(self, name, message): # XXX: put Soichi type of logging if not self.simul_config.get("log", False): return line = "{} [{}] ".format(self.scheduler.get_clock(), name) + message print(line) if self.log_file != None: self.log_file.write(line + "\n") def _log(self, message): self.log("sim", message) # XXX:optimize def get_link_by_id(self, src_id=None, dst_id=None): result = [] for link in sorted(self.link_set): if (((src_id is None) or (link.from_id == src_id)) and ((dst_id is None) or (link.to_id == dst_id))): result.append(link) return result def send_packet(self, packet, src_id, dst_id=None, callback=None, callback_args=tuple()): if not self.frame_loss.check(): self._log("send-packet {}->{} {}".format(src_id, dst_id, packet)) # 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("packet was lost {}->{}".format(src_id, dst_id)) 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 send_packet_on_link(self, link, packet): node_to = self.node_table[link.to_id] node_to.event_receive(link.from_id, packet) return 1 # 1 -> one packet was sent def add_link(self, from_node, to_node, delay=1): """Create a link from from_id to to_id. Transmitted packets on the link will have a the specified delay before being received""" assert (from_node.id in self.node_table and to_node.id in self.node_table) link = Link(from_id=from_node.id, to_id=to_node.id, delay=delay) # XXX: check not another link there with same from_id, same to_id self._log("add-link {}->{}".format(from_node.id, to_node.id)) self.link_set.add(link) def add_sym_link(self, from_node, to_node, delay=1): """Create a symmetrical link between the two nodes, by creating two unidirectional links""" self.add_link(from_node, to_node, delay) self.add_link(to_node, from_node, delay) # don't call: automatically called by Node(...) def _add_node(self, node): """Internal: add a node in the node_table (automatically called by Node constructor)""" assert node.id not in self.node_table self.node_table[node.id] = node def run(self): self.scheduler.run()
# C.A. 2018 from simsched import SimulScheduler scheduler = SimulScheduler() cancel_event_id = None def callback(arg1, arg2, scheduler=scheduler): global cancel_event_id if cancel_event_id != None: canceled_event = scheduler.cancel_event(cancel_event_id) else: canceled_event = None print("clock=%s callback, argw=%s next_event_time=%s canceled=%s" % (scheduler.get_clock(), repr( (arg1, arg2)), scheduler.get_next_event_time(), canceled_event)) if arg1 == "1": scheduler.add_event(1, callback, ("2", "two")) cancel_event_id = scheduler.add_event(1, callback, ("none", "none")) scheduler.add_event(3, callback, ("4", "four")) scheduler.add_event(1, callback, ("1", "one")) scheduler.run()
class Simul: def __init__(self, simul_config = {}): self.ACK_SUCCESS = "ACK_SUCCESS" self.ACK_FAILURE = "ACK_FAILURE" self.RECEIVER_ABORT = "RECEIVER_ABORT" self.SEND_ALL_1 = "SEND_ALL_1" self.WAITING_FOR_ACK = "WAITING_FOR_ACK" self.ACK_TIMEOUT = "ACK_TIMEOUT" self.simul_config = simul_config self.node_table = {} self.link_set = set() self.event_id = 0 self.scheduler = Scheduler() self.log_file = None self.frame_loss = ConditionalTrue( **self.simul_config.get("loss", {"mode":"cycle"})) def set_log_file(self, filename): self.log_file = open(filename, "w") def log(self, name, message): # XXX: put Soichi type of logging if not self.simul_config.get("log", False): return line = "{} [{}] ".format(self.scheduler.get_clock(), name) + message print(line) if self.log_file != None: self.log_file.write(line+"\n") def _log(self, message): self.log("sim", message) # XXX:optimize def get_link_by_id(self, src_id=None, dst_id=None): result = [] for link in sorted(self.link_set): if ( ((src_id is None) or (link.from_id == src_id)) and ((dst_id is None) or (link.to_id == dst_id))): result.append(link) return result 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 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 send_packet_on_link(self, link, packet): node_to = self.node_table[link.to_id] node_to.event_receive(link.from_id, packet) return 1 # 1 -> one packet was sent def add_link(self, from_node, to_node, delay=1): """Create a link from from_id to to_id. Transmitted packets on the link will have a the specified delay before being received""" assert (from_node.id in self.node_table and to_node.id in self.node_table) link = Link(from_id=from_node.id, to_id=to_node.id, delay=delay) # XXX: check not another link there with same from_id, same to_id self._log("add-link {}->{}".format(from_node.id, to_node.id)) self.link_set.add(link) def add_sym_link(self, from_node, to_node, delay=1): """Create a symmetrical link between the two nodes, by creating two unidirectional links""" self.add_link(from_node, to_node, delay) self.add_link(to_node, from_node, delay) # don't call: automatically called by Node(...) def _add_node(self, node): """Internal: add a node in the node_table (automatically called by Node constructor)""" assert node.id not in self.node_table self.node_table[node.id] = node def run(self): self.scheduler.run()
import sys sys.path.insert(0, ".") sys.path.insert(0, "..") from simsched import SimulScheduler if sys.implementation.name != "micropython": import pytest scheduler = SimulScheduler() cancel_event_id = None def callback(arg1, arg2, scheduler=scheduler): global cancel_event_id if cancel_event_id != None: canceled_event = scheduler.cancel_event(cancel_event_id) else: canceled_event = None print("clock=%s callback, argw=%s next_event_time=%s canceled=%s" % (scheduler.get_clock(), repr( (arg1, arg2)), scheduler.get_next_event_time(), canceled_event)) if arg1 == "1": scheduler.add_event(1, callback, ("2", "two")) cancel_event_id = scheduler.add_event(1, callback, ("none", "none")) def test_simsched_01(): scheduler.add_event(3, callback, ("4", "four")) scheduler.add_event(1, callback, ("1", "one")) scheduler.run()