def test_send_single_packet_with_delay(self): """Test fetching a single content object over the simulation bus""" delay_func = lambda packet: 0.5 self.fetchiface = self.simulation_bus.add_interface("fetch", delay_func=delay_func) self.icn_forwarder1 = ICNForwarder(port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("icnfwd1", delay_func=delay_func)]) self.icn_forwarder2 = ICNForwarder(port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("icnfwd2", delay_func=delay_func)]) self.simulation_bus.start_process() self.icn_forwarder1.start_forwarder() self.icn_forwarder2.start_forwarder() fid1 = self.icn_forwarder1.linklayer.faceidtable.get_or_create_faceid(AddressInfo("icnfwd2", 0)) self.icn_forwarder1.icnlayer.fib.add_fib_entry(Name("/test"), [fid1]) self.icn_forwarder2.icnlayer.cs.add_content_object(Content("/test/data", "HelloWorld"), static=True) interest = Interest("/test/data") wire_data = self.encoder.encode(interest) self.fetchiface.send(wire_data, "icnfwd1") res, src = self.fetchiface.receive() self.assertEqual(src, "icnfwd1") c = self.encoder.decode(res) self.assertEqual(c, Content("/test/data", "HelloWorld"))
def measure_breadth_scaling( n: int, ageing: float, random_startup_delay: bool) -> Tuple[int, float, float, bool]: forwarders: List[ICNForwarder] = [] for i in range(n): forwarders.append( ICNForwarder(0, encoder=NdnTlvEncoder(), routing=True)) forwarders.append(ICNForwarder(0, encoder=NdnTlvEncoder(), routing=True)) forwarders.append(ICNForwarder(0, encoder=NdnTlvEncoder(), routing=True)) repo = ICNDataRepository(None, Name('/picn/routing/testrepo'), port=0, encoder=NdnTlvEncoder()) for i in range(n): forwarders[i].routinglayer._peers.append( forwarders[-1].linklayer.sock.getsockname()) forwarders[-2].routinglayer._peers.append( forwarders[i].linklayer.sock.getsockname()) for f in forwarders: f.routinglayer._ageing_interval = ageing fid = forwarders[-1].linklayer.get_or_create_fid( repo.linklayer.sock.getsockname(), static=True) rib: BaseRoutingInformationBase = forwarders[-1].data_structs['rib'] rib.insert(Name('/picn/routing/testrepo'), fid, 1) forwarders[-1].data_structs['rib'] = rib repo.repo.add_content(Name('/picn/routing/testrepo/testcontent'), 'testcontent') fetchaddr = forwarders[-2].linklayer.sock.getsockname() fetch = Fetch(fetchaddr[0], fetchaddr[1], encoder=NdnTlvEncoder()) time, data = measure(fetch, repo, forwarders, n * ageing * 3, random_startup_delay) return n, ageing, time, data == 'testcontent'
def test_send_single_packet_with_packet_loss(self): """Test fetching a single content object over the simulation bus""" packet_loss_func = lambda packet: True self.fetchiface = self.simulation_bus.add_interface("fetch", packet_loss_func=packet_loss_func) self.icn_forwarder1 = ICNForwarder(port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("icnfwd1", packet_loss_func=packet_loss_func)]) self.icn_forwarder2 = ICNForwarder(port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("icnfwd2", packet_loss_func=packet_loss_func)]) self.simulation_bus.start_process() self.icn_forwarder1.start_forwarder() self.icn_forwarder2.start_forwarder() fid1 = self.icn_forwarder1.linklayer.faceidtable.get_or_create_faceid(AddressInfo("icnfwd2", 0)) self.icn_forwarder1.icnlayer.fib.add_fib_entry(Name("/test"), [fid1]) self.icn_forwarder2.icnlayer.cs.add_content_object(Content("/test/data", "HelloWorld"), static=True) interest = Interest("/test/data") wire_data = self.encoder.encode(interest) self.fetchiface.send(wire_data, "icnfwd1") try: self.fetchiface.receive(timeout=4) except queue.Empty: pass else: self.fail()
def test_bandwidth_limit(self): #TODO better test here """Simple Test for checking the bandwidth limit""" self.icn_forwarder1 = ICNForwarder(port=0, encoder=self.encoder_type(), interfaces=[ self.simulation_bus.add_interface("icnfwd1", max_bandwidth=1)]) self.icn_forwarder2 = ICNForwarder(port=0, encoder=self.encoder_type(), interfaces=[ self.simulation_bus.add_interface("icnfwd2", max_bandwidth=1)]) self.icn_forwarder1.start_forwarder() self.icn_forwarder2.start_forwarder() self.simulation_bus.start_process() mgmt_client1 = MgmtClient(self.icn_forwarder1.mgmt.mgmt_sock.getsockname()[1]) mgmt_client1.add_face("icnfwd2", None, 0) mgmt_client1.add_forwarding_rule(Name("/test"), [0]) mgmt_client2 = MgmtClient(self.icn_forwarder2.mgmt.mgmt_sock.getsockname()[1]) mgmt_client2.add_new_content(Name("/test/data"), "HelloWorld") interest = Interest("/test/data") wire_data = self.encoder.encode(interest) self.fetchiface.send(wire_data, "icnfwd1") res, src = self.fetchiface.receive() self.assertEqual(src, "icnfwd1") c = self.encoder.decode(res) self.assertEqual(c, Content("/test/data", "HelloWorld")) mgmt_client1.shutdown() mgmt_client2.shutdown()
def setUp(self): self.encoder_type = self.get_encoder() self.simulation_bus = SimulationBus(packetencoder=self.encoder_type()) self.fetchiface = self.simulation_bus.add_interface("fetch") self.encoder = self.encoder_type() self.icn_forwarder1 = ICNForwarder(port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("icnfwd1")], log_level=255) self.icn_forwarder2 = ICNForwarder(port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("icnfwd2")], log_level=255)
def setUp(self): self.encoder = self.get_encoder() self.forwarder1 = ICNForwarder(0, encoder=self.get_encoder(), log_level=255) self.forwarder2 = ICNForwarder(0, encoder=self.get_encoder(), log_level=255) self.forwarder1_port = self.forwarder1.linklayer.interfaces[ 0].get_port() self.forwarder2_port = self.forwarder2.linklayer.interfaces[ 0].get_port() self.testSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.testSock.bind(("0.0.0.0", 0))
def _setup_mobile_nodes(self): """configure the mobile nodes""" for node in self._mobile_nodes: node.forwarder = ICNForwarder( 0, encoder=SimpleStringEncoder(), routing=True, interfaces=[ self._simulation_bus.add_interface(f"car{node.node_id}") ]) node.fetch = Fetch( f"car{node.node_id}", None, 255, SimpleStringEncoder(), interfaces=[ self._simulation_bus.add_interface(f"ftcar{node.node_id}") ]) node.mgmt_tool = MgmtClient( node.forwarder.mgmt.mgmt_sock.getsockname()[1]) for stationary_node in self._stationary_nodes: car_face_id = node.forwarder.linklayer.faceidtable.get_or_create_faceid( AddressInfo(f"rsu{stationary_node.node_id}", 0)) self.to_rsu_faces[stationary_node.node_id][ node.node_id] = car_face_id rsu_face_id = node.forwarder.linklayer.faceidtable.get_or_create_faceid( AddressInfo(f"car{stationary_node.node_id}", 0)) self.to_car_faces[stationary_node.node_id][ node.node_id] = rsu_face_id
def setUp(self): self.encoder_type = SimpleStringEncoder() self.simulation_bus = SimulationBus(packetencoder=self.encoder_type) chunk_size = 4 self.chunkifyer = SimpleContentChunkifyer(chunk_size) self.car = ICNForwarder( 0, encoder=self.encoder_type, routing=True, interfaces=[self.simulation_bus.add_interface("car")]) self.fetch_tool_car = Fetch( "car", None, 255, self.encoder_type, interfaces=[self.simulation_bus.add_interface("ftcar")]) self.mgmt_client_car = MgmtClient( self.car.mgmt.mgmt_sock.getsockname()[1]) self.rsus = [] self.fetch_tools = [] self.mgmt_clients = [] for i in range(4): self.rsus.append( NFNForwarderData( 0, encoder=self.encoder_type, interfaces=[self.simulation_bus.add_interface(f"rsu{i}")], chunk_size=chunk_size, num_of_forwards=0)) self.fetch_tools.append( Fetch(f"rsu{i}", None, 255, self.encoder_type, interfaces=[self.simulation_bus.add_interface(f"ft{i}") ])) self.rsus[i].nfnlayer.optimizer = EdgeComputingOptimizer( self.rsus[i].icnlayer.cs, self.rsus[i].icnlayer.fib, self.rsus[i].icnlayer.pit, self.rsus[i].linklayer.faceidtable) self.mgmt_clients.append( MgmtClient(self.rsus[i].mgmt.mgmt_sock.getsockname()[1])) self.fetch_tools[i].timeoutpreventionlayer.timeout_interval = 30
def setUp(self): self.encoder_type = SimpleStringEncoder() self.simulation_bus = SimulationBus(packetencoder=self.encoder_type) chunk_size = 8192 self.chunkifyer = SimpleContentChunkifyer(chunk_size) # Initialize two cars self.cars = [] self.fetch_tool_cars = [] self.mgmt_client_cars = [] for i in range(2): self.cars.append( ICNForwarder( 0, encoder=self.encoder_type, routing=True, interfaces=[self.simulation_bus.add_interface(f"car{i}")])) self.fetch_tool_cars.append( Fetch(f"car{i}", None, 255, self.encoder_type, interfaces=[ self.simulation_bus.add_interface(f"ftcar{i}") ])) self.mgmt_client_cars.append( MgmtClient(self.cars[i].mgmt.mgmt_sock.getsockname()[1])) self.cars[i].icnlayer.cs.set_cs_timeout(40) # Initialize RSUs self.rsus = [] self.fetch_tools = [] self.mgmt_clients = [] for i in range(3): self.rsus.append( NFNForwarderData( 0, encoder=self.encoder_type, interfaces=[self.simulation_bus.add_interface(f"rsu{i}")], chunk_size=chunk_size, num_of_forwards=1, ageing_interval=10)) self.fetch_tools.append( Fetch(f"rsu{i}", None, 255, self.encoder_type, interfaces=[self.simulation_bus.add_interface(f"ft{i}") ])) self.rsus[i].nfnlayer.optimizer = EdgeComputingOptimizer( self.rsus[i].icnlayer.cs, self.rsus[i].icnlayer.fib, self.rsus[i].icnlayer.pit, self.rsus[i].linklayer.faceidtable) self.mgmt_clients.append( MgmtClient(self.rsus[i].mgmt.mgmt_sock.getsockname()[1])) self.fetch_tools[i].timeoutpreventionlayer.timeout_interval = 40 self.rsus[i].icnlayer.cs.set_cs_timeout(60)
def setUp(self): self.f9000 = ICNForwarder(9000, encoder=NdnTlvEncoder(), routing=True, peers=[('127.0.0.1', 9001), ('127.0.0.1', 9002), ('127.0.0.1', 9090)]) self.f9001 = ICNForwarder(9001, encoder=NdnTlvEncoder(), routing=True, peers=[('127.0.0.1', 9000), ('127.0.0.1', 9002), ('127.0.0.1', 9003), ('127.0.0.1', 9004)]) self.f9002 = ICNForwarder(9002, encoder=NdnTlvEncoder(), routing=True, peers=[('127.0.0.1', 9000), ('127.0.0.1', 9001), ('127.0.0.1', 9004)]) self.f9003 = ICNForwarder(9003, encoder=NdnTlvEncoder(), routing=True, peers=[('127.0.0.1', 9001), ('127.0.0.1', 9004)]) self.f9004 = ICNForwarder(9004, encoder=NdnTlvEncoder(), routing=True, peers=[('127.0.0.1', 9001), ('127.0.0.1', 9002), ('127.0.0.1', 9003)]) os.makedirs('/tmp/test_repo', exist_ok=True) f = open('/tmp/test_repo/helloworld', 'w') f.write('Hello, World!\n') f.close() self.repo = ICNDataRepository('/tmp/test_repo', Name('/testrepo'), port=9090, encoder=NdnTlvEncoder()) self.fetch = Fetch('127.0.0.1', 9004, encoder=NdnTlvEncoder()) # Create RIB entry for the repository (with Python weirdness) repo_fid: int = self.f9000.linklayer.faceidtable.get_or_create_faceid(AddressInfo(('127.0.0.1', 9090), 0)) rib: BaseRoutingInformationBase = self.f9000.routinglayer.rib rib.insert(Name('/testrepo'), repo_fid, distance=1, timeout=None)
def test_fetching_content_from_second_repo_after_nack(self): """Test sending an interest to forwarder with no matching content, choose second route to fetch content""" self.forwarder2: ICNForwarder = ICNForwarder(0, encoder=self.get_encoder(), log_level=255) self.ICNRepo.start_repo() self.forwarder.start_forwarder() self.forwarder2.start_forwarder() #check for nack on first route self.mgmtClient = MgmtClient(self.forwarder_port) self.mgmtClient.add_face("127.0.0.1", self.forwarder2.linklayer.interfaces[0].get_port(), 0) data = self.mgmtClient.add_forwarding_rule(Name("/test/data"), [0]) nack = self.fetch.fetch_data(Name("/test/data/f3")) self.assertEqual(nack, "Received Nack: " + NackReason.NO_ROUTE.value) time.sleep(0.1) #install second forwarding rule and check for result. data = self.mgmtClient.add_face("127.0.0.1", self.repo_port, 0) self.mgmtClient.add_forwarding_rule(Name("/test"), [2]) time.sleep(0.1) fetch2 = Fetch("127.0.0.1", self.forwarder_port) content = self.fetch.fetch_data(Name("/test/data/f3")) self.assertEqual(content, self.data3) self.forwarder2.stop_forwarder()
def setUp(self): self.data1 = "data1" self.data2 = 'A' * 5000 self.data3 = 'B' * 20000 self.path = "/tmp/repo_unit_test" try: os.stat(self.path) except: os.mkdir(self.path) with open(self.path + "/f1", 'w+') as content_file: content_file.write(self.data1) with open(self.path + "/f2", 'w+') as content_file: content_file.write(self.data2) with open(self.path + "/f3", 'w+') as content_file: content_file.write('B' * 20000) self.ICNRepo: ICNDataRepository = ICNDataRepository("/tmp/repo_unit_test", Name("/test/data"), port=0, encoder=self.get_encoder(), log_level=255) self.forwarder: ICNForwarder = ICNForwarder(port=0, encoder=self.get_encoder(), log_level=255) self.repo_port = self.ICNRepo.linklayer.interfaces[0].get_port() self.forwarder_port = self.forwarder.linklayer.interfaces[0].get_port() self.fetch = Fetch("127.0.0.1", self.forwarder_port, encoder=self.get_encoder())
simulation_bus = SimulationBus(log_level=0) # Use BasicStringEncoder repo0 = ICNDataRepository( port=0, prefix=Name('/test/t1'), foldername=None, interfaces=[simulation_bus.add_interface('repo0')], log_level=255) # Initialize repository 0 repo1 = ICNDataRepositorySession( port=0, prefix=Name('/test/t2'), foldername=None, interfaces=[simulation_bus.add_interface('repo1')], log_level=0) # Initialize repository 1 (this one has sessions) fw0 = ICNForwarder(port=0, log_level=255, interfaces=[simulation_bus.add_interface('fw0')], node_name='forwarder0') # Initialize forwarder 0 fw1 = ICNForwarder(port=0, log_level=0, interfaces=[simulation_bus.add_interface('fw1')], node_name='forwarder1') # Initialize forwarder 1 mgmt_repo0 = MgmtClient( repo0.mgmt.mgmt_sock.getsockname()[1]) # Mgmt client for repository 0 mgmt_repo1 = MgmtClient( repo1.mgmt.mgmt_sock.getsockname()[1]) # Mgmt client for repository 1 mgmt_fw0 = MgmtClient( fw0.mgmt.mgmt_sock.getsockname()[1]) # Mgmt client for forwarder 0 mgmt_fw1 = MgmtClient( fw1.mgmt.mgmt_sock.getsockname()[1]) # Mgmt client for forwarder 1
class cases_ICNForwarder(object): """Test the ICN Forwarder""" @abc.abstractmethod def get_encoder(self): """returns the encoder to be used """ def setUp(self): self.encoder = self.get_encoder() self.forwarder1 = ICNForwarder(0, encoder=self.get_encoder(), log_level=255) self.forwarder2 = ICNForwarder(0, encoder=self.get_encoder(), log_level=255) self.forwarder1_port = self.forwarder1.linklayer.interfaces[ 0].get_port() self.forwarder2_port = self.forwarder2.linklayer.interfaces[ 0].get_port() self.testSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.testSock.bind(("0.0.0.0", 0)) def tearDown(self): self.testSock.close() self.forwarder1.stop_forwarder() self.forwarder2.stop_forwarder() pass def test_ICNForwarder_simple_find_content_one_node(self): """Test a simple forwarding scenario, getting content from a Node""" self.forwarder1.start_forwarder() # new content testMgmtSock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) testMgmtSock1.connect(("127.0.0.1", self.forwarder1_port)) testMgmtSock1.send( "GET /icnlayer/newcontent/%2Ftest%2Fdata%2Fobject:HelloWorld HTTP/1.1\r\n\r\n" .encode()) data = testMgmtSock1.recv(1024) testMgmtSock1.close() time.sleep(3) self.assertEqual( data.decode(), "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newcontent OK\r\n" ) #create test content name = Name("/test/data/object") test_content = Content(name, content="HelloWorld") cs_fwd1 = self.forwarder1.icnlayer.cs self.assertEqual( cs_fwd1.find_content_object(name).content, test_content) #create interest interest = Interest("/test/data/object") encoded_interest = self.encoder.encode(interest) #send interest self.testSock.sendto(encoded_interest, ("127.0.0.1", self.forwarder1_port)) #receive content encoded_content, addr = self.testSock.recvfrom(8192) content = self.encoder.decode(encoded_content) self.assertEqual(content, test_content) def test_ICNForwarder_simple_find_content_two_nodes(self): """Test a simple forwarding scenario with one additional node forwarding the data""" self.forwarder1.start_forwarder() self.forwarder2.start_forwarder() #client <---> node1 <---> node2 #create a face testMgmtSock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) testMgmtSock1.connect(("127.0.0.1", self.forwarder1_port)) port_to = self.forwarder2_port testMgmtSock1.send(("GET /linklayer/newface/127.0.0.1:" + str(port_to) + ":0 HTTP/1.1\r\n\r\n").encode()) data = testMgmtSock1.recv(1024) testMgmtSock1.close() self.assertEqual( data.decode(), "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newface OK:0\r\n" ) self.assertEqual( self.forwarder1.linklayer.faceidtable.get_face_id( AddressInfo(("127.0.0.1", self.forwarder2_port), 0)), 0) #register a prefix testMgmtSock2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) testMgmtSock2.connect(("127.0.0.1", self.forwarder1_port)) testMgmtSock2.send( "GET /icnlayer/newforwardingrule/%2Ftest%2Fdata:0 HTTP/1.1\r\n\r\n" .encode()) data = testMgmtSock2.recv(1024) testMgmtSock2.close() self.assertEqual( data.decode(), "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newforwardingrule OK:0\r\n" ) self.assertEqual( self.forwarder1.icnlayer.fib.find_fib_entry( Name("/test/data")).faceid, [0]) # new content testMgmtSock3 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) testMgmtSock3.connect(("127.0.0.1", self.forwarder2_port)) testMgmtSock3.send( "GET /icnlayer/newcontent/%2Ftest%2Fdata%2Fobject:HelloWorld HTTP/1.1\r\n\r\n" .encode()) data = testMgmtSock3.recv(1024) testMgmtSock3.close() self.assertEqual( data.decode(), "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newcontent OK\r\n" ) #create test content name = Name("/test/data/object") test_content = Content(name, content="HelloWorld") cs_fwd2 = self.forwarder2.icnlayer.cs self.assertEqual( cs_fwd2.find_content_object(name).content, test_content) #create interest interest = Interest("/test/data/object") encoded_interest = self.encoder.encode(interest) #send interest self.testSock.sendto(encoded_interest, ("127.0.0.1", self.forwarder1_port)) #receive content encoded_content, addr = self.testSock.recvfrom(8192) content = self.encoder.decode(encoded_content) self.assertEqual(content, test_content) time.sleep(2) self.assertEqual(self.forwarder1.icnlayer.pit.get_container_size(), 0)
def measure_repo_hopping(run: int, routing_interval: float, hopping_interval: float, lease_time: float, edge_traverse: bool = False): testname = f'repo_hopping{"_edge_traverse" if edge_traverse else ""}' print( f'{testname} routing interval={routing_interval}, hopping interval={hopping_interval}, lease time=' + f'{lease_time}, run {run}') global repo, lock, running manager = multiprocessing.Manager() autoconfig_edgeprefix: List[Tuple[Name, bool]] = [(Name('/edge'), False)] nodes: Dict[int, ICNForwarder] = dict() ports: Dict[int, Tuple[str, int]] = dict() edgeports: List[int] = list() # Initialize core nodes for c in [00, 10, 20, 30]: nodes[c] = ICNForwarder(9000 + c, encoder=NdnTlvEncoder(), routing=True, peers=[]) ports[c] = nodes[c].linklayer.sock.getsockname() # Initialize edge nodes for e in [11, 12, 13, 21, 22, 23, 31, 32, 33]: nodes[e] = ICNForwarder(9000 + e, encoder=NdnTlvEncoder(), routing=True, peers=[], autoconfig=True) ports[e] = nodes[e].linklayer.sock.getsockname() edgeports.append(ports[e][1]) # Assign routing peers after the OS assigned UDP ports. Each node knows the nodes one layer "beneath" itself # in above graph as its routing peers. nodes[00].routinglayer._peers = [ports[10], ports[20], ports[30]] nodes[10].routinglayer._peers = [ports[11], ports[12], ports[13]] nodes[20].routinglayer._peers = [ports[21], ports[22], ports[23]] nodes[30].routinglayer._peers = [ports[31], ports[32], ports[33]] # Set up faces and static FIB of core00 node. fid00to10: int = nodes[00].linklayer.get_or_create_fid(ports[10], static=True) fid00to20: int = nodes[00].linklayer.get_or_create_fid(ports[20], static=True) fid00to30: int = nodes[00].linklayer.get_or_create_fid(ports[30], static=True) fib00: BaseForwardingInformationBase = nodes[00].data_structs['fib'] if edge_traverse: fib00.add_fib_entry(Name('/edge'), fid00to10, static=True) fib00.add_fib_entry(Name('/edge'), fid00to20, static=True) fib00.add_fib_entry(Name('/edge'), fid00to30, static=True) nodes[00].data_structs['fib'] = fib00 nodes[00].data_structs['rib'] = TreeRoutingInformationBase( manager, shortest_only=False) # Set up faces and static FIB of core10 node. fid10to11: int = nodes[10].linklayer.get_or_create_fid(ports[11], static=True) fid10to12: int = nodes[10].linklayer.get_or_create_fid(ports[12], static=True) fid10to13: int = nodes[10].linklayer.get_or_create_fid(ports[13], static=True) fib10: BaseForwardingInformationBase = nodes[10].data_structs['fib'] if edge_traverse: fib10.add_fib_entry(Name('/edge'), fid10to11, static=True) fib10.add_fib_entry(Name('/edge'), fid10to12, static=True) fib10.add_fib_entry(Name('/edge'), fid10to13, static=True) nodes[10].data_structs['fib'] = fib10 nodes[10].data_structs['rib'] = TreeRoutingInformationBase( manager, shortest_only=False) # Set up faces and static FIB of core20 node. fid20to21: int = nodes[20].linklayer.get_or_create_fid(ports[21], static=True) fid20to22: int = nodes[20].linklayer.get_or_create_fid(ports[22], static=True) fid20to23: int = nodes[20].linklayer.get_or_create_fid(ports[23], static=True) fib20: BaseForwardingInformationBase = nodes[20].data_structs['fib'] if edge_traverse: fib20.add_fib_entry(Name('/edge'), fid20to21, static=True) fib20.add_fib_entry(Name('/edge'), fid20to22, static=True) fib20.add_fib_entry(Name('/edge'), fid20to23, static=True) nodes[20].data_structs['fib'] = fib20 nodes[20].data_structs['rib'] = TreeRoutingInformationBase( manager, shortest_only=False) # Set up faces and static FIB of core30 node. fid30to31: int = nodes[30].linklayer.get_or_create_fid(ports[31], static=True) fid30to32: int = nodes[30].linklayer.get_or_create_fid(ports[32], static=True) fid30to33: int = nodes[30].linklayer.get_or_create_fid(ports[33], static=True) fib30: BaseForwardingInformationBase = nodes[30].data_structs['fib'] if edge_traverse: fib30.add_fib_entry(Name('/edge'), fid30to31, static=True) fib30.add_fib_entry(Name('/edge'), fid30to32, static=True) fib30.add_fib_entry(Name('/edge'), fid30to33, static=True) nodes[30].data_structs['fib'] = fib30 nodes[30].data_structs['rib'] = TreeRoutingInformationBase( manager, shortest_only=False) for node in [00, 10, 20, 30]: nodes[node].routinglayer._ageing_interval = routing_interval # Set up network edge autoconfig. for e in [11, 12, 13, 21, 22, 23, 31, 32, 33]: nodes[ e].autoconfiglayer._service_registration_prefixes = autoconfig_edgeprefix nodes[e].routinglayer._ageing_interval = routing_interval nodes[e].autoconfiglayer._service_registration_timeout = timedelta( seconds=lease_time) forwarders = list(nodes.values()) random.shuffle(forwarders) for n in forwarders: n.start_forwarder() sleep(0.05) imr = SimpleMemoryRepository(Name('/edge/hoppingrepo'), manager) n = 600 for i in range(n): imr.add_content(Name(f'/edge/hoppingrepo/{i}'), f'content {i}') def repo_hop(): global repo, edge_index, lock, running if repo is not None: repo.linklayer.sock.close() dumpster.append(repo) with lock: if not running: return repo = ICNDataRepository(None, Name('/hoppingrepo'), 0, encoder=NdnTlvEncoder(), autoconfig=True, autoconfig_routed=True) repo.repolayer._repository = imr edge_index = (edge_index + 1) % len(edgeports) repo.autoconfiglayer._broadcast_port = edgeports[edge_index] repo.start_repo() threading.Timer(hopping_interval, repo_hop).start() repo_hop() class DurationTaggingLayer(LayerProcess): def __init__(self): super().__init__('DurationTaggingLayer') self._names: Dict[Name, datetime] = dict() def data_from_lower(self, to_lower: multiprocessing.Queue, to_higher: multiprocessing.Queue, data): fid, packet = data if packet.name in self._names: to_higher.put([ fid, packet, (datetime.utcnow() - self._names[packet.name]).total_seconds() ]) del self._names[packet.name] else: to_higher.put([fid, packet, None]) def data_from_higher(self, to_lower: multiprocessing.Queue, to_higher: multiprocessing.Queue, data): fid, packet = data self._names[packet.name] = datetime.utcnow() to_lower.put(data) linklayer = UDP4LinkLayer(port=0) fetch_fid = linklayer.create_new_fid(ports[00], True) fetch = LayerStack([ DurationTaggingLayer(), BasicChunkLayer(), BasicPacketEncodingLayer(NdnTlvEncoder()), linklayer ]) end_time = datetime.utcnow() fetch.start_all() for i in range(n): fetch.queue_from_higher.put( [fetch_fid, Interest(Name(f'/edge/hoppingrepo/{i}'))]) end_time += timedelta(milliseconds=100) delay = (end_time - datetime.utcnow()).total_seconds() if delay > 0: sleep(delay) satisfied_interests: Dict[int, bool] = dict() satisfied_interests_outoforder: Dict[int, bool] = dict() maxreceived = -1 avgduration = 0 while not fetch.queue_to_higher.empty(): _, data, duration = fetch.queue_to_higher.get() if isinstance(data, Content) and data.content.startswith('content '): _, i = data.content.split(' ', 1) i = int(i) satisfied_interests_outoforder[i] = True avgduration += duration if i <= maxreceived: continue maxreceived = i satisfied_interests[i] = True success = len(satisfied_interests) success_outoforder = len(satisfied_interests_outoforder) os.makedirs('raw', exist_ok=True) filename = f'raw/{now}_{testname}.csv' with open(filename, 'a') as f: f.write( f'{routing_interval},{hopping_interval},{lease_time},{success / n},{success_outoforder / n},{avgduration / success if success > 0 else 0.0}\n' ) print(f'Wrote data to file {filename}') fetch.stop_all() for f in nodes.values(): f.stop_forwarder() if repo is not None: repo.stop_repo() for r in dumpster: r.stop_repo() with lock: running = False
def setup(dummy): dummy.simulation_bus = SimulationBus() # Use BasicStringEncoder dummy.icn_repo0 = ICNDataRepository( port=0, prefix=Name("/test/t1"), foldername=None, interfaces=[dummy.simulation_bus.add_interface("repo0")], log_level=255) # Initialize repository 0 dummy.icn_repo1 = ICNDataRepository( port=0, prefix=Name("/test/t2"), foldername=None, interfaces=[dummy.simulation_bus.add_interface("repo1")], log_level=255) # Initialize repository 1 (this one has sessions) dummy.icn_forwarder0 = ICNForwarder( port=0, log_level=255, interfaces=[dummy.simulation_bus.add_interface("fw0") ]) # Initialize forwarder 0 dummy.icn_forwarder1 = ICNForwarder( port=0, interfaces=[dummy.simulation_bus.add_interface("fw1") ]) # Initialize forwarder 1 dummy.mgmt_client0 = MgmtClient( dummy.icn_repo0.mgmt.mgmt_sock.getsockname() [1]) # Mgmt client for repository 0 dummy.mgmt_client1 = MgmtClient( dummy.icn_repo1.mgmt.mgmt_sock.getsockname() [1]) # Mgmt client for repository 1 dummy.mgmt_client2 = MgmtClient( dummy.icn_forwarder0.mgmt.mgmt_sock.getsockname() [1]) # Mgmt client for forwarder 0 dummy.mgmt_client3 = MgmtClient( dummy.icn_forwarder1.mgmt.mgmt_sock.getsockname() [1]) # Mgmt client for forwarder 1 # This is unintuitive. Why does the fetch tool add its own face, but the other components don't? dummy.fetch_tool = Fetch("fw0", None, log_level=255, interfaces=[ dummy.simulation_bus.add_interface("fetcht0") ]) # Initialize a client (fetch tool) dummy.icn_repo0.start_repo() dummy.icn_repo1.start_repo() dummy.icn_forwarder0.start_forwarder() dummy.icn_forwarder1.start_forwarder() dummy.simulation_bus.start_process() dummy.mgmt_client2.add_face( "repo0", None, 0) # Add a connection between fw0 and repo0 interface dummy.mgmt_client2.add_forwarding_rule(Name("/test/t1"), [ 0 ]) # Add a rule to forward packages with this prefix to this forwarder. # dummy.mgmt_client0.add_forwarding_rule(Name("/test/t1"), [0]) # Add dummy.mgmt_client3.add_face( "repo1", None, 0) # Add a network interface to forwarder 0 and give it ip and port dummy.mgmt_client3.add_forwarding_rule(Name("/test/t2"), [0]) # Add # dummy.mgmt_client0.add_face("repo0", None, 0) # Add a network interface to forwarder 0 and give it ip and port # dummy.mgmt_client0.add_forwarding_rule(Name("/test/t1"), [0]) # Add # dummy.mgmt_client1.add_face("repo1", None, 0) # Add a network interface to forwarder 0 and give it ip and port # dummy.mgmt_client1.add_forwarding_rule(Name("/test/t2"), [0]) # Add dummy.icn_repo0.repolayer._repository.add_content( Name("/test/t1/content_object"), "This is just a test for repo0.") dummy.mgmt_client1.add_new_content(Name("/test/t2/session_connector"), "This shouldn't be needed.") interest0 = Name("/test/t1/content_object") interest1 = Name("/test/t2/session_connector") res0 = dummy.fetch_tool.fetch_data(interest0, timeout=20) #res1 = dummy.fetch_tool.fetch_data(interest1, timeout=20) print(res0)
def setUp(self): """ c00 _________/ | \_________ | | | | | | c10 c20 c30 / | \ / | \ / | \ e11 e12 e13 e21 e22 e23 e31 e32 e33 """ self.manager = multiprocessing.Manager() self.autoconfig_edgeprefix: List[Tuple[Name, bool]] = [(Name('/edge'), False)] self.nodes: Dict[int, ICNForwarder] = dict() self.ports: Dict[int, int] = dict() # Initialize core nodes for c in [00, 10, 20, 30]: self.nodes[c] = ICNForwarder(0, encoder=NdnTlvEncoder(), routing=True, peers=[]) self.ports[c] = self.nodes[c].linklayer.interfaces[0].get_port() # Initialize edge nodes for e in [11, 12, 13, 21, 22, 23, 31, 32, 33]: self.nodes[e] = ICNForwarder(0, encoder=NdnTlvEncoder(), routing=True, peers=[], autoconfig=True) self.ports[e] = self.nodes[e].linklayer.interfaces[0].get_port() # Assign routing peers after the OS assigned UDP ports. Each node knows the nodes one layer "beneath" itself # in above graph as its routing peers. self.nodes[00].routinglayer._peers = [('127.0.0.1', self.ports[10]), ('127.0.0.1', self.ports[20]), ('127.0.0.1', self.ports[30])] self.nodes[10].routinglayer._peers = [('127.0.0.1', self.ports[11]), ('127.0.0.1', self.ports[12]), ('127.0.0.1', self.ports[13])] self.nodes[20].routinglayer._peers = [('127.0.0.1', self.ports[21]), ('127.0.0.1', self.ports[22]), ('127.0.0.1', self.ports[23])] self.nodes[30].routinglayer._peers = [('127.0.0.1', self.ports[31]), ('127.0.0.1', self.ports[32]), ('127.0.0.1', self.ports[33])] # Set up faces and static FIB of core00 node. fid00to10: int = self.nodes[ 00].linklayer.faceidtable.get_or_create_faceid( AddressInfo(('127.0.0.1', self.ports[10]), 0)) fid00to20: int = self.nodes[ 00].linklayer.faceidtable.get_or_create_faceid( AddressInfo(('127.0.0.1', self.ports[20]), 0)) fid00to30: int = self.nodes[ 00].linklayer.faceidtable.get_or_create_faceid( AddressInfo(('127.0.0.1', self.ports[30]), 0)) fib00: BaseForwardingInformationBase = self.nodes[00].icnlayer.fib fib00.add_fib_entry(Name('/edge'), fid00to10, static=True) fib00.add_fib_entry(Name('/edge'), fid00to20, static=True) fib00.add_fib_entry(Name('/edge'), fid00to30, static=True) self.nodes[00].routinglayer.rib.shortest_only = False # Set up faces and static FIB of core10 node. fid10to11: int = self.nodes[ 10].linklayer.faceidtable.get_or_create_faceid( AddressInfo(('127.0.0.1', self.ports[11]), 0)) fid10to12: int = self.nodes[ 10].linklayer.faceidtable.get_or_create_faceid( AddressInfo(('127.0.0.1', self.ports[12]), 0)) fid10to13: int = self.nodes[ 10].linklayer.faceidtable.get_or_create_faceid( AddressInfo(('127.0.0.1', self.ports[13]), 0)) fib10: BaseForwardingInformationBase = self.nodes[10].icnlayer.fib fib10.add_fib_entry(Name('/edge'), fid10to11, static=True) fib10.add_fib_entry(Name('/edge'), fid10to12, static=True) fib10.add_fib_entry(Name('/edge'), fid10to13, static=True) self.nodes[10].routinglayer.rib.shortest_only = False # Set up faces and static FIB of core20 node. fid20to21: int = self.nodes[ 20].linklayer.faceidtable.get_or_create_faceid( AddressInfo(('127.0.0.1', self.ports[21]), 0)) fid20to22: int = self.nodes[ 20].linklayer.faceidtable.get_or_create_faceid( AddressInfo(('127.0.0.1', self.ports[22]), 0)) fid20to23: int = self.nodes[ 20].linklayer.faceidtable.get_or_create_faceid( AddressInfo(('127.0.0.1', self.ports[23]), 0)) fib20: BaseForwardingInformationBase = self.nodes[20].icnlayer.fib fib20.add_fib_entry(Name('/edge'), fid20to21, static=True) fib20.add_fib_entry(Name('/edge'), fid20to22, static=True) fib20.add_fib_entry(Name('/edge'), fid20to23, static=True) self.nodes[20].routinglayer.rib.shortest_only = False # Set up faces and static FIB of core30 node. fid30to31: int = self.nodes[ 30].linklayer.faceidtable.get_or_create_faceid( AddressInfo(('127.0.0.1', self.ports[31]), 0)) fid30to32: int = self.nodes[ 30].linklayer.faceidtable.get_or_create_faceid( AddressInfo(('127.0.0.1', self.ports[32]), 0)) fid30to33: int = self.nodes[ 30].linklayer.faceidtable.get_or_create_faceid( AddressInfo(('127.0.0.1', self.ports[33]), 0)) fib30: BaseForwardingInformationBase = self.nodes[30].icnlayer.fib fib30.add_fib_entry(Name('/edge'), fid30to31, static=True) fib30.add_fib_entry(Name('/edge'), fid30to32, static=True) fib30.add_fib_entry(Name('/edge'), fid30to33, static=True) self.nodes[30].routinglayer.rib.shortest_only = False self.nodes[00].routinglayer._ageing_interval = 1.0 self.nodes[10].routinglayer._ageing_interval = 1.0 self.nodes[20].routinglayer._ageing_interval = 1.0 self.nodes[30].routinglayer._ageing_interval = 1.0 # Set up network edge autoconfig. for e in [11, 12, 13, 21, 22, 23, 31, 32, 33]: self.nodes[ e].autoconfiglayer._service_registration_prefixes = self.autoconfig_edgeprefix self.nodes[e].routinglayer._ageing_interval = 1.0 self.nodes[ e].autoconfiglayer._service_registration_timeout = timedelta( seconds=15) # Set up repository directory. os.makedirs('/tmp/test_hopping_repo', exist_ok=True) for c in [1, 2, 3, 4, 5, 6, 7, 8, 9]: with open(f'/tmp/test_hopping_repo/c{c}', 'w') as f: f.write(f'This is test chunk {c}!')
simulation_bus = SimulationBus(log_level=0) # Use BasicStringEncoder fetch0 = FetchSessions( 'fw0', None, log_level=0, interfaces=[simulation_bus.add_interface('fetcht0')], name='fetch0') repo0 = ICNDataRepositorySession( port=0, prefix=Name('/unibas/sim1'), foldername=None, interfaces=[simulation_bus.add_interface('repo0')], log_level=0) fw0 = ICNForwarder(port=0, log_level=0, interfaces=[simulation_bus.add_interface('fw0')], node_name='forwarder0') fw1 = ICNForwarder(port=0, log_level=0, interfaces=[simulation_bus.add_interface('fw1')], node_name='forwarder1') fw2 = ICNForwarder(port=0, log_level=0, interfaces=[simulation_bus.add_interface('fw2')], node_name='forwarder2') fw3 = ICNForwarder(port=0, log_level=0, interfaces=[simulation_bus.add_interface('fw3')], node_name='forwarder3') mgmt_repo0 = MgmtClient(repo0.mgmt.mgmt_sock.getsockname()[1])
class TestDisconnections(unittest.TestCase): """Run the data-offloading simulation""" def setUp(self): self.encoder_type = SimpleStringEncoder() self.simulation_bus = SimulationBus(packetencoder=self.encoder_type) chunk_size = 4 self.chunkifyer = SimpleContentChunkifyer(chunk_size) self.car = ICNForwarder( 0, encoder=self.encoder_type, routing=True, interfaces=[self.simulation_bus.add_interface("car")]) self.fetch_tool_car = Fetch( "car", None, 255, self.encoder_type, interfaces=[self.simulation_bus.add_interface("ftcar")]) self.mgmt_client_car = MgmtClient( self.car.mgmt.mgmt_sock.getsockname()[1]) self.rsus = [] self.fetch_tools = [] self.mgmt_clients = [] for i in range(4): self.rsus.append( NFNForwarderData( 0, encoder=self.encoder_type, interfaces=[self.simulation_bus.add_interface(f"rsu{i}")], chunk_size=chunk_size, num_of_forwards=0)) self.fetch_tools.append( Fetch(f"rsu{i}", None, 255, self.encoder_type, interfaces=[self.simulation_bus.add_interface(f"ft{i}") ])) self.rsus[i].nfnlayer.optimizer = EdgeComputingOptimizer( self.rsus[i].icnlayer.cs, self.rsus[i].icnlayer.fib, self.rsus[i].icnlayer.pit, self.rsus[i].linklayer.faceidtable) self.mgmt_clients.append( MgmtClient(self.rsus[i].mgmt.mgmt_sock.getsockname()[1])) self.fetch_tools[i].timeoutpreventionlayer.timeout_interval = 30 def tearDown(self): self.car.stop_forwarder() self.fetch_tool_car.stop_fetch() for rsu in self.rsus: rsu.stop_forwarder() for fetch_tool in self.fetch_tools: fetch_tool.stop_fetch() self.simulation_bus.stop_process() def setup_faces_and_connections(self): self.car.start_forwarder() for rsu in self.rsus: rsu.start_forwarder() self.simulation_bus.start_process() function = "PYTHON\nf\ndef f(a):\n return a + ' World'" # Setup rsu0 self.mgmt_clients[0].add_face("car", None, 0) self.mgmt_clients[0].add_face("rsu1", None, 0) self.mgmt_clients[0].add_forwarding_rule(Name("/car"), [0]) self.mgmt_clients[0].add_forwarding_rule(Name("/nR"), [1]) self.mgmt_clients[0].add_new_content(Name("/rsu/func/f1"), function) # Setup rsu1 self.mgmt_clients[1].add_face("car", None, 0) self.mgmt_clients[1].add_face("rsu0", None, 0) self.mgmt_clients[1].add_face("rsu2", None, 0) self.mgmt_clients[1].add_forwarding_rule(Name("/car"), [0]) self.mgmt_clients[1].add_forwarding_rule(Name("/nL"), [1]) self.mgmt_clients[1].add_forwarding_rule(Name("/nR"), [2]) self.mgmt_clients[1].add_new_content(Name("/rsu/func/f1"), function) # Setup rsu2 self.mgmt_clients[2].add_face("car", None, 0) self.mgmt_clients[2].add_face("rsu1", None, 0) self.mgmt_clients[2].add_face("rsu3", None, 0) self.mgmt_clients[2].add_forwarding_rule(Name("/car"), [0]) self.mgmt_clients[2].add_forwarding_rule(Name("/nL"), [1]) self.mgmt_clients[2].add_forwarding_rule(Name("/nR"), [2]) self.mgmt_clients[2].add_new_content(Name("/rsu/func/f1"), function) # Setup rsu3 self.mgmt_clients[3].add_face("car", None, 0) self.mgmt_clients[3].add_face("rsu2", None, 0) self.mgmt_clients[3].add_forwarding_rule(Name("/car"), [0]) self.mgmt_clients[3].add_forwarding_rule(Name("/nL"), [1]) self.mgmt_clients[3].add_new_content(Name("/rsu/func/f1"), function) # Setup car self.mgmt_client_car.add_face("rsu0", None, 0) self.mgmt_client_car.add_face("rsu1", None, 0) self.mgmt_client_car.add_face("rsu2", None, 0) self.mgmt_client_car.add_forwarding_rule(Name("/rsu"), [0]) self.d = Content(Name("/car/data/test1"), "Test" * 10) self.meta_data, self.data = self.chunkifyer.chunk_data(self.d) for md in self.meta_data[:]: self.car.icnlayer.cs.add_content_object(md) # Only load first 6 chunks to car to simulate car getting out of each while uploading for d in self.data[:5]: self.mgmt_client_car.add_new_content(d.name, d.content) def test_two_disconnections(self): self.setup_faces_and_connections() name = Name("/rsu/func/f1") name += '_(/car/data/test1)' name += "NFN" print("RSU 0 FETCHING") start = time() res0 = self.fetch_tools[0].fetch_data(name, timeout=10) print(res0) sleep(1) print("\n" * 20 + "RSU 1 FETCHING") for d in self.data[5:-2]: self.mgmt_client_car.add_new_content(d.name, d.content) for d in self.data[:5]: self.car.icnlayer.cs.remove_content_object(d.name) res1 = self.fetch_tools[1].fetch_data(name, timeout=10) print(res1) sleep(1) print("\n" * 20 + "RSU 2 FETCHING") for d in self.data[-2:]: self.mgmt_client_car.add_new_content(d.name, d.content) for d in self.data[:-2]: self.car.icnlayer.cs.remove_content_object(d.name) res2 = self.fetch_tools[2].fetch_data(name, timeout=10) print(res2, time() - start) self.assertEqual(self.d.content + " World", res2) def test_skip_two_rsus(self): # Increase the number of forwards for rsu in self.rsus: rsu.chunklayer.set_number_of_forwards(2) self.setup_faces_and_connections() name = Name("/rsu/func/f1") name += '_(/car/data/test1)' name += "NFN" res0 = self.fetch_tools[0].fetch_data(name, timeout=10) print(res0) sleep(1) print("\n" * 20 + "RSU 4 STARTING") for d in self.data[5:]: self.mgmt_client_car.add_new_content(d.name, d.content) for d in self.data[:5]: self.car.icnlayer.cs.remove_content_object(d.name) res1 = self.fetch_tools[3].fetch_data(name, timeout=10) print(res1) self.assertEqual(self.d.content + " World", res1)
def setUp(self): self.simulation_bus = SimulationBus( log_level=0) # Use BasicStringEncoder self.icn_repo0 = ICNDataRepository( port=0, prefix=Name('/test/t1'), foldername=None, interfaces=[self.simulation_bus.add_interface('repo0')], log_level=255) # Initialize repository 0 self.icn_repo1 = ICNDataRepositorySession( port=0, prefix=Name('/test/t2'), foldername=None, interfaces=[self.simulation_bus.add_interface('repo1')], log_level=0) # Initialize repository 1 (this one has sessions) self.icn_forwarder0 = ICNForwarder( port=0, log_level=255, interfaces=[self.simulation_bus.add_interface('fw0') ]) # Initialize forwarder 0 self.icn_forwarder1 = ICNForwarder( port=0, log_level=255, interfaces=[self.simulation_bus.add_interface('fw1') ]) # Initialize forwarder 1 self.mgmt_client0 = MgmtClient( self.icn_repo0.mgmt.mgmt_sock.getsockname() [1]) # Mgmt client for repository 0 self.mgmt_client1 = MgmtClient( self.icn_repo1.mgmt.mgmt_sock.getsockname() [1]) # Mgmt client for repository 1 self.mgmt_client2 = MgmtClient( self.icn_forwarder0.mgmt.mgmt_sock.getsockname() [1]) # Mgmt client for forwarder 0 self.mgmt_client3 = MgmtClient( self.icn_forwarder1.mgmt.mgmt_sock.getsockname() [1]) # Mgmt client for forwarder 1 # This is unintuitive. Why does the fetch tool add its own face, but the other components don't? self.fetch0 = Fetch('fw0', None, log_level=255, interfaces=[ self.simulation_bus.add_interface('fetcht0') ]) # Initialize a client (fetch tool) self.fetch1 = FetchSessions( 'fw0', None, log_level=255, interfaces=[self.simulation_bus.add_interface('fetcht1')]) self.icn_repo0.start_repo() self.icn_repo1.start_repo() self.icn_forwarder0.start_forwarder() self.icn_forwarder1.start_forwarder() self.simulation_bus.start_process() time.sleep(1) # Be safe and wait for all processes to start self.mgmt_client2.add_face( 'repo0', None, 0) # Add new interface to forwarder 0, index has to be 0. self.mgmt_client2.add_face( 'fw1', None, 0) # # Add new interface to forwarder 0, index has to be 0. self.mgmt_client2.add_forwarding_rule( Name('/test/t1'), [0]) # Add a forward-rule this prefix to interface with index 0. self.mgmt_client2.add_forwarding_rule( Name('/test/t2'), [1]) # Add a forward-rule this prefix to interface with index 1. self.mgmt_client3.add_face( 'repo1', None, 0) # Add new interface to forwarder 1, index has to be 0. self.mgmt_client3.add_forwarding_rule( Name('/test/t2'), [0]) # Add a forward-rule this prefix to interface with index 0. # Repositories do not need a forwarding rule or an interface. All done in constructor. self.test_content = 'This is just a test for repo0.' self.icn_repo0.repo.add_content( Name('/test/t1/content_object'), self.test_content ) # TODO: Create add_new_content command for DataRepository in ManagementClient print(self.icn_forwarder0.icnlayer.fib) print(self.icn_forwarder1.icnlayer.fib)
class ICNSessionSimulation(unittest.TestCase): """Simulate a Scenario where timeout prevention is required""" def setUp(self): self.simulation_bus = SimulationBus( log_level=0) # Use BasicStringEncoder self.icn_repo0 = ICNDataRepository( port=0, prefix=Name('/test/t1'), foldername=None, interfaces=[self.simulation_bus.add_interface('repo0')], log_level=255) # Initialize repository 0 self.icn_repo1 = ICNDataRepositorySession( port=0, prefix=Name('/test/t2'), foldername=None, interfaces=[self.simulation_bus.add_interface('repo1')], log_level=0) # Initialize repository 1 (this one has sessions) self.icn_forwarder0 = ICNForwarder( port=0, log_level=255, interfaces=[self.simulation_bus.add_interface('fw0') ]) # Initialize forwarder 0 self.icn_forwarder1 = ICNForwarder( port=0, log_level=255, interfaces=[self.simulation_bus.add_interface('fw1') ]) # Initialize forwarder 1 self.mgmt_client0 = MgmtClient( self.icn_repo0.mgmt.mgmt_sock.getsockname() [1]) # Mgmt client for repository 0 self.mgmt_client1 = MgmtClient( self.icn_repo1.mgmt.mgmt_sock.getsockname() [1]) # Mgmt client for repository 1 self.mgmt_client2 = MgmtClient( self.icn_forwarder0.mgmt.mgmt_sock.getsockname() [1]) # Mgmt client for forwarder 0 self.mgmt_client3 = MgmtClient( self.icn_forwarder1.mgmt.mgmt_sock.getsockname() [1]) # Mgmt client for forwarder 1 # This is unintuitive. Why does the fetch tool add its own face, but the other components don't? self.fetch0 = Fetch('fw0', None, log_level=255, interfaces=[ self.simulation_bus.add_interface('fetcht0') ]) # Initialize a client (fetch tool) self.fetch1 = FetchSessions( 'fw0', None, log_level=255, interfaces=[self.simulation_bus.add_interface('fetcht1')]) self.icn_repo0.start_repo() self.icn_repo1.start_repo() self.icn_forwarder0.start_forwarder() self.icn_forwarder1.start_forwarder() self.simulation_bus.start_process() time.sleep(1) # Be safe and wait for all processes to start self.mgmt_client2.add_face( 'repo0', None, 0) # Add new interface to forwarder 0, index has to be 0. self.mgmt_client2.add_face( 'fw1', None, 0) # # Add new interface to forwarder 0, index has to be 0. self.mgmt_client2.add_forwarding_rule( Name('/test/t1'), [0]) # Add a forward-rule this prefix to interface with index 0. self.mgmt_client2.add_forwarding_rule( Name('/test/t2'), [1]) # Add a forward-rule this prefix to interface with index 1. self.mgmt_client3.add_face( 'repo1', None, 0) # Add new interface to forwarder 1, index has to be 0. self.mgmt_client3.add_forwarding_rule( Name('/test/t2'), [0]) # Add a forward-rule this prefix to interface with index 0. # Repositories do not need a forwarding rule or an interface. All done in constructor. self.test_content = 'This is just a test for repo0.' self.icn_repo0.repo.add_content( Name('/test/t1/content_object'), self.test_content ) # TODO: Create add_new_content command for DataRepository in ManagementClient print(self.icn_forwarder0.icnlayer.fib) print(self.icn_forwarder1.icnlayer.fib) def tearDown(self): time.sleep( 1 ) # Be safe and wait for all messages to trickle in before shutting down everything self.icn_forwarder0.stop_forwarder() self.icn_forwarder1.stop_forwarder() self.fetch0.stop_fetch() self.fetch1.stop_fetch() self.simulation_bus.stop_process() self.mgmt_client0.shutdown() self.mgmt_client1.shutdown() self.mgmt_client2.shutdown() self.mgmt_client3.shutdown() def test_simple_interest(self): interest = Name( '/test/t1/content_object') # Test routing, no new features. res = self.fetch0.fetch_data(interest, timeout=20) print(f"Return value: {res}") self.assertEqual(self.test_content, res, 'The content matches.') def test_simple_session_initiation(self): interest = Name( "/test/t2/session_connector" ) # Test session connection string. This should return 16bits res = self.fetch1.fetch_data(interest, timeout=20) print(f"Return value : {res}") self.assertEqual(self.icn_repo1.repolayer._pending_sessions[0], self.fetch1._session_keys[0], 'The session keys match.')
class test_RoutingLayerFullStack(unittest.TestCase): def setUp(self): self.f9000 = ICNForwarder(9000, encoder=NdnTlvEncoder(), routing=True, peers=[('127.0.0.1', 9001), ('127.0.0.1', 9002), ('127.0.0.1', 9090)]) self.f9001 = ICNForwarder(9001, encoder=NdnTlvEncoder(), routing=True, peers=[('127.0.0.1', 9000), ('127.0.0.1', 9002), ('127.0.0.1', 9003), ('127.0.0.1', 9004)]) self.f9002 = ICNForwarder(9002, encoder=NdnTlvEncoder(), routing=True, peers=[('127.0.0.1', 9000), ('127.0.0.1', 9001), ('127.0.0.1', 9004)]) self.f9003 = ICNForwarder(9003, encoder=NdnTlvEncoder(), routing=True, peers=[('127.0.0.1', 9001), ('127.0.0.1', 9004)]) self.f9004 = ICNForwarder(9004, encoder=NdnTlvEncoder(), routing=True, peers=[('127.0.0.1', 9001), ('127.0.0.1', 9002), ('127.0.0.1', 9003)]) os.makedirs('/tmp/test_repo', exist_ok=True) f = open('/tmp/test_repo/helloworld', 'w') f.write('Hello, World!\n') f.close() self.repo = ICNDataRepository('/tmp/test_repo', Name('/testrepo'), port=9090, encoder=NdnTlvEncoder()) self.fetch = Fetch('127.0.0.1', 9004, encoder=NdnTlvEncoder()) # Create RIB entry for the repository (with Python weirdness) repo_fid: int = self.f9000.linklayer.faceidtable.get_or_create_faceid(AddressInfo(('127.0.0.1', 9090), 0)) rib: BaseRoutingInformationBase = self.f9000.routinglayer.rib rib.insert(Name('/testrepo'), repo_fid, distance=1, timeout=None) def tearDown(self): self.f9000.stop_forwarder() self.f9001.stop_forwarder() self.f9002.stop_forwarder() self.f9003.stop_forwarder() self.f9004.stop_forwarder() self.repo.stop_repo() self.fetch.stop_fetch() shutil.rmtree('/tmp/test_repo') def test_network(self): """ C | F[9004] -- F[9003] / \ / / \ / / \ / F[9002] -- F[9001] \ / \ / \ / F[9000] | R """ self.f9000.start_forwarder() self.f9001.start_forwarder() self.f9002.start_forwarder() self.f9003.start_forwarder() self.f9004.start_forwarder() self.repo.start_repo() hw: str = '' until = datetime.utcnow() + timedelta(seconds=20) while hw != 'Hello, World!\n' and datetime.utcnow() < until: try: hw = self.fetch.fetch_data(Name('/testrepo/helloworld'), timeout=1.0) except queue.Empty: pass self.assertEqual('Hello, World!\n', hw) until = datetime.utcnow() + timedelta(seconds=20) hw = '' start = datetime.utcnow() end = start while hw != 'Hello, World!\n' and datetime.utcnow() < until: try: hw = self.fetch.fetch_data(Name('/testrepo/helloworld'), timeout=1.0) end = datetime.utcnow() except queue.Empty: pass self.assertEqual('Hello, World!\n', hw) self.assertLess(end-start, timedelta(seconds=4))
def setup_network(self): """configure a network according to the configuration""" #create RSUs and RSU mgmt tools self.rsus = [] self.mgmt_rsu = [] for i in range(0, self.number_of_rsus): rsu = NFNForwarderData(0, encoder=self.encoder_type, interfaces=[self.simulation_bus.add_interface(f"rsu{i}")], chunk_size=self.chunk_size, num_of_forwards=1, ageing_interval=10) rsu.nfnlayer.optimizer = self.optimizer(rsu.icnlayer.cs, rsu.icnlayer.fib, rsu.icnlayer.pit, rsu.linklayer.faceidtable) mgmt_client = MgmtClient(rsu.mgmt.mgmt_sock.getsockname()[1]) self.rsus.append(rsu) self.mgmt_rsu.append(mgmt_client) self.rsus[i].icnlayer.cs.set_cs_timeout(60) #setup connections for i in range(1, self.number_of_rsus-1): #setup connections between rsus faceid_rsu_l = self.rsus[i].linklayer.faceidtable.get_or_create_faceid(AddressInfo("rsu" + str(i-1), 0)) faceid_rsu_r = self.rsus[i].linklayer.faceidtable.get_or_create_faceid(AddressInfo("rsu" + str(i+1), 0)) self.rsus[i].icnlayer.fib.add_fib_entry(Name("/nL"), [faceid_rsu_l]) self.rsus[i].icnlayer.fib.add_fib_entry(Name("/nR"), [faceid_rsu_r]) #setup first and last rsu faceid_rsu_1st = self.rsus[0].linklayer.faceidtable.get_or_create_faceid(AddressInfo("rsu" + str(1), 0)) faceid_rsu_last = self.rsus[self.number_of_rsus-1].linklayer.faceidtable.get_or_create_faceid(AddressInfo("rsu" + str(self.number_of_rsus-2), 0)) self.rsus[0].icnlayer.fib.add_fib_entry(Name("/nR"), [faceid_rsu_1st]) self.rsus[self.number_of_rsus-1].icnlayer.fib.add_fib_entry(Name("/nL"), [faceid_rsu_last]) #add function for i in range(0, self.number_of_rsus): for n in zip(self.named_functions.keys(), self.named_functions.values()): add_msg = self.rsus[i].icnlayer.cs.add_content_object(Content(Name(n[0]), n[1]), static=True) #setup cars self.car_forwarders = [] self.car_fetch = [] self.car_mgmt = [] for i in range (0, self.number_of_cars): self.car_forwarders.append(ICNForwarder(0, encoder=self.encoder_type, routing=True, interfaces=[self.simulation_bus.add_interface(f"car{i}")])) self.car_fetch.append(Fetch(f"car{i}", None, 255, self.encoder_type, interfaces=[self.simulation_bus.add_interface(f"ftcar{i}")])) mgmt_client_car = MgmtClient(self.car_forwarders[i].mgmt.mgmt_sock.getsockname()[1]) self.car_mgmt.append(mgmt_client_car) for j in range(0, self.number_of_rsus): car_face_id = self.car_forwarders[i].linklayer.faceidtable.get_or_create_faceid(AddressInfo(f"rsu{j}", 0)) self.to_rsu_faces[j][i] = car_face_id rsu_face_id = self.rsus[j].linklayer.faceidtable.get_or_create_faceid(AddressInfo(f"car{i}", 0)) self.to_car_faces[j][i] = rsu_face_id #Starting nodes for i in range(0, self.number_of_rsus): self.rsus[i].start_forwarder() for i in range(0, self.number_of_cars): self.car_forwarders[i].start_forwarder() self.simulation_bus.start_process()
from PiCN.Mgmt import MgmtClient from PiCN.Packets import Name import time if __name__ == "__main__": simulation_bus = SimulationBus(log_level=0) # Use BasicStringEncoder repo0 = ICNDataRepositorySession( port=0, prefix=Name('/unibas/sim1'), foldername=None, interfaces=[simulation_bus.add_interface('repo0')], log_level=0) fw0 = ICNForwarder(port=0, log_level=0, interfaces=[simulation_bus.add_interface('fw0')], node_name='forwarder0') mgmt_repo0 = MgmtClient(repo0.mgmt.mgmt_sock.getsockname()[1]) mgmt_fw0 = MgmtClient(fw0.mgmt.mgmt_sock.getsockname()[1]) fetch0 = FetchSessions( 'fw0', None, log_level=0, interfaces=[simulation_bus.add_interface('fetcht0')], name='fetch0') repo0.start_repo() fw0.start_forwarder() simulation_bus.start_process()