def example1(env): """ Example at the link level. h1----l1----| r1 |---l2----h2 """ h1 = spn.Host(env, spn.Mac("00.00.00.00.00"), spn.IP("192.0.0.0")) h2 = spn.Host(env, spn.Mac("00.00.00.00.01"), spn.IP("192.0.0.1")) l1 = spn.Link(env, lambda x, y: 3, lambda src, trg, l: 1) l2 = spn.Link(env, lambda x, y: 3, lambda src, trg, l: 1) def host_handler(data): print data def forwarding(datagram, src_mac): if datagram.dst_ip == spn.IP("192.0.0.0"): return spn.Mac("00.00.00.00.00") else: return spn.Mac("00.00.00.00.01") r1 = spn.Router(env, spn.Mac("00.00.00.00.02"), forwarding, lambda x, y: 0.1) spn.physical.plug(l1, r1) spn.physical.plug(l2, r1) spn.physical.plug(l1, h1) spn.physical.plug(l2, h2) send1 = h1.add_transport_handler(host_handler) send2 = h2.add_transport_handler(host_handler) def funct(): yield env.timeout(0) send1("Ciao inviato da host 1", spn.IP("192.0.0.1")) def funct2(): yield env.timeout(1) yield env.timeout(10) send2("Ciao inviato da host 2", spn.IP("192.0.0.0")) r_r = r1.collector_receiveddatagrams r_s = r1.collector_sentdatagrams r_t = r1.collector_processingtime h2_r = h2.collector_receiveddatagrams h1_s = h1.collector_sentdatagrams env.add_collector_functions('router received', r_r, 1) env.add_collector_functions('router sent', r_s, 2) env.add_collector_functions('router proc. time', r_t, 1) env.add_collector_functions('h2 received', h2_r, 1) env.add_collector_functions('h1 sent', h1_s, 1) env.process(funct()) env.process(funct2())
def problem21(env): """ """ rate0 = 4 rate1 = 4 rate2 = 6 rate3 = 2 def trm_delay(name, data): if repr(name) == repr(l0): return rate0 if repr(name) == repr(l1): return rate1 if repr(name) == repr(l2): return rate2 if repr(name) == repr(l3): return rate3 l0 = spn.Link(env, trm_delay) l1 = spn.Link(env, trm_delay) l2 = spn.Link(env, trm_delay) l3 = spn.Link(env, trm_delay) def h_receiver(name, data, trm_delay): # receiver' s handler def _new_handler(name, data, trm_delay): print 'Ricevuto' yield env.timeout(trm_delay) return env.process(_new_handler(name, data, trm_delay)) spn.physical.plug(l0, l1) spn.physical.plug(l1, l2) spn.physical.plug(l2, l3) data = spn.Databits(32000000) #32 Mbits f_send = l0.get_handler() l3.add_handler('rcv', h_receiver) def sender(): yield f_send('sender', data) d = l3.collector_sentdatabits env.add_collector_functions(' link3 receive', d) env.process(sender())
def example(env): """ dhcp_client------l1------dhcp_server """ # -------------------------------------------------------- c_host = spn.Host(env, spn.Mac('aa.aa.aa.aa.aa.aa'), spn.IP('0.0.0.0')) s_host = spn.Host(env, spn.Mac('bb.bb.bb.bb.bb.bb'), spn.IP('192.168.1.1')) client = spn.DHCP_client(env, c_host) server = spn.DHCP_server(env, s_host) def choose_ip_server(lista_ip): if len(lista_ip) > 0: return lista_ip.pop(0) else: return None def choose_ip_for_client(): return '123' server.add_function_choose(choose_ip_for_client) client.add_function_choose(choose_ip_server) l1 = spn.Link(env, lambda x, y: 1, lambda src, trg, l: 1) spn.physical.plug(l1, c_host) spn.physical.plug(l1, s_host) def funct(): yield env.timeout(1) client.send_DHCP_DISCOVERED() env.process(funct())
def example2(env): """" example figure 5.17 p. 492 (Computer Networking, 6th edition, Kurose-Ross). c----l1-----| | b----l2--- |so|---l4---ro | a----l3-----| """ env = spn.Environment() host_c = spn.Host(env, spn.Mac('1A:23:F9:CD:06:9B'), spn.IP('222.222.222.220')) host_b = spn.Host(env, spn.Mac('5C:66:AB:90:75:B1'), spn.IP('222.222.222.221')) host_a = spn.Host(env, spn.Mac('49:BD:D2:C7:56:2A'), spn.IP('222.222.222.222')) l1 = spn.Link(env, trm_delay=lambda name, data: 1, prg_delay=lambda s, r, l: 1) l2 = spn.Link(env, trm_delay=lambda name, data: 1, prg_delay=lambda s, r, l: 1) l3 = spn.Link(env, trm_delay=lambda name, data: 1, prg_delay=lambda s, r, l: 1) l4 = spn.Link(env, trm_delay=lambda name, data: 1, prg_delay=lambda s, r, l: 1) s0 = spn.Switch(env) def frw(datagram, mac): print mac if mac == spn.Mac('88:B2:2F:54:1A:0F'): print 'forwanding' return spn.Mac('5C:66:AB:90:75:B1') else: print 'no fornarding' r0 = spn.Router(env, spn.Mac('88:B2:2F:54:1A:0F'), frw) spn.physical.plug(host_c, l1) spn.physical.plug(l1, s0) spn.physical.plug(host_b, l2) spn.physical.plug(l2, s0) spn.physical.plug(host_a, l3) spn.physical.plug(l3, s0) spn.physical.plug(r0, l4) spn.physical.plug(l4, r0) def tcp_handler(segment): print 'attivato tcp' def h_receiver(name, data, trm_delay): # receiver' s handler def _new_handler(name, data, trm_delay): yield env.timeout(trm_delay) return env.process(_new_handler(name, data, trm_delay)) f_send = host_c.add_transport_handler(tcp_handler) host_b.add_handler('rcv', h_receiver) def rcv_r(): pass host_c.default_gateway = spn.Mac('88:B2:2F:54:1A:0F') def sender(): yield env.timeout(0) f_send('ciao da host 1', spn.IP('222.222.222.220')) env.process(sender())
def example2(env): """ This example simulates the transfer of a packet of 4 million bytes, from A to B. A /---l1----/-----l2----/----l3----/ B The path has three link of rates: r1= 500 Kbps, r2 = 2Mbps, r3 = 1 Mbps. Make reference to exercise R19 at page 95 (Computer Networking, 6th edition, Kurose_Ross) """ packet_size = 40000000 # 40 Mbit trm_1 = 2000000.0 #bps trm_2 = 500000.0 #bps trm_3 = 1000000.0 #bps prg_delay = lambda src, dst, link: 0 # no propagation delay packet = spn.Databits(packet_size) def trm_d(name, data): if (name == repr(l1)): return len(data) / trm_1 if (name == repr(l2)): return len(data) / trm_2 if (name == repr(l3)): return len(data) / trm_3 l1 = spn.Link(env, trm_d, prg_delay) l2 = spn.Link(env, trm_d, prg_delay) l3 = spn.Link(env, trm_d, prg_delay) spn.physical.plug(l1, l2) spn.physical.plug(l2, l3) def h_receiver(name, data, trm_delay): def _new_handler(name, data, trm_delay): yield env.timeout(trm_delay) return env.process(_new_handler(name, data, trm_delay)) l3.add_handler('receiver', h_receiver) f_send = l1.get_handler() def sender(): yield f_send('sender', packet, trm_delay=0) clt_l1 = l1.collector_sentdatabits clt_l2 = l2.collector_sentdatabits clt_l3 = l3.collector_sentdatabits env.add_collector_functions('l1 sent data', clt_l1, 2) env.add_collector_functions('l2 sent data', clt_l2, 2) env.add_collector_functions('l3 sent data', clt_l3, 2) env.process(sender())
def example4(env): """ This example simulates the transfer of a list of 100 packets, of 1000 bits everyone, through a link with -distance 2.5 km, -propagation speed 2.5*10^8 m/s -transmission rate 2 Mbps. -probabilità perdita di pacchetti= 0.3 -funzione di errore nei pacchetti A ---link-----B """ packet_size = 1000 # bit distance = 2500000 # m speed_propagation = 25000000 # m/s transmission_rate = 2000 #bps packet = spn.Databits(packet_size) list_packet = [] trm_delay = lambda name, data: packet_size / float(transmission_rate ) # 0.5 s prg_delay = lambda src, trg, link: distance / float(speed_propagation ) #0.1 s loss_prob = lambda src, trg, link: 0.2 f_error = lambda src, trg, data, link: data.set_error(True) link = spn.Link(env, trm_delay, prg_delay, loss_prob, f_error) def h_receiver(name, data, trm_delay): # receiver' s handler def _new_handler(name, data, trm_delay): yield env.timeout(trm_delay) return env.process(_new_handler(name, data, trm_delay)) link.add_handler('rcv', h_receiver) f_send = link.get_handler() # handler of link for i in range(10): list_packet.append(packet) def sender(): while (len(list_packet) > 0): packet = list_packet.pop(0) yield f_send('sender', packet) d = link.collector_sentdatabits c = link.collector_lostdatabits env.add_collector_functions('link sent Data', d, 1) env.add_collector_functions('link lost Data', c, 1) def somma(l): return sum(l) def massimo(l): return max(l) # env.set_stat_file_name('PHY.EX4') # env.add_statistical_index( np.mean ) # env.add_statistical_index( massimo ) # env.add_statistical_index( somma ) #env.set_stat_file_name('stat_example4') env.process(sender())