def setUp(self): self.encoder_type = self.get_encoder() self.simulation_bus = SimulationBus(packetencoder=self.encoder_type()) self.fetch_tool1 = Fetch("rsu1", None, 255, self.encoder_type(), [self.simulation_bus.add_interface("fetchtool1")]) self.fetch_tool2 = Fetch("rsu2", None, 255, self.encoder_type(), [self.simulation_bus.add_interface("fetchtool2")]) self.rsu1 = NFNForwarder(port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("rsu1")], log_level=255, ageing_interval=1) self.rsu2 = NFNForwarder(port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("rsu2")], log_level=255, ageing_interval=1) self.rsu3 = NFNForwarder(port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("rsu3")], log_level=255, ageing_interval=1) self.rsu1.icnlayer.pit.set_pit_timeout(0) self.rsu1.icnlayer.cs.set_cs_timeout(30) self.rsu2.icnlayer.pit.set_pit_timeout(0) self.rsu2.icnlayer.cs.set_cs_timeout(30) self.rsu3.icnlayer.pit.set_pit_timeout(0) self.rsu3.icnlayer.cs.set_cs_timeout(30) self.rsu1.nfnlayer.optimizer = EdgeComputingOptimizer(self.rsu1.icnlayer.cs, self.rsu1.icnlayer.fib, self.rsu1.icnlayer.pit, self.rsu1.linklayer.faceidtable) self.rsu2.nfnlayer.optimizer = EdgeComputingOptimizer(self.rsu2.icnlayer.cs, self.rsu2.icnlayer.fib, self.rsu2.icnlayer.pit, self.rsu2.linklayer.faceidtable) self.rsu3.nfnlayer.optimizer = EdgeComputingOptimizer(self.rsu3.icnlayer.cs, self.rsu3.icnlayer.fib, self.rsu3.icnlayer.pit, self.rsu3.linklayer.faceidtable) self.mgmt_client1 = MgmtClient(self.rsu1.mgmt.mgmt_sock.getsockname()[1]) self.mgmt_client2 = MgmtClient(self.rsu2.mgmt.mgmt_sock.getsockname()[1]) self.mgmt_client3 = MgmtClient(self.rsu3.mgmt.mgmt_sock.getsockname()[1])
def __init__(self): """define the configuration""" self.number_of_rsus = 4 self.number_of_cars = 5 self.to_car_faces = [[0] * self.number_of_cars for i in range(self.number_of_rsus)] #rsu, car -> faceid self.to_rsu_faces = [[0] * self.number_of_cars for i in range(self.number_of_rsus)] #rsu, car -> faceid self.car_speed = [60, 60, 60, 60, 60] #in km/h self.car_direction = [1, 1 ,1 ,1 ,1] # 1 or -1, consider starting rsu by choosing direction. self.starting_rsu = [0, 0, 0, 0, 0] self.rsu_distance = 0.5 #in km, range is identical here self.computations = self.get_computations() #Store computations in here, car will randomly choose one self.named_functions ={"/rsu/func/f1": "PYTHON\nf\ndef f(a, b, c):\n return a+b+c", "/rsu/func/f2": "PYTHON\nf\ndef f(a, b, c):\n return a*b*c"} #named functions stored on the self.chunk_size = 8192 self.optimizer = EdgeComputingOptimizer #NFN resolution strategy optimizer self.encoder_type = SimpleStringEncoder() self.simulation_bus = SimulationBus(packetencoder=self.encoder_type) self.rsu_name = Name("/rsu") self.car_to_computation = [0,0,0,0,0] #index which car issues which computation self._compute_rsu_connection_time() self.setup_network()
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.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_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 = self.get_encoder() self.simulation_bus = SimulationBus(packetencoder=self.encoder_type()) self.fetch_tool1 = Fetch( "nfn1", None, 255, self.encoder_type(), interfaces=[self.simulation_bus.add_interface("fetchtool1")]) self.nfn1 = NFNForwarder( port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("nfn1")], log_level=255, ageing_interval=1) self.nfn2 = NFNForwarder( port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("nfn2")], log_level=255, ageing_interval=1) self.simulation_bus.add_interface("deadend") self.nfn1.icnlayer.pit.set_pit_timeout(0) self.nfn1.icnlayer.cs.set_cs_timeout(30) self.nfn2.icnlayer.pit.set_pit_timeout(0) self.nfn2.icnlayer.cs.set_cs_timeout(30) self.nfn1.nfnlayer.optimizer = ToDataFirstOptimizer( self.nfn1.icnlayer.cs, self.nfn1.icnlayer.fib, self.nfn1.icnlayer.pit, self.nfn1.linklayer.faceidtable) self.nfn2.nfnlayer.optimizer = ToDataFirstOptimizer( self.nfn2.icnlayer.cs, self.nfn2.icnlayer.fib, self.nfn2.icnlayer.pit, self.nfn2.linklayer.faceidtable) self.mgmt_client1 = MgmtClient( self.nfn1.mgmt.mgmt_sock.getsockname()[1]) self.mgmt_client2 = MgmtClient( self.nfn2.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.')
"""Simulation environment to test long-term sessions. This is part of the bachelors thesis by Luc Kury@2020.""" from PiCN.ProgramLibs.Fetch import Fetch, FetchSessions from PiCN.ProgramLibs.ICNDataRepository import ICNDataRepository, ICNDataRepositorySession from PiCN.ProgramLibs.ICNForwarder import ICNForwarder from PiCN.Layers.LinkLayer.Interfaces import SimulationBus 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',
def simulation(classic, amount_of_parts): generateExampleFiles("../InputFiles/exampleInputFile", amount_of_parts) simulation_bus = SimulationBus(packetencoder=NdnTlvEncoder()) nfn_fwd0 = NFNForwarder( port=0, encoder=NdnTlvEncoder(), interfaces=[simulation_bus.add_interface("nfn0")], log_level=255, executors={"PYTHONSTREAM": NFNPythonExecutorStreaming()}, ageing_interval=1) nfn_fwd0.executors["PYTHONSTREAM"].initialize_executor( nfn_fwd0.nfnlayer.queue_to_lower, nfn_fwd0.nfnlayer.queue_from_lower, nfn_fwd0.nfnlayer.cs, classic) nfn_fwd0.icnlayer.pit.ageing = ageing nfn_fwd0.timeoutpreventionlayer.ageing = ageing nfn_fwd1 = NFNForwarder( port=0, encoder=NdnTlvEncoder(), interfaces=[simulation_bus.add_interface("nfn1")], log_level=255, executors={"PYTHONSTREAM": NFNPythonExecutorStreaming()}, ageing_interval=1) nfn_fwd1.executors["PYTHONSTREAM"].initialize_executor( nfn_fwd1.nfnlayer.queue_to_lower, nfn_fwd1.nfnlayer.queue_from_lower, nfn_fwd1.nfnlayer.cs, classic) nfn_fwd1.icnlayer.pit.ageing = ageing nfn_fwd1.timeoutpreventionlayer.ageing = ageing nfn_fwd2 = NFNForwarder( port=0, encoder=NdnTlvEncoder(), interfaces=[simulation_bus.add_interface("nfn2")], log_level=255, executors={"PYTHONSTREAM": NFNPythonExecutorStreaming()}, ageing_interval=1) nfn_fwd2.executors["PYTHONSTREAM"].initialize_executor( nfn_fwd2.nfnlayer.queue_to_lower, nfn_fwd2.nfnlayer.queue_from_lower, nfn_fwd2.nfnlayer.cs, classic) nfn_fwd2.icnlayer.pit.ageing = ageing nfn_fwd2.timeoutpreventionlayer.ageing = ageing repo = ICNDataRepository("../InputFiles", Name("/repo/r1"), 0, 255, NdnTlvEncoder(), False, False, interfaces=[simulation_bus.add_interface("repo")]) repo.start_repo() mgmt_client0 = MgmtClient(nfn_fwd0.mgmt.mgmt_sock.getsockname()[1]) mgmt_client1 = MgmtClient(nfn_fwd1.mgmt.mgmt_sock.getsockname()[1]) mgmt_client2 = MgmtClient(nfn_fwd2.mgmt.mgmt_sock.getsockname()[1]) fetch_tool = Fetch("nfn0", None, 255, NdnTlvEncoder(), interfaces=[simulation_bus.add_interface("fetchtoo1")]) nfn_fwd0.start_forwarder() nfn_fwd1.start_forwarder() nfn_fwd2.start_forwarder() simulation_bus.start_process() mgmt_client0.add_face("nfn1", None, 0) mgmt_client0.add_forwarding_rule(Name("/lib"), [0]) mgmt_client1.add_face("nfn2", None, 0) mgmt_client1.add_forwarding_rule(Name("/lib"), [0]) mgmt_client2.add_face("repo", None, 0) mgmt_client2.add_forwarding_rule(Name("/repo/r1"), [0]) mgmt_client0.add_new_content( Name("/lib/node0"), "PYTHONSTREAM\ngetnext_on_writeout\ndef getnext_on_writeout(arg):\n print('Start dritte')\n res = ''\n a = get_next(arg)\n while a != None:\n res = res + a\n a = get_next(arg)\n print('Ende dritte')\n return res" ) mgmt_client1.add_new_content( Name("/lib/node1"), "PYTHONSTREAM\nwriteout_on_getnext\ndef writeout_on_getnext(arg):\n print('Start zweite')\n a = get_next(arg)\n while a and check_end_streaming(a) is False:\n a = a.upper()\n sleep(1)\n write_out(a)\n a = get_next(arg)\n last_write_out()\n return print('Ende zweite')\n" ) mgmt_client2.add_new_content( Name("/lib/node2"), "PYTHONSTREAM\nwriteout_on_getnext\ndef writeout_on_getnext(arg):\n print('Start erste')\n write_out_on_get_next(arg)\n return print('Ende erste')\n" ) scenario_node_2 = Name("/lib/node2") scenario_node_2 += "#(=/repo/r1/exampleInputFile=)" scenario_node_2 += "NFN" scenario_node_1 = Name("/lib/node1") scenario_node_1 += "#(=" + str(scenario_node_2) + "=)" scenario_node_1 += "NFN" scenario_node_0 = Name("/lib/node0") scenario_node_0 += '_("' + str(scenario_node_1) + '")' scenario_node_0 += "NFN" start_time = time.perf_counter() res = fetch_tool.fetch_data(scenario_node_0, timeout=60) end_time = time.perf_counter() time_needed = end_time - start_time if res.startswith("CONTENT OF NAME"): print("Time:", time_needed, res) return time_needed else: print("Something went wrong") None
def __init__(self, run_id: int, mobile_nodes: List[MobileNode], stationary_nodes: List[StationaryNode], stationary_node_distance: float, named_functions: dict, function_names: list, forwarder: str = "NFNForwarder", optimizer: str = "ToDataFirstOptimizer", use_distribution_helper: bool = False, log_level=logging.DEBUG): """ Configuration of the mobility simulation :param run_id the identifier of the simulation run :param mobile_nodes a list of mobile nodes part of the simulation :param stationary_nodes a list of stationary nodes forming the infrastructure :param stationary_node_distance the distance between the stationary nodes :param named_functions a dictionary of named function definitions used to be executed :param function_names a list of function names to be assigned to the mobile nodes :param forwarder the NFN forwarder to be used :param optimizer the NFN resolution strategy optimizer to be used in the simulation :param use_distribution_helper A flag indicating if the default distribution helper (ZipfMandelbrotDistribution) shall be used or not; default = False :param log_level the log level of the logger to be used; default: logging.DEBUG """ self._run_id = run_id self._forwarder = forwarder self._optimizer = optimizer self._mobile_nodes = mobile_nodes self._stationary_nodes = stationary_nodes self._stationary_node_distance = stationary_node_distance self.logger = Logger("MobilitySimulation", log_level) self.to_car_faces = [[0] * len(self._mobile_nodes) for i in range(len(self._stationary_nodes)) ] # rsu, car -> faceid self.to_rsu_faces = [[0] * len(self._mobile_nodes) for i in range(len(self._stationary_nodes)) ] # rsu, car -> faceid self._velocities = [] self._heading_directions = [] self._starting_points = [] for node in self._mobile_nodes: self._velocities.append(node.speed) self._heading_directions.append(node.direction) self._starting_points.append(node.spawn_point) self._is_running = False # flag indicating if the simulation is running or not self._function_names = function_names # list of function names to be invoked by the nodes self._named_functions = named_functions # a dict of function definitions to be invoked self._chunk_size = 8192 self._simulation_bus = SimulationBus( packetencoder=SimpleStringEncoder()) self._stationary_node_name_prefix = Name("/rsu") self._mobile_node_to_computation = [0] * len( mobile_nodes) # index which mobile node issues which computation if use_distribution_helper: # TODO in the future support more distribution types, e.g., uniform, gaussian, etc. dist_array = ZipfMandelbrotDistribution.create_zipf_mandelbrot_distribution( len(self._function_names), 0.7, 0.7) for i in range(0, len(mobile_nodes)): self._mobile_node_to_computation[i] = ZipfMandelbrotDistribution.\ get_next_zipfmandelbrot_random_number(dist_array, len(self._function_names)) - 1 # get_next_zipfmandelbrot_random_number(dist_array, len(self._function_names), run_id) - 1 self._compute_rsu_connection_time() self._setup_simulation_network()
class Initiation(unittest.TestCase): """Simulate a Map Reduce Scenario where timeout prevention is required.""" @abc.abstractmethod def get_encoder(self) -> BasicEncoder: return SimpleStringEncoder def setUp(self): self.encoder_type = self.get_encoder() self.simulation_bus = SimulationBus(packetencoder=self.encoder_type()) self.fetch_tool1 = Fetch( "distributor", None, 255, self.encoder_type(), interfaces=[self.simulation_bus.add_interface("fetchtool1")]) self.distributor = NFNForwarder( port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("distributor")], log_level=255, ageing_interval=3) self.nfn1 = NFNForwarder( port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("nfn1")], log_level=255, ageing_interval=3) self.nfn2 = NFNForwarder( port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("nfn2")], log_level=255, ageing_interval=3) self.nfn3 = NFNForwarder( port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("nfn3")], log_level=255, ageing_interval=3) self.nfn4 = NFNForwarder( port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("nfn4")], log_level=255, ageing_interval=3) self.repo1 = ICNDataRepository( "/tmp/repo1", Name("/repo/r1"), 0, 255, self.encoder_type(), False, False, [self.simulation_bus.add_interface("repo1")]) self.repo2 = ICNDataRepository( "/tmp/repo2", Name("/repo/r2"), 0, 255, self.encoder_type(), False, False, [self.simulation_bus.add_interface("repo2")]) self.repo3 = ICNDataRepository( "/tmp/repo3", Name("/repo/r3"), 0, 255, self.encoder_type(), False, False, [self.simulation_bus.add_interface("repo3")]) self.repo4 = ICNDataRepository( "/tmp/repo4", Name("/repo/r4"), 0, 255, self.encoder_type(), False, False, [self.simulation_bus.add_interface("repo4")]) self.nfn1.icnlayer.pit.set_pit_timeout(50) self.nfn1.icnlayer.cs.set_cs_timeout(0) self.nfn2.icnlayer.pit.set_pit_timeout(50) self.nfn2.icnlayer.cs.set_cs_timeout(0) self.nfn3.icnlayer.pit.set_pit_timeout(50) self.nfn3.icnlayer.cs.set_cs_timeout(0) self.nfn4.icnlayer.pit.set_pit_timeout(50) self.nfn4.icnlayer.cs.set_cs_timeout(0) self.mgmt_client0 = MgmtClient( self.distributor.mgmt.mgmt_sock.getsockname()[1]) self.mgmt_client1 = MgmtClient( self.nfn1.mgmt.mgmt_sock.getsockname()[1]) self.mgmt_client2 = MgmtClient( self.nfn2.mgmt.mgmt_sock.getsockname()[1]) self.mgmt_client3 = MgmtClient( self.nfn3.mgmt.mgmt_sock.getsockname()[1]) self.mgmt_client4 = MgmtClient( self.nfn4.mgmt.mgmt_sock.getsockname()[1]) def tearDown(self): self.distributor.stop_forwarder() self.nfn1.stop_forwarder() self.nfn2.stop_forwarder() self.nfn3.stop_forwarder() self.nfn4.stop_forwarder() self.repo1.stop_repo() self.repo2.stop_repo() self.repo3.stop_repo() self.repo4.stop_repo() self.fetch_tool1.stop_fetch() self.simulation_bus.stop_process() self.tearDown_repo() def setup_faces_and_connections(self): self.distributor.start_forwarder() self.nfn1.start_forwarder() self.nfn2.start_forwarder() self.nfn3.start_forwarder() self.nfn4.start_forwarder() self.repo1.start_repo() self.repo2.start_repo() self.repo3.start_repo() self.repo4.start_repo() self.simulation_bus.start_process() time.sleep(3) # setup forwarding rules self.mgmt_client0.add_face("nfn1", None, 0) self.mgmt_client0.add_forwarding_rule(Name("/lib"), [0]) self.mgmt_client0.add_face("nfn2", None, 0) self.mgmt_client0.add_forwarding_rule(Name("/lib"), [1]) self.mgmt_client0.add_face("nfn3", None, 0) self.mgmt_client0.add_forwarding_rule(Name("/lib"), [2]) self.mgmt_client0.add_face("nfn4", None, 0) self.mgmt_client0.add_forwarding_rule(Name("/lib4/func4"), [3]) self.mgmt_client1.add_face("repo1", None, 0) self.mgmt_client1.add_forwarding_rule(Name("/repo/r1"), [0]) self.mgmt_client2.add_face("repo2", None, 0) self.mgmt_client2.add_forwarding_rule(Name("/repo/r2"), [0]) self.mgmt_client3.add_face("repo3", None, 0) self.mgmt_client3.add_forwarding_rule(Name("/repo/r3"), [0]) self.mgmt_client4.add_face("repo4", None, 0) self.mgmt_client4.add_forwarding_rule(Name("/repo/r4"), [0]) self.mgmt_client1.add_face("nfn0", None, 0) self.mgmt_client1.add_forwarding_rule(Name("/lib"), [1]) self.mgmt_client2.add_face("nfn0", None, 0) self.mgmt_client2.add_forwarding_rule(Name("/lib"), [1]) self.mgmt_client3.add_face("nfn0", None, 0) self.mgmt_client3.add_forwarding_rule(Name("/lib"), [1]) self.mgmt_client4.add_face("nfn0", None, 0) self.mgmt_client4.add_forwarding_rule(Name("/lib4/func4"), [1]) #setup function code #self.mgmt_client1.add_new_content(Name("/lib/func1"),"PYTHON\nf\ndef f(n):\n return n") self.mgmt_client1.add_new_content( Name("/lib/func1"), "PYTHON\nf\ndef f():\n result =[]\n x,y =0,1\n while x<n:\n result.append(x)\n x,y = y, y+x\n return result" ) self.mgmt_client2.add_new_content( Name("/lib/func1"), "PYTHON\nf\ndef f(n):\n result =[]\n x,y =0,1\n while x<n:\n result.append(x)\n x,y = y, y+x\n return result" ) # self.mgmt_client2.add_new_content(Name("/lib/func2"),"func2") self.mgmt_client3.add_new_content( Name("/lib/func1"), "PYTHON\nf\ndef f(n):\n result =[]\n x,y =0,1\n while x<n:\n result.append(x)\n x,y = y, y+x\n return result" ) self.mgmt_client4.add_new_content(Name("/lib4/func4"), "func4") # self.mgmt_client1.add_new_content(Name("/lib/func1"), # "PYTHON\nf\ndef f():\n for i in range(0,100000000):\n a.upper()\n return a.upper()") # self.mgmt_client2.add_new_content(Name("/lib/func2"), # "PYTHON\nf\ndef f(a):\n for i in range(0,100000000):\n a.upper()\n return a.upper()") # self.mgmt_client3.add_new_content(Name("/lib/func3"), # "PYTHON\nf\ndef f(a):\n for i in range(0,100000000):\n a.upper()\n return a.upper()") # self.mgmt_client4.add_new_content(Name("/lib/func4"), # "PYTHON\nf\ndef f(a):\n for i in range(0,100000000):\n a.upper()\n return a.upper()") # def setup_repo(self): for i in range(1, 5): self.path = "/tmp/repo" + str(i) try: os.stat(self.path) except: os.mkdir(self.path) with open(self.path + "/data" + str(i), 'w+') as content_file: content_file.write("data" + str(i)) def tearDown_repo(self): try: shutil.rmtree(self.path) os.remove("/tmp/repo") except: pass def test_simple_Fs(self): self.setup_repo() self.setup_faces_and_connections() name1 = Name("/lib/func1") name1 += '_()' name1 += "NFN" name2 = Name("/lib/func1") name2 += '_(500000)' name2 += "NFN" name3 = Name("/lib/func1") name3 += '_(5000)' name3 += "NFN" name4 = Name("/lib/func1") name4 += '_(900000000000000000)' name4 += "NFN" name5 = Name("/lib/func1") name5 += '_(68899455874)' name5 += "NFN" t1 = Fs_thread(name1, fetch_tool=self.fetch_tool1) t2 = Fs_thread(name2, fetch_tool=self.fetch_tool1) t3 = Fs_thread(name3, fetch_tool=self.fetch_tool1) t4 = Fs_thread(name4, fetch_tool=self.fetch_tool1) t5 = Fs_thread(name5, fetch_tool=self.fetch_tool1) t1.start() t2.start() t3.start() t4.start() t5.start() t1.join() t2.join() t3.join() t4.join() t5.join()
class StreamingSimulation(unittest.TestCase): """Simulate a Streaming Scenario.""" def ageing(self): return def setUp(self): self.simulation_bus = SimulationBus(packetencoder=NdnTlvEncoder()) self.fetch_tool1 = Fetch( "nfn0", None, 255, NdnTlvEncoder(), interfaces=[self.simulation_bus.add_interface("fetchtoo11")]) self.nfn0 = NFNForwarder( port=0, encoder=NdnTlvEncoder(), interfaces=[self.simulation_bus.add_interface("nfn0")], log_level=255, executors={"PYTHONSTREAM": NFNPythonExecutorStreaming()}, ageing_interval=1) self.nfn0.executors["PYTHONSTREAM"].initialize_executor( self.nfn0.nfnlayer.queue_to_lower, self.nfn0.nfnlayer.queue_from_lower, self.nfn0.nfnlayer.cs, False) self.nfn0.icnlayer.pit.ageing = self.ageing self.nfn0.timeoutpreventionlayer.ageing = self.ageing self.nfn1 = NFNForwarder( port=0, encoder=NdnTlvEncoder(), interfaces=[self.simulation_bus.add_interface("nfn1")], log_level=255, executors={"PYTHONSTREAM": NFNPythonExecutorStreaming()}, ageing_interval=1) self.nfn1.executors["PYTHONSTREAM"].initialize_executor( self.nfn1.nfnlayer.queue_to_lower, self.nfn1.nfnlayer.queue_from_lower, self.nfn1.nfnlayer.cs, False) self.nfn1.icnlayer.pit.ageing = self.ageing self.nfn1.timeoutpreventionlayer.ageing = self.ageing self.nfn12 = NFNForwarder( port=0, encoder=NdnTlvEncoder(), interfaces=[self.simulation_bus.add_interface("nfn12")], log_level=255, executors={"PYTHONSTREAM": NFNPythonExecutorStreaming()}, ageing_interval=1) self.nfn12.executors["PYTHONSTREAM"].initialize_executor( self.nfn12.nfnlayer.queue_to_lower, self.nfn12.nfnlayer.queue_from_lower, self.nfn12.nfnlayer.cs, False) self.nfn12.icnlayer.pit.ageing = self.ageing self.nfn12.timeoutpreventionlayer.ageing = self.ageing self.repo1 = ICNDataRepository( "/tmp/repo1", Name("/repo/r1"), 0, 255, NdnTlvEncoder(), False, False, interfaces=[self.simulation_bus.add_interface("repo1")]) self.mgmt_client0 = MgmtClient( self.nfn0.mgmt.mgmt_sock.getsockname()[1]) self.mgmt_client1 = MgmtClient( self.nfn1.mgmt.mgmt_sock.getsockname()[1]) self.mgmt_client01 = MgmtClient( self.nfn12.mgmt.mgmt_sock.getsockname()[1]) def tearDown(self): self.nfn0.stop_forwarder() self.nfn1.stop_forwarder() self.nfn12.stop_forwarder() self.repo1.stop_repo() self.fetch_tool1.stop_fetch() self.simulation_bus.stop_process() self.tearDown_repo() def setup_faces_and_connections(self): self.nfn0.start_forwarder() self.nfn1.start_forwarder() self.nfn12.start_forwarder() self.repo1.start_repo() self.simulation_bus.start_process() time.sleep(3) #setup forwarding rules self.mgmt_client0.add_face("nfn1", None, 0) self.mgmt_client0.add_forwarding_rule(Name("/lib0"), [0]) self.mgmt_client0.add_face("nfn12", None, 0) self.mgmt_client0.add_forwarding_rule(Name("/lib1"), [1]) self.mgmt_client01.add_face("nfn1", None, 0) self.mgmt_client01.add_forwarding_rule(Name("/lib1"), [0]) self.mgmt_client1.add_face("repo1", None, 0) self.mgmt_client1.add_forwarding_rule(Name("/repo/r1"), [0]) self.mgmt_client1.add_face("nfn0", None, 0) self.mgmt_client1.add_forwarding_rule(Name("/lib0"), [1]) #setup function code #multi name self.mgmt_client1.add_new_content( Name("/lib0/multiname"), "PYTHONSTREAM\nmultiname\ndef multiname(arg):\n a = get_next(arg)\n a = a.upper()\n b = get_next(arg)\n b = b.upper()\n c = get_next(arg)\n c = c.upper()\n d = get_next(arg)\n d = d.upper()\n e = get_next(arg)\n e = e.upper()\n f = get_next(arg)\n f = f.upper()\n g = get_next(arg)\n g = g.upper()\n h = get_next(arg)\n h = h.upper()\n i = get_next(arg)\n i = i.upper()\n j = get_next(arg)\n j = j.upper()\n return a + b + c + d + e + f + g + h + i + j" ) #single name - two layer self.mgmt_client0.add_new_content( Name("/lib0/node0"), "PYTHONSTREAM\ngetnext_on_writeout\ndef getnext_on_writeout(arg):\n print('Start äussere')\n a = get_next(arg)\n a = a.upper()\n b = get_next(arg)\n b = b.upper()\n c = get_next(arg)\n c = c.upper()\n d = get_next(arg)\n d = d.upper()\n e = get_next(arg)\n e = e.upper()\n f = get_next(arg)\n f = f.upper()\n g = get_next(arg)\n g = g.upper()\n h = get_next(arg)\n h = h.upper()\n i = get_next(arg)\n i = i.upper()\n j = get_next(arg)\n j = j.upper()\n print('Ende äussere')\n return a + b + c + d + e + f + g + h + i + j" ) self.mgmt_client1.add_new_content( Name("/lib0/node1"), "PYTHONSTREAM\nwriteout_on_getnext\ndef writeout_on_getnext(arg):\n print('Start innere')\n write_out_on_get_next(arg)\n return print('Ende innere')\n" ) #single name - three layer self.mgmt_client0.add_new_content( Name("/lib1/node0"), "PYTHONSTREAM\ngetnext_on_writeout\ndef getnext_on_writeout(arg):\n print('Start äussere')\n a = get_next(arg)\n a = a.upper()\n b = get_next(arg)\n b = b.upper()\n c = get_next(arg)\n c = c.upper()\n d = get_next(arg)\n d = d.upper()\n e = get_next(arg)\n e = e.upper()\n f = get_next(arg)\n f = f.upper()\n g = get_next(arg)\n g = g.upper()\n h = get_next(arg)\n h = h.upper()\n i = get_next(arg)\n i = i.upper()\n j = get_next(arg)\n j = j.upper()\n print('Ende äussere')\n return a + b + c + d + e + f + g + h + i + j" ) self.mgmt_client01.add_new_content( Name("/lib1/node1"), "PYTHONSTREAM\nwriteout_on_getnext\ndef writeout_on_getnext(arg):\n print('Start mittlere')\n write_out_on_get_next(arg)\n return print('Ende mittlere')\n" ) self.mgmt_client1.add_new_content( Name("/lib1/node2"), "PYTHONSTREAM\nwriteout_on_getnext\ndef writeout_on_getnext(arg):\n print('Start innere')\n write_out_on_get_next(arg)\n return print('Ende innere')\n" ) def generate_name_files(self, path: str, number: int): with open(path + "/name" + str(number), "w") as f: f.write("Content of name" + str(number) + ". ") f.close() def setup_repo(self): self.path = "/tmp/repo1" try: os.stat(self.path) except: os.mkdir(self.path) with open(self.path + "/data1", 'w+') as content_file: content_file.write("sdo:\n") for i in range(1, 11): content_file.write("/repo/r1/name" + str(i) + "\n") self.generate_name_files(self.path, i) content_file.write("sdo:endstreaming\n") def tearDown_repo(self): try: shutil.rmtree(self.path) os.remove("/tmp/repo") except: pass def test_multiname(self): """Multiname test with input data from repo""" self.setup_repo() self.setup_faces_and_connections() name = Name("/lib0/multiname") name += '_(/repo/r1/data1)' name += "NFN" res = self.fetch_tool1.fetch_data(name, timeout=40) print(res) self.assertEqual( "CONTENT OF NAME1. CONTENT OF NAME2. CONTENT OF NAME3. CONTENT OF NAME4. CONTENT OF NAME5. CONTENT OF NAME6. CONTENT OF NAME7. CONTENT OF NAME8. CONTENT OF NAME9. CONTENT OF NAME10. ", res) def test_twolayer(self): """Singlename test over two layers with input data from repo""" self.setup_repo() self.setup_faces_and_connections() scenario_node_1 = Name("/lib0/node1") scenario_node_1 += "#(=/repo/r1/data1=)" scenario_node_1 += "NFN" scenario_node_0 = Name("/lib0/node0") scenario_node_0 += '_("' + str(scenario_node_1) + '")' scenario_node_0 += "NFN" res = self.fetch_tool1.fetch_data(scenario_node_0, timeout=40) print(res) self.assertEqual( "CONTENT OF NAME1. CONTENT OF NAME2. CONTENT OF NAME3. CONTENT OF NAME4. CONTENT OF NAME5. CONTENT OF NAME6. CONTENT OF NAME7. CONTENT OF NAME8. CONTENT OF NAME9. CONTENT OF NAME10. ", res) def test_threelayer(self): """Singlename test over three layers with input data from repo""" self.setup_repo() self.setup_faces_and_connections() scenario_node_2 = Name("/lib1/node2") scenario_node_2 += "#(=/repo/r1/data1=)" scenario_node_2 += "NFN" scenario_node_1 = Name("/lib1/node1") scenario_node_1 += "#(=" + str(scenario_node_2) + "=)" scenario_node_1 += "NFN" scenario_node_0 = Name("/lib1/node0") scenario_node_0 += '_("' + str(scenario_node_1) + '")' scenario_node_0 += "NFN" res = self.fetch_tool1.fetch_data(scenario_node_0, timeout=40) print(res) self.assertEqual( "CONTENT OF NAME1. CONTENT OF NAME2. CONTENT OF NAME3. CONTENT OF NAME4. CONTENT OF NAME5. CONTENT OF NAME6. CONTENT OF NAME7. CONTENT OF NAME8. CONTENT OF NAME9. CONTENT OF NAME10. ", res)
class DetectionMapSimulation(): """Run a simple Data Offloading Scenario Simulation""" 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 tearDown(self): for car in self.cars: car.stop_forwarder() for fetch_tool_car in self.fetch_tool_cars: 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): for car in self.cars: car.start_forwarder() for rsu in self.rsus: rsu.start_forwarder() self.simulation_bus.start_process() function1 = "PYTHON\nf\ndef f(a, b, c):\n return detection_map(a, b, c)" function2 = "PYTHON\nf\ndef f(a, b, c):\n return detection_map_2(a, b, c)" # Setup rsu0 self.mgmt_clients[0].add_face("car0", None, 0) self.mgmt_clients[0].add_face("car1", None, 0) self.mgmt_clients[0].add_face("rsu1", None, 0) self.mgmt_clients[0].add_forwarding_rule(Name("/car0"), [0]) self.mgmt_clients[0].add_forwarding_rule(Name("/car1"), [1]) self.mgmt_clients[0].add_forwarding_rule(Name("/nR"), [2]) self.mgmt_clients[0].add_new_content(Name("/rsu/func/f1"), function1) self.mgmt_clients[0].add_new_content(Name("/rsu/func/f2"), function2) # Setup rsu1 self.mgmt_clients[1].add_face("car0", None, 0) self.mgmt_clients[1].add_face("car1", 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("/car0"), [0]) self.mgmt_clients[1].add_forwarding_rule(Name("/car1"), [1]) self.mgmt_clients[1].add_forwarding_rule(Name("/nL"), [2]) self.mgmt_clients[1].add_forwarding_rule(Name("/nR"), [3]) self.mgmt_clients[1].add_new_content(Name("/rsu/func/f1"), function1) self.mgmt_clients[1].add_new_content(Name("/rsu/func/f2"), function2) # Setup rsu2 self.mgmt_clients[2].add_face("car0", None, 0) self.mgmt_clients[2].add_face("car1", None, 0) self.mgmt_clients[2].add_face("rsu1", None, 0) self.mgmt_clients[2].add_forwarding_rule(Name("/car0"), [0]) self.mgmt_clients[2].add_forwarding_rule(Name("/car1"), [1]) self.mgmt_clients[2].add_forwarding_rule(Name("/nL"), [2]) # Setup car0 self.mgmt_client_cars[0].add_face("rsu0", None, 0) self.mgmt_client_cars[0].add_face("rsu1", None, 0) self.mgmt_client_cars[0].add_face("rsu2", None, 0) self.mgmt_client_cars[0].add_forwarding_rule(Name("/rsu"), [0]) # Setup car1 self.mgmt_client_cars[1].add_face("rsu0", None, 0) self.mgmt_client_cars[1].add_face("rsu1", None, 0) self.mgmt_client_cars[1].add_face("rsu2", None, 0) self.mgmt_client_cars[1].add_forwarding_rule(Name("/rsu"), [0]) # Add image to content store of car0 # Streetview 1 # image_path = os.path.join(ROOT_DIR, "Demos/DetectionMap/Assets/street1.jpg") # image_path = os.path.join(ROOT_DIR, "Demos/DetectionMap/Assets/image1_small.jpg") image_path = os.path.join(ROOT_DIR, "Demos/DetectionMap/Assets/image8.jpg") base64_image = Helper.pre_process(image_path, 2048, 2, ".jpg") # map_det_obj0 = DetectionMapObject(base64_image, 47.566192, 7.590686, 65, 0.4, 1.8) # map_det_obj0 = DetectionMapObject(base64_image, 47.377391, 8.539347, 294, 0.8) map_det_obj0 = DetectionMapObject(base64_image, 47.369645, 8.539652, 300, 0.7, 0.8) image = Content(Name("/car0/image"), map_det_obj0.to_string()) self.meta_data0, self.data0 = self.chunkifyer.chunk_data(image) for md in self.meta_data0: self.cars[0].icnlayer.cs.add_content_object(md) for d in self.data0[:10]: self.cars[0].icnlayer.cs.add_content_object(d) # Add image to content store of car1 # Streetview 2 image_path = os.path.join(ROOT_DIR, "Demos/DetectionMap/Assets/street2.jpg") base64_image = Helper.pre_process(image_path, 1024, 2, ".jpg") map_det_obj1 = DetectionMapObject(base64_image, 47.566463, 7.590982, 345, 0.4, 1.8) image = Content(Name("/car1/image"), map_det_obj1.to_string()) self.meta_data1, self.data1 = self.chunkifyer.chunk_data(image) for md in self.meta_data1: self.cars[1].icnlayer.cs.add_content_object(md) for d in self.data1: self.cars[1].icnlayer.cs.add_content_object(d) def test_map_detection_1_image(self): self.setup_faces_and_connections() name = Name("/rsu/func/f1") name += "_(/car0/image,1,0)" name += "NFN" print("RSU 0 FETCHING") result = self.fetch_tools[0].fetch_data(name, 360) print(result) sleep(2) print("\n" * 20 + "RSU 1 FETCHING") for d in self.data0[10:]: self.cars[0].icnlayer.cs.add_content_object(d) for d in self.data0[:10]: self.cars[0].icnlayer.cs.remove_content_object(d.name) # for md in self.meta_data0: # self.cars[0].icnlayer.cs.remove_content_object(md.name) # for md in self.meta_data0: # self.cars[0].icnlayer.cs.add_content_object(md) result = self.fetch_tools[1].fetch_data(name, 60) print(result)
class TimeoutPreventionSimulation(unittest.TestCase): """Simulate a Scenario where timeout prevention is required""" @abc.abstractmethod def get_encoder(self) -> BasicEncoder: return SimpleStringEncoder def setUp(self): self.encoder_type = self.get_encoder() self.simulation_bus = SimulationBus(packetencoder=self.encoder_type()) self.fetch_tool1 = Fetch( "nfn1", None, 255, self.encoder_type(), interfaces=[self.simulation_bus.add_interface("fetchtool1")]) self.nfn1 = NFNForwarder( port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("nfn1")], log_level=255, ageing_interval=1) self.nfn2 = NFNForwarder( port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("nfn2")], log_level=255, ageing_interval=1) self.simulation_bus.add_interface("deadend") self.nfn1.icnlayer.pit.set_pit_timeout(0) self.nfn1.icnlayer.cs.set_cs_timeout(30) self.nfn2.icnlayer.pit.set_pit_timeout(0) self.nfn2.icnlayer.cs.set_cs_timeout(30) self.nfn1.nfnlayer.optimizer = ToDataFirstOptimizer( self.nfn1.icnlayer.cs, self.nfn1.icnlayer.fib, self.nfn1.icnlayer.pit, self.nfn1.linklayer.faceidtable) self.nfn2.nfnlayer.optimizer = ToDataFirstOptimizer( self.nfn2.icnlayer.cs, self.nfn2.icnlayer.fib, self.nfn2.icnlayer.pit, self.nfn2.linklayer.faceidtable) self.mgmt_client1 = MgmtClient( self.nfn1.mgmt.mgmt_sock.getsockname()[1]) self.mgmt_client2 = MgmtClient( self.nfn2.mgmt.mgmt_sock.getsockname()[1]) def tearDown(self): self.nfn1.stop_forwarder() self.nfn2.stop_forwarder() self.fetch_tool1.stop_fetch() self.simulation_bus.stop_process() def setup_faces_and_connections(self): self.nfn1.start_forwarder() self.nfn2.start_forwarder() self.simulation_bus.start_process() # setup nfn1 self.mgmt_client1.add_face("nfn2", None, 0) self.mgmt_client1.add_forwarding_rule(Name("/lib"), [0]) self.mgmt_client1.add_new_content(Name("/test/data/string"), "This is a String") # setup nfn2 self.mgmt_client1.add_face("nfn1", None, 0) self.mgmt_client1.add_forwarding_rule(Name("/test"), [0]) self.mgmt_client2.add_new_content( Name("/lib/func/f1"), "PYTHON\nf\ndef f(a):\n for i in range(0,100000000):\n a.upper()\n return a.upper() + ' WITHOUT TIMEOUT'" ) def test_simple_timeout_prevention(self): """Simple test to see if timeout prevention works""" self.setup_faces_and_connections() name = Name("/lib/func/f1") name += '_("helloworld")' name += "NFN" res = self.fetch_tool1.fetch_data(name, timeout=0) time.sleep(3) self.assertEqual("HELLOWORLD WITHOUT TIMEOUT", res) print(res) def test_timeout_prevention_if_no_comp(self): """Simple test to see if timeout prevention works if no computation is available and no fwd rule""" self.setup_faces_and_connections() name = Name("/lib/func/f2") name += '_("helloworld")' name += "NFN" res = self.fetch_tool1.fetch_data(name, timeout=0) self.assertEqual( "Received Nack: one or many input data (function or data) is unavailable", res) time.sleep(3) print(res) def test_timeout_prevention_if_no_comp_no_data_fwd(self): """Simple test to see if timeout prevention works if no computation is available and no data are replied""" self.setup_faces_and_connections() name = Name("/lib/func/f2") name += '_("helloworld")' name += "NFN" self.mgmt_client2.add_face("deadend", None, 0) self.mgmt_client2.add_forwarding_rule(Name("/lib"), [0]) res = self.fetch_tool1.fetch_data(name, timeout=0) self.assertEqual( "Received Nack: one or many input data (function or data) is unavailable", res) time.sleep(3) print(res)
from PiCN.ProgramLibs.Fetch import Fetch from PiCN.ProgramLibs.NFNForwarder import NFNForwarder from PiCN.Layers.LinkLayer.Interfaces import SimulationBus from PiCN.Layers.LinkLayer.Interfaces import AddressInfo from PiCN.Layers.PacketEncodingLayer.Encoder import BasicEncoder, SimpleStringEncoder, NdnTlvEncoder from PiCN.Mgmt import MgmtClient from PiCN.Packets import Content, Interest, Name simulation_bus = SimulationBus(packetencoder=NdnTlvEncoder()) nfn_fwd0 = NFNForwarder(port=0, encoder=NdnTlvEncoder(), interfaces=[simulation_bus.add_interface("nfn0")], log_level=255, ageing_interval=1) nfn_fwd1 = NFNForwarder(port=0, encoder=NdnTlvEncoder(), interfaces=[simulation_bus.add_interface("nfn1")], log_level=255, ageing_interval=1) mgmt_client0 = MgmtClient(nfn_fwd0.mgmt.mgmt_sock.getsockname()[1]) mgmt_client1 = MgmtClient(nfn_fwd1.mgmt.mgmt_sock.getsockname()[1]) fetch_tool = Fetch("nfn0", None, 255, NdnTlvEncoder(), interfaces=[simulation_bus.add_interface("fetchtool1")]) nfn_fwd0.start_forwarder()
class DetectionMapVideoSimulation(): """Run a simple data-offloading simulation""" 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(30) # 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)) 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): for car in self.cars: car.stop_forwarder() for fetch_tool_car in self.fetch_tool_cars: 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): for car in self.cars: car.start_forwarder() for rsu in self.rsus: rsu.start_forwarder() self.simulation_bus.start_process() function = "PYTHON\nf\ndef f(a, b, c):\n return detection_map(a, b, c)" # setup rsu0 self.mgmt_clients[0].add_face("car0", None, 0) self.mgmt_clients[0].add_face("car1", None, 0) self.mgmt_clients[0].add_face("rsu1", None, 0) self.mgmt_clients[0].add_forwarding_rule(Name("/car0"), [0]) self.mgmt_clients[0].add_forwarding_rule(Name("/car1"), [1]) self.mgmt_clients[0].add_forwarding_rule(Name("/nR"), [2]) self.mgmt_clients[0].add_new_content(Name("/rsu/func/f1"), function) # setup rsu1 self.mgmt_clients[1].add_face("car0", None, 0) self.mgmt_clients[1].add_face("car1", 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("/car0"), [0]) self.mgmt_clients[1].add_forwarding_rule(Name("/car1"), [1]) self.mgmt_clients[1].add_forwarding_rule(Name("/nL"), [2]) self.mgmt_clients[1].add_forwarding_rule(Name("/nR"), [3]) self.mgmt_clients[1].add_new_content(Name("/rsu/func/f1"), function) # setup rsu2 self.mgmt_clients[2].add_face("car0", None, 0) self.mgmt_clients[2].add_face("car1", None, 0) self.mgmt_clients[2].add_face("rsu1", None, 0) self.mgmt_clients[2].add_forwarding_rule(Name("/car0"), [0]) self.mgmt_clients[2].add_forwarding_rule(Name("/car1"), [1]) self.mgmt_clients[2].add_forwarding_rule(Name("/nL"), [2]) # setup car0 self.mgmt_client_cars[0].add_face("rsu0", None, 0) self.mgmt_client_cars[0].add_face("rsu1", None, 0) self.mgmt_client_cars[0].add_face("rsu2", None, 0) self.mgmt_client_cars[0].add_forwarding_rule(Name("/rsu"), [0]) # extract frames from video fps, frames = Helper.video_to_frames( os.path.join(ROOT_DIR, "Demos/DetectionMap/Assets/108.mp4")) fps = int(np.round(fps)) # read json file containing gps coords and bearing # file contains one entry per second with open( os.path.join(ROOT_DIR, "Demos/DetectionMap/Assets/locations.json")) as f: data = json.load(f) locations = data.get("locations") # duplicate last entry, needed for the interpolation in the next step locations.append(locations[-1]) # interpolate between the values to create one entry per frame lats = [] longs = [] bearings = [] for i in range(len(locations) - 1): lat_current = locations[i].get("latitude") lat_next = locations[i + 1].get("latitude") long_current = locations[i].get("longitude") long_next = locations[i + 1].get("longitude") bearing_current = locations[i].get("course") bearing_next = locations[i + 1].get("course") if np.abs(lat_next - lat_current) > 0: lats.append( np.arange(lat_current, lat_next, (lat_next - lat_current) / fps)) else: lats.append([lat_current] * fps) if np.abs(long_next - long_current) > 0: longs.append( np.arange(long_current, long_next, (long_next - long_current) / fps)) else: longs.append([long_current] * fps) if np.abs(bearing_next - bearing_current) > 0: bearings.append( np.arange(bearing_current, bearing_next, (bearing_next - bearing_current) / fps)) else: bearings.append([bearing_current] * fps) # flatten all the lists lats = [item for sublist in lats for item in sublist] longs = [item for sublist in longs for item in sublist] bearings = [item for sublist in bearings for item in sublist] self.mdos = [] for i, frame in enumerate(frames): encoded = cv2.imencode('.jpg', frame)[1].tostring() base64_image = base64.b64encode(encoded) map_det_obj0 = DetectionMapObject(base64_image, 0.8, lats[i], longs[i], bearings[i]) self.mdos.append(map_det_obj0) def test_detection_map_video(self): self.setup_faces_and_connections() n = 0 for i, mdo in enumerate(self.mdos[n:60]): name = Name("/rsu/func/f1") name += f"_(/car0/image{i+n},1,{i+n})" name += "NFN" image = Content(Name(f"/car0/image{i+n}"), mdo.to_string()) self.meta_data, self.data = self.chunkifyer.chunk_data(image) for md in self.meta_data: self.cars[0].icnlayer.cs.add_content_object(md) for d in self.data: self.cars[0].icnlayer.cs.add_content_object(d) print("\t" * 5) print("RSU 0 FETCHING") result = self.fetch_tools[0].fetch_data(name, 360) print(result) sleep(1) # create video file from the resulting plots and detections print("Creating video files...") path_in = os.path.join(ROOT_DIR, "Demos/DetectionMap/Assets/Plots/") path_out = os.path.join( ROOT_DIR, "Demos/DetectionMap/Assets/Videos/plots_video.mp4") Helper.frames_to_video(path_in, path_out, 30) path_in = os.path.join(ROOT_DIR, "Demos/DetectionMap/Assets/Classified/") path_out = os.path.join( ROOT_DIR, "Demos/DetectionMap/Assets/Videos/detections_video.mp4") Helper.frames_to_video(path_in, path_out, 30)
class MobileEdgeComputingSimulation(): """Mobile Edge Computing Simulation""" def __init__(self): """define the configuration""" self.number_of_rsus = 4 self.number_of_cars = 5 self.to_car_faces = [[0] * self.number_of_cars for i in range(self.number_of_rsus)] #rsu, car -> faceid self.to_rsu_faces = [[0] * self.number_of_cars for i in range(self.number_of_rsus)] #rsu, car -> faceid self.car_speed = [60, 60, 60, 60, 60] #in km/h self.car_direction = [1, 1 ,1 ,1 ,1] # 1 or -1, consider starting rsu by choosing direction. self.starting_rsu = [0, 0, 0, 0, 0] self.rsu_distance = 0.5 #in km, range is identical here self.computations = self.get_computations() #Store computations in here, car will randomly choose one self.named_functions ={"/rsu/func/f1": "PYTHON\nf\ndef f(a, b, c):\n return a+b+c", "/rsu/func/f2": "PYTHON\nf\ndef f(a, b, c):\n return a*b*c"} #named functions stored on the self.chunk_size = 8192 self.optimizer = EdgeComputingOptimizer #NFN resolution strategy optimizer self.encoder_type = SimpleStringEncoder() self.simulation_bus = SimulationBus(packetencoder=self.encoder_type) self.rsu_name = Name("/rsu") self.car_to_computation = [0,0,0,0,0] #index which car issues which computation self._compute_rsu_connection_time() self.setup_network() def get_computations(self): comps = [] c1 = Name("/rsu/func/f1") c1 += "_(1,2,3)" c1 += "NFN" comps.append(c1) return comps def _compute_rsu_connection_time(self): """compute the connection time based on speed and distance""" self.contact_time = [] for s in self.car_speed: speed_in_ms = s/3.6 distance_in_m = 1000*self.rsu_distance self.contact_time.append(distance_in_m/speed_in_ms * 1e9) 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() def reconnect_car(self, car_number, new_rsu_number): if self.number_of_rsus <= new_rsu_number or new_rsu_number < 0: print(f"{time.time():.5f} --- Cannot Reconnect Car {car_number} to RSU {new_rsu_number}, not part of this simulation") return connected_rsu = self.connected_rsu[car_number] self.car_forwarders[car_number].icnlayer.fib.remove_fib_entry(self.rsu_name) self.car_forwarders[car_number].icnlayer.fib.add_fib_entry(self.rsu_name, [self.to_rsu_faces[new_rsu_number][car_number]]) self.rsus[connected_rsu].icnlayer.fib.remove_fib_entry(Name(f"/car/car{car_number}")) self.rsus[connected_rsu].icnlayer.pit.remove_pit_entry_by_fid(self.to_car_faces[connected_rsu][car_number]) self.rsus[new_rsu_number].icnlayer.fib.add_fib_entry(Name(f"/car/car{car_number}"), [self.to_car_faces[new_rsu_number][car_number]]) self.connected_rsu[car_number] = self.connected_rsu[connected_rsu] + self.car_direction[connected_rsu] self.car_send_interest(car_number, self.computations[self.car_to_computation[car_number]]) def car_send_interest(self, number, name): self.car_fetch[number].fetch_data(name, timeout=1) # if trouble reduce timeout to 0.1. Parse result from log def mainloop(self): """run the experiment, hand over the cars""" isRunning = True self.connected_rsu = [] #connect cars to the initial RSU for i in range(0, self.number_of_cars): self.connected_rsu.append(self.starting_rsu[i]) self.car_forwarders[i].icnlayer.fib.add_fib_entry(self.rsu_name, [self.to_rsu_faces[self.connected_rsu[i]][i]]) self.rsus[self.connected_rsu[i]].icnlayer.fib.add_fib_entry(Name(f"car{i}"), [self.to_car_faces[self.connected_rsu[i]][i]]) self.car_send_interest(i, self.computations[self.car_to_computation[i]]) self.connection_time = [time.time_ns()] * self.number_of_cars steps = 3 * self.number_of_cars while(isRunning): time_ns = time.time_ns() for i in range(0, self.number_of_cars): if time_ns - self.connection_time[i] > self.contact_time[i]: print(f"{time.time():.5f} -- " + "Car", i, "reconnects from", self.connected_rsu[i], "to", self.connected_rsu[i] + self.car_direction[i]) new_rsu_number = self.connected_rsu[i] + self.car_direction[i] self.reconnect_car(i, new_rsu_number) self.connection_time[i] = time.time_ns() steps -=1 if steps < 0: break
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)
class cases_Simulation(): """Test the PiCN Simulation System""" @abc.abstractmethod def get_encoder(self) -> BasicEncoder: pass 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) #self.simulation_bus.start_process() def tearDown(self): try: self.simulation_bus.stop_process() self.icn_forwarder1.stop_forwarder() self.icn_forwarder2.stop_forwarder() except: self.icn_forwarder2.stop_repo() pass def test_send_single_packet(self): """Test fetching a single content object over the simulation bus""" self.icn_forwarder1.start_forwarder() self.icn_forwarder2.start_forwarder() self.simulation_bus.start_process() 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 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 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_creation_of_simulation_face_mgmt(self): """Test the creation of a simulation face using a mgmt client""" 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 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 test_single_nfn_interest(self): """Test simulation with NFN nodes and a single packet """ self.icn_forwarder1 = NFNForwarder(port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("icnfwd1")]) self.icn_forwarder2 = NFNForwarder(port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("icnfwd2")]) 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_client1.add_new_content(Name("/func/f1"), "PYTHON\nf\ndef f(a):\n return a.upper()") mgmt_client2 = MgmtClient(self.icn_forwarder2.mgmt.mgmt_sock.getsockname()[1]) mgmt_client2.add_new_content(Name("/test/data"), "HelloWorld") mgmt_client2.add_face("icnfwd1", None, 0) mgmt_client2.add_forwarding_rule(Name("/func"), [0]) interest = Interest("/test/data") interest.name += "/func/f1(_)" interest.name += "NFN" 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(interest.name, "HELLOWORLD")) mgmt_client1.shutdown() mgmt_client2.shutdown() def test_singl_interest_repo(self): """Test simulation by requesting data from a repo""" 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("A"*20000) self.icn_forwarder2 = ICNDataRepository(self.path, Name("/test/data"), 0, log_level=255, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("icnfwd2")]) self.icn_forwarder1.start_forwarder() self.icn_forwarder2.start_repo() 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]) interest = Interest("/test/data/f1") 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(interest.name, "mdo:20000:/test/data/f1/c0;/test/data/f1/c1;/test/data/f1/c2;/test/data/f1/c3:/test/data/f1/m1")) mgmt_client1.shutdown() def test_single_interest_repo_fetch(self): """Test simulation by requesting data from a repo using the fetch program lib and chunking""" 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("A" * 50000) self.icn_forwarder2 = ICNDataRepository(self.path, Name("/test/data"), 0, log_level=255, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("icnfwd2")]) self.fetchtool = Fetch("icnfwd1", None, 255, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("fetchtool")]) self.icn_forwarder1.start_forwarder() self.icn_forwarder2.start_repo() 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]) res = self.fetchtool.fetch_data(Name("/test/data/f1"), 3) self.assertEqual("A"*50000, res) mgmt_client1.shutdown() self.fetchtool.stop_fetch()
def setUp(self): self.simulation_bus = SimulationBus(packetencoder=NdnTlvEncoder()) self.fetch_tool1 = Fetch( "nfn0", None, 255, NdnTlvEncoder(), interfaces=[self.simulation_bus.add_interface("fetchtoo11")]) self.nfn0 = NFNForwarder( port=0, encoder=NdnTlvEncoder(), interfaces=[self.simulation_bus.add_interface("nfn0")], log_level=255, executors={"PYTHONSTREAM": NFNPythonExecutorStreaming()}, ageing_interval=1) self.nfn0.executors["PYTHONSTREAM"].initialize_executor( self.nfn0.nfnlayer.queue_to_lower, self.nfn0.nfnlayer.queue_from_lower, self.nfn0.nfnlayer.cs, False) self.nfn0.icnlayer.pit.ageing = self.ageing self.nfn0.timeoutpreventionlayer.ageing = self.ageing self.nfn1 = NFNForwarder( port=0, encoder=NdnTlvEncoder(), interfaces=[self.simulation_bus.add_interface("nfn1")], log_level=255, executors={"PYTHONSTREAM": NFNPythonExecutorStreaming()}, ageing_interval=1) self.nfn1.executors["PYTHONSTREAM"].initialize_executor( self.nfn1.nfnlayer.queue_to_lower, self.nfn1.nfnlayer.queue_from_lower, self.nfn1.nfnlayer.cs, False) self.nfn1.icnlayer.pit.ageing = self.ageing self.nfn1.timeoutpreventionlayer.ageing = self.ageing self.nfn12 = NFNForwarder( port=0, encoder=NdnTlvEncoder(), interfaces=[self.simulation_bus.add_interface("nfn12")], log_level=255, executors={"PYTHONSTREAM": NFNPythonExecutorStreaming()}, ageing_interval=1) self.nfn12.executors["PYTHONSTREAM"].initialize_executor( self.nfn12.nfnlayer.queue_to_lower, self.nfn12.nfnlayer.queue_from_lower, self.nfn12.nfnlayer.cs, False) self.nfn12.icnlayer.pit.ageing = self.ageing self.nfn12.timeoutpreventionlayer.ageing = self.ageing self.repo1 = ICNDataRepository( "/tmp/repo1", Name("/repo/r1"), 0, 255, NdnTlvEncoder(), False, False, interfaces=[self.simulation_bus.add_interface("repo1")]) self.mgmt_client0 = MgmtClient( self.nfn0.mgmt.mgmt_sock.getsockname()[1]) self.mgmt_client1 = MgmtClient( self.nfn1.mgmt.mgmt_sock.getsockname()[1]) self.mgmt_client01 = MgmtClient( self.nfn12.mgmt.mgmt_sock.getsockname()[1])
class EdgeComputingSimpleSimulation1(unittest.TestCase): """run the simple Edge Computing Scenario Simulation""" @abc.abstractmethod def get_encoder(self) -> BasicEncoder: return SimpleStringEncoder def setUp(self): self.encoder_type = self.get_encoder() self.simulation_bus = SimulationBus(packetencoder=self.encoder_type()) self.fetch_tool1 = Fetch( "rsu1", None, 255, self.encoder_type(), interfaces=[self.simulation_bus.add_interface("fetchtool1")]) self.fetch_tool2 = Fetch( "rsu2", None, 255, self.encoder_type(), interfaces=[self.simulation_bus.add_interface("fetchtool2")]) self.fetch_tool3 = Fetch( "rsu3", None, 255, self.encoder_type(), interfaces=[self.simulation_bus.add_interface("fetchtool3")]) self.rsu1 = NFNForwarder( port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("rsu1")], log_level=255, ageing_interval=1) self.rsu2 = NFNForwarder( port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("rsu2")], log_level=255, ageing_interval=1) self.rsu3 = NFNForwarder( port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("rsu3")], log_level=255, ageing_interval=1) self.rsu1.icnlayer.pit.set_pit_timeout(0) self.rsu1.icnlayer.cs.set_cs_timeout(30) self.rsu2.icnlayer.pit.set_pit_timeout(0) self.rsu2.icnlayer.cs.set_cs_timeout(30) self.rsu3.icnlayer.pit.set_pit_timeout(0) self.rsu3.icnlayer.cs.set_cs_timeout(30) self.rsu1.nfnlayer.optimizer = EdgeComputingOptimizer( self.rsu1.icnlayer.cs, self.rsu1.icnlayer.fib, self.rsu1.icnlayer.pit, self.rsu1.linklayer.faceidtable) self.rsu2.nfnlayer.optimizer = EdgeComputingOptimizer( self.rsu2.icnlayer.cs, self.rsu2.icnlayer.fib, self.rsu2.icnlayer.pit, self.rsu2.linklayer.faceidtable) self.rsu3.nfnlayer.optimizer = EdgeComputingOptimizer( self.rsu3.icnlayer.cs, self.rsu3.icnlayer.fib, self.rsu3.icnlayer.pit, self.rsu3.linklayer.faceidtable) self.mgmt_client1 = MgmtClient( self.rsu1.mgmt.mgmt_sock.getsockname()[1]) self.mgmt_client2 = MgmtClient( self.rsu2.mgmt.mgmt_sock.getsockname()[1]) self.mgmt_client3 = MgmtClient( self.rsu3.mgmt.mgmt_sock.getsockname()[1]) def tearDown(self): self.rsu1.stop_forwarder() self.rsu2.stop_forwarder() self.rsu3.stop_forwarder() self.fetch_tool1.stop_fetch() self.fetch_tool2.stop_fetch() self.fetch_tool3.stop_fetch() self.simulation_bus.stop_process() def setup_faces_and_connections(self): self.rsu1.start_forwarder() self.rsu2.start_forwarder() self.rsu3.start_forwarder() self.simulation_bus.start_process() #setup rsu1 self.mgmt_client1.add_face("rsu2", None, 0) self.mgmt_client1.add_forwarding_rule(Name("/rsu"), [0]) self.mgmt_client1.add_new_content( Name("/rsu/func/f1"), "PYTHON\nf\ndef f(a):\n for i in range(0,20000000):\n a.upper()\n return a.upper() + ' RSU1'" ) #setup rsu2 self.mgmt_client2.add_face("rsu1", None, 0) self.mgmt_client2.add_face("rsu3", None, 0) self.mgmt_client2.add_forwarding_rule(Name("/rsu"), [0, 1]) self.mgmt_client2.add_new_content( Name("/rsu/func/f1"), "PYTHON\nf\ndef f(a):\n for i in range(0,30000000):\n a.upper()\n return a.upper() + ' RSU2'" ) #setup rsu3 self.mgmt_client3.add_face("rsu2", None, 0) self.mgmt_client3.add_forwarding_rule(Name("/rsu"), [0]) self.mgmt_client3.add_new_content( Name("/rsu/func/f1"), "PYTHON\nf\ndef f(a):\n for i in range(0,40000000):\n a.upper()\n return a.upper() + ' RSU3'" ) def test_without_data_from_client(self): """execute a simple function on the rsus""" self.setup_faces_and_connections() name = Name("/rsu/func/f1") name += '_("helloworld")' name += "NFN" res = self.fetch_tool1.fetch_data(name, timeout=5) self.assertEqual(res, "HELLOWORLD RSU1") print("Result at RSU1:", res) time.sleep(5) res = self.fetch_tool2.fetch_data(name, timeout=5) print("Result as fetched via RSU2:", res) self.assertEqual(res, "HELLOWORLD RSU1") def test_inner_call_without_data_from_client(self): """execute one function on the first node and another function on the second node""" self.setup_faces_and_connections() self.mgmt_client2.add_new_content( Name("/rsu/func/f2"), "PYTHON\nf\ndef f(a):\n return a[::-1]") name1 = Name("/rsu/func/f1") name1 += '_("helloworld")' name1 += "NFN" name2 = Name("/rsu/func/f2") name2 += '_(/rsu/func/f1("helloworld"))' name2 += "NFN" res1 = self.fetch_tool1.fetch_data(name1, timeout=20) print(res1) time.sleep(6) res2 = self.fetch_tool2.fetch_data(name2, timeout=20) print(res2) def test_result_placement_using_names(self): """execute one function on a node where the client will be, when the computation is finished""" self.setup_faces_and_connections() self.mgmt_client3.add_new_content( Name("/rsu/id3/func/f2"), "PYTHON\nf\ndef f(a):\n for i in range(0,20000000):\n a.upper()\n return a.upper() + ' PLACED ON RSU3'" ) name1 = Name("/rsu/id3/func/f2") name1 += '_("helloworld")' name1 += "NFN" res1 = self.fetch_tool1.fetch_data(name1, timeout=10) print(res1) res2 = self.fetch_tool3.fetch_data(name1, timeout=10) print(res2)
def setUp(self): self.encoder_type = self.get_encoder() self.simulation_bus = SimulationBus(packetencoder=self.encoder_type()) self.fetch_tool1 = Fetch( "distributor", None, 255, self.encoder_type(), interfaces=[self.simulation_bus.add_interface("fetchtool1")]) self.distributor = NFNForwarder( port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("distributor")], log_level=255, ageing_interval=3) self.nfn1 = NFNForwarder( port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("nfn1")], log_level=255, ageing_interval=3) self.nfn2 = NFNForwarder( port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("nfn2")], log_level=255, ageing_interval=3) self.nfn3 = NFNForwarder( port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("nfn3")], log_level=255, ageing_interval=3) self.nfn4 = NFNForwarder( port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("nfn4")], log_level=255, ageing_interval=3) self.repo1 = ICNDataRepository( "/tmp/repo1", Name("/repo/r1"), 0, 255, self.encoder_type(), False, False, [self.simulation_bus.add_interface("repo1")]) self.repo2 = ICNDataRepository( "/tmp/repo2", Name("/repo/r2"), 0, 255, self.encoder_type(), False, False, [self.simulation_bus.add_interface("repo2")]) self.repo3 = ICNDataRepository( "/tmp/repo3", Name("/repo/r3"), 0, 255, self.encoder_type(), False, False, [self.simulation_bus.add_interface("repo3")]) self.repo4 = ICNDataRepository( "/tmp/repo4", Name("/repo/r4"), 0, 255, self.encoder_type(), False, False, [self.simulation_bus.add_interface("repo4")]) self.nfn1.icnlayer.pit.set_pit_timeout(50) self.nfn1.icnlayer.cs.set_cs_timeout(0) self.nfn2.icnlayer.pit.set_pit_timeout(50) self.nfn2.icnlayer.cs.set_cs_timeout(0) self.nfn3.icnlayer.pit.set_pit_timeout(50) self.nfn3.icnlayer.cs.set_cs_timeout(0) self.nfn4.icnlayer.pit.set_pit_timeout(50) self.nfn4.icnlayer.cs.set_cs_timeout(0) self.mgmt_client0 = MgmtClient( self.distributor.mgmt.mgmt_sock.getsockname()[1]) self.mgmt_client1 = MgmtClient( self.nfn1.mgmt.mgmt_sock.getsockname()[1]) self.mgmt_client2 = MgmtClient( self.nfn2.mgmt.mgmt_sock.getsockname()[1]) self.mgmt_client3 = MgmtClient( self.nfn3.mgmt.mgmt_sock.getsockname()[1]) self.mgmt_client4 = MgmtClient( self.nfn4.mgmt.mgmt_sock.getsockname()[1])
class MapReduceSimulation(unittest.TestCase): """Simulate a Map Reduce Scenario where timeout prevention is required.""" @abc.abstractmethod def get_encoder(self) -> BasicEncoder: return SimpleStringEncoder def setUp(self): self.encoder_type = self.get_encoder() self.simulation_bus = SimulationBus(packetencoder=self.encoder_type()) self.fetch_tool1 = Fetch( "nfn0", None, 255, self.encoder_type(), interfaces=[self.simulation_bus.add_interface("fetchtool1")]) self.nfn0 = NFNForwarder( port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("nfn0")], log_level=255, ageing_interval=1) self.nfn1 = NFNForwarder( port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("nfn1")], log_level=255, ageing_interval=1) self.nfn2 = NFNForwarder( port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("nfn2")], log_level=255, ageing_interval=1) self.nfn3 = NFNForwarder( port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("nfn3")], log_level=255, ageing_interval=1) self.nfn4 = NFNForwarder( port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("nfn4")], log_level=255, ageing_interval=1) self.repo1 = ICNDataRepository( "/tmp/repo1", Name("/repo/r1"), 0, 255, self.encoder_type(), False, False, [self.simulation_bus.add_interface("repo1")]) self.repo2 = ICNDataRepository( "/tmp/repo2", Name("/repo/r2"), 0, 255, self.encoder_type(), False, False, [self.simulation_bus.add_interface("repo2")]) self.repo3 = ICNDataRepository( "/tmp/repo3", Name("/repo/r3"), 0, 255, self.encoder_type(), False, False, [self.simulation_bus.add_interface("repo3")]) self.repo4 = ICNDataRepository( "/tmp/repo4", Name("/repo/r4"), 0, 255, self.encoder_type(), False, False, [self.simulation_bus.add_interface("repo4")]) self.nfn1.icnlayer.pit.set_pit_timeout(0) self.nfn1.icnlayer.cs.set_cs_timeout(30) self.nfn2.icnlayer.pit.set_pit_timeout(0) self.nfn2.icnlayer.cs.set_cs_timeout(30) self.nfn3.icnlayer.pit.set_pit_timeout(0) self.nfn3.icnlayer.cs.set_cs_timeout(30) self.nfn4.icnlayer.pit.set_pit_timeout(0) self.nfn4.icnlayer.cs.set_cs_timeout(30) self.nfn0.nfnlayer.optimizer = MapReduceOptimizer( self.nfn0.icnlayer.cs, self.nfn0.icnlayer.fib, self.nfn0.icnlayer.pit, self.nfn0.linklayer.faceidtable) self.nfn1.nfnlayer.optimizer = MapReduceOptimizer( self.nfn1.icnlayer.cs, self.nfn1.icnlayer.fib, self.nfn1.icnlayer.pit, self.nfn1.linklayer.faceidtable) self.nfn2.nfnlayer.optimizer = MapReduceOptimizer( self.nfn2.icnlayer.cs, self.nfn2.icnlayer.fib, self.nfn2.icnlayer.pit, self.nfn2.linklayer.faceidtable) self.nfn3.nfnlayer.optimizer = MapReduceOptimizer( self.nfn3.icnlayer.cs, self.nfn3.icnlayer.fib, self.nfn3.icnlayer.pit, self.nfn3.linklayer.faceidtable) self.nfn4.nfnlayer.optimizer = MapReduceOptimizer( self.nfn4.icnlayer.cs, self.nfn4.icnlayer.fib, self.nfn4.icnlayer.pit, self.nfn4.linklayer.faceidtable) self.mgmt_client0 = MgmtClient( self.nfn0.mgmt.mgmt_sock.getsockname()[1]) self.mgmt_client1 = MgmtClient( self.nfn1.mgmt.mgmt_sock.getsockname()[1]) self.mgmt_client2 = MgmtClient( self.nfn2.mgmt.mgmt_sock.getsockname()[1]) self.mgmt_client3 = MgmtClient( self.nfn3.mgmt.mgmt_sock.getsockname()[1]) self.mgmt_client4 = MgmtClient( self.nfn4.mgmt.mgmt_sock.getsockname()[1]) def tearDown(self): self.nfn0.stop_forwarder() self.nfn1.stop_forwarder() self.nfn2.stop_forwarder() self.nfn3.stop_forwarder() self.nfn4.stop_forwarder() self.repo1.stop_repo() self.repo2.stop_repo() self.repo3.stop_repo() self.repo4.stop_repo() self.fetch_tool1.stop_fetch() self.simulation_bus.stop_process() self.tearDown_repo() def setup_faces_and_connections(self): self.nfn0.start_forwarder() self.nfn1.start_forwarder() self.nfn2.start_forwarder() self.nfn3.start_forwarder() self.nfn4.start_forwarder() self.repo1.start_repo() self.repo2.start_repo() self.repo3.start_repo() self.repo4.start_repo() self.simulation_bus.start_process() time.sleep(3) # setup forwarding rules self.mgmt_client0.add_face("nfn1", None, 0) self.mgmt_client0.add_forwarding_rule(Name("/lib/func1"), [0]) self.mgmt_client0.add_face("nfn2", None, 0) self.mgmt_client0.add_forwarding_rule(Name("/lib/func2"), [1]) self.mgmt_client0.add_face("nfn3", None, 0) self.mgmt_client0.add_forwarding_rule(Name("/lib/func3"), [2]) self.mgmt_client0.add_face("nfn4", None, 0) self.mgmt_client0.add_forwarding_rule(Name("/lib/func4"), [3]) self.mgmt_client1.add_face("repo1", None, 0) self.mgmt_client1.add_forwarding_rule(Name("/repo/r1"), [0]) self.mgmt_client2.add_face("repo2", None, 0) self.mgmt_client2.add_forwarding_rule(Name("/repo/r2"), [0]) self.mgmt_client3.add_face("repo3", None, 0) self.mgmt_client3.add_forwarding_rule(Name("/repo/r3"), [0]) self.mgmt_client4.add_face("repo4", None, 0) self.mgmt_client4.add_forwarding_rule(Name("/repo/r4"), [0]) self.mgmt_client1.add_face("nfn0", None, 0) self.mgmt_client1.add_forwarding_rule(Name("/lib"), [1]) #setup function code self.mgmt_client0.add_new_content( Name("/lib/reduce4"), "PYTHON\nreduce4\ndef reduce4(a,b,c,d):\n return a+b+c+d") self.mgmt_client1.add_new_content( Name("/lib/func1"), "PYTHON\nf\ndef f(a):\n for i in range(0,100000000):\n a.upper()\n return a.upper()" ) self.mgmt_client2.add_new_content( Name("/lib/func2"), "PYTHON\nf\ndef f(a):\n for i in range(0,100000000):\n a.upper()\n return a.upper()" ) self.mgmt_client3.add_new_content( Name("/lib/func3"), "PYTHON\nf\ndef f(a):\n for i in range(0,100000000):\n a.upper()\n return a.upper()" ) self.mgmt_client4.add_new_content( Name("/lib/func4"), "PYTHON\nf\ndef f(a):\n for i in range(0,100000000):\n a.upper()\n return a.upper()" ) def setup_repo(self): for i in range(1, 5): self.path = "/tmp/repo" + str(i) try: os.stat(self.path) except: os.mkdir(self.path) with open(self.path + "/data" + str(i), 'w+') as content_file: content_file.write("data" + str(i)) def tearDown_repo(self): try: shutil.rmtree(self.path) os.remove("/tmp/repo") except: pass def test_simple_map_reduce(self): """Simple map reduce test with input data as string parameter""" self.setup_faces_and_connections() name = Name("/lib/reduce4") name += '_(/lib/func1("helloworld1"),/lib/func2("helloworld2"),/lib/func3("helloworld3"),/lib/func4("helloworld4"))' name += "NFN" res = self.fetch_tool1.fetch_data(name, timeout=0) time.sleep(3) print(res) self.assertEqual("HELLOWORLD1HELLOWORLD2HELLOWORLD3HELLOWORLD4", res) def test_simple_map_reduce_data_from_repo(self): """Simple map reduce test with input data from repo""" self.setup_repo() self.setup_faces_and_connections() name = Name("/lib/reduce4") name += '_(/lib/func1(/repo/r1/data1),/lib/func2(/repo/r2/data2),/lib/func3(/repo/r3/data3),/lib/func4(/repo/r4/data4))' name += "NFN" res = self.fetch_tool1.fetch_data(name, timeout=0) time.sleep(3) print(res) self.assertEqual("DATA1DATA2DATA3DATA4", res) def test_simple_map_reduce_data_from_repo_to_data(self): """Simple map reduce test with input data from repo forwarding to data""" self.setup_repo() self.setup_faces_and_connections() name = Name("/repo/r1/data1") name += '/lib/reduce4(/lib/func1(_),/lib/func2(/repo/r2/data2),/lib/func3(/repo/r3/data3),/lib/func4(/repo/r4/data4))' name += "NFN" res = self.fetch_tool1.fetch_data(name, timeout=0) time.sleep(3) print(res) self.assertEqual("DATA1DATA2DATA3DATA4", res)
def setUp(self): self.encoder_type = self.get_encoder() self.simulation_bus = SimulationBus(packetencoder=self.encoder_type()) self.fetch_tool1 = Fetch( "nfn0", None, 255, self.encoder_type(), interfaces=[self.simulation_bus.add_interface("fetchtool1")]) self.nfn0 = NFNForwarder( port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("nfn0")], log_level=255, ageing_interval=1) self.nfn1 = NFNForwarder( port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("nfn1")], log_level=255, ageing_interval=1) self.nfn2 = NFNForwarder( port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("nfn2")], log_level=255, ageing_interval=1) self.nfn3 = NFNForwarder( port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("nfn3")], log_level=255, ageing_interval=1) self.nfn4 = NFNForwarder( port=0, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("nfn4")], log_level=255, ageing_interval=1) self.repo1 = ICNDataRepository( "/tmp/repo1", Name("/repo/r1"), 0, 255, self.encoder_type(), False, False, [self.simulation_bus.add_interface("repo1")]) self.repo2 = ICNDataRepository( "/tmp/repo2", Name("/repo/r2"), 0, 255, self.encoder_type(), False, False, [self.simulation_bus.add_interface("repo2")]) self.repo3 = ICNDataRepository( "/tmp/repo3", Name("/repo/r3"), 0, 255, self.encoder_type(), False, False, [self.simulation_bus.add_interface("repo3")]) self.repo4 = ICNDataRepository( "/tmp/repo4", Name("/repo/r4"), 0, 255, self.encoder_type(), False, False, [self.simulation_bus.add_interface("repo4")]) self.nfn1.icnlayer.pit.set_pit_timeout(0) self.nfn1.icnlayer.cs.set_cs_timeout(30) self.nfn2.icnlayer.pit.set_pit_timeout(0) self.nfn2.icnlayer.cs.set_cs_timeout(30) self.nfn3.icnlayer.pit.set_pit_timeout(0) self.nfn3.icnlayer.cs.set_cs_timeout(30) self.nfn4.icnlayer.pit.set_pit_timeout(0) self.nfn4.icnlayer.cs.set_cs_timeout(30) self.nfn0.nfnlayer.optimizer = MapReduceOptimizer( self.nfn0.icnlayer.cs, self.nfn0.icnlayer.fib, self.nfn0.icnlayer.pit, self.nfn0.linklayer.faceidtable) self.nfn1.nfnlayer.optimizer = MapReduceOptimizer( self.nfn1.icnlayer.cs, self.nfn1.icnlayer.fib, self.nfn1.icnlayer.pit, self.nfn1.linklayer.faceidtable) self.nfn2.nfnlayer.optimizer = MapReduceOptimizer( self.nfn2.icnlayer.cs, self.nfn2.icnlayer.fib, self.nfn2.icnlayer.pit, self.nfn2.linklayer.faceidtable) self.nfn3.nfnlayer.optimizer = MapReduceOptimizer( self.nfn3.icnlayer.cs, self.nfn3.icnlayer.fib, self.nfn3.icnlayer.pit, self.nfn3.linklayer.faceidtable) self.nfn4.nfnlayer.optimizer = MapReduceOptimizer( self.nfn4.icnlayer.cs, self.nfn4.icnlayer.fib, self.nfn4.icnlayer.pit, self.nfn4.linklayer.faceidtable) self.mgmt_client0 = MgmtClient( self.nfn0.mgmt.mgmt_sock.getsockname()[1]) self.mgmt_client1 = MgmtClient( self.nfn1.mgmt.mgmt_sock.getsockname()[1]) self.mgmt_client2 = MgmtClient( self.nfn2.mgmt.mgmt_sock.getsockname()[1]) self.mgmt_client3 = MgmtClient( self.nfn3.mgmt.mgmt_sock.getsockname()[1]) self.mgmt_client4 = MgmtClient( self.nfn4.mgmt.mgmt_sock.getsockname()[1])
class MobilitySimulation(object): """This simulation setup is used to simulate a set of mobile nodes within a NFN based infrastructure""" def __init__(self, run_id: int, mobile_nodes: List[MobileNode], stationary_nodes: List[StationaryNode], stationary_node_distance: float, named_functions: dict, function_names: list, forwarder: str = "NFNForwarder", optimizer: str = "ToDataFirstOptimizer", use_distribution_helper: bool = False, log_level=logging.DEBUG): """ Configuration of the mobility simulation :param run_id the identifier of the simulation run :param mobile_nodes a list of mobile nodes part of the simulation :param stationary_nodes a list of stationary nodes forming the infrastructure :param stationary_node_distance the distance between the stationary nodes :param named_functions a dictionary of named function definitions used to be executed :param function_names a list of function names to be assigned to the mobile nodes :param forwarder the NFN forwarder to be used :param optimizer the NFN resolution strategy optimizer to be used in the simulation :param use_distribution_helper A flag indicating if the default distribution helper (ZipfMandelbrotDistribution) shall be used or not; default = False :param log_level the log level of the logger to be used; default: logging.DEBUG """ self._run_id = run_id self._forwarder = forwarder self._optimizer = optimizer self._mobile_nodes = mobile_nodes self._stationary_nodes = stationary_nodes self._stationary_node_distance = stationary_node_distance self.logger = Logger("MobilitySimulation", log_level) self.to_car_faces = [[0] * len(self._mobile_nodes) for i in range(len(self._stationary_nodes)) ] # rsu, car -> faceid self.to_rsu_faces = [[0] * len(self._mobile_nodes) for i in range(len(self._stationary_nodes)) ] # rsu, car -> faceid self._velocities = [] self._heading_directions = [] self._starting_points = [] for node in self._mobile_nodes: self._velocities.append(node.speed) self._heading_directions.append(node.direction) self._starting_points.append(node.spawn_point) self._is_running = False # flag indicating if the simulation is running or not self._function_names = function_names # list of function names to be invoked by the nodes self._named_functions = named_functions # a dict of function definitions to be invoked self._chunk_size = 8192 self._simulation_bus = SimulationBus( packetencoder=SimpleStringEncoder()) self._stationary_node_name_prefix = Name("/rsu") self._mobile_node_to_computation = [0] * len( mobile_nodes) # index which mobile node issues which computation if use_distribution_helper: # TODO in the future support more distribution types, e.g., uniform, gaussian, etc. dist_array = ZipfMandelbrotDistribution.create_zipf_mandelbrot_distribution( len(self._function_names), 0.7, 0.7) for i in range(0, len(mobile_nodes)): self._mobile_node_to_computation[i] = ZipfMandelbrotDistribution.\ get_next_zipfmandelbrot_random_number(dist_array, len(self._function_names)) - 1 # get_next_zipfmandelbrot_random_number(dist_array, len(self._function_names), run_id) - 1 self._compute_rsu_connection_time() self._setup_simulation_network() ########################### # METHODS ########################### def _compute_rsu_connection_time(self): """this method computes the connection time of a mobile node to a stationary node based on velocity of the mobile node and the communication range of the stationary node. Further enhancements of this simulation should include physical and MAC layer related communication conditions (e.g., propagation delay, fading, etc.) """ self._contact_time = [] for mobile_node in self._mobile_nodes: speed_in_ms = mobile_node.speed / 3.6 distance_in_m = 1000 * self._stationary_node_distance self._contact_time.append(distance_in_m / speed_in_ms * 1e9) def _setup_stationary_nodes(self): """configure the NFN com. stack at the stationary nodes""" for node in self._stationary_nodes: # install the NFN forwarder and the mgmt client tool at the stationary node if self._forwarder == "NFNForwarder": node.nfn_forwarder = NFNForwarder( 0, encoder=SimpleStringEncoder(), interfaces=[ self._simulation_bus.add_interface( f"rsu{node.node_id}") ], ageing_interval=10) elif self._forwarder == "NFNForwarderData": node.nfn_forwarder = NFNForwarderData( 0, encoder=SimpleStringEncoder(), interfaces=[ self._simulation_bus.add_interface( f"rsu{node.node_id}") ], chunk_size=self._chunk_size, num_of_forwards=1, ageing_interval=10) else: self.logger.error( "Forwarder: " + self._forwarder + " is not supported! Use 'NFNForwarder' or 'NFNForwarderData'!" ) # install the optimizer if self._optimizer == "ToDataFirstOptimizer": node.nfn_forwarder.nfnlayer.optimizer = ToDataFirstOptimizer( node.nfn_forwarder.icnlayer.cs, node.nfn_forwarder.icnlayer.fib, node.nfn_forwarder.icnlayer.pit, node.nfn_forwarder.linklayer.faceidtable) elif self._optimizer == "EdgeComputingOptimizer": node.nfn_forwarder.nfnlayer.optimizer = EdgeComputingOptimizer( node.nfn_forwarder.icnlayer.cs, node.nfn_forwarder.icnlayer.fib, node.nfn_forwarder.icnlayer.pit, node.nfn_forwarder.linklayer.faceidtable) # install the mgmt client tool at the node node.mgmt_tool = MgmtClient( node.nfn_forwarder.mgmt.mgmt_sock.getsockname()[1]) node.nfn_forwarder.icnlayer.cs.set_cs_timeout(60) def _setup_connections_for_stationary_nodes(self): """configure the connections """ loop_variable = 0 for node in self._stationary_nodes: if loop_variable == 0: # setup first RSU faceid_rsu_1st = node.nfn_forwarder.linklayer.faceidtable.get_or_create_faceid( AddressInfo("rsu" + str(1), 0)) node.nfn_forwarder.icnlayer.fib.add_fib_entry( Name("/nR"), [faceid_rsu_1st]) elif loop_variable == (len(self._stationary_nodes) - 1): # setup last RSU faceid_rsu_last = node.nfn_forwarder.linklayer.faceidtable.get_or_create_faceid( AddressInfo("rsu" + str(loop_variable - 2), 0)) node.nfn_forwarder.icnlayer.fib.add_fib_entry( Name("/nL"), [faceid_rsu_last]) else: faceid_node_left = node.nfn_forwarder.linklayer.faceidtable.get_or_create_faceid( AddressInfo("rsu" + str(loop_variable - 1), 0)) faceid_node_right = node.nfn_forwarder.linklayer.faceidtable.get_or_create_faceid( AddressInfo("rsu" + str(loop_variable + 1), 0)) node.nfn_forwarder.icnlayer.fib.add_fib_entry( Name("/nL"), [faceid_node_left]) node.nfn_forwarder.icnlayer.fib.add_fib_entry( Name("/nR"), [faceid_node_right]) loop_variable = +1 def _assign_named_functions_to_stationary_execution_nodes(self): """configure executables to the stationary nodes""" for node in self._stationary_nodes: for function in zip(self._named_functions.keys(), self._named_functions.values()): node.nfn_forwarder.icnlayer.cs.add_content_object(Content( Name(function[0]), function[1]), static=True) 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_simulation_network(self): """configure a network according to the configuration""" self.logger.debug("Setup simulation network ...") # setup stationary nodes self._setup_stationary_nodes() self.logger.debug("\t setup stationary nodes done") # setup connections self._setup_connections_for_stationary_nodes() self.logger.debug("\t setup connections between stationary nodes done") # assign functions to stationary nodes self._assign_named_functions_to_stationary_execution_nodes() self.logger.debug("\t assign named functions to stationary nodes done") # setup mobile nodes self._setup_mobile_nodes() self.logger.debug("\t setup mobile nodes done") # start node self.start_nodes() self.logger.debug("\t setup complete -> start nodes") def reconnect_car(self, mobile_node_number, new_rsu_number): if len(self._stationary_nodes) <= new_rsu_number or new_rsu_number < 0: self.logger.error( f"{time.time():.5f} --- Cannot reconnect mobile node with id {mobile_node_number} " f"to stationary node with id {new_rsu_number}, not part of this simulation" ) return connected_rsu = self.connected_rsu[mobile_node_number] self._mobile_nodes[ mobile_node_number].forwarder.icnlayer.fib.remove_fib_entry( self._stationary_node_name_prefix) self._mobile_nodes[ mobile_node_number].forwarder.icnlayer.fib.add_fib_entry( self._stationary_node_name_prefix, [self.to_rsu_faces[new_rsu_number][mobile_node_number]]) self._stationary_nodes[ connected_rsu].nfn_forwarder.icnlayer.fib.remove_fib_entry( Name(f"/car/car{mobile_node_number}")) self._stationary_nodes[ connected_rsu].nfn_forwarder.icnlayer.pit.remove_pit_entry_by_fid( self.to_car_faces[connected_rsu][mobile_node_number]) self._stationary_nodes[ new_rsu_number].nfn_forwarder.icnlayer.fib.add_fib_entry( Name(f"/car/car{mobile_node_number}"), [self.to_car_faces[new_rsu_number][mobile_node_number]]) self.connected_rsu[mobile_node_number] = connected_rsu + \ self._heading_directions[mobile_node_number] self._car_send_interest( self._mobile_nodes[mobile_node_number], self._function_names[ self._mobile_node_to_computation[mobile_node_number]]) def _car_send_interest(self, mobile_node, name): try: mobile_node.fetch.fetch_data( name, timeout=1 ) # if trouble reduce timeout to 0.1. Parse result from log except: pass def start_nodes(self): # Starting nodes for stationary_node in self._stationary_nodes: stationary_node.nfn_forwarder.start_forwarder() for mobile_node in self._mobile_nodes: mobile_node.forwarder.start_forwarder() self._simulation_bus.start_process() def stop_nodes(self): # stop nodes if not self._is_running: for stationary_node in self._stationary_nodes: stationary_node.nfn_forwarder.stop_forwarder() for mobile_node in self._mobile_nodes: mobile_node.forwarder.stop_forwarder() self._simulation_bus.stop_process() else: self.logger.error( "Simulation not started yet -- cleaning resources is necessary!" ) def run(self): """run the experiment, hand over the cars""" self._is_running = True self.connected_rsu = [] self.logger.debug("Start Simulation") for i in range(0, len(self._mobile_nodes)): self.connected_rsu.append(self._starting_points[i]) self._mobile_nodes[i].forwarder.icnlayer.fib.add_fib_entry( self._stationary_node_name_prefix, [self.to_rsu_faces[self.connected_rsu[i]][i]]) self._stationary_nodes[self.connected_rsu[ i]].nfn_forwarder.icnlayer.fib.add_fib_entry( Name(f"car{i}"), [self.to_car_faces[self.connected_rsu[i]][i]]) self._car_send_interest( self._mobile_nodes[i], self._function_names[self._mobile_node_to_computation[i]]) self.connection_time = [time.time()] * len(self._mobile_nodes) steps = 5 * len(self._mobile_nodes) while (self._is_running): time_ns = time.time_ns() for i in range(0, len(self._mobile_nodes)): if time_ns - self.connection_time[i] > self._contact_time[i]: #print(f"{time.time():.5f} -- " + "Car", i, "reconnects from", self.connected_rsu[i], "to", self.connected_rsu[i] + self._heading_directions[i]) self.logger.info("Car " + str(i) + " reconnects from " + str(self.connected_rsu[i]) + " to " + str(self.connected_rsu[i] + self._heading_directions[i])) new_rsu_number = self.connected_rsu[ i] + self._heading_directions[i] self.reconnect_car(i, new_rsu_number) self.connection_time[i] = time.time_ns() steps -= 1 if steps <= 0: self._is_running = False self.logger.debug("Simulation Terminated!") self.stop_nodes()