示例#1
0
    async def _process_notify_interest(self, int_name, int_param, app_param):
        """
        Async helper for ``_on_notify_interest()``.
        """
        logging.debug(f'received notify interest: {Name.to_str(int_name)}')
        topic = int_name[:-2]  # remove digest and `notify`

        # parse notify interest
        app_param = NotifyAppParam.parse(app_param)
        publisher_prefix = app_param.publisher_prefix
        notify_nonce = app_param.notify_nonce
        publisher_fwd_hint = app_param.publisher_fwd_hint
        int_param = InterestParam()
        if publisher_fwd_hint:
            # support only 1 forwarding hint now
            int_param.forwarding_hint = [(0x0, publisher_fwd_hint.name)]

        # send msg interest, retransmit 3 times
        msg_int_name = publisher_prefix + ['msg'] + topic + [
            Component.from_bytes(notify_nonce)
        ]
        n_retries = 3

        # de-duplicate notify interests of the same nonce
        if notify_nonce in self.nonce_processed:
            logging.info(
                f'Received duplicate notify interest for nonce {notify_nonce}')
            return
        self.nonce_processed.add(notify_nonce)
        aio.ensure_future(self._erase_subsciber_state_after(notify_nonce, 60))

        while n_retries > 0:
            try:
                logging.debug(
                    f'sending msg interest: {Name.to_str(msg_int_name)}')
                data_name, meta_info, msg = await self.app.express_interest(
                    msg_int_name, int_param=int_param)
                break
            except InterestNack as e:
                logging.debug(f'Nacked with reason: {e.reason}')
                await aio.sleep(1)
                n_retries -= 1
            except InterestTimeout:
                logging.debug(f'Timeout')
                n_retries -= 1
        if msg == None:
            return

        # pass msg to application
        logging.info(f'received subscribed msg: {Name.to_str(msg_int_name)}')
        self.topic_to_cb[topic](bytes(msg))

        # acknowledge notify interest with an empty data packet
        logging.debug(f'acknowledging notify interest {Name.to_str(int_name)}')
        self.app.put_data(int_name, None)
示例#2
0
 def test_forwarding_hint():
     name = '/local/ndn/prefix'
     int_param = InterestParam()
     int_param.nonce = 0x01020304
     int_param.forwarding_hint = [(0x87, '/name/A'),
                                  (0x02, Name.from_str('/ndn/B')),
                                  (0x12, b'\x07\x0d\x08\x0bshekkuenseu')]
     interest = make_interest(name, int_param)
     assert (interest ==
             b'\x05\x55\x07\x14\x08\x05local\x08\x03ndn\x08\x06prefix'
             b'\x1e\x33'
             b'\x1f\x0e\x1e\x01\x87\x07\x09\x08\x04name\x08\x01A'
             b'\x1f\x0d\x1e\x01\x02\x07\x08\x08\x03ndn\x08\x01B'
             b'\x1f\x12\x1e\x01\x12\x07\r\x08\x0bshekkuenseu'
             b'\x0a\x04\x01\x02\x03\x04\x0c\x02\x0f\xa0')
    async def _process_notify_interest(self, int_name, int_param, app_param):
        """
        Async helper for ``_on_notify_interest()``.
        """
        logging.debug(f'received notify interest: {Name.to_str(int_name)}')
        topic = int_name[:-2]  # remove digest and `notify`

        # parse notify interest
        app_param = PubSub.NotifyAppParam.parse(app_param)
        publisher_prefix = app_param.publisher_prefix
        nonce = app_param.nonce
        publisher_fwd_hint = app_param.publisher_fwd_hint
        int_param = InterestParam()
        if publisher_fwd_hint:
            int_param.forwarding_hint = publisher_fwd_hint

        # send msg interest, retransmit 3 times
        msg_int_name = publisher_prefix + ['msg', str(nonce)]
        n_retries = 3
        while n_retries > 0:
            try:
                logging.debug(
                    f'sending msg interest: {Name.to_str(msg_int_name)}')
                data_name, meta_info, msg = await self.app.express_interest(
                    msg_int_name, int_param=int_param)
                break
            except InterestNack as e:
                logging.debug(f'Nacked with reason: {e.reason}')
                await aio.sleep(1)
            except InterestTimeout:
                logging.debug(f'Timeout')
        if msg == None:
            return

        # pass msg to application
        logging.info(f'received subscribed msg: {Name.to_str(msg_int_name)}')
        self.topic_to_cb[topic](bytes(msg))

        # acknowledge notify interest with an empty data packet
        logging.debug(f'acknowledging notify interest {Name.to_str(int_name)}')
        self.app.put_data(int_name, None)