Beispiel #1
0
    def __init__(self, ip: str, port: Optional[int], log_level=255, encoder: BasicEncoder = None,
                 autoconfig: bool = False, interfaces=None, name: str = None):
        self.ip = ip
        self.name = name
        # create encoder and chunkifyer
        if encoder is None:
            self.encoder = SimpleStringEncoder(log_level=log_level)
        else:
            encoder.set_log_level(log_level)
            self.encoder = encoder
        self.chunkifyer = SimpleContentChunkifyer()

        # initialize layers
        synced_data_struct_factory = PiCNSyncDataStructFactory()
        synced_data_struct_factory.register("faceidtable", FaceIDDict)
        synced_data_struct_factory.register("timeoutprevention_dict", TimeoutPreventionMessageDict)
        synced_data_struct_factory.create_manager()
        faceidtable = synced_data_struct_factory.manager.faceidtable()
        timeoutprevention_dict = synced_data_struct_factory.manager.timeoutprevention_dict()

        if interfaces is None:
            interfaces = [UDP4Interface(0)]
        else:
            interfaces = interfaces

        # create layers
        self.linklayer = BasicLinkLayer(interfaces, faceidtable, log_level=log_level)
        self.packetencodinglayer = BasicPacketEncodingLayer(self.encoder, log_level=log_level)
        self.chunklayer = BasicChunkLayer(self.chunkifyer, log_level=log_level)
        self.timeoutpreventionlayer = BasicTimeoutPreventionLayer(timeoutprevention_dict, None, log_level=log_level)
        self.lstack: LayerStack = LayerStack([
            self.chunklayer,
            self.timeoutpreventionlayer,
            self.packetencodinglayer,
            self.linklayer
        ])
        self.timeoutpreventionlayer.ageing()
        self.autoconfig = autoconfig
        if autoconfig:
            self.autoconfiglayer: AutoconfigClientLayer = AutoconfigClientLayer(self.linklayer)
            self.lstack.insert(self.autoconfiglayer, on_top_of=self.packetencodinglayer)

        # setup communication
        if port is None:
            self.fid = self.linklayer.faceidtable.get_or_create_faceid(AddressInfo(ip, 0))
        else:
            self.fid = self.linklayer.faceidtable.get_or_create_faceid(AddressInfo((ip, port), 0))

        # send packet
        self.lstack.start_all()
Beispiel #2
0
    def setUp(self):
        synced_data_struct_factory = PiCNSyncDataStructFactory()
        synced_data_struct_factory.register("timeoutPreventionMessageDict", TimeoutPreventionMessageDict)
        synced_data_struct_factory.register("NFNComputationList", NFNComputationList)
        synced_data_struct_factory.create_manager()

        tpmd = synced_data_struct_factory.manager.timeoutPreventionMessageDict()
        nfncl = synced_data_struct_factory.manager.NFNComputationList(None, None)

        self.timeoutPreventionLayer = BasicTimeoutPreventionLayer(tpmd, nfncl, log_level=255)
        self.timeoutPreventionLayer.queue_from_higher = multiprocessing.Queue()
        self.timeoutPreventionLayer.queue_from_lower = multiprocessing.Queue()
        self.timeoutPreventionLayer.queue_to_higher = multiprocessing.Queue()
        self.timeoutPreventionLayer.queue_to_lower = multiprocessing.Queue()
        self.timeoutPreventionLayer.start_process()
Beispiel #3
0
    def __init__(self,
                 port=9000,
                 log_level=255,
                 encoder: BasicEncoder = None,
                 interfaces: List[BaseInterface] = None,
                 executors: BaseNFNExecutor = None,
                 ageing_interval: int = 3,
                 use_thunks=False):
        # debug level
        logger = Logger("NFNForwarder", log_level)
        logger.info("Start PiCN NFN Forwarder on port " + str(port))

        # packet encoder
        if encoder is None:
            self.encoder = SimpleStringEncoder(log_level=log_level)
        else:
            encoder.set_log_level(log_level)
            self.encoder = encoder

    # setup data structures
        synced_data_struct_factory = PiCNSyncDataStructFactory()
        synced_data_struct_factory.register("cs", ContentStoreMemoryExact)
        synced_data_struct_factory.register(
            "fib", ForwardingInformationBaseMemoryPrefix)
        synced_data_struct_factory.register("pit",
                                            PendingInterstTableMemoryExact)
        synced_data_struct_factory.register("faceidtable", FaceIDDict)

        synced_data_struct_factory.register("computation_table",
                                            NFNComputationList)
        synced_data_struct_factory.register("timeoutprevention_dict",
                                            TimeoutPreventionMessageDict)
        if use_thunks:
            synced_data_struct_factory.register("thunktable", ThunkList)
            synced_data_struct_factory.register("plantable", PlanTable)

        synced_data_struct_factory.create_manager()

        cs = synced_data_struct_factory.manager.cs()
        fib = synced_data_struct_factory.manager.fib()
        pit = synced_data_struct_factory.manager.pit()
        faceidtable = synced_data_struct_factory.manager.faceidtable()

        self.parser = DefaultNFNParser()
        if use_thunks:
            thunktable = synced_data_struct_factory.manager.thunktable()
            plantable = synced_data_struct_factory.manager.plantable(
                self.parser)

        #setup chunkifier
        self.chunkifier = SimpleContentChunkifyer()

        # default interface
        if interfaces is not None:
            self.interfaces = interfaces
            mgmt_port = port
        else:
            interfaces = [UDP4Interface(port)]
            mgmt_port = interfaces[0].get_port()

        # initialize layers
        self.linklayer = BasicLinkLayer(interfaces,
                                        faceidtable,
                                        log_level=log_level)
        self.packetencodinglayer = BasicPacketEncodingLayer(
            self.encoder, log_level=log_level)
        self.icnlayer = BasicICNLayer(log_level=log_level,
                                      ageing_interval=ageing_interval)
        self.chunklayer = BasicChunkLayer(self.chunkifier, log_level=log_level)

        # setup nfn
        self.icnlayer._interest_to_app = True
        if executors is None:
            self.executors = {"PYTHON": NFNPythonExecutor()}
        else:
            self.executors = executors
        self.r2cclient = TimeoutR2CHandler()
        comp_table = synced_data_struct_factory.manager.computation_table(
            self.r2cclient, self.parser)
        self.nfnlayer = BasicNFNLayer(cs,
                                      fib,
                                      pit,
                                      faceidtable,
                                      comp_table,
                                      self.executors,
                                      self.parser,
                                      self.r2cclient,
                                      log_level=log_level)
        if use_thunks:
            self.thunk_layer = BasicThunkLayer(cs,
                                               fib,
                                               pit,
                                               faceidtable,
                                               thunktable,
                                               plantable,
                                               self.parser,
                                               log_level=log_level)
            self.nfnlayer.optimizer = ThunkPlanExecutor(
                cs, fib, pit, faceidtable, plantable)

        timeoutprevention_dict = synced_data_struct_factory.manager.timeoutprevention_dict(
        )
        self.timeoutpreventionlayer = BasicTimeoutPreventionLayer(
            timeoutprevention_dict, comp_table, pit=pit, log_level=log_level)

        if use_thunks:
            self.lstack: LayerStack = LayerStack([
                self.nfnlayer, self.chunklayer, self.timeoutpreventionlayer,
                self.thunk_layer, self.icnlayer, self.packetencodinglayer,
                self.linklayer
            ])
        else:
            self.lstack: LayerStack = LayerStack([
                self.nfnlayer, self.chunklayer, self.timeoutpreventionlayer,
                self.icnlayer, self.packetencodinglayer, self.linklayer
            ])

        self.icnlayer.cs = cs
        self.icnlayer.fib = fib
        self.icnlayer.pit = pit

        # mgmt
        self.mgmt = Mgmt(self.icnlayer.cs,
                         self.icnlayer.fib,
                         self.icnlayer.pit,
                         self.linklayer,
                         mgmt_port,
                         self.stop_forwarder,
                         log_level=log_level)
Beispiel #4
0
class Fetch(object):
    """Fetch Tool for PiCN"""
    def __init__(self,
                 ip: str,
                 port: int,
                 log_level=255,
                 encoder: BasicEncoder = None,
                 autoconfig: bool = False,
                 interfaces=None):

        # create encoder and chunkifyer
        if encoder is None:
            self.encoder = SimpleStringEncoder(log_level=log_level)
        else:
            encoder.set_log_level(log_level)
            self.encoder = encoder
        self.chunkifyer = SimpleContentChunkifyer()

        # initialize layers
        synced_data_struct_factory = PiCNSyncDataStructFactory()
        synced_data_struct_factory.register("faceidtable", FaceIDDict)
        synced_data_struct_factory.register("timeoutprevention_dict",
                                            TimeoutPreventionMessageDict)
        synced_data_struct_factory.create_manager()
        faceidtable = synced_data_struct_factory.manager.faceidtable()
        timeoutprevention_dict = synced_data_struct_factory.manager.timeoutprevention_dict(
        )

        if interfaces is None:
            interfaces = [UDP4Interface(0)]
        else:
            interfaces = interfaces

        # create layers
        self.linklayer = BasicLinkLayer(interfaces,
                                        faceidtable,
                                        log_level=log_level)
        self.packetencodinglayer = BasicPacketEncodingLayer(
            self.encoder, log_level=log_level)
        self.chunklayer = BasicChunkLayer(self.chunkifyer, log_level=log_level)
        self.timeoutpreventionlayer = BasicTimeoutPreventionLayer(
            timeoutprevention_dict, None, log_level=log_level)
        self.lstack: LayerStack = LayerStack([
            self.chunklayer, self.timeoutpreventionlayer,
            self.packetencodinglayer, self.linklayer
        ])
        self.timeoutpreventionlayer.ageing()
        self.autoconfig = autoconfig
        if autoconfig:
            self.autoconfiglayer: AutoconfigClientLayer = AutoconfigClientLayer(
                self.linklayer)
            self.lstack.insert(self.autoconfiglayer,
                               on_top_of=self.packetencodinglayer)

        # setup communication
        if port is None:
            self.fid = self.linklayer.faceidtable.get_or_create_faceid(
                AddressInfo(ip, 0))
        else:
            self.fid = self.linklayer.faceidtable.get_or_create_faceid(
                AddressInfo((ip, port), 0))

        # send packet
        self.lstack.start_all()

    def fetch_data(self, name: Name, timeout=4.0) -> str:
        """Fetch data from the server
        :param name Name to be fetched
        :param timeout Timeout to wait for a response. Use 0 for infinity
        """
        # create interest
        interest: Interest = Interest(name)
        if self.autoconfig:
            self.lstack.queue_from_higher.put([None, interest])
        else:
            self.lstack.queue_from_higher.put([self.fid, interest])

        if timeout == 0:
            packet = self.lstack.queue_to_higher.get()[1]
        else:
            packet = self.lstack.queue_to_higher.get(timeout=timeout)[1]
        if isinstance(packet, Content):
            return packet.content
        if isinstance(packet, Nack):
            return "Received Nack: " + str(packet.reason.value)
        return None

    def stop_fetch(self):
        """Close everything"""
        self.lstack.stop_all()
        self.lstack.close_all()
Beispiel #5
0
class test_BasicTimeoutPreventionLayer(unittest.TestCase):
    """Tests of the BasicR2CLayer"""

    def setUp(self):
        synced_data_struct_factory = PiCNSyncDataStructFactory()
        synced_data_struct_factory.register("timeoutPreventionMessageDict", TimeoutPreventionMessageDict)
        synced_data_struct_factory.register("NFNComputationList", NFNComputationList)
        synced_data_struct_factory.create_manager()

        tpmd = synced_data_struct_factory.manager.timeoutPreventionMessageDict()
        nfncl = synced_data_struct_factory.manager.NFNComputationList(None, None)

        self.timeoutPreventionLayer = BasicTimeoutPreventionLayer(tpmd, nfncl, log_level=255)
        self.timeoutPreventionLayer.queue_from_higher = multiprocessing.Queue()
        self.timeoutPreventionLayer.queue_from_lower = multiprocessing.Queue()
        self.timeoutPreventionLayer.queue_to_higher = multiprocessing.Queue()
        self.timeoutPreventionLayer.queue_to_lower = multiprocessing.Queue()
        self.timeoutPreventionLayer.start_process()

    def tearDown(self):
        self.timeoutPreventionLayer.stop_process()


    def test_interest_from_lower(self):
        """test that an interest from lower is directly forwarded to upper"""
        interest =  Interest("/test/data")
        self.timeoutPreventionLayer.queue_from_lower.put([1, interest])
        res = self.timeoutPreventionLayer.queue_to_higher.get(timeout=4.0)
        self.assertEqual([1, interest], res)

    def test_interest_from_higher(self):
        """test sending an interest from higher without adding it to the dict"""
        interest = Interest("/test/data")
        self.timeoutPreventionLayer.queue_from_higher.put([1, interest])
        res = self.timeoutPreventionLayer.queue_to_lower.get(timeout=4.0)
        self.assertEqual([1, interest], res)
        e = self.timeoutPreventionLayer.message_dict.get_entry(interest.name)
        self.assertTrue(e is not None)

    def test_nfn_interest_from_higher(self):
        """test sending an interest from higher and adding it to the dict"""
        interest = Interest("/test/func()/NFN")
        self.timeoutPreventionLayer.queue_from_higher.put([1, interest])
        res = self.timeoutPreventionLayer.queue_to_lower.get(timeout=2.0)
        self.assertEqual([1, interest], res)
        e = self.timeoutPreventionLayer.message_dict.get_entry(interest.name)
        self.assertTrue(e is not None)

    def test_content_from_lower_no_message_dict_entry(self):
        """test content from lower with no message dict entry"""
        content = Content("/test/data")
        self.timeoutPreventionLayer.queue_from_lower.put([1, content])
        res = self.timeoutPreventionLayer.queue_to_higher.get(timeout=2.0)
        self.assertEqual([1, content], res)
        e = self.timeoutPreventionLayer.message_dict.get_entry(content.name)
        self.assertTrue(e is None)

    def test_content_from_lower_message_dict_entry(self):
        """test content from lower with message dict entry"""
        content = Content("/test/data")
        self.timeoutPreventionLayer.message_dict.create_entry(content.name, 1)
        e = self.timeoutPreventionLayer.message_dict.get_entry(content.name)
        self.assertTrue(e is not None)
        self.timeoutPreventionLayer.queue_from_lower.put([1, content])
        res = self.timeoutPreventionLayer.queue_to_higher.get(timeout=2.0)
        self.assertEqual([1, content], res)
        e = self.timeoutPreventionLayer.message_dict.get_entry(content.name)
        self.assertTrue(e is None)

    def test_content_from_higher(self):
        """test content from higher"""
        content = Content("/test/data")
        self.timeoutPreventionLayer.queue_from_higher.put([1, content])
        res = self.timeoutPreventionLayer.queue_to_lower.get(timeout=2.0)
        self.assertEqual([1, content], res)

    def test_nack_from_lower_message_dict_entry(self):
        """test nack from lower WITH message dict entry"""
        nack = Nack("/test/data", interest=Interest("/test/data"), reason=NackReason.CONGESTION)
        self.timeoutPreventionLayer.message_dict.create_entry(nack.name, 1)
        e = self.timeoutPreventionLayer.message_dict.get_entry(nack.name)
        self.assertTrue(e is not None)
        self.timeoutPreventionLayer.queue_from_lower.put([1, nack])
        res = self.timeoutPreventionLayer.queue_to_higher.get(timeout=2.0)
        self.assertEqual([1, nack], res)
        e = self.timeoutPreventionLayer.message_dict.get_entry(nack.name)
        self.assertTrue(e is None)

    def test_nack_from_higher(self):
        """test nack from higher """
        nack = Nack("/test/data", interest=Interest("/test/data"), reason=NackReason.CONGESTION)
        self.timeoutPreventionLayer.queue_from_higher.put([1, nack])
        res = self.timeoutPreventionLayer.queue_to_lower.get(timeout=2.0)
        self.assertEqual([1, nack], res)

    def test_keepalive_from_lower_message_dict_entry(self):
        """test r2c from lower with message dict entry"""
        content = Content("/test/data/_()/KEEPALIVE/NFN")
        self.timeoutPreventionLayer.message_dict.create_entry(content.name, 1)
        e1 = self.timeoutPreventionLayer.message_dict.get_entry(content.name)
        ts = e1.timestamp
        self.assertTrue(e1 is not None)
        self.timeoutPreventionLayer.queue_from_lower.put([1, content])
        time.sleep(1.0)
        e2 = self.timeoutPreventionLayer.message_dict.get_entry(content.name)
        self.assertTrue(e2 is not None)
        self.assertTrue(e2.timestamp > ts)

    def test_keep_alive_ageing_no_reply(self):
        """test ageing with keepalive with no keep alive reply"""
        self.timeoutPreventionLayer.ageing()

        interest = Interest("/test/func/_()/NFN")
        keepalive = Interest("/test/func/_()/KEEPALIVE/NFN")
        self.timeoutPreventionLayer.queue_from_higher.put([1, interest])

        res1 = self.timeoutPreventionLayer.queue_to_lower.get(timeout=2.0)
        self.assertEqual(res1, [1, interest])
        e1 = self.timeoutPreventionLayer.message_dict.get_entry(interest.name)
        self.assertTrue(e1 is not None)
        e2 = self.timeoutPreventionLayer.message_dict.get_entry(keepalive.name)
        self.assertTrue(e2 is not None)

        res2 = self.timeoutPreventionLayer.queue_to_lower.get(timeout=2.0)
        self.assertEqual(res2, [1, interest])

        res3 = self.timeoutPreventionLayer.queue_to_lower.get(timeout=2.0)
        self.assertEqual(res3, [1, keepalive])

        res4 = self.timeoutPreventionLayer.queue_to_lower.get(timeout=2.0)
        self.assertEqual(res4, [1, interest])

        res5 = self.timeoutPreventionLayer.queue_to_lower.get(timeout=2.0)
        self.assertEqual(res5, [1, keepalive])

        self.assertTrue(self.timeoutPreventionLayer.queue_to_lower.empty())

        res7 = self.timeoutPreventionLayer.queue_to_higher.get(timeout=2.0)
        self.assertEqual(res7, [1, Nack(name=interest.name, reason=NackReason.COMP_NOT_RUNNING, interest=interest)])

    def test_keep_alive_ageing_reply(self):
        """test ageing with keepalive with no keep alive reply"""
        self.timeoutPreventionLayer.ageing()

        interest = Interest("/test/func/_()/NFN")
        content = Content(interest.name, "data")
        keepalive = Interest("/test/func/_()/KEEPALIVE/NFN")
        keepalive_rely = Content(keepalive.name, "")

        self.timeoutPreventionLayer.queue_from_higher.put([1, interest])

        res1 = self.timeoutPreventionLayer.queue_to_lower.get(timeout=2.0)
        self.assertEqual(res1, [1, interest])
        e1 = self.timeoutPreventionLayer.message_dict.get_entry(interest.name)
        self.assertTrue(e1 is not None)
        e2 = self.timeoutPreventionLayer.message_dict.get_entry(keepalive.name)
        self.assertTrue(e2 is not None)

        res2 = self.timeoutPreventionLayer.queue_to_lower.get(timeout=2.0)
        self.assertEqual(res2, [1, interest])

        res3 = self.timeoutPreventionLayer.queue_to_lower.get(timeout=2.0)
        self.assertEqual(res3, [1, keepalive])

        self.timeoutPreventionLayer.queue_from_lower.put([4, keepalive_rely])
        e1 = self.timeoutPreventionLayer.message_dict.get_entry(interest.name)
        self.assertTrue(e1 is not None)
        e2 = self.timeoutPreventionLayer.message_dict.get_entry(keepalive.name)
        self.assertTrue(e2 is not None)

        res4 = self.timeoutPreventionLayer.queue_to_lower.get(timeout=2.0)
        self.assertEqual(res4, [1, interest])

        res5 = self.timeoutPreventionLayer.queue_to_lower.get(timeout=2.0)
        self.assertEqual(res5, [1, keepalive])


        self.timeoutPreventionLayer.queue_from_lower.put([4, keepalive_rely])
        e1 = self.timeoutPreventionLayer.message_dict.get_entry(interest.name)
        self.assertTrue(e1 is not None)
        e2 = self.timeoutPreventionLayer.message_dict.get_entry(keepalive.name)
        self.assertTrue(e2 is not None)

        res6 = self.timeoutPreventionLayer.queue_to_lower.get(timeout=2.0)
        self.assertEqual(res6, [1, interest])

        res7 = self.timeoutPreventionLayer.queue_to_lower.get(timeout=2.0)
        self.assertEqual(res7, [1, keepalive])

        self.timeoutPreventionLayer.queue_from_lower.put([4, keepalive_rely])

        res8 = self.timeoutPreventionLayer.queue_to_lower.get(timeout=2.0)
        self.assertEqual(res8, [1, interest])

        res9 = self.timeoutPreventionLayer.queue_to_lower.get(timeout=2.0)
        self.assertEqual(res9, [1, keepalive])

        self.timeoutPreventionLayer.queue_from_lower.put([5, content])

        time.sleep(1)
        e1 = self.timeoutPreventionLayer.message_dict.get_entry(interest.name)
        self.assertTrue(e1 is None)
        e2 = self.timeoutPreventionLayer.message_dict.get_entry(keepalive.name)
        self.assertTrue(e2 is None)
        self.assertEqual(len(self.timeoutPreventionLayer.message_dict.get_container()), 0)

        res = self.timeoutPreventionLayer.queue_to_higher.get(timeout=2.0)
        self.assertEqual(res, [5, content])
        self.assertTrue(self.timeoutPreventionLayer.queue_to_lower.empty())


    def test_keep_alive_request(self):
        """test replying a incoming keep alive request"""
        interest = Interest("/test/func/_()/NFN")
        keep_alive = Interest("/test/func/_()/KEEPALIVE/NFN")
        content = Content(keep_alive.name)

        self.timeoutPreventionLayer.computation_table.add_computation(interest.name, 3, interest)

        self.timeoutPreventionLayer.queue_from_lower.put([3, keep_alive])
        self.assertTrue(self.timeoutPreventionLayer.queue_to_higher.empty())
        res = self.timeoutPreventionLayer.queue_to_lower.get(timeout=2.0)
        self.assertEqual(res, [3, content])

    def test_keep_alive_request_no_comp_running(self):
        """test replying a incoming keep alive request if no comp running"""
        keep_alive = Interest("/test/func/_()/KEEPALIVE/NFN")
        nack = Nack(keep_alive.name, reason=NackReason.COMP_NOT_RUNNING, interest=keep_alive)

        self.timeoutPreventionLayer.queue_from_lower.put([3, keep_alive])
        self.assertTrue(self.timeoutPreventionLayer.queue_to_higher.empty())
        res = self.timeoutPreventionLayer.queue_to_lower.get(timeout=2.0)
        self.assertEqual(res, [3, nack])