예제 #1
0
    def _initialTimeOut(self, interest):
        """
        Initial sync interest timeout, which means there are no other publishers
        yet.
        """
        if not self._enabled:
            # Ignore callbacks after the application calls shutdown().
            return

        logging.getLogger(__name__).info("initial sync timeout")
        logging.getLogger(__name__).info("no other people")
        self._sequenceNo += 1
        if self._sequenceNo != 0:
            # Since there were no other users, we expect sequence no 0.
            raise RuntimeError(
              "ChronoSync: sequenceNo_ is not the expected value of 0 for first use.")

        tempContent = sync_state_pb2.SyncStateMsg()
        content = getattr(tempContent, "ss").add()
        content.name = self._applicationDataPrefixUri
        content.type = SyncState_UPDATE
        content.seqno.seq = self._sequenceNo
        content.seqno.session = self._sessionNo
        self._update(getattr(tempContent, "ss"))

        self._onInitialized()

        name = Name(self._applicationBroadcastPrefix)
        name.append(self._digestTree.getRoot())
        retryInterest = Interest(name)
        retryInterest.setInterestLifetimeMilliseconds(self._syncLifetime)
        self._face.expressInterest(retryInterest, self._onData, self._syncTimeout)
        logging.getLogger(__name__).info("Syncinterest expressed:")
        logging.getLogger(__name__).info("%s", name.toUri())
예제 #2
0
    def __init__(self, onReceivedSyncState, onInitialized,
      applicationDataPrefix, applicationBroadcastPrefix, sessionNo, face,
      keyChain, certificateName, syncLifetime, onRegisterFailed):
        self._onReceivedSyncState = onReceivedSyncState
        self._onInitialized = onInitialized
        self._applicationDataPrefixUri = applicationDataPrefix.toUri()
        self._applicationBroadcastPrefix = Name(applicationBroadcastPrefix)
        self._sessionNo = sessionNo
        self._face = face
        self._keyChain = keyChain
        self._certificateName = Name(certificateName)
        self._syncLifetime = syncLifetime
        self._contentCache = MemoryContentCache(face)

        self._digestLog = [] # of _DigestLogEntry
        self._digestTree = DigestTree()
        self._sequenceNo = -1
        self._enabled = True

        emptyContent = SyncStateMsg()
        # Use getattr to avoid pylint errors.
        self._digestLog.append(self._DigestLogEntry("00", getattr(emptyContent, "ss")))

        # Register the prefix with the contentCache_ and use our own onInterest
        #   as the onDataNotFound fallback.
        self._contentCache.registerPrefix(
          self._applicationBroadcastPrefix, onRegisterFailed, self._onInterest)

        interest = Interest(self._applicationBroadcastPrefix)
        interest.getName().append("00")
        interest.setInterestLifetimeMilliseconds(1000)
        interest.setMustBeFresh(True)
        face.expressInterest(interest, self._onData, self._initialTimeOut)
        logging.getLogger(__name__).info("initial sync expressed")
        logging.getLogger(__name__).info("%s", interest.getName().toUri())
예제 #3
0
    def __init__(self, onReceivedSyncState, onInitialized,
      applicationDataPrefix, applicationBroadcastPrefix, sessionNo, face,
      keyChain, certificateName, syncLifetime, onRegisterFailed):
        self._onReceivedSyncState = onReceivedSyncState
        self._onInitialized = onInitialized
        self._applicationDataPrefixUri = applicationDataPrefix.toUri()
        self._applicationBroadcastPrefix = Name(applicationBroadcastPrefix)
        self._sessionNo = sessionNo
        self._face = face
        self._keyChain = keyChain
        self._certificateName = Name(certificateName)
        self._syncLifetime = syncLifetime
        self._contentCache = MemoryContentCache(face)

        self._digestLog = [] # of _DigestLogEntry
        self._digestTree = DigestTree()
        self._sequenceNo = -1
        self._enabled = True

        emptyContent = sync_state_pb2.SyncStateMsg()
        # Use getattr to avoid pylint errors.
        self._digestLog.append(self._DigestLogEntry("00", getattr(emptyContent, "ss")))

        # Register the prefix with the contentCache_ and use our own onInterest
        #   as the onDataNotFound fallback.
        self._contentCache.registerPrefix(
          self._applicationBroadcastPrefix, onRegisterFailed, self._onInterest)

        interest = Interest(self._applicationBroadcastPrefix)
        interest.getName().append("00")
        interest.setInterestLifetimeMilliseconds(1000)
        interest.setMustBeFresh(True)
        face.expressInterest(interest, self._onData, self._initialTimeOut)
        logging.getLogger(__name__).info("initial sync expressed")
        logging.getLogger(__name__).info("%s", interest.getName().toUri())
예제 #4
0
    def _onData(self, interest, data):
        """
        Process Sync Data.
        """
        if not self._enabled:
            # Ignore callbacks after the application calls shutdown().
            return

        logging.getLogger(__name__).info(
          "Sync ContentObject received in callback")
        logging.getLogger(__name__).info(
            "name: %s", data.getName().toUri())
        # TODO: Check if this works in Python 3.
        tempContent = SyncStateMsg()
#pylint: disable=E1103
        tempContent.ParseFromString(data.getContent().toBytes())
#pylint: enable=E1103
        content = getattr(tempContent, "ss")
        if self._digestTree.getRoot() == "00":
            isRecovery = True
            #processing initial sync data
            self._initialOndata(content)
        else:
            self._update(content)
            if (interest.getName().size() ==
                self._applicationBroadcastPrefix.size() + 2):
                # Assume this is a recovery interest.
                isRecovery = True
            else:
                isRecovery = False

        # Send the interests to fetch the application data.
        syncStates = []
        for i in range(len(content)):
            syncState = content[i]

            # Only report UPDATE sync states.
            if syncState.type == SyncState_UPDATE:
                if len(syncState.application_info) > 0:
                    applicationInfo = Blob(syncState.application_info, True)
                else:
                    applicationInfo = Blob()

                syncStates.append(self.SyncState(
                  syncState.name, syncState.seqno.session,
                  syncState.seqno.seq, applicationInfo))

        try:
            self._onReceivedSyncState(syncStates, isRecovery)
        except:
            logging.exception("Error in onReceivedSyncState")

        name = Name(self._applicationBroadcastPrefix)
        name.append(self._digestTree.getRoot())
        syncInterest = Interest(name)
        syncInterest.setInterestLifetimeMilliseconds(self._syncLifetime)
        self._face.expressInterest(syncInterest, self._onData, self._syncTimeout)
        logging.getLogger(__name__).info("Syncinterest expressed:")
        logging.getLogger(__name__).info("%s", name.toUri())
예제 #5
0
    def _onData(self, interest, data):
        """
        Process Sync Data.
        """
        if not self._enabled:
            # Ignore callbacks after the application calls shutdown().
            return

        logging.getLogger(__name__).info(
            "Sync ContentObject received in callback")
        logging.getLogger(__name__).info("name: %s", data.getName().toUri())
        # TODO: Check if this works in Python 3.
        tempContent = SyncStateMsg()
        #pylint: disable=E1103
        tempContent.ParseFromString(data.getContent().toBytes())
        #pylint: enable=E1103
        content = getattr(tempContent, "ss")
        if self._digestTree.getRoot() == "00":
            isRecovery = True
            #processing initial sync data
            self._initialOndata(content)
        else:
            self._update(content)
            if (interest.getName().size() ==
                    self._applicationBroadcastPrefix.size() + 2):
                # Assume this is a recovery interest.
                isRecovery = True
            else:
                isRecovery = False

        # Send the interests to fetch the application data.
        syncStates = []
        for i in range(len(content)):
            syncState = content[i]

            # Only report UPDATE sync states.
            if syncState.type == SyncState_UPDATE:
                if len(syncState.application_info) > 0:
                    applicationInfo = Blob(syncState.application_info, True)
                else:
                    applicationInfo = Blob()

                syncStates.append(
                    self.SyncState(syncState.name, syncState.seqno.session,
                                   syncState.seqno.seq, applicationInfo))

        try:
            self._onReceivedSyncState(syncStates, isRecovery)
        except:
            logging.exception("Error in onReceivedSyncState")

        name = Name(self._applicationBroadcastPrefix)
        name.append(self._digestTree.getRoot())
        syncInterest = Interest(name)
        syncInterest.setInterestLifetimeMilliseconds(self._syncLifetime)
        self._face.expressInterest(syncInterest, self._onData,
                                   self._syncTimeout)
        logging.getLogger(__name__).info("Syncinterest expressed:")
        logging.getLogger(__name__).info("%s", name.toUri())
예제 #6
0
파일: node.py 프로젝트: WeiqiJust/NDN-total
    def _registerPrefixHelper(
      self, registeredPrefixId, prefix, onInterest, onRegisterFailed, flags,
      wireFormat):
        """
        Do the work of registerPrefix to register with NDNx once we have an
        _ndndId.

        :param int registeredPrefixId: The
          _RegisteredPrefix.getNextRegisteredPrefixId() which registerPrefix got
          so it could return it to the caller. If this is 0, then don't add to
          _registeredPrefixTable (assuming it has already been done).
        """
        # Create a ForwardingEntry.
        # Note: ndnd ignores any freshness that is larger than 3600 seconds and
        #   sets 300 seconds instead. To register "forever", (=2000000000 sec),
        #   the freshness period must be omitted.
        forwardingEntry = ForwardingEntry()
        forwardingEntry.setAction("selfreg")
        forwardingEntry.setPrefix(prefix)
        forwardingEntry.setForwardingFlags(flags)
        content = forwardingEntry.wireEncode(wireFormat)

        # Set the ForwardingEntry as the content of a Data packet and sign.
        data = Data()
        data.setContent(content)
        # Set the name to a random value so that each request is unique.
        nonce = bytearray(4)
        for i in range(len(nonce)):
            nonce[i] = _systemRandom.randint(0, 0xff)
        data.getName().append(nonce)
        # The ndnd ignores the signature, so set to blank values.
        data.getSignature().getKeyLocator().setType(
          KeyLocatorType.KEY_LOCATOR_DIGEST)
        data.getSignature().getKeyLocator().setKeyData(
          Blob(bytearray(32), False))
        data.getSignature().setSignature(Blob(bytearray(128), False))
        encodedData = data.wireEncode(wireFormat)

        # Create an interest where the name has the encoded Data packet.
        interestName = Name().append("ndnx").append(self._ndndId).append(
          "selfreg").append(encodedData)

        interest = Interest(interestName)
        interest.setInterestLifetimeMilliseconds(4000.0)
        interest.setScope(1)
        encodedInterest = interest.wireEncode(wireFormat)

        if registeredPrefixId != 0:
            # Save the onInterest callback and send the registration interest.
            self._registeredPrefixTable.append(Node._RegisteredPrefix(
              registeredPrefixId, prefix, onInterest))

        response = Node._RegisterResponse(
          self, prefix, onInterest, onRegisterFailed, flags, wireFormat, False)
        self.expressInterest(
          interest, response.onData, response.onTimeout, wireFormat)
예제 #7
0
    def _registerPrefixHelper(self, registeredPrefixId, prefix, onInterest,
                              onRegisterFailed, flags, wireFormat):
        """
        Do the work of registerPrefix to register with NDNx once we have an 
        ndndId_.
        
        :param int registeredPrefixId: The 
          _RegisteredPrefix.getNextRegisteredPrefixId() which registerPrefix got
          so it could return it to the caller. If this is 0, then don't add to 
          registeredPrefixTable_ (assuming it has already been done).  
        """
        # Create a ForwardingEntry.
        # Note: ndnd ignores any freshness that is larger than 3600 seconds and
        #   sets 300 seconds instead. To register "forever", (=2000000000 sec),
        #   the freshness period must be omitted.
        forwardingEntry = ForwardingEntry()
        forwardingEntry.setAction("selfreg")
        forwardingEntry.setPrefix(prefix)
        forwardingEntry.setForwardingFlags(flags)
        content = forwardingEntry.wireEncode(wireFormat)

        # Set the ForwardingEntry as the content of a Data packet and sign.
        data = Data()
        data.setContent(content)
        # Set the name to a random value so that each request is unique.
        nonce = bytearray(4)
        for i in range(len(nonce)):
            nonce[i] = _systemRandom.randint(0, 0xff)
        data.getName().append(nonce)
        # The ndnd ignores the signature, so set to blank values.
        data.getSignature().getKeyLocator().setType(
            KeyLocatorType.KEY_LOCATOR_DIGEST)
        data.getSignature().getKeyLocator().setKeyData(
            Blob(bytearray(32), False))
        data.getSignature().setSignature(Blob(bytearray(128), False))
        encodedData = data.wireEncode(wireFormat)

        # Create an interest where the name has the encoded Data packet.
        interestName = Name().append("ndnx").append(
            self._ndndId).append("selfreg").append(encodedData)

        interest = Interest(interestName)
        interest.setInterestLifetimeMilliseconds(4000.0)
        interest.setScope(1)
        encodedInterest = interest.wireEncode(wireFormat)

        if registeredPrefixId != 0:
            # Save the onInterest callback and send the registration interest.
            self._registeredPrefixTable.append(
                Node._RegisteredPrefix(registeredPrefixId, prefix, onInterest))

        response = Node._RegisterResponse(self, prefix, onInterest,
                                          onRegisterFailed, flags, wireFormat,
                                          False)
        self.expressInterest(interest, response.onData, response.onTimeout,
                             wireFormat)
예제 #8
0
파일: node.py 프로젝트: meng72/PyNDN2
    def _nfdRegisterPrefix(
      self, registeredPrefixId, prefix, onInterest, onRegisterFailed, flags,
      commandKeyChain, commandCertificateName, face):
        """
        Do the work of registerPrefix to register with NFD.

        :param int registeredPrefixId: The getNextEntryId() which registerPrefix
          got so it could return it to the caller. If this is 0, then don't add
          to _registeredPrefixTable (assuming it has already been done).
        """
        if commandKeyChain == None:
            raise RuntimeError(
              "registerPrefix: The command KeyChain has not been set. You must call setCommandSigningInfo.")
        if commandCertificateName.size() == 0:
            raise RuntimeError(
              "registerPrefix: The command certificate name has not been set. You must call setCommandSigningInfo.")

        controlParameters = ControlParameters()
        controlParameters.setName(prefix)
        controlParameters.setForwardingFlags(flags)

        commandInterest = Interest()
        if self.isLocal():
            commandInterest.setName(Name("/localhost/nfd/rib/register"))
            # The interest is answered by the local host, so set a short timeout.
            commandInterest.setInterestLifetimeMilliseconds(2000.0)
        else:
            commandInterest.setName(Name("/localhop/nfd/rib/register"))
            # The host is remote, so set a longer timeout.
            commandInterest.setInterestLifetimeMilliseconds(4000.0)
        # NFD only accepts TlvWireFormat packets.
        commandInterest.getName().append(controlParameters.wireEncode(TlvWireFormat.get()))
        self.makeCommandInterest(
          commandInterest, commandKeyChain, commandCertificateName,
          TlvWireFormat.get())

        if registeredPrefixId != 0:
            interestFilterId = 0
            if onInterest != None:
                # registerPrefix was called with the "combined" form that includes
                # the callback, so add an InterestFilterEntry.
                interestFilterId = self.getNextEntryId()
                self.setInterestFilter(
                  interestFilterId, InterestFilter(prefix), onInterest, face)

            self._registeredPrefixTable.append(Node._RegisteredPrefix(
              registeredPrefixId, prefix, interestFilterId))

        # Send the registration interest.
        response = Node._RegisterResponse(
          self, prefix, onInterest, onRegisterFailed, flags,
          TlvWireFormat.get(), True, face)
        self.expressInterest(
          self.getNextEntryId(), commandInterest, response.onData,
          response.onTimeout, TlvWireFormat.get(), face)
예제 #9
0
파일: node.py 프로젝트: tuple71/PyNDN2
    def _nfdRegisterPrefix(self, registeredPrefixId, prefix, onInterest,
                           onRegisterFailed, onRegisterSuccess,
                           registrationOptions, commandKeyChain,
                           commandCertificateName, face):
        """
        Do the work of registerPrefix to register with NFD.

        :param int registeredPrefixId: The getNextEntryId() which registerPrefix
          got so it could return it to the caller. If this is 0, then don't add
          to _registeredPrefixTable (assuming it has already been done).
        """
        if commandKeyChain == None:
            raise RuntimeError(
                "registerPrefix: The command KeyChain has not been set. You must call setCommandSigningInfo."
            )
        if commandCertificateName.size() == 0:
            raise RuntimeError(
                "registerPrefix: The command certificate name has not been set. You must call setCommandSigningInfo."
            )

        controlParameters = ControlParameters()
        controlParameters.setName(prefix)
        controlParameters.setForwardingFlags(registrationOptions)
        if (registrationOptions.getOrigin() != None
                and registrationOptions.getOrigin() >= 0):
            controlParameters.setOrigin(registrationOptions.getOrigin())
            # Remove the origin value from the flags since it is not used to encode.
            controlParameters.getForwardingFlags().setOrigin(None)

        commandInterest = Interest()
        commandInterest.setCanBePrefix(True)
        commandInterest.setMustBeFresh(True)
        if self.isLocal():
            commandInterest.setName(Name("/localhost/nfd/rib/register"))
            # The interest is answered by the local host, so set a short timeout.
            commandInterest.setInterestLifetimeMilliseconds(2000.0)
        else:
            commandInterest.setName(Name("/localhop/nfd/rib/register"))
            # The host is remote, so set a longer timeout.
            commandInterest.setInterestLifetimeMilliseconds(4000.0)
        # NFD only accepts TlvWireFormat packets.
        commandInterest.getName().append(
            controlParameters.wireEncode(TlvWireFormat.get()))
        self.makeCommandInterest(commandInterest, commandKeyChain,
                                 commandCertificateName, TlvWireFormat.get())

        # Send the registration interest.
        response = Node._RegisterResponse(prefix, onRegisterFailed,
                                          onRegisterSuccess,
                                          registeredPrefixId, self, onInterest,
                                          face)
        self.expressInterest(self.getNextEntryId(), commandInterest,
                             response.onData, response.onTimeout, None,
                             TlvWireFormat.get(), face)
예제 #10
0
 def _sendRecovery(self, syncDigest):
     """
     Send Recovery Interest.
     """
     logging.getLogger(__name__).info("unknown digest: ")
     name = Name(self._applicationBroadcastPrefix)
     name.append("recovery").append(syncDigest)
     interest = Interest(name)
     interest.setInterestLifetimeMilliseconds(self._syncLifetime)
     self._face.expressInterest(interest, self._onData, self._syncTimeout)
     logging.getLogger(__name__).info("Recovery Syncinterest expressed:")
     logging.getLogger(__name__).info("%s", name.toUri())
예제 #11
0
 def _sendRecovery(self, syncDigest):
     """
     Send Recovery Interest.
     """
     logging.getLogger(__name__).info("unknown digest: ")
     name = Name(self._applicationBroadcastPrefix)
     name.append("recovery").append(syncDigest)
     interest = Interest(name)
     interest.setInterestLifetimeMilliseconds(self._syncLifetime)
     self._face.expressInterest(interest, self._onData, self._syncTimeout)
     logging.getLogger(__name__).info("Recovery Syncinterest expressed:")
     logging.getLogger(__name__).info("%s", name.toUri())
예제 #12
0
파일: node.py 프로젝트: named-data/PyNDN2
    def _nfdRegisterPrefix(
      self, registeredPrefixId, prefix, onInterest, onRegisterFailed,
      onRegisterSuccess, registrationOptions, commandKeyChain,
      commandCertificateName, face):
        """
        Do the work of registerPrefix to register with NFD.

        :param int registeredPrefixId: The getNextEntryId() which registerPrefix
          got so it could return it to the caller. If this is 0, then don't add
          to _registeredPrefixTable (assuming it has already been done).
        """
        if commandKeyChain == None:
            raise RuntimeError(
              "registerPrefix: The command KeyChain has not been set. You must call setCommandSigningInfo.")
        if commandCertificateName.size() == 0:
            raise RuntimeError(
              "registerPrefix: The command certificate name has not been set. You must call setCommandSigningInfo.")

        controlParameters = ControlParameters()
        controlParameters.setName(prefix)
        controlParameters.setForwardingFlags(registrationOptions)
        if (registrationOptions.getOrigin() != None and
              registrationOptions.getOrigin() >= 0):
            controlParameters.setOrigin(registrationOptions.getOrigin())
            # Remove the origin value from the flags since it is not used to encode.
            controlParameters.getForwardingFlags().setOrigin(None)

        commandInterest = Interest()
        commandInterest.setCanBePrefix(True)
        if self.isLocal():
            commandInterest.setName(Name("/localhost/nfd/rib/register"))
            # The interest is answered by the local host, so set a short timeout.
            commandInterest.setInterestLifetimeMilliseconds(2000.0)
        else:
            commandInterest.setName(Name("/localhop/nfd/rib/register"))
            # The host is remote, so set a longer timeout.
            commandInterest.setInterestLifetimeMilliseconds(4000.0)
        # NFD only accepts TlvWireFormat packets.
        commandInterest.getName().append(controlParameters.wireEncode(TlvWireFormat.get()))
        self.makeCommandInterest(
          commandInterest, commandKeyChain, commandCertificateName,
          TlvWireFormat.get())

        # Send the registration interest.
        response = Node._RegisterResponse(
          prefix, onRegisterFailed, onRegisterSuccess, registeredPrefixId, self,
          onInterest, face)
        self.expressInterest(
          self.getNextEntryId(), commandInterest, response.onData,
          response.onTimeout, None, TlvWireFormat.get(), face)
예제 #13
0
    def publishNextSequenceNo(self, applicationInfo=None):
        """
        Increment the sequence number, create a sync message with the new
        sequence number and publish a data packet where the name is
        the applicationBroadcastPrefix + the root digest of the current digest
        tree. Then add the sync message to the digest tree and digest log which
        creates a new root digest. Finally, express an interest for the next sync
        update with the name applicationBroadcastPrefix + the new root digest.
        After this, your application should publish the content for the new
        sequence number. You can get the new sequence number with getSequenceNo().
        Note: Your application must call processEvents. Since processEvents
        modifies the internal ChronoSync data structures, your application should
        make sure that it calls processEvents in the same thread as
        publishNextSequenceNo() (which also modifies the data structures).

        :param Blob applicationInfo: (optional) This appends applicationInfo to
          the content of the sync messages. This same info is provided to the
          receiving application in the SyncState state object provided to the
          onReceivedSyncState callback.
        """
        applicationInfo = (applicationInfo if isinstance(
            applicationInfo, Blob) else Blob(applicationInfo))

        self._sequenceNo += 1

        syncMessage = SyncStateMsg()
        content = getattr(syncMessage, "ss").add()
        content.name = self._applicationDataPrefixUri
        content.type = SyncState_UPDATE
        content.seqno.seq = self._sequenceNo
        content.seqno.session = self._sessionNo
        if not applicationInfo.isNull() and applicationInfo.size() > 0:
            content.application_info = applicationInfo.toBytes()

        self._broadcastSyncState(self._digestTree.getRoot(), syncMessage)

        if not self._update(getattr(syncMessage, "ss")):
            # Since we incremented the sequence number, we expect there to be a
            #   new digest log entry.
            raise RuntimeError(
                "ChronoSync: update did not create a new digest log entry")

        # TODO: Should we have an option to not express an interest if this is the
        #   final publish of the session?
        interest = Interest(self._applicationBroadcastPrefix)
        interest.getName().append(self._digestTree.getRoot())
        interest.setInterestLifetimeMilliseconds(self._syncLifetime)
        self._face.expressInterest(interest, self._onData, self._syncTimeout)
예제 #14
0
    def publishNextSequenceNo(self, applicationInfo = None):
        """
        Increment the sequence number, create a sync message with the new
        sequence number and publish a data packet where the name is
        the applicationBroadcastPrefix + the root digest of the current digest
        tree. Then add the sync message to the digest tree and digest log which
        creates a new root digest. Finally, express an interest for the next sync
        update with the name applicationBroadcastPrefix + the new root digest.
        After this, your application should publish the content for the new
        sequence number. You can get the new sequence number with getSequenceNo().
        Note: Your application must call processEvents. Since processEvents
        modifies the internal ChronoSync data structures, your application should
        make sure that it calls processEvents in the same thread as
        publishNextSequenceNo() (which also modifies the data structures).

        :param Blob applicationInfo: (optional) This appends applicationInfo to
          the content of the sync messages. This same info is provided to the
          receiving application in the SyncState state object provided to the
          onReceivedSyncState callback.
        """
        applicationInfo = (applicationInfo if isinstance(applicationInfo, Blob)
          else Blob(applicationInfo))

        self._sequenceNo += 1

        syncMessage = SyncStateMsg()
        content = getattr(syncMessage, "ss").add()
        content.name = self._applicationDataPrefixUri
        content.type = SyncState_UPDATE
        content.seqno.seq = self._sequenceNo
        content.seqno.session = self._sessionNo
        if not applicationInfo.isNull() and applicationInfo.size() > 0:
            content.application_info = applicationInfo.toBytes()

        self._broadcastSyncState(self._digestTree.getRoot(), syncMessage)

        if not self._update(getattr(syncMessage, "ss")):
          # Since we incremented the sequence number, we expect there to be a
          #   new digest log entry.
          raise RuntimeError(
            "ChronoSync: update did not create a new digest log entry")

        # TODO: Should we have an option to not express an interest if this is the
        #   final publish of the session?
        interest = Interest(self._applicationBroadcastPrefix)
        interest.getName().append(self._digestTree.getRoot())
        interest.setInterestLifetimeMilliseconds(self._syncLifetime)
        self._face.expressInterest(interest, self._onData, self._syncTimeout)
예제 #15
0
    def _onInterest(self, prefix, interest, face, interestFilterId, filter):
        """
        Process the sync interest from the applicationBroadcastPrefix. If we
        can't satisfy the interest, add it to the pending interest table in
        the _contentCache so that a future call to contentCacheAdd may satisfy it.
        """
        if not self._enabled:
            # Ignore callbacks after the application calls shutdown().
            return

        # Search if the digest already exists in the digest log.
        logging.getLogger(__name__).info("Sync Interest received in callback.")
        logging.getLogger(__name__).info("%s", interest.getName().toUri())

        syncDigest = interest.getName().get(
            self._applicationBroadcastPrefix.size()).toEscapedString()
        if interest.getName().size(
        ) == self._applicationBroadcastPrefix.size() + 2:
            # Assume this is a recovery interest.
            syncDigest = interest.getName().get(
                self._applicationBroadcastPrefix.size() + 1).toEscapedString()
        logging.getLogger(__name__).info("syncDigest: %s", syncDigest)
        if (interest.getName().size()
                == self._applicationBroadcastPrefix.size() + 2
                or syncDigest == "00"):
            # Recovery interest or newcomer interest.
            self._processRecoveryInterest(interest, syncDigest, face)
        else:
            self._contentCache.storePendingInterest(interest, face)

            if syncDigest != self._digestTree.getRoot():
                index = self._logFind(syncDigest)
                if index == -1:
                    # To see whether there is any data packet coming back, wait
                    #   2 seconds using the Interest timeout mechanism.
                    # TODO: Are we sure using a "/local/timeout" interest is the
                    #   best future call approach?
                    timeout = Interest(Name("/local/timeout"))
                    timeout.setInterestLifetimeMilliseconds(2000)
                    self._face.expressInterest(
                        timeout, self._dummyOnData,
                        self._makeJudgeRecovery(syncDigest, face))
                    logging.getLogger(__name__).info("set timer recover")
                else:
                    # common interest processing
                    self._processSyncInterest(index, syncDigest, face)
예제 #16
0
    def _onInterest(self, prefix, interest, face, interestFilterId, filter):
        """
        Process the sync interest from the applicationBroadcastPrefix. If we
        can't satisfy the interest, add it to the pending interest table in
        the _contentCache so that a future call to contentCacheAdd may satisfy it.
        """
        if not self._enabled:
            # Ignore callbacks after the application calls shutdown().
            return

        # Search if the digest already exists in the digest log.
        logging.getLogger(__name__).info("Sync Interest received in callback.")
        logging.getLogger(__name__).info("%s", interest.getName().toUri())

        syncDigest = interest.getName().get(
          self._applicationBroadcastPrefix.size()).toEscapedString()
        if interest.getName().size() == self._applicationBroadcastPrefix.size() + 2:
            # Assume this is a recovery interest.
            syncDigest = interest.getName().get(
              self._applicationBroadcastPrefix.size() + 1).toEscapedString()
        logging.getLogger(__name__).info("syncDigest: %s", syncDigest)
        if (interest.getName().size() == self._applicationBroadcastPrefix.size() + 2 or
             syncDigest == "00"):
            # Recovery interest or newcomer interest.
            self._processRecoveryInterest(interest, syncDigest, face)
        else:
            self._contentCache.storePendingInterest(interest, face)

            if syncDigest != self._digestTree.getRoot():
                index = self._logFind(syncDigest)
                if index == -1:
                    # To see whether there is any data packet coming back, wait
                    #   2 seconds using the Interest timeout mechanism.
                    # TODO: Are we sure using a "/local/timeout" interest is the
                    #   best future call approach?
                    timeout = Interest(Name("/local/timeout"))
                    timeout.setInterestLifetimeMilliseconds(2000)
                    self._face.expressInterest(
                      timeout, self._dummyOnData,
                      self._makeJudgeRecovery(syncDigest, face))
                    logging.getLogger(__name__).info("set timer recover")
                else:
                    # common interest processing
                    self._processSyncInterest(index, syncDigest, face)
예제 #17
0
    def _sendSyncInterest(self):
        """
        Send the sync interest for full synchronization. This forms the interest
        name: /<sync-prefix>/<own-IBLT>. This cancels any pending sync interest
        we sent earlier on the face.
        """
        # Debug: Implement stopping an ongoing fetch.
        ## If we send two sync interest one after the other
        ## since there is no new data in the network yet,
        ## when data is available it may satisfy both of them
        #if self._fetcher != None:
        #    self._fetcher.stop()

        # Sync Interest format for full sync: /<sync-prefix>/<ourLatestIBF>
        syncInterestName = Name(self._syncPrefix)

        # Append our latest IBLT.
        syncInterestName.append(self._iblt.encode())

        self._outstandingInterestName = syncInterestName

        # random1 is from 0.0 to 1.0.
        random1 = self._systemRandom.random()
        # Get a jitter of +/- syncInterestLifetime_ * 0.2 .
        jitter = (random1 - 0.5) * (self._syncInterestLifetime * 0.2)

        self._face.callLater(self._syncInterestLifetime / 2 + jitter,
                             self._sendSyncInterest)

        syncInterest = Interest(syncInterestName)
        syncInterest.setInterestLifetimeMilliseconds(
            self._syncInterestLifetime)
        syncInterest.refreshNonce()

        SegmentFetcher.fetch(
            self._face, syncInterest, None,
            lambda content: self._onSyncData(content, syncInterest),
            FullPSync2017._onError)

        logging.getLogger(__name__).debug("sendFullSyncInterest, nonce: " +
                                          syncInterest.getNonce().toHex() +
                                          ", hash: " +
                                          str(abs(hash(syncInterestName))))
예제 #18
0
    def _nfdRegisterPrefix(self, registeredPrefixId, prefix, onInterest,
                           onRegisterFailed, flags, commandKeyChain,
                           commandCertificateName):
        """
        Do the work of registerPrefix to register with NFD.
        
        :param int registeredPrefixId: The 
          _RegisteredPrefix.getNextRegisteredPrefixId() which registerPrefix got
          so it could return it to the caller. If this is 0, then don't add to 
          registeredPrefixTable_ (assuming it has already been done).  
        """
        if commandKeyChain == None:
            raise RuntimeError(
                "registerPrefix: The command KeyChain has not been set. You must call setCommandSigningInfo."
            )
        if commandCertificateName.size() == 0:
            raise RuntimeError(
                "registerPrefix: The command certificate name has not been set. You must call setCommandSigningInfo."
            )

        controlParameters = ControlParameters()
        controlParameters.setName(prefix)

        commandInterest = Interest(Name("/localhost/nfd/rib/register"))
        # NFD only accepts TlvWireFormat packets.
        commandInterest.getName().append(
            controlParameters.wireEncode(TlvWireFormat.get()))
        self.makeCommandInterest(commandInterest, commandKeyChain,
                                 commandCertificateName, TlvWireFormat.get())
        # The interest is answered by the local host, so set a short timeout.
        commandInterest.setInterestLifetimeMilliseconds(2000.0)

        if registeredPrefixId != 0:
            # Save the onInterest callback and send the registration interest.
            self._registeredPrefixTable.append(
                Node._RegisteredPrefix(registeredPrefixId, prefix, onInterest))

        response = Node._RegisterResponse(self, prefix, onInterest,
                                          onRegisterFailed, flags,
                                          TlvWireFormat.get(), True)
        self.expressInterest(commandInterest, response.onData,
                             response.onTimeout, TlvWireFormat.get())
예제 #19
0
    def _makeNotificationInterest(self):
        """
        Make and return a new Interest where the name is
        _applicationBroadcastPrefix plus the encoding of _stateVector. Also
        use _hmacKey to sign it with HmacWithSha256.

        :return: The new signed notification interest.
        :rtype: Interest
        """
        interest = Interest(self._applicationBroadcastPrefix)
        interest.setInterestLifetimeMilliseconds(
            self._notificationInterestLifetime)
        interest.getName().append(
            StateVectorSync2018.encodeStateVector(self._stateVector,
                                                  self._sortedStateVectorKeys))

        # TODO: Should we just use key name /A ?
        KeyChain.signWithHmacWithSha256(interest, self._hmacKey, Name("/A"))

        return interest
예제 #20
0
파일: node.py 프로젝트: WeiqiJust/NDN-total
    def _nfdRegisterPrefix(
      self, registeredPrefixId, prefix, onInterest, onRegisterFailed, flags,
      commandKeyChain, commandCertificateName):
        """
        Do the work of registerPrefix to register with NFD.

        :param int registeredPrefixId: The
          _RegisteredPrefix.getNextRegisteredPrefixId() which registerPrefix got
          so it could return it to the caller. If this is 0, then don't add to
          _registeredPrefixTable (assuming it has already been done).
        """
        if commandKeyChain == None:
            raise RuntimeError(
              "registerPrefix: The command KeyChain has not been set. You must call setCommandSigningInfo.")
        if commandCertificateName.size() == 0:
            raise RuntimeError(
              "registerPrefix: The command certificate name has not been set. You must call setCommandSigningInfo.")

        controlParameters = ControlParameters()
        controlParameters.setName(prefix)

        commandInterest = Interest(Name("/localhost/nfd/rib/register"))
        # NFD only accepts TlvWireFormat packets.
        commandInterest.getName().append(controlParameters.wireEncode(TlvWireFormat.get()))
        self.makeCommandInterest(
          commandInterest, commandKeyChain, commandCertificateName,
          TlvWireFormat.get())
        # The interest is answered by the local host, so set a short timeout.
        commandInterest.setInterestLifetimeMilliseconds(2000.0)

        if registeredPrefixId != 0:
            # Save the onInterest callback and send the registration interest.
            self._registeredPrefixTable.append(Node._RegisteredPrefix(
              registeredPrefixId, prefix, onInterest))

        response = Node._RegisterResponse(
          self, prefix, onInterest, onRegisterFailed, flags,
          TlvWireFormat.get(), True)
        self.expressInterest(
          commandInterest, response.onData, response.onTimeout,
          TlvWireFormat.get())
예제 #21
0
    def _onInterest(self, prefix, interest, transport, registerPrefixId):
        if not self._enabled:
            # Ignore callbacks after the application calls shutdown().
            return

        # Search if the digest already exists in the digest log.
        logging.getLogger(__name__).info("Sync Interest received in callback.")
        logging.getLogger(__name__).info("%s", interest.getName().toUri())

        syncDigest = interest.getName().get(
          self._applicationBroadcastPrefix.size()).toEscapedString()
        if interest.getName().size() == self._applicationBroadcastPrefix.size() + 2:
            # Assume this is a recovery interest.
            syncDigest = interest.getName().get(
              self._applicationBroadcastPrefix.size() + 1).toEscapedString()
        logging.getLogger(__name__).info("syncDigest: %s", syncDigest)
        if (interest.getName().size() == self._applicationBroadcastPrefix.size() + 2 or
             syncDigest == "00"):
            # Recovery interest or newcomer interest.
            self._processRecoveryInterest(interest, syncDigest, transport)
        else:
            # Save the unanswered interest in our local pending interest table.
            self._pendingInterestTable.append(self._PendingInterest(
              interest, transport))

            if syncDigest != self._digestTree.getRoot():
                index = self._logFind(syncDigest)
                if index == -1:
                    # To see whether there is any data packet coming back, wait
                    #   2 seconds using the Interest timeout mechanism.
                    # TODO: Are we sure using a "/local/timeout" interest is the
                    #   best future call approach?
                    timeout = Interest(Name("/local/timeout"))
                    timeout.setInterestLifetimeMilliseconds(2000)
                    self._face.expressInterest(
                      timeout, self._dummyOnData,
                      self._makeJudgeRecovery(syncDigest, transport))
                    logging.getLogger(__name__).info("set timer recover")
                else:
                    # common interest processing
                    self._processSyncInterest(index, syncDigest, transport)
예제 #22
0
    def _sendSyncInterest(self):
        """
        Send the sync interest for full synchronization. This forms the interest
        name: /<sync-prefix>/<own-IBLT>. This cancels any pending sync interest
        we sent earlier on the face.
        """
        # Debug: Implement stopping an ongoing fetch.
        ## If we send two sync interest one after the other
        ## since there is no new data in the network yet,
        ## when data is available it may satisfy both of them
        #if self._fetcher != None:
        #    self._fetcher.stop()

        # Sync Interest format for full sync: /<sync-prefix>/<ourLatestIBF>
        syncInterestName = Name(self._syncPrefix)

        # Append our latest IBLT.
        syncInterestName.append(self._iblt.encode())

        self._outstandingInterestName = syncInterestName

        # random1 is from 0.0 to 1.0.
        random1 = self._systemRandom.random()
        # Get a jitter of +/- syncInterestLifetime_ * 0.2 .
        jitter = (random1 - 0.5) * (self._syncInterestLifetime * 0.2)

        self._face.callLater(
          self._syncInterestLifetime / 2 + jitter, self._sendSyncInterest)

        syncInterest = Interest(syncInterestName)
        syncInterest.setInterestLifetimeMilliseconds(self._syncInterestLifetime)
        syncInterest.refreshNonce()

        SegmentFetcher.fetch(
          self._face, syncInterest, None,
          lambda content: self._onSyncData(content, syncInterest),
          FullPSync2017._onError)

        logging.getLogger(__name__).debug("sendFullSyncInterest, nonce: " +
          syncInterest.getNonce().toHex() + ", hash: " +
          str(abs(hash(syncInterestName))))
예제 #23
0
    def _syncTimeout(self, interest):
        """
        Sync interest time out.  If the interest is the static one send again.
        """
        if not self._enabled:
            # Ignore callbacks after the application calls shutdown().
            return

        logging.getLogger(__name__).info("Sync Interest time out.")
        logging.getLogger(__name__).info(
          "Sync Interest name: %s", interest.getName().toUri())
        component = interest.getName().get(4).toEscapedString()
        if component == self._digestTree.getRoot():
            name = Name(interest.getName())
            retryInterest = Interest(interest.getName())
            retryInterest.setInterestLifetimeMilliseconds(self._syncLifetime)
            self._face.expressInterest(
              retryInterest, self._onData, self._syncTimeout)

            logging.getLogger(__name__).info("Syncinterest expressed:")
            logging.getLogger(__name__).info("%s", name.toUri())
예제 #24
0
    def _syncTimeout(self, interest):
        """
        Sync interest time out.  If the interest is the static one send again.
        """
        if not self._enabled:
            # Ignore callbacks after the application calls shutdown().
            return

        logging.getLogger(__name__).info("Sync Interest time out.")
        logging.getLogger(__name__).info("Sync Interest name: %s",
                                         interest.getName().toUri())
        component = interest.getName().get(4).toEscapedString()
        if component == self._digestTree.getRoot():
            name = Name(interest.getName())
            retryInterest = Interest(interest.getName())
            retryInterest.setInterestLifetimeMilliseconds(self._syncLifetime)
            self._face.expressInterest(retryInterest, self._onData,
                                       self._syncTimeout)

            logging.getLogger(__name__).info("Syncinterest expressed:")
            logging.getLogger(__name__).info("%s", name.toUri())
예제 #25
0
    def _initialTimeOut(self, interest):
        """
        Initial sync interest timeout, which means there are no other publishers
        yet.
        """
        if not self._enabled:
            # Ignore callbacks after the application calls shutdown().
            return

        logging.getLogger(__name__).info("initial sync timeout")
        logging.getLogger(__name__).info("no other people")
        self._sequenceNo += 1
        if self._sequenceNo != 0:
            # Since there were no other users, we expect sequence no 0.
            raise RuntimeError(
                "ChronoSync: sequenceNo_ is not the expected value of 0 for first use."
            )

        tempContent = SyncStateMsg()
        content = getattr(tempContent, "ss").add()
        content.name = self._applicationDataPrefixUri
        content.type = SyncState_UPDATE
        content.seqno.seq = self._sequenceNo
        content.seqno.session = self._sessionNo
        self._update(getattr(tempContent, "ss"))

        try:
            self._onInitialized()
        except:
            logging.exception("Error in onInitialized")

        name = Name(self._applicationBroadcastPrefix)
        name.append(self._digestTree.getRoot())
        retryInterest = Interest(name)
        retryInterest.setInterestLifetimeMilliseconds(self._syncLifetime)
        self._face.expressInterest(retryInterest, self._onData,
                                   self._syncTimeout)
        logging.getLogger(__name__).info("Syncinterest expressed:")
        logging.getLogger(__name__).info("%s", name.toUri())
예제 #26
0
    def publishNextSequenceNo(self):
        """
        Increment the sequence number, create a sync message with the new
        sequence number and publish a data packet where the name is
        the applicationBroadcastPrefix + the root digest of the current digest
        tree. Then add the sync message to the digest tree and digest log which
        creates a new root digest. Finally, express an interest for the next sync
        update with the name applicationBroadcastPrefix + the new root digest.
        After this, your application should publish the content for the new
        sequence number. You can get the new sequence number with getSequenceNo().
        Note: Your application must call processEvents. Since processEvents
        modifies the internal ChronoSync data structures, your application should
        make sure that it calls processEvents in the same thread as
        publishNextSequenceNo() (which also modifies the data structures).
        """
        self._sequenceNo += 1

        syncMessage = sync_state_pb2.SyncStateMsg()
        content = getattr(syncMessage, "ss").add()
        content.name = self._applicationDataPrefixUri
        content.type = SyncState_UPDATE
        content.seqno.seq = self._sequenceNo
        content.seqno.session = self._sessionNo

        self._broadcastSyncState(self._digestTree.getRoot(), syncMessage)

        if not self._update(getattr(syncMessage, "ss")):
          # Since we incremented the sequence number, we expect there to be a
          #   new digest log entry.
          raise RuntimeError(
            "ChronoSync: update did not create a new digest log entry")

        # TODO: Should we have an option to not express an interest if this is the
        #   final publish of the session?
        interest = Interest(self._applicationBroadcastPrefix)
        interest.getName().append(self._digestTree.getRoot())
        interest.setInterestLifetimeMilliseconds(self._syncLifetime)
        self._face.expressInterest(interest, self._onData, self._syncTimeout)
예제 #27
0
    def publishNextSequenceNo(self):
        """
        Increment the sequence number, create a sync message with the new
        sequence number and publish a data packet where the name is
        the applicationBroadcastPrefix + the root digest of the current digest
        tree. Then add the sync message to the digest tree and digest log which
        creates a new root digest. Finally, express an interest for the next sync
        update with the name applicationBroadcastPrefix + the new root digest.
        After this, your application should publish the content for the new
        sequence number. You can get the new sequence number with getSequenceNo().
        Note: Your application must call processEvents. Since processEvents
        modifies the internal ChronoSync data structures, your application should
        make sure that it calls processEvents in the same thread as
        publishNextSequenceNo() (which also modifies the data structures).
        """
        self._sequenceNo += 1

        syncMessage = sync_state_pb2.SyncStateMsg()
        content = getattr(syncMessage, "ss").add()
        content.name = self._applicationDataPrefixUri
        content.type = SyncState_UPDATE
        content.seqno.seq = self._sequenceNo
        content.seqno.session = self._sessionNo

        self._broadcastSyncState(self._digestTree.getRoot(), syncMessage)

        if not self._update(getattr(syncMessage, "ss")):
          # Since we incremented the sequence number, we expect there to be a
          #   new digest log entry.
          raise RuntimeError(
            "ChronoSync: update did not create a new digest log entry")

        # TODO: Should we have an option to not express an interest if this is the
        #   final publish of the session?
        interest = Interest(self._applicationBroadcastPrefix)
        interest.getName().append(self._digestTree.getRoot())
        interest.setInterestLifetimeMilliseconds(self._syncLifetime)
        self._face.expressInterest(interest, self._onData, self._syncTimeout)
예제 #28
0
    def _onTimeout(self, interest):
        interestLifetime = interest.getInterestLifetimeMilliseconds()
        if interestLifetime == None:
            # Can't re-express.
            if self._callerOnTimeout != None:
                try:
                    self._callerOnTimeout(interest)
                except:
                    logging.exception("Error in onTimeout")
            return

        nextInterestLifetime = interestLifetime * 2
        if nextInterestLifetime > self._maxInterestLifetime:
            if self._callerOnTimeout != None:
                try:
                    self._callerOnTimeout(interest)
                except:
                    logging.exception("Error in onTimeout")
            return

        nextInterest = Interest(interest)
        nextInterest.setInterestLifetimeMilliseconds(nextInterestLifetime)
        self._face.expressInterest(
          nextInterest, self._callerOnData, self._onTimeout)
예제 #29
0
    def _onTimeout(self, interest):
        interestLifetime = interest.getInterestLifetimeMilliseconds()
        if interestLifetime == None:
            # Can't re-express.
            if self._callerOnTimeout != None:
                try:
                    self._callerOnTimeout(interest)
                except:
                    logging.exception("Error in onTimeout")
            return

        nextInterestLifetime = interestLifetime * 2
        if nextInterestLifetime > self._maxInterestLifetime:
            if self._callerOnTimeout != None:
                try:
                    self._callerOnTimeout(interest)
                except:
                    logging.exception("Error in onTimeout")
            return

        nextInterest = Interest(interest)
        nextInterest.setInterestLifetimeMilliseconds(nextInterestLifetime)
        self._face.expressInterest(
          nextInterest, self._callerOnData, self._onTimeout)
예제 #30
0
파일: face.py 프로젝트: ISHITADG/PyNDN2
    def _getExpressInterestArgs(self, interestOrName, arg2, arg3, arg4, arg5,
                                arg6):
        """
        This is a protected helper method to resolve the different overloaded
        forms of Face.expressInterest and return the arguments to pass to
        Node.expressInterest. This is necessary to prepare arguments such as
        interestCopy before dispatching to Node.expressInterest.

        :return: A dictionary with the following keys: 'pendingInterestId',
          'interestCopy', 'onData', 'onTimeout', 'onNetworkNack' and 'wireFormat'.
        :rtype: dict
        """
        if type(interestOrName) is Interest:
            # Node.expressInterest requires a copy of the interest.
            interestCopy = Interest(interestOrName)
        else:
            # The first argument is a name. Make the interest from the name and
            #   possible template.
            if type(arg2) is Interest:
                template = arg2
                # Copy the template.
                interestCopy = Interest(template)
                interestCopy.setName(interestOrName)

                # Shift the remaining args to be processed below.
                arg2 = arg3
                arg3 = arg4
                arg4 = arg5
                arg5 = arg6
            else:
                # No template.
                interestCopy = Interest(interestOrName)
                # Set a default interest lifetime.
                interestCopy.setInterestLifetimeMilliseconds(4000.0)

        onData = arg2
        # arg3,       arg4,          arg5 may be:
        # OnTimeout,  OnNetworkNack, WireFormat
        # OnTimeout,  OnNetworkNack, None
        # OnTimeout,  WireFormat,    None
        # OnTimeout,  None,          None
        # WireFormat, None,          None
        # None,       None,          None
        if isinstance(arg3, collections.Callable):
            onTimeout = arg3
        else:
            onTimeout = None

        if isinstance(arg4, collections.Callable):
            onNetworkNack = arg4
        else:
            onNetworkNack = None

        if isinstance(arg3, WireFormat):
            wireFormat = arg3
        elif isinstance(arg4, WireFormat):
            wireFormat = arg4
        elif isinstance(arg5, WireFormat):
            wireFormat = arg5
        else:
            wireFormat = WireFormat.getDefaultWireFormat()

        return {
            'pendingInterestId': self._node.getNextEntryId(),
            'interestCopy': interestCopy,
            'onData': onData,
            'onTimeout': onTimeout,
            'onNetworkNack': onNetworkNack,
            'wireFormat': wireFormat
        }
예제 #31
0
    def _getExpressInterestArgs(self, interestOrName, arg2, arg3, arg4, arg5):
        """
        This is a protected helper method to resolve the different overloaded
        forms of Face.expressInterest and return the arguments to pass to
        Node.expressInterest. This is necessary to prepare arguments such as
        interestCopy before dispatching to Node.expressInterest.

        :return: A dictionary with the following keys: 'pendingInterestId',
          'interestCopy', 'onData', 'onTimeout' and 'wireFormat'.
        :rtype: dict
        """
        # expressInterest(interest, onData)
        # expressInterest(interest, onData, wireFormat)
        # expressInterest(interest, onData, onTimeout)
        # expressInterest(interest, onData, onTimeout, wireFormat)
        if type(interestOrName) is Interest:
            # Node.expressInterest requires a copy of the interest.
            interestCopy = Interest(interestOrName)
            onData = arg2
            if isinstance(arg3, WireFormat):
                onTimeout = None
                wireFormat = arg3
            else:
                onTimeout = arg3
                wireFormat = arg4
        else:
            # The first argument is a name. Make the interest from the name and
            #   possible template.

            # expressInterest(name, interestTemplate, onData)
            # expressInterest(name, interestTemplate, onData, wireFormat)
            # expressInterest(name, interestTemplate, onData, onTimeout)
            # expressInterest(name, interestTemplate, onData, onTimeout, wireFormat)
            if type(arg2) is Interest:
                template = arg2
                # Copy the template.
                interestCopy = Interest(template)
                interestCopy.setName(interestOrName)

                onData = arg3
                if isinstance(arg4, WireFormat):
                    onTimeout = None
                    wireFormat = arg4
                else:
                    onTimeout = arg4
                    wireFormat = arg5
            # expressInterest(name, onData)
            # expressInterest(name, onData, wireFormat)
            # expressInterest(name, onData, onTimeout)
            # expressInterest(name, onData, onTimeout, wireFormat)
            else:
                interestCopy = Interest(interestOrName)
                # Set a default interest lifetime.
                interestCopy.setInterestLifetimeMilliseconds(4000.0)
                onData = arg2
                if isinstance(arg3, WireFormat):
                    onTimeout = None
                    wireFormat = arg3
                else:
                    onTimeout = arg3
                    wireFormat = arg4

        if wireFormat == None:
            # Don't use a default argument since getDefaultWireFormat can change.
            wireFormat = WireFormat.getDefaultWireFormat()

        return { 'pendingInterestId': self._node.getNextEntryId(),
          'interestCopy': interestCopy, 'onData': onData, 'onTimeout': onTimeout,
          'wireFormat': wireFormat }
예제 #32
0
파일: node.py 프로젝트: meng72/PyNDN2
    def _registerPrefixHelper(
      self, registeredPrefixId, prefix, onInterest, onRegisterFailed, flags,
      wireFormat, face):
        """
        Do the work of registerPrefix to register with NDNx once we have an
        _ndndId.

        :param int registeredPrefixId: The getNextEntryId() which registerPrefix
          got so it could return it to the caller. If this is 0, then don't add
          to _registeredPrefixTable (assuming it has already been done).
        """
        if not WireFormat.ENABLE_NDNX:
            # We can get here if the command signing info is set, but running NDNx.
            raise RuntimeError(
              "registerPrefix with NDNx is deprecated. To enable while you upgrade your code to use NFD, set WireFormat.ENABLE_NDNX = True")

        # Create a ForwardingEntry.
        # Note: ndnd ignores any freshness that is larger than 3600 seconds and
        #   sets 300 seconds instead. To register "forever", (=2000000000 sec),
        #   the freshness period must be omitted.
        forwardingEntry = ForwardingEntry()
        forwardingEntry.setAction("selfreg")
        forwardingEntry.setPrefix(prefix)
        forwardingEntry.setForwardingFlags(flags)
        content = forwardingEntry.wireEncode(wireFormat)

        # Set the ForwardingEntry as the content of a Data packet and sign.
        data = Data()
        data.setContent(content)
        # Set the name to a random value so that each request is unique.
        nonce = bytearray(4)
        for i in range(len(nonce)):
            nonce[i] = _systemRandom.randint(0, 0xff)
        data.getName().append(nonce)
        # The ndnd ignores the signature, so set to blank values.
        data.getSignature().getKeyLocator().setType(
          KeyLocatorType.KEY_LOCATOR_DIGEST)
        data.getSignature().getKeyLocator().setKeyData(
          Blob(bytearray(32), False))
        data.getSignature().setSignature(Blob(bytearray(128), False))
        encodedData = data.wireEncode(wireFormat)

        # Create an interest where the name has the encoded Data packet.
        interestName = Name().append("ndnx").append(self._ndndId).append(
          "selfreg").append(encodedData)

        interest = Interest(interestName)
        interest.setInterestLifetimeMilliseconds(4000.0)
        interest.setScope(1)

        if registeredPrefixId != 0:
            interestFilterId = 0
            if onInterest != None:
                # registerPrefix was called with the "combined" form that includes
                # the callback, so add an InterestFilterEntry.
                interestFilterId = self.getNextEntryId()
                self.setInterestFilter(
                  interestFilterId, InterestFilter(prefix), onInterest, face)

            self._registeredPrefixTable.append(Node._RegisteredPrefix(
              registeredPrefixId, prefix, interestFilterId))

        # Send the registration interest.
        response = Node._RegisterResponse(
          self, prefix, onInterest, onRegisterFailed, flags, wireFormat, False,
          face)
        self.expressInterest(
          self.getNextEntryId(), interest, response.onData, response.onTimeout,
          wireFormat, face)
예제 #33
0
파일: face.py 프로젝트: MAHIS/PyNDN2
    def _getExpressInterestArgs(self, interestOrName, arg2, arg3, arg4, arg5):
        """
        This is a protected helper method to resolve the different overloaded
        forms of Face.expressInterest and return the arguments to pass to
        Node.expressInterest. This is necessary to prepare arguments such as
        interestCopy before dispatching to Node.expressInterest.

        :return: A dictionary with the following keys: 'pendingInterestId',
          'interestCopy', 'onData', 'onTimeout' and 'wireFormat'.
        :rtype: dict
        """
        # expressInterest(interest, onData)
        # expressInterest(interest, onData, wireFormat)
        # expressInterest(interest, onData, onTimeout)
        # expressInterest(interest, onData, onTimeout, wireFormat)
        if type(interestOrName) is Interest:
            # Node.expressInterest requires a copy of the interest.
            interestCopy = Interest(interestOrName)
            onData = arg2
            if isinstance(arg3, WireFormat):
                onTimeout = None
                wireFormat = arg3
            else:
                onTimeout = arg3
                wireFormat = arg4
        else:
            # The first argument is a name. Make the interest from the name and
            #   possible template.

            # expressInterest(name, interestTemplate, onData)
            # expressInterest(name, interestTemplate, onData, wireFormat)
            # expressInterest(name, interestTemplate, onData, onTimeout)
            # expressInterest(name, interestTemplate, onData, onTimeout, wireFormat)
            if type(arg2) is Interest:
                template = arg2
                # Copy the template.
                interestCopy = Interest(template)
                interestCopy.setName(interestOrName)

                onData = arg3
                if isinstance(arg4, WireFormat):
                    onTimeout = None
                    wireFormat = arg4
                else:
                    onTimeout = arg4
                    wireFormat = arg5
            # expressInterest(name, onData)
            # expressInterest(name, onData, wireFormat)
            # expressInterest(name, onData, onTimeout)
            # expressInterest(name, onData, onTimeout, wireFormat)
            else:
                interestCopy = Interest(interestOrName)
                # Set a default interest lifetime.
                interestCopy.setInterestLifetimeMilliseconds(4000.0)
                onData = arg2
                if isinstance(arg3, WireFormat):
                    onTimeout = None
                    wireFormat = arg3
                else:
                    onTimeout = arg3
                    wireFormat = arg4

        if wireFormat == None:
            # Don't use a default argument since getDefaultWireFormat can change.
            wireFormat = WireFormat.getDefaultWireFormat()

        return { 'pendingInterestId': self._node.getNextEntryId(),
          'interestCopy': interestCopy, 'onData': onData, 'onTimeout': onTimeout,
          'wireFormat': wireFormat }
예제 #34
0
파일: node.py 프로젝트: meng72/PyNDN2
class Node(object):
    """
    Create a new Node for communication with an NDN hub with the given Transport
    object and connectionInfo.

    :param Transport transport: An object of a subclass of Transport used for
      communication.
    :param Transport.ConnectionInfo connectionInfo: An object of a subclass of
      Transport.ConnectionInfo to be used to connect to the transport.
    """
    def __init__(self, transport, connectionInfo):
        self._transport = transport
        self._connectionInfo = connectionInfo
        # An array of _PendingInterest
        self._pendingInterestTable = []
        # An array of _RegisteredPrefix
        self._registeredPrefixTable = []
        # An array of _InterestFilterEntry
        self._interestFilterTable = []
        # An array of _DelayedCall
        self._delayedCallTable = []
        # An array of function objects
        self._onConnectedCallbacks = []
        self._ndndIdFetcherInterest = Interest(
          Name("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY"))
        self._ndndIdFetcherInterest.setInterestLifetimeMilliseconds(4000.0)
        self._ndndId = None
        self._commandInterestGenerator = CommandInterestGenerator()
        self._timeoutPrefix = Name("/local/timeout")
        self._lastEntryId = 0
        self._lastEntryIdLock = threading.Lock()
        self._connectStatus = Node._ConnectStatus.UNCONNECTED

    def expressInterest(
      self, pendingInterestId, interestCopy, onData, onTimeout, wireFormat, face):
        """
        Send the Interest through the transport, read the entire response and
        call onData(interest, data).

        :param int pendingInterestId: The getNextEntryId() for the pending
          interest ID which Face got so it could return it to the caller.
        :param Interest interestCopy: The Interest which is NOT copied for this
          internal Node method.  The Face expressInterest is responsible for
          making a copy for Node to use.
        :param onData: A function object to call when a matching data packet is
          received.
        :type onData: function object
        :param onTimeout: A function object to call if the interest times out.
          If onTimeout is None, this does not use it.
        :type onTimeout: function object
        :param wireFormat: A WireFormat object used to encode the message.
        :type wireFormat: a subclass of WireFormat
        :param Face face: The face which has the callLater method, used for
          interest timeouts. The callLater method may be overridden in a
          subclass of Face.
        :throws: RuntimeError If the encoded interest size exceeds
          getMaxNdnPacketSize().
        """
        # TODO: Properly check if we are already connected to the expected host.
        if self._connectStatus == self._ConnectStatus.CONNECT_COMPLETE:
            # We are connected. Simply send the interest.
            self._expressInterestHelper(
              pendingInterestId, interestCopy, onData, onTimeout, wireFormat,
              face)
            return

        if self._connectStatus == Node._ConnectStatus.UNCONNECTED:
            self._connectStatus = Node._ConnectStatus.CONNECT_REQUESTED

            # expressInterestHelper will be called by onConnected.
            self._onConnectedCallbacks.append(
              lambda: self._expressInterestHelper
                (pendingInterestId, interestCopy, onData, onTimeout, wireFormat,
                 face))

            def onConnected():
                # Assume that further calls to expressInterest dispatched to the
                # event loop are queued and won't enter expressInterest until
                # this method completes and sets CONNECT_COMPLETE.
                # Call each callback added while the connection was opening.
                for onConnectedCallback in self._onConnectedCallbacks:
                    onConnectedCallback()
                self._onConnectedCallbacks = []

                # Make future calls to expressInterest send directly to the
                # Transport.
                self._connectStatus = Node._ConnectStatus.CONNECT_COMPLETE
                    
            self._transport.connect(self._connectionInfo, self, onConnected)
        elif self._connectStatus == self._ConnectStatus.CONNECT_REQUESTED:
            # Still connecting. add to the interests to express by onConnected.
            self._onConnectedCallbacks.append(
              lambda: self._expressInterestHelper
                (pendingInterestId, interestCopy, onData, onTimeout, wireFormat,
                 face))
        else:
            # Don't expect this to happen.
            raise RuntimeError(
              "Node: Unrecognized _connectStatus " + str(self._connectStatus))

    def removePendingInterest(self, pendingInterestId):
        """
        Remove the pending interest entry with the pendingInterestId from the
        pending interest table. This does not affect another pending interest
        with a different pendingInterestId, even if it has the same interest
        name. If there is no entry with the pendingInterestId, do nothing.

        :param int pendingInterestId: The ID returned from expressInterest.
        """
        count = 0
        # Go backwards through the list so we can erase entries.
        # Remove all entries even though pendingInterestId should be unique.
        i = len(self._pendingInterestTable) - 1
        while i >= 0:
            if (self._pendingInterestTable[i].getPendingInterestId() ==
                  pendingInterestId):
                count += 1
                # For efficiency, mark this as removed so that
                # _processInterestTimeout doesn't look for it.
                self._pendingInterestTable[i].setIsRemoved()
                self._pendingInterestTable.pop(i)
            i -= 1

        if count == 0:
            logging.getLogger(__name__).debug(
              "removePendingInterest: Didn't find pendingInterestId " + pendingInterestId)

    def makeCommandInterest(self, interest, keyChain, certificateName, wireFormat):
        """
        Append a timestamp component and a random value component to interest's
        name. Then use the keyChain and certificateName to sign the interest.
        If the interest lifetime is not set, this sets it.

        :param Interest interest: The interest whose name is append with
          components.
        :param KeyChain keyChain: The KeyChain for calling sign.
        :param Name certificateName: The certificate name of the key to use for
          signing.
        :param wireFormat: A WireFormat object used to encode the
          SignatureInfo and to encode the interest name for signing.
        :type wireFormat: A subclass of WireFormat
        """
        self._commandInterestGenerator.generate(
          interest, keyChain, certificateName, wireFormat)

    def registerPrefix(
      self, registeredPrefixId, prefixCopy, onInterest, onRegisterFailed, flags,
      wireFormat, commandKeyChain, commandCertificateName, face):
        """
        Register prefix with the connected NDN hub and call onInterest when a
        matching interest is received.

        :param int registeredPrefixId: The getNextEntryId() for the registered
          prefix ID which Face got so it could return it to the caller.
        :param Name prefixCopy: The Name for the prefix to register which is NOT
          copied for this internal Node method. The Face registerPrefix is
          responsible for making a copy for Node to use.
        :param onInterest: (optional) If not None, this creates an interest
          filter from prefixCopy so that when an Interest is received which matches
          the filter, this calls
          onInterest(prefix, interest, face, interestFilterId, filter).
          NOTE: You must not change the prefix or filter objects - if you need to
          change them then make a copy. If onInterest is None, it is ignored and
          you must call setInterestFilter.
        :type onInterest: function object
        :param onRegisterFailed: A function object to call if failed to retrieve
          the connected hub's ID or failed to register the prefix.
        :type onRegisterFailed: function object
        :param ForwardingFlags flags: The flags for finer control of which
          interests are forwardedto the application.
        :param wireFormat: A WireFormat object used to encode the message.
        :type wireFormat: a subclass of WireFormat
        :param KeyChain commandKeyChain: The KeyChain object for signing
          interests. If null, assume we are connected to a legacy NDNx forwarder.
        :param Name commandCertificateName: The certificate name for signing
          interests.
        :param Face face: The face which is passed to the onInterest callback.
          If onInterest is None, this is ignored.
        """
        # If we have an _ndndId, we know we already connected to NDNx.
        if self._ndndId != None or commandKeyChain == None:
            # Assume we are connected to a legacy NDNx server.
            if not WireFormat.ENABLE_NDNX:
                raise RuntimeError(
                  "registerPrefix with NDNx is deprecated. To enable while you upgrade your code to use NFD, set WireFormat.ENABLE_NDNX = True")

            if self._ndndId == None:
                # First fetch the ndndId of the connected hub.
                fetcher = Node._NdndIdFetcher(
                  self, registeredPrefixId, prefixCopy, onInterest, onRegisterFailed,
                  flags, wireFormat, face)
                # We send the interest using the given wire format so that the hub
                # receives (and sends) in the application's desired wire format.
                self.expressInterest(
                  self.getNextEntryId(), self._ndndIdFetcherInterest,
                  fetcher.onData, fetcher.onTimeout, wireFormat, face)
            else:
                self._registerPrefixHelper(
                  registeredPrefixId, prefixCopy, onInterest, onRegisterFailed,
                  flags, wireFormat, face)
        else:
            # The application set the KeyChain for signing NFD interests.
            self._nfdRegisterPrefix(
              registeredPrefixId, prefixCopy, onInterest,
              onRegisterFailed, flags, commandKeyChain, commandCertificateName,
              face)

    def removeRegisteredPrefix(self, registeredPrefixId):
        """
        Remove the registered prefix entry with the registeredPrefixId from the
        registered prefix table. This does not affect another registered prefix
        with a different registeredPrefixId, even if it has the same prefix
        name. If an interest filter was automatically created by registerPrefix,
        also remove it. If there is no entry with the registeredPrefixId, do
        nothing.

        :param int registeredPrefixId: The ID returned from registerPrefix.
        """
        count = 0
        # Go backwards through the list so we can erase entries.
        # Remove all entries even though registeredPrefixId should be unique.
        i = len(self._registeredPrefixTable) - 1
        while i >= 0:
            entry = self._registeredPrefixTable[i]
            if (entry.getRegisteredPrefixId() == registeredPrefixId):
                count += 1

                if entry.getRelatedInterestFilterId() > 0:
                    # Remove the related interest filter.
                    self.unsetInterestFilter(entry.getRelatedInterestFilterId())

                self._registeredPrefixTable.pop(i)
            i -= 1

        if count == 0:
            logging.getLogger(__name__).debug(
              "removeRegisteredPrefix: Didn't find registeredPrefixId " + registeredPrefixId)

    def setInterestFilter(self, interestFilterId, filterCopy, onInterest, face):
        """
        Add an entry to the local interest filter table to call the onInterest
        callback for a matching incoming Interest. This method only modifies the
        library's local callback table and does not register the prefix with the
        forwarder. It will always succeed. To register a prefix with the
        forwarder, use registerPrefix.

        :param int interestFilterId: The getNextEntryId() for the interest
          filter ID which Face got so it could return it to the caller.
        :param InterestFilter filterCopy: The InterestFilter with a prefix and
          optional regex filter used to match the name of an incoming Interest,
          which is NOT copied for this internal Node method. The Face
          setInterestFilter is responsible for making a copy for Node to use.
        :param onInterest: When an Interest is received which matches the filter,
          this calls onInterest(prefix, interest, face, interestFilterId, filter).
        :type onInterest: function object
        :param Face face: The face which is passed to the onInterest callback.
        """
        self._interestFilterTable.append(Node._InterestFilterEntry
          (interestFilterId, filterCopy, onInterest, face))

    def unsetInterestFilter(self, interestFilterId):
        """
        Remove the interest filter entry which has the interestFilterId from the
        interest filter table. This does not affect another interest filter with
        a different interestFilterId, even if it has the same prefix name. If
        there is no entry with the interestFilterId, do nothing.

        :param int interestFilterId: The ID returned from setInterestFilter.
        """
        count = 0
        # Go backwards through the list so we can erase entries.
        # Remove all entries even though interestFilterId should be unique.
        i = len(self._interestFilterTable) - 1
        while i >= 0:
            if (self._interestFilterTable[i].getInterestFilterId() ==
                  interestFilterId):
                count += 1
                self._interestFilterTable.pop(i)
            i -= 1

        if count == 0:
            logging.getLogger(__name__).debug(
              "unsetInterestFilter: Didn't find interestFilterId " + interestFilterId)

    def send(self, encoding):
        """
        Send the encoded packet out through the transport.

        :param encoding: The array of bytes for the encoded packet to send.
        :type encoding: An array type with int elements
        :throws: RuntimeError If the packet size exceeds getMaxNdnPacketSize().
        """
        if len(encoding) > self.getMaxNdnPacketSize():
            raise RuntimeError(
              "The encoded packet size exceeds the maximum limit getMaxNdnPacketSize()")

        self._transport.send(encoding)

    def processEvents(self):
        """
        Process any packets to receive and call callbacks such as onData,
        onInterest or onTimeout. This returns immediately if there is no data to
        receive. This blocks while calling the callbacks. You should repeatedly
        call this from an event loop, with calls to sleep as needed so that the
        loop doesn't use 100% of the CPU. Since processEvents modifies the pending
        interest table, your application should make sure that it calls
        processEvents in the same thread as expressInterest (which also modifies
        the pending interest table).

        :raises: This may raise an exception for reading data or in the callback
          for processing the data.  If you call this from an main event loop,
          you may want to catch and log/disregard all exceptions.
        """
        self._transport.processEvents()

        # Check for delayed calls. Since callLater does a sorted insert into
        # _delayedCallTable, the check for timeouts is quick and does not
        # require searching the entire table. If callLater is overridden to use
        # a different mechanism, then processEvents is not needed to check for
        # delayed calls.
        now = Common.getNowMilliseconds()
        # _delayedCallTable is sorted on _callTime, so we only need to process
        # the timed-out entries at the front, then quit.
        while (len(self._delayedCallTable) > 0 and
               self._delayedCallTable[0].getCallTime() <= now):
            delayedCall = self._delayedCallTable[0]
            del self._delayedCallTable[0]
            delayedCall.callCallback()

    def getTransport(self):
        """
        Get the transport object given to the constructor.

        :return: The transport object.
        :rtype: Transport
        """
        return self._transport

    def getConnectionInfo(self):
        """
        Get the connectionInfo object given to the constructor.

        :return: The connectionInfo object.
        :rtype: Transport.ConnectionInfo
        """
        return self._connectionInfo

    def onReceivedElement(self, element):
        """
        This is called by the transport's ElementReader to process an
        entire received Data or Interest element.

        :param element: The bytes of the incoming element.
        :type element: An array type with int elements
        """
        # The type codes for TLV Interest and Data packets are chosen to not
        #   conflict with the first byte of a binary XML packet, so we canjust
        #   look at the first byte.
        if not (element[0] == Tlv.Interest or element[0] == Tlv.Data):
            # Ignore non-TLV elements.
            # Assume it is Binary XML.
            if not WireFormat.ENABLE_NDNX:
                raise RuntimeError(
                  "BinaryXmlWireFormat (NDNx) is deprecated. To enable while you upgrade your network to use NDN-TLV, set WireFormat.ENABLE_NDNX = True")

            return

        # First, decode as Interest or Data.
        interest = None
        data = None
        decoder = TlvDecoder(element)
        if decoder.peekType(Tlv.Interest, len(element)):
            interest = Interest()
            interest.wireDecode(element, TlvWireFormat.get())
        elif decoder.peekType(Tlv.Data, len(element)):
            data = Data()
            data.wireDecode(element, TlvWireFormat.get())

        # Now process as Interest or Data.
        if interest != None:
            # Call all interest filter callbacks which match.
            for i in range(len(self._interestFilterTable)):
                entry = self._interestFilterTable[i]
                if entry.getFilter().doesMatch(interest.getName()):
                    includeFilter = True
                    # Use getcallargs to test if onInterest accepts 5 args.
                    try:
                        inspect.getcallargs(entry.getOnInterest(),
                          None, None, None, None, None)
                    except TypeError:
                        # Assume onInterest is old-style with 4 arguments.
                        includeFilter = False

                    if includeFilter:
                        entry.getOnInterest()(
                          entry.getFilter().getPrefix(), interest,
                          entry.getFace(), entry.getInterestFilterId(),
                          entry.getFilter())
                    else:
                        # Old-style onInterest without the filter argument. We
                        # still pass a Face instead of Transport since Face also
                        # has a send method.
                        entry.getOnInterest()(
                          entry.getFilter().getPrefix(), interest,
                          entry.getFace(), entry.getInterestFilterId())
        elif data != None:
            pendingInterests = self._extractEntriesForExpressedInterest(
              data.getName())
            for pendingInterest in pendingInterests:
                pendingInterest.getOnData()(pendingInterest.getInterest(), data)

    def isLocal(self):
        """
        Check if the face is local based on the current connection through the
        Transport; some Transport may cause network I/O (e.g. an IP host name
        lookup).

        :return: True if the face is local, False if not.
        :rtype bool:
        """
        return self._transport.isLocal(self._connectionInfo)

    def shutdown(self):
        """
        Call getTransport().close().
        """
        self._transport.close()

    @staticmethod
    def getMaxNdnPacketSize():
        """
        Get the practical limit of the size of a network-layer packet. If a packet
        is larger than this, the library or application MAY drop it.

        :return: The maximum NDN packet size.
        :rtype: int
        """
        return Common.MAX_NDN_PACKET_SIZE

    def _expressInterestHelper(
      self, pendingInterestId, interestCopy, onData, onTimeout, wireFormat, face):
        """
        Do the work of expressInterest once we know we are connected. Add the
        entry to the PIT, encode and send the interest.

        :param int pendingInterestId: The getNextEntryId() for the pending
          interest ID which Face got so it could return it to the caller.
        :param Interest interestCopy: The Interest to send, which has already
          been copied.
        :param onData: A function object to call when a matching data packet is
          received.
        :type onData: function object
        :param onTimeout: A function object to call if the interest times out.
          If onTimeout is None, this does not use it.
        :type onTimeout: function object
        :param wireFormat: A WireFormat object used to encode the message.
        :type wireFormat: a subclass of WireFormat
        :param Face face: The face which has the callLater method, used for
          interest timeouts. The callLater method may be overridden in a
          subclass of Face.
        :throws: RuntimeError If the encoded interest size exceeds
          getMaxNdnPacketSize().
        """
        pendingInterest = Node._PendingInterest(
          pendingInterestId, interestCopy, onData, onTimeout)
        self._pendingInterestTable.append(pendingInterest)
        if (interestCopy.getInterestLifetimeMilliseconds() != None and
            interestCopy.getInterestLifetimeMilliseconds() >= 0.0):
            # Set up the timeout.
            face.callLater(interestCopy.getInterestLifetimeMilliseconds(),
                           lambda: self._processInterestTimeout(pendingInterest))

        # Special case: For _timeoutPrefix we don't actually send the interest.
        if not self._timeoutPrefix.match(interestCopy.getName()):
            encoding = interestCopy.wireEncode(wireFormat)
            if encoding.size() > self.getMaxNdnPacketSize():
                raise RuntimeError(
                  "The encoded interest size exceeds the maximum limit getMaxNdnPacketSize()")

            self._transport.send(encoding.toBuffer())

    def _extractEntriesForExpressedInterest(self, name):
        """
        Find all entries from the _pendingInterestTable where the name conforms
        to the entry's interest selectors, remove the entries from the table
        and return them.

        :param Name name: The name to find the interest for (from the incoming
          data packet).
        :return: The matching entries from the _pendingInterestTable, or []
          if none are found.
        :rtype: array of _PendingInterest
        """
        result = []

        # Go backwards through the list so we can erase entries.
        i = len(self._pendingInterestTable) - 1
        while i >= 0:
            pendingInterest = self._pendingInterestTable[i]

            if pendingInterest.getInterest().matchesName(name):
                result.append(pendingInterest)
                # We let the callback from callLater call _processInterestTimeout,
                # but for efficiency, mark this as removed so that it returns
                # right away.
                self._pendingInterestTable.pop(i)
                pendingInterest.setIsRemoved()
            i -= 1

        return result

    def _registerPrefixHelper(
      self, registeredPrefixId, prefix, onInterest, onRegisterFailed, flags,
      wireFormat, face):
        """
        Do the work of registerPrefix to register with NDNx once we have an
        _ndndId.

        :param int registeredPrefixId: The getNextEntryId() which registerPrefix
          got so it could return it to the caller. If this is 0, then don't add
          to _registeredPrefixTable (assuming it has already been done).
        """
        if not WireFormat.ENABLE_NDNX:
            # We can get here if the command signing info is set, but running NDNx.
            raise RuntimeError(
              "registerPrefix with NDNx is deprecated. To enable while you upgrade your code to use NFD, set WireFormat.ENABLE_NDNX = True")

        # Create a ForwardingEntry.
        # Note: ndnd ignores any freshness that is larger than 3600 seconds and
        #   sets 300 seconds instead. To register "forever", (=2000000000 sec),
        #   the freshness period must be omitted.
        forwardingEntry = ForwardingEntry()
        forwardingEntry.setAction("selfreg")
        forwardingEntry.setPrefix(prefix)
        forwardingEntry.setForwardingFlags(flags)
        content = forwardingEntry.wireEncode(wireFormat)

        # Set the ForwardingEntry as the content of a Data packet and sign.
        data = Data()
        data.setContent(content)
        # Set the name to a random value so that each request is unique.
        nonce = bytearray(4)
        for i in range(len(nonce)):
            nonce[i] = _systemRandom.randint(0, 0xff)
        data.getName().append(nonce)
        # The ndnd ignores the signature, so set to blank values.
        data.getSignature().getKeyLocator().setType(
          KeyLocatorType.KEY_LOCATOR_DIGEST)
        data.getSignature().getKeyLocator().setKeyData(
          Blob(bytearray(32), False))
        data.getSignature().setSignature(Blob(bytearray(128), False))
        encodedData = data.wireEncode(wireFormat)

        # Create an interest where the name has the encoded Data packet.
        interestName = Name().append("ndnx").append(self._ndndId).append(
          "selfreg").append(encodedData)

        interest = Interest(interestName)
        interest.setInterestLifetimeMilliseconds(4000.0)
        interest.setScope(1)

        if registeredPrefixId != 0:
            interestFilterId = 0
            if onInterest != None:
                # registerPrefix was called with the "combined" form that includes
                # the callback, so add an InterestFilterEntry.
                interestFilterId = self.getNextEntryId()
                self.setInterestFilter(
                  interestFilterId, InterestFilter(prefix), onInterest, face)

            self._registeredPrefixTable.append(Node._RegisteredPrefix(
              registeredPrefixId, prefix, interestFilterId))

        # Send the registration interest.
        response = Node._RegisterResponse(
          self, prefix, onInterest, onRegisterFailed, flags, wireFormat, False,
          face)
        self.expressInterest(
          self.getNextEntryId(), interest, response.onData, response.onTimeout,
          wireFormat, face)

    def _nfdRegisterPrefix(
      self, registeredPrefixId, prefix, onInterest, onRegisterFailed, flags,
      commandKeyChain, commandCertificateName, face):
        """
        Do the work of registerPrefix to register with NFD.

        :param int registeredPrefixId: The getNextEntryId() which registerPrefix
          got so it could return it to the caller. If this is 0, then don't add
          to _registeredPrefixTable (assuming it has already been done).
        """
        if commandKeyChain == None:
            raise RuntimeError(
              "registerPrefix: The command KeyChain has not been set. You must call setCommandSigningInfo.")
        if commandCertificateName.size() == 0:
            raise RuntimeError(
              "registerPrefix: The command certificate name has not been set. You must call setCommandSigningInfo.")

        controlParameters = ControlParameters()
        controlParameters.setName(prefix)
        controlParameters.setForwardingFlags(flags)

        commandInterest = Interest()
        if self.isLocal():
            commandInterest.setName(Name("/localhost/nfd/rib/register"))
            # The interest is answered by the local host, so set a short timeout.
            commandInterest.setInterestLifetimeMilliseconds(2000.0)
        else:
            commandInterest.setName(Name("/localhop/nfd/rib/register"))
            # The host is remote, so set a longer timeout.
            commandInterest.setInterestLifetimeMilliseconds(4000.0)
        # NFD only accepts TlvWireFormat packets.
        commandInterest.getName().append(controlParameters.wireEncode(TlvWireFormat.get()))
        self.makeCommandInterest(
          commandInterest, commandKeyChain, commandCertificateName,
          TlvWireFormat.get())

        if registeredPrefixId != 0:
            interestFilterId = 0
            if onInterest != None:
                # registerPrefix was called with the "combined" form that includes
                # the callback, so add an InterestFilterEntry.
                interestFilterId = self.getNextEntryId()
                self.setInterestFilter(
                  interestFilterId, InterestFilter(prefix), onInterest, face)

            self._registeredPrefixTable.append(Node._RegisteredPrefix(
              registeredPrefixId, prefix, interestFilterId))

        # Send the registration interest.
        response = Node._RegisterResponse(
          self, prefix, onInterest, onRegisterFailed, flags,
          TlvWireFormat.get(), True, face)
        self.expressInterest(
          self.getNextEntryId(), commandInterest, response.onData,
          response.onTimeout, TlvWireFormat.get(), face)

    def callLater(self, delayMilliseconds, callback):
        """
        Call callback() after the given delay. This adds to
        self._delayedCallTable which is used by processEvents().

        :param float delayMilliseconds: The delay in milliseconds.
        :param callback: This calls callback() after the delay.
        :type callback: function object
        """
        delayedCall = Node._DelayedCall(delayMilliseconds, callback)
        # Insert into _delayedCallTable, sorted on delayedCall.getCallTime().
        # Search from the back since we expect it to go there.
        i = len(self._delayedCallTable) - 1
        while i >= 0:
            if (self._delayedCallTable[i].getCallTime() <= delayedCall.getCallTime()):
                break
            i -= 1

        # Element i is the greatest less than or equal to
        # delayedCall.getCallTime(), so insert after it.
        self._delayedCallTable.insert(i + 1, delayedCall)

    def _processInterestTimeout(self, pendingInterest):
        """
        This is used in callLater for when the pending interest expires. If
        the pendingInterest is still in the _pendingInterestTable, remove it and
        call its onTimeout callback.
        """
        if pendingInterest.getIsRemoved():
            # _extractEntriesForExpressedInterest or removePendingInterest has
            # removed pendingInterest from _pendingInterestTable, so we don't
            # need to look for it. Do nothing.
            return

        try:
            index = self._pendingInterestTable.index(pendingInterest)
        except ValueError:
            # The pending interest has been removed. Do nothing.
            return

        del self._pendingInterestTable[index]
        pendingInterest.callTimeout()

    def getNextEntryId(self):
        """
        Get the next unique entry ID for the pending interest table, interest
        filter table, etc. This uses a threading.Lock() to be thread safe. Most
        entry IDs are for the pending interest table (there usually are not many
        interest filter table entries) so we use a common pool to only have to
        do the thread safe lock in one method which is called by Face.

        :return: The next entry ID.
        :rtype: int
        """
        with self._lastEntryIdLock:
            self._lastEntryId += 1
            return self._lastEntryId

    class _ConnectStatus(object):
        UNCONNECTED = 1
        CONNECT_REQUESTED = 2
        CONNECT_COMPLETE = 3

    class _DelayedCall(object):
        """
        _DelayedCall is a private class for the members of the _delayedCallTable.
        Create a new _DelayedCall and set the call time based on the current
        time and the delayMilliseconds.

        :param float delayMilliseconds: The delay in milliseconds.
        :param callback: This calls callback() after the delay.
        :type callback: function object
        """
        def __init__(self, delayMilliseconds, callback):
            self._callback = callback
            self._callTime = Common.getNowMilliseconds() + delayMilliseconds

        def getCallTime(self):
            """
            Get the time at which the callback should be called.

            :return: The call time in milliseconds, similar to
              Common.getNowMilliseconds().
            :rtype: float
            """
            return self._callTime

        def callCallback(self):
            """
            Call the callback given to the constructor. This does not catch
            exceptions.
            """
            self._callback()

    class _PendingInterest(object):
        """
        _PendingInterest is a private class for the members of the
        _pendingInterestTable.

        :param int pendingInterestId: A unique ID for this entry, which you
          should get with getNextEntryId().
        :param Interest interest: The interest.
        :param onData: A function object to call when a matching data packet is
          received.
        :type onData: function object
        :param onTimeout: A function object to call if the interest times out.
          If onTimeout is None, this does not use it.
        :type onTimeout: function object
        """
        def __init__(self, pendingInterestId, interest, onData, onTimeout):
            self._pendingInterestId = pendingInterestId
            self._interest = interest
            self._onData = onData
            self._onTimeout = onTimeout
            self._isRemoved = False

        def getPendingInterestId(self):
            """
            Get the pendingInterestId given to the constructor.

            :return: The pending interest ID.
            :rtype: int
            """
            return self._pendingInterestId

        def getInterest(self):
            """
            Get the interest given to the constructor.

            :return: The interest.
            :rtype: int
            """
            return self._interest

        def getOnData(self):
            """
            Get the onData function object given to the constructor.

            :return: The onData function object.
            :rtype: function object
            """
            return self._onData

        def callTimeout(self):
            """
            Call _onTimeout (if defined).  This ignores exceptions from
            _onTimeout.
            """
            if self._onTimeout:
                # Ignore all exceptions.
                try:
                    self._onTimeout(self._interest)
                except:
                    pass

        def setIsRemoved(self):
            """
            Set the isRemoved flag which is returned by getIsRemoved().
            """
            self._isRemoved = True

        def getIsRemoved(self):
            """
            Check if setIsRemoved() was called.

            :return: True if setIsRemoved() was called.
            :rtype: bool
            """
            return self._isRemoved

    class _RegisteredPrefix(object):
        """
        A _RegisteredPrefix holds a registeredPrefixId and information necessary
        to remove the registration later. It optionally holds a related
        interestFilterId if the InterestFilter was set in the same
        registerPrefix operation.

        :param int registeredPrefixId: A unique ID for this entry, which you
          should get with getNextEntryId().
        :param Name prefix: The name prefix.
        :param int relatedInterestFilterId: (optional) The related
          interestFilterId for the filter set in the same registerPrefix
          operation. If omitted, set to 0.
        """
        def __init__(self, registeredPrefixId, prefix, relatedInterestFilterId):
            self._registeredPrefixId = registeredPrefixId
            self._prefix = prefix
            self._relatedInterestFilterId = relatedInterestFilterId

        def getRegisteredPrefixId(self):
            """
            Get the registeredPrefixId given to the constructor.

            :return: The registered prefix ID.
            :rtype: int
            """
            return self._registeredPrefixId

        def getPrefix(self):
            """
            Get the name prefix to the constructor.

            :return: The name prefix.
            :rtype: Name
            """
            return self._prefix

        def getRelatedInterestFilterId(self):
            """
            Get the related interestFilterId given to the constructor.

            :return: The related interestFilterId.
            :rtype: int
            """
            return self._relatedInterestFilterId

    class _InterestFilterEntry(object):
        """
        An _InterestFilterEntry holds an interestFilterId, an InterestFilter
        and the OnInterestCallback with its related Face.
        Create a new InterestFilterEntry with the given values.

        :param int interestFilterId: The ID from getNextEntryId().
        :param InterestFilter filter: The InterestFilter for this entry.
        :param onInterest: The callback to call.
        :type onInterest: function object
        :param Face face: The face on which was called registerPrefix or
          setInterestFilter which is passed to the onInterest callback.
        """
        def __init__(self, interestFilterId, filter, onInterest, face):
            self._interestFilterId = interestFilterId
            self._filter = filter
            self._onInterest = onInterest
            self._face = face

        def getInterestFilterId(self):
            """
            Get the interestFilterId given to the constructor.

            :return: The interestFilterId.
            :rtype: int
            """
            return self._interestFilterId

        def getFilter(self):
            """
            Get the InterestFilter given to the constructor.

            :return: The InterestFilter.
            :rtype: InterestFilter
            """
            return self._filter

        def getOnInterest(self):
            """
            Get the OnInterestCallback given to the constructor.

            :return: The OnInterestCallback.
            :rtype: function object
            """
            return self._onInterest

        def getFace(self):
            """
            Get the Face given to the constructor.

            :return: The Face.
            :rtype: Face
            """
            return self._face

    class _NdndIdFetcher(object):
        """
        An _NdndIdFetcher receives the Data packet with the publisher public key
        digest for the connected NDN hub.
        """
        def __init__(self, node, registeredPrefixId, prefix, onInterest,
                     onRegisterFailed, flags, wireFormat, face):
            self._node = node
            self._registeredPrefixId = registeredPrefixId
            self._prefix = prefix
            self._onInterest = onInterest
            self._onRegisterFailed = onRegisterFailed
            self._flags = flags
            self._wireFormat = wireFormat
            self._face = face

        def onData(self, interest, ndndIdData):
            """
            We received the ndnd ID.
            """
            # Assume that the content is a DER encoded public key of the ndnd.
            #   Do a quick check that the first byte is for DER encoding.
            if (ndndIdData.getContent().size() < 1 or
                  ndndIdData.getContent().buf()[0] != 0x30):
                logging.getLogger(__name__).info(
                  "Register prefix failed: The content returned when fetching the NDNx ID does not appear to be a public key")
                self._onRegisterFailed(self._prefix)
                return

            # Get the digest of the public key.
            digest = bytearray(
              hashlib.sha256(ndndIdData.getContent().toBuffer()).digest())

            # Set the _ndndId and continue.
            # TODO: If there are multiple connected hubs, the NDN ID is really
            #   stored per connected hub.
            self._node._ndndId = Blob(digest, False)
            self._node._registerPrefixHelper(
              self._registeredPrefixId, self._prefix, self._onInterest,
              self._onRegisterFailed, self._flags, self._wireFormat, self._face)

        def onTimeout(self, interest):
            """
            We timed out fetching the ndnd ID.
            """
            logging.getLogger(__name__).info(
              "Register prefix failed: Timeout fetching the NDNx ID")
            self._onRegisterFailed(self._prefix)

    class _RegisterResponse(object):
        """
        A _RegisterResponse receives the response Data packet from the register
        prefix interest sent to the connected NDN hub. If this gets a bad
        response or a timeout, call onRegisterFailed.
        """
        def __init__(self, node, prefix, onInterest, onRegisterFailed, flags,
                     wireFormat, isNfdCommand, face):
            self._node = node
            self._prefix = prefix
            self._onInterest = onInterest
            self._onRegisterFailed = onRegisterFailed
            self._flags = flags
            self._wireFormat = wireFormat
            self._isNfdCommand = isNfdCommand
            self._face = face

        def onData(self, interest, responseData):
            """
            We received the response. Do a quick check of expected name
            components.
            """
            if self._isNfdCommand:
                # Decode responseData->getContent() and check for a success code.
                # TODO: Move this into the TLV code.
                statusCode = None
                try:
                    decoder = TlvDecoder(responseData.getContent().buf())
                    decoder.readNestedTlvsStart(Tlv.NfdCommand_ControlResponse)
                    statusCode = decoder.readNonNegativeIntegerTlv(Tlv.NfdCommand_StatusCode)
                except ValueError as ex:
                    logging.getLogger(__name__).info(
                      "Register prefix failed: Error decoding the NFD response: %s",
                      str(ex))
                    self._onRegisterFailed(self._prefix)
                    return

                # Status code 200 is "OK".
                if statusCode != 200:
                  logging.getLogger(__name__).info(
                    "Register prefix failed: Expected NFD status code 200, got: %d",
                    statusCode)
                  self._onRegisterFailed(self._prefix)
                  return

                logging.getLogger(__name__).info(
                  "Register prefix succeeded with the NFD forwarder for prefix %s",
                  self._prefix.toUri())
            else:
                expectedName = Name("/ndnx/.../selfreg")
                if (responseData.getName().size() < 4 or
                      responseData.getName()[0] != expectedName[0] or
                      responseData.getName()[2] != expectedName[2]):
                    logging.getLogger(__name__).info(
                      "Register prefix failed: Unexpected name in NDNx response: %s",
                      responseData.getName().toUri())
                    self._onRegisterFailed(self._prefix)
                    return

                logging.getLogger(__name__).info(
                  "Register prefix succeeded with the NDNx forwarder for prefix %s",
                  self._prefix.toUri())

        def onTimeout(self, interest):
            """
            We timed out waiting for the response.
            """
            if self._isNfdCommand:
                logging.getLogger(__name__).info(
                  "Timeout for NFD register prefix command. Attempting an NDNx command...")
                # The application set the commandKeyChain, but we may be
                #   connected to NDNx.
                if self._node._ndndId == None:
                    # First fetch the ndndId of the connected hub.
                    # Pass 0 for registeredPrefixId since the entry was already added to
                    #   _registeredPrefixTable on the first try.
                    fetcher = Node._NdndIdFetcher(
                      self._node, 0, self._prefix, self._onInterest,
                      self._onRegisterFailed, self._flags, self._wireFormat,
                      self._face)
                    # We send the interest using the given wire format so that the hub
                    # receives (and sends) in the application's desired wire format.
                    self._node.expressInterest(
                      self._node.getNextEntryId(), self._node._ndndIdFetcherInterest,
                      fetcher.onData, fetcher.onTimeout, self._wireFormat,
                      self._face)
                else:
                    # Pass 0 for registeredPrefixId since the entry was already
                    #   added to _registeredPrefixTable on the first try.
                    self._node._registerPrefixHelper(
                      0, self._prefix, self._onInterest, self._onRegisterFailed,
                      self._flags, self._wireFormat)
            else:
                # An NDNx command was sent because there is no commandKeyChain,
                #   so we can't try an NFD command. Or it was sent from this
                #   callback after trying an NFD command. Fail.
                logging.getLogger(__name__).info(
                  "Register prefix failed: Timeout waiting for the response from the register prefix interest")
                self._onRegisterFailed(self._prefix)
예제 #35
0
파일: node.py 프로젝트: WeiqiJust/NDN-total
class Node(object):
    """
    Create a new Node for communication with an NDN hub with the given Transport
    object and connectionInfo.

    :param Transport transport: An object of a subclass of Transport used for
      communication.
    :param Transport.ConnectionInfo connectionInfo: An object of a subclass of
      Transport.ConnectionInfo to be used to connect to the transport.
    """
    def __init__(self, transport, connectionInfo):
        self._transport = transport
        self._connectionInfo = connectionInfo
        # An array of PendintInterest
        self._pendingInterestTable = []
        # An array of RegisteredPrefix
        self._registeredPrefixTable = []
        self._ndndIdFetcherInterest = Interest(
          Name("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY"))
        self._ndndIdFetcherInterest.setInterestLifetimeMilliseconds(4000.0)
        self._ndndId = None
        self._commandInterestGenerator = CommandInterestGenerator()
        self._timeoutPrefix = Name("/local/timeout")

    def expressInterest(self, interest, onData, onTimeout, wireFormat):
        """
        Send the Interest through the transport, read the entire response and
        call onData(interest, data).

        :param Interest interest: The Interest which is NOT copied for this
          internal Node method.  The Face expressInterest is reponsible for
          making a copy for Node to use.
        :param onData: A function object to call when a matching data packet is
          received.
        :type onData: function object
        :param onTimeout: A function object to call if the interest times out.
          If onTimeout is None, this does not use it.
        :type onTimeout: function object
        :param wireFormat: A WireFormat object used to encode the message.
        :type wireFormat: a subclass of WireFormat
        """
        # TODO: Properly check if we are already connected to the expected host.
        if not self._transport.getIsConnected():
            self._transport.connect(self._connectionInfo, self)

        pendingInterestId = Node._PendingInterest.getNextPendingInterestId()
        self._pendingInterestTable.append(
          Node._PendingInterest(pendingInterestId, interest, onData,
                          onTimeout))

        # Special case: For _timeoutPrefix we don't actually send the interest.
        if not self._timeoutPrefix.match(interest.getName()):
          self._transport.send(interest.wireEncode(wireFormat).toBuffer())

        return pendingInterestId

    def removePendingInterest(self, pendingInterestId):
        """
        Remove the pending interest entry with the pendingInterestId from the
        pending interest table. This does not affect another pending interest
        with a different pendingInterestId, even if it has the same interest
        name. If there is no entry with the pendingInterestId, do nothing.

        :param int pendingInterestId: The ID returned from expressInterest.
        """
        # Go backwards through the list so we can erase entries.
        # Remove all entries even though pendingInterestId should be unique.
        i = len(self._pendingInterestTable) - 1
        while i >= 0:
            if (self._pendingInterestTable[i].getPendingInterestId() ==
                  pendingInterestId):
                self._pendingInterestTable.pop(i)
            i -= 1

    def makeCommandInterest(self, interest, keyChain, certificateName, wireFormat):
        """
        Append a timestamp component and a random value component to interest's
        name. Then use the keyChain and certificateName to sign the interest.
        If the interest lifetime is not set, this sets it.

        :param Interest interest: The interest whose name is append with
          components.
        :param KeyChain keyChain: The KeyChain for calling sign.
        :param Name certificateName: The certificate name of the key to use for
          signing.
        :param wireFormat: A WireFormat object used to encode the
          SignatureInfo and to encode the interest name for signing.
        :type wireFormat: A subclass of WireFormat
        """
        self._commandInterestGenerator.generate(
          interest, keyChain, certificateName, wireFormat)

    def registerPrefix(
      self, prefix, onInterest, onRegisterFailed, flags, wireFormat,
      commandKeyChain, commandCertificateName):
        """
        Register prefix with the connected NDN hub and call onInterest when a
        matching interest is received.

        :param Name prefix: The Name for the prefix to register which is NOT
          copied for this internal Node method. The Face registerPrefix is
          reponsible for making a copy for Node to use..
        :param onInterest: A function object to call when a matching interest is
          received.
        :type onInterest: function object
        :param onRegisterFailed: A function object to call if failed to retrieve
          the connected hub's ID or failed to register the prefix.
        :type onRegisterFailed: function object
        :param ForwardingFlags flags: The flags for finer control of which
          interests are forwardedto the application.
        :param wireFormat: A WireFormat object used to encode the message.
        :type wireFormat: a subclass of WireFormat
        :param KeyChain commandKeyChain: The KeyChain object for signing
          interests. If null, assume we are connected to a legacy NDNx forwarder.
        :param Name commandCertificateName: The certificate name for signing
          interests.
        """
        # Get the registeredPrefixId now so we can return it to the caller.
        registeredPrefixId = Node._RegisteredPrefix.getNextRegisteredPrefixId()

        # If we have an _ndndId, we know we already connected to NDNx.
        if self._ndndId != None or commandKeyChain == None:
            # Assume we are connected to a legacy NDNx server.

            if self._ndndId == None:
                # First fetch the ndndId of the connected hub.
                fetcher = Node._NdndIdFetcher(
                  self, registeredPrefixId, prefix, onInterest, onRegisterFailed,
                  flags, wireFormat)
                # We send the interest using the given wire format so that the hub
                # receives (and sends) in the application's desired wire format.
                self.expressInterest(
                  self._ndndIdFetcherInterest, fetcher.onData, fetcher.onTimeout,
                  wireFormat)
            else:
                self._registerPrefixHelper(
                  registeredPrefixId, Name(prefix), onInterest, onRegisterFailed,
                  flags, wireFormat)
        else:
            # The application set the KeyChain for signing NFD interests.
            self._nfdRegisterPrefix(
              registeredPrefixId, Name(prefix), onInterest,
              onRegisterFailed, flags, commandKeyChain, commandCertificateName)

        return registeredPrefixId

    def removeRegisteredPrefix(self, registeredPrefixId):
        """
        Remove the registered prefix entry with the registeredPrefixId from the
        registered prefix table. This does not affect another registered prefix
        with a different registeredPrefixId, even if it has the same prefix
        name. If there is no entry with the registeredPrefixId, do nothing.

        :param int registeredPrefixId: The ID returned from registerPrefix.
        """
        # Go backwards through the list so we can erase entries.
        # Remove all entries even though registeredPrefixId should be unique.
        i = len(self._registeredPrefixTable) - 1
        while i >= 0:
            if (self._registeredPrefixTable[i].getRegisteredPrefixId() ==
                  registeredPrefixId):
                self._registeredPrefixTable.pop(i)
            i -= 1

    def processEvents(self):
        """
        Process any packets to receive and call callbacks such as onData,
        onInterest or onTimeout. This returns immediately if there is no data to
        receive. This blocks while calling the callbacks. You should repeatedly
        call this from an event loop, with calls to sleep as needed so that the
        loop doesn't use 100% of the CPU. Since processEvents modifies the pending
        interest table, your application should make sure that it calls
        processEvents in the same thread as expressInterest (which also modifies
        the pending interest table).

        :raises: This may raise an exception for reading data or in the callback
          for processing the data.  If you call this from an main event loop,
          you may want to catch and log/disregard all exceptions.
        """
        self._transport.processEvents()

        # Check for PIT entry timeouts. Go backwards through the list so we can
        #   erase entries.
        nowMilliseconds = Common.getNowMilliseconds()
        i = len(self._pendingInterestTable) - 1
        while i >= 0:
            if self._pendingInterestTable[i].isTimedOut(nowMilliseconds):
                # Save the PendingInterest and remove it from the PIT.  Then
                #   call the callback.
                pendingInterest = self._pendingInterestTable[i]
                self._pendingInterestTable.pop(i)
                pendingInterest.callTimeout()

                # Refresh now since the timeout callback might have delayed.
                nowMilliseconds = Common.getNowMilliseconds()

            i -= 1

    def getTransport(self):
        """
        Get the transport object given to the constructor.

        :return: The transport object.
        :rtype: Transport
        """
        return self._transport

    def getConnectionInfo(self):
        """
        Get the connectionInfo object given to the constructor.

        :return: The connectionInfo object.
        :rtype: Transport.ConnectionInfo
        """
        return self._connectionInfo

    def onReceivedElement(self, element):
        """
        This is called by the transport's ElementReader to process an
        entire received Data or Interest element.

        :param element: The bytes of the incoming element.
        :type element: An array type with int elements
        """
        # The type codes for TLV Interest and Data packets are chosen to not
        #   conflict with the first byte of a binary XML packet, so we canjust
        #   look at the first byte.
        if not (element[0] == Tlv.Interest or element[0] == Tlv.Data):
            # Ignore non-TLV elements.
            return

        # First, decode as Interest or Data.
        interest = None
        data = None
        decoder = TlvDecoder(element)
        if decoder.peekType(Tlv.Interest, len(element)):
            interest = Interest()
            interest.wireDecode(element, TlvWireFormat.get())
        elif decoder.peekType(Tlv.Data, len(element)):
            data = Data()
            data.wireDecode(element, TlvWireFormat.get())

        # Now process as Interest or Data.
        if interest != None:
            entry = self._getEntryForRegisteredPrefix(interest.getName())
            if entry != None:
                entry.getOnInterest()(
                  entry.getPrefix(), interest, self._transport,
                  entry.getRegisteredPrefixId())
        elif data != None:
            pendingInterests = self._extractEntriesForExpressedInterest(
              data.getName())
            for pendingInterest in pendingInterests:
                pendingInterest.getOnData()(pendingInterest.getInterest(), data)

    def shutdown(self):
        """
        Call getTransport().close().
        """
        self._transport.close()

    def _extractEntriesForExpressedInterest(self, name):
        """
        Find all entries from the _pendingInterestTable where the name conforms
        to the entry's interest selectors, remove the entries from the table
        and return them.

        :param Name name: The name to find the interest for (from the incoming
          data packet).
        :return: The matching entries from the _pendingInterestTable, or []
          if none are found.
        :rtype: array of _PendingInterest
        """
        result = []

        # Go backwards through the list so we can erase entries.
        i = len(self._pendingInterestTable) - 1
        while i >= 0:
            if self._pendingInterestTable[i].getInterest().matchesName(name):
                result.append(self._pendingInterestTable[i])
                self._pendingInterestTable.pop(i)
            i -= 1

        return result

    def _getEntryForRegisteredPrefix(self, name):
        """
        Find the first entry from the _registeredPrefixTable where the entry
        prefix is the longest that matches name.

        :param Name name: The name to find the RegisteredPrefix for (from the
          incoming interest packet).
        :return: The registered prefix entry, or None of not found.
        :rtype: _RegisteredPrefix
        """
        iResult = -1

        for i in range(len(self._registeredPrefixTable)):
            if self._registeredPrefixTable[i].getPrefix().match(name):
                if (iResult < 0 or
                      self._registeredPrefixTable[i].getPrefix().size() >
                      self._registeredPrefixTable[iResult].getPrefix().size()):
                    # Update to the longer match.
                    iResult = i

        if iResult >= 0:
            return self._registeredPrefixTable[iResult]
        else:
            return None

    def _registerPrefixHelper(
      self, registeredPrefixId, prefix, onInterest, onRegisterFailed, flags,
      wireFormat):
        """
        Do the work of registerPrefix to register with NDNx once we have an
        _ndndId.

        :param int registeredPrefixId: The
          _RegisteredPrefix.getNextRegisteredPrefixId() which registerPrefix got
          so it could return it to the caller. If this is 0, then don't add to
          _registeredPrefixTable (assuming it has already been done).
        """
        # Create a ForwardingEntry.
        # Note: ndnd ignores any freshness that is larger than 3600 seconds and
        #   sets 300 seconds instead. To register "forever", (=2000000000 sec),
        #   the freshness period must be omitted.
        forwardingEntry = ForwardingEntry()
        forwardingEntry.setAction("selfreg")
        forwardingEntry.setPrefix(prefix)
        forwardingEntry.setForwardingFlags(flags)
        content = forwardingEntry.wireEncode(wireFormat)

        # Set the ForwardingEntry as the content of a Data packet and sign.
        data = Data()
        data.setContent(content)
        # Set the name to a random value so that each request is unique.
        nonce = bytearray(4)
        for i in range(len(nonce)):
            nonce[i] = _systemRandom.randint(0, 0xff)
        data.getName().append(nonce)
        # The ndnd ignores the signature, so set to blank values.
        data.getSignature().getKeyLocator().setType(
          KeyLocatorType.KEY_LOCATOR_DIGEST)
        data.getSignature().getKeyLocator().setKeyData(
          Blob(bytearray(32), False))
        data.getSignature().setSignature(Blob(bytearray(128), False))
        encodedData = data.wireEncode(wireFormat)

        # Create an interest where the name has the encoded Data packet.
        interestName = Name().append("ndnx").append(self._ndndId).append(
          "selfreg").append(encodedData)

        interest = Interest(interestName)
        interest.setInterestLifetimeMilliseconds(4000.0)
        interest.setScope(1)
        encodedInterest = interest.wireEncode(wireFormat)

        if registeredPrefixId != 0:
            # Save the onInterest callback and send the registration interest.
            self._registeredPrefixTable.append(Node._RegisteredPrefix(
              registeredPrefixId, prefix, onInterest))

        response = Node._RegisterResponse(
          self, prefix, onInterest, onRegisterFailed, flags, wireFormat, False)
        self.expressInterest(
          interest, response.onData, response.onTimeout, wireFormat)

    def _nfdRegisterPrefix(
      self, registeredPrefixId, prefix, onInterest, onRegisterFailed, flags,
      commandKeyChain, commandCertificateName):
        """
        Do the work of registerPrefix to register with NFD.

        :param int registeredPrefixId: The
          _RegisteredPrefix.getNextRegisteredPrefixId() which registerPrefix got
          so it could return it to the caller. If this is 0, then don't add to
          _registeredPrefixTable (assuming it has already been done).
        """
        if commandKeyChain == None:
            raise RuntimeError(
              "registerPrefix: The command KeyChain has not been set. You must call setCommandSigningInfo.")
        if commandCertificateName.size() == 0:
            raise RuntimeError(
              "registerPrefix: The command certificate name has not been set. You must call setCommandSigningInfo.")

        controlParameters = ControlParameters()
        controlParameters.setName(prefix)

        commandInterest = Interest(Name("/localhost/nfd/rib/register"))
        # NFD only accepts TlvWireFormat packets.
        commandInterest.getName().append(controlParameters.wireEncode(TlvWireFormat.get()))
        self.makeCommandInterest(
          commandInterest, commandKeyChain, commandCertificateName,
          TlvWireFormat.get())
        # The interest is answered by the local host, so set a short timeout.
        commandInterest.setInterestLifetimeMilliseconds(2000.0)

        if registeredPrefixId != 0:
            # Save the onInterest callback and send the registration interest.
            self._registeredPrefixTable.append(Node._RegisteredPrefix(
              registeredPrefixId, prefix, onInterest))

        response = Node._RegisterResponse(
          self, prefix, onInterest, onRegisterFailed, flags,
          TlvWireFormat.get(), True)
        self.expressInterest(
          commandInterest, response.onData, response.onTimeout,
          TlvWireFormat.get())

    class _PendingInterest(object):
        """
        _PendingInterest is a private class for the members of the
        _pendingInterestTable.  Create a new PendingInterest and set the
        _timeoutTime based on the current time and the interest lifetime.

        :param int pendingInterestId: A unique ID for this entry, which you
          should get with getNextPendingInteresId().
        :param Interest interest: The interest.
        :param onData: A function object to call when a matching data packet is
          received.
        :type onData: function object
        :param onTimeout: A function object to call if the interest times out.
          If onTimeout is None, this does not use it.
        :type onTimeout: function object
        """
        def __init__(self, pendingInterestId, interest, onData, onTimeout):
            self._pendingInterestId = pendingInterestId
            self._interest = interest
            self._onData = onData
            self._onTimeout = onTimeout

            # Set up _timeoutTimeMilliseconds.
            if (self._interest.getInterestLifetimeMilliseconds() != None and
                  self._interest.getInterestLifetimeMilliseconds() >= 0.0):
                self._timeoutTimeMilliseconds = (Common.getNowMilliseconds() +
                  self._interest.getInterestLifetimeMilliseconds())
            else:
                # No timeout.
                self._timeoutTimeMilliseconds = None

        _lastPendingInterestId = 0

        @staticmethod
        def getNextPendingInterestId():
            """
            Get the next unique pending interest ID.

            :return: The next pending interest ID.
            :rtype: int
            """
            Node._PendingInterest._lastPendingInterestId += 1
            return Node._PendingInterest._lastPendingInterestId

        def getPendingInterestId(self):
            """
            Get the pendingInterestId given to the constructor.

            :return: The pending interest ID.
            :rtype: int
            """
            return self._pendingInterestId

        def getInterest(self):
            """
            Get the interest given to the constructor.

            :return: The interest.
            :rtype: int
            """
            return self._interest

        def getOnData(self):
            """
            Get the onData function object given to the constructor.

            :return: The onData function object.
            :rtype: function object
            """
            return self._onData

        def isTimedOut(self, nowMilliseconds):
            """
            Check if this interest is timed out.

            :param float nowMilliseconds: The current time in milliseconds from
              Common.getNowMilliseconds().
            :return: True if this interest timed out, otherwise False.
            :rtype: bool
            """
            return (self._timeoutTimeMilliseconds != None and
                    nowMilliseconds >= self._timeoutTimeMilliseconds)

        def callTimeout(self):
            """
            Call _onTimeout (if defined).  This ignores exceptions from
            _onTimeout.
            """
            if self._onTimeout:
                # Ignore all exceptions.
                try:
                    self._onTimeout(self._interest)
                except:
                    pass

    class _RegisteredPrefix(object):
        """
        _RegisteredPrefix is a private class for the members of the
        _registeredPrefixTable. Create a new RegisteredPrefix with the
        given values.

        :param int registeredPrefixId: A unique ID for this entry, which you
          should get with getNextRegisteredPrefixId().
        :param Name prefix: The name prefix.
        :param onInterest: A function object to call when a matching data packet
          is received.
        :type onInterest: function object
        """
        def __init__(self, registeredPrefixId, prefix, onInterest):
            self._registeredPrefixId = registeredPrefixId
            self._prefix = prefix
            self._onInterest = onInterest

        _lastRegisteredPrefixId = 0

        @staticmethod
        def getNextRegisteredPrefixId():
            """
            Get the next unique registered prefix ID.

            :return: The next registered prefix ID.
            :rtype: int
            """
            Node._RegisteredPrefix._lastRegisteredPrefixId += 1
            return Node._RegisteredPrefix._lastRegisteredPrefixId

        def getRegisteredPrefixId(self):
            """
            Get the registeredPrefixId given to the constructor.

            :return: The registered prefix ID.
            :rtype: int
            """
            return self._registeredPrefixId

        def getPrefix(self):
            """
            Get the name prefix to the constructor.

            :return: The name prefix.
            :rtype: Name
            """
            return self._prefix

        def getOnInterest(self):
            """
            Get the onInterest function object given to the constructor.

            :return: The onInterest function object.
            :rtype: function object
            """
            return self._onInterest

    class _NdndIdFetcher(object):
        """
        An _NdndIdFetcher receives the Data packet with the publisher public key
        digest for the connected NDN hub.
        """
        def __init__(self, node, registeredPrefixId, prefix, onInterest,
                     onRegisterFailed, flags, wireFormat):
            self._node = node
            self._registeredPrefixId = registeredPrefixId
            self._prefix = prefix
            self._onInterest = onInterest
            self._onRegisterFailed = onRegisterFailed
            self._flags = flags
            self._wireFormat = wireFormat

        def onData(self, interest, ndndIdData):
            """
            We received the ndnd ID.
            """
            # Assume that the content is a DER encoded public key of the ndnd.
            #   Do a quick check that the first byte is for DER encoding.
            if (ndndIdData.getContent().size() < 1 or
                  ndndIdData.getContent().buf()[0] != 0x30):
                logging.getLogger(__name__).info(
                  "Register prefix failed: The content returned when fetching the NDNx ID does not appear to be a public key")
                self._onRegisterFailed(self._prefix)
                return

            # Get the digest of the public key.
            digest = bytearray(
              hashlib.sha256(ndndIdData.getContent().toBuffer()).digest())

            # Set the _ndndId and continue.
            # TODO: If there are multiple connected hubs, the NDN ID is really
            #   stored per connected hub.
            self._node._ndndId = Blob(digest, False)
            self._node._registerPrefixHelper(
              self._registeredPrefixId, self._prefix, self._onInterest,
              self._onRegisterFailed, self._flags, self._wireFormat)

        def onTimeout(self, interest):
            """
            We timed out fetching the ndnd ID.
            """
            logging.getLogger(__name__).info(
              "Register prefix failed: Timeout fetching the NDNx ID")
            self._onRegisterFailed(self._prefix)

    class _RegisterResponse(object):
        """
        A _RegisterResponse receives the response Data packet from the register
        prefix interest sent to the connected NDN hub. If this gets a bad
        response or a timeout, call onRegisterFailed.
        """
        def __init__(self, node, prefix, onInterest, onRegisterFailed, flags,
                     wireFormat, isNfdCommand):
            self._node = node
            self._prefix = prefix
            self._onInterest = onInterest
            self._onRegisterFailed = onRegisterFailed
            self._flags = flags
            self._wireFormat = wireFormat
            self._isNfdCommand = isNfdCommand

        def onData(self, interest, responseData):
            """
            We received the response. Do a quick check of expected name
            components.
            """
            if self._isNfdCommand:
                # Decode responseData->getContent() and check for a success code.
                # TODO: Move this into the TLV code.
                statusCode = None
                try:
                    decoder = TlvDecoder(responseData.getContent().buf())
                    decoder.readNestedTlvsStart(Tlv.NfdCommand_ControlResponse)
                    statusCode = decoder.readNonNegativeIntegerTlv(Tlv.NfdCommand_StatusCode)
                except ValueError as ex:
                    logging.getLogger(__name__).info(
                      "Register prefix failed: Error decoding the NFD response: %s",
                      str(ex))
                    self._onRegisterFailed(self._prefix)
                    return

                # Status code 200 is "OK".
                if statusCode != 200:
                  logging.getLogger(__name__).info(
                    "Register prefix failed: Expected NFD status code 200, got: %d",
                    statusCode)
                  self._onRegisterFailed(self._prefix)

                logging.getLogger(__name__).info(
                  "Register prefix succeeded with the NFD forwarder for prefix %s",
                  self._prefix.toUri())
            else:
                expectedName = Name("/ndnx/.../selfreg")
                if (responseData.getName().size() < 4 or
                      responseData.getName()[0] != expectedName[0] or
                      responseData.getName()[2] != expectedName[2]):
                    logging.getLogger(__name__).info(
                      "Register prefix failed: Unexpected name in NDNx response: %s",
                      responseData.getName().toUri())
                    self._onRegisterFailed(self._prefix)
                    return

                logging.getLogger(__name__).info(
                  "Register prefix succeeded with the NDNx forwarder for prefix %s",
                  self._prefix.toUri())

        def onTimeout(self, interest):
            """
            We timed out waiting for the response.
            """
            if self._isNfdCommand:
                logging.getLogger(__name__).info(
                  "Timeout for NFD register prefix command. Attempting an NDNx command...")
                # The application set the commandKeyChain, but we may be
                #   connected to NDNx.
                if self._node._ndndId == None:
                    # First fetch the ndndId of the connected hub.
                    # Pass 0 for registeredPrefixId since the entry was already added to
                    #   _registeredPrefixTable on the first try.
                    fetcher = Node._NdndIdFetcher(
                      self._node, 0, self._prefix, self._onInterest,
                      self._onRegisterFailed, self._flags, self._wireFormat)
                    # We send the interest using the given wire format so that the hub
                    # receives (and sends) in the application's desired wire format.
                    self._node.expressInterest(
                      self._node._ndndIdFetcherInterest, fetcher.onData,
                      fetcher.onTimeout, self._wireFormat)
                else:
                    # Pass 0 for registeredPrefixId since the entry was already
                    #   added to _registeredPrefixTable on the first try.
                    self._node._registerPrefixHelper(
                      0, self._prefix, self._onInterest, self._onRegisterFailed,
                      self._flags, self._wireFormat)
            else:
                # An NDNx command was sent because there is no commandKeyChain,
                #   so we can't try an NFD command. Or it was sent from this
                #   callback after trying an NFD command. Fail.
                logging.getLogger(__name__).info(
                  "Register prefix failed: Timeout waiting for the response from the register prefix interest")
                self._onRegisterFailed(self._prefix)
예제 #36
0
    def expressInterest(
      self, interestOrName, arg2, arg3 = None, arg4 = None, arg5 = None):
        """
        Send the Interest through the transport, read the entire response and 
        call onData(interest, data).  There are two forms of expressInterest.  
        The first form takes the exact interest (including lifetime):
        expressInterest(interest, onData [, onTimeout] [, wireFormat]).  
        The second form creates the interest from a name and optional 
        interest template:
        expressInterest(name [, interestTemplate], onData [, onTimeout] 
        [, wireFormat]).
        
        :param Interest interest: The Interest (if the first form is used). This 
          copies the Interest.
        :param Name name: A name for the Interest (if the second form is used).
        :param Interest interestTemplate: (optional) if not None, copy interest 
          selectors from the template (if the second form is used).  If omitted, 
          use a default interest lifetime.
        :param onData: When a matching data packet is received, this calls 
          onData(interest, data) where interest is the interest given to 
          expressInterest and data is the received Data object. NOTE: You must
          not change the interest object - if you need to change it then make a
          copy.
        :type onData: function object
        :param onTimeout: (optional) If the interest times out according to the 
          interest lifetime, this calls onTimeout(interest) where interest is 
          the interest given to expressInterest. However, if onTimeout is None 
          or omitted, this does not use it.
        :type onTimeout: function object
        :param wireFormat: (optional) A WireFormat object used to encode the 
           message. If omitted, use WireFormat.getDefaultWireFormat().
        :type wireFormat: A subclass of WireFormat
        :return:  The pending interest ID which can be used with 
          removePendingInterest.
        :rtype: int
        """
        # expressInterest(interest, onData)
        # expressInterest(interest, onData, wireFormat)
        # expressInterest(interest, onData, onTimeout)
        # expressInterest(interest, onData, onTimeout, wireFormat)
        if type(interestOrName) is Interest:
            # Node.expressInterest requires a copy of the interest.
            interest = Interest(interestOrName)
            onData = arg2
            if isinstance(arg3, WireFormat):
                onTimeout = None
                wireFormat = arg3
            else:
                onTimeout = arg3
                wireFormat = arg4
        else:
            # The first argument is a name. Make the interest from the name and 
            #   possible template.
            interest = Interest(interestOrName)
            
            # expressInterest(name, interestTemplate, onData) 
            # expressInterest(name, interestTemplate, onData, wireFormat) 
            # expressInterest(name, interestTemplate, onData, onTimeout) 
            # expressInterest(name, interestTemplate, onData, onTimeout, wireFormat) 
            if type(arg2) is Interest:
                template = arg2
                interest.setMinSuffixComponents(template.getMinSuffixComponents())
                interest.setMaxSuffixComponents(template.getMaxSuffixComponents())
                interest.setKeyLocator(template.getKeyLocator())
                interest.setExclude(template.getExclude())
                interest.setChildSelector(template.getChildSelector())
                interest.setMustBeFresh(template.getMustBeFresh())
                interest.setScope(template.getScope())
                interest.setInterestLifetimeMilliseconds(
                  template.getInterestLifetimeMilliseconds())
                # Don't copy the nonce.

                onData = arg3
                if isinstance(arg4, WireFormat):
                    onTimeout = None
                    wireFormat = arg4
                else:
                    onTimeout = arg4
                    wireFormat = arg5
            # expressInterest(name, onData) 
            # expressInterest(name, onData, wireFormat)
            # expressInterest(name, onData, onTimeout)
            # expressInterest(name, onData, onTimeout, wireFormat)
            else:
                # Set a default interest lifetime.
                interest.setInterestLifetimeMilliseconds(4000.0)
                onData = arg2
                if isinstance(arg3, WireFormat):
                    onTimeout = None
                    wireFormat = arg3
                else:
                    onTimeout = arg3
                    wireFormat = arg4
            
        if wireFormat == None:
            # Don't use a default argument since getDefaultWireFormat can change.
            wireFormat = WireFormat.getDefaultWireFormat()

        return self._node.expressInterest(
          interest, onData, onTimeout, wireFormat)
예제 #37
0
파일: face.py 프로젝트: named-data/PyNDN2
    def _getExpressInterestArgs(self, interestOrName, arg2, arg3, arg4, arg5, arg6):
        """
        This is a protected helper method to resolve the different overloaded
        forms of Face.expressInterest and return the arguments to pass to
        Node.expressInterest. This is necessary to prepare arguments such as
        interestCopy before dispatching to Node.expressInterest.

        :return: A dictionary with the following keys: 'pendingInterestId',
          'interestCopy', 'onData', 'onTimeout', 'onNetworkNack' and 'wireFormat'.
        :rtype: dict
        """
        if isinstance(interestOrName, Interest):
            # Node.expressInterest requires a copy of the interest.
            interestCopy = Interest(interestOrName)
        else:
            # The first argument is a name. Make the interest from the name and
            #   possible template.
            if isinstance(arg2, Interest):
                template = arg2
                # Copy the template.
                interestCopy = Interest(template)
                interestCopy.setName(interestOrName)

                # Shift the remaining args to be processed below.
                arg2 = arg3
                arg3 = arg4
                arg4 = arg5
                arg5 = arg6
            else:
                # No template.
                interestCopy = Interest(interestOrName)
                # Set a default interest lifetime.
                interestCopy.setInterestLifetimeMilliseconds(4000.0)

        onData = arg2
        # arg3,       arg4,          arg5 may be:
        # OnTimeout,  OnNetworkNack, WireFormat
        # OnTimeout,  OnNetworkNack, None
        # OnTimeout,  WireFormat,    None
        # OnTimeout,  None,          None
        # WireFormat, None,          None
        # None,       None,          None
        if isinstance(arg3, collections.Callable):
            onTimeout = arg3
        else:
            onTimeout = None

        if isinstance(arg4, collections.Callable):
            onNetworkNack = arg4
        else:
            onNetworkNack = None

        if isinstance(arg3, WireFormat):
            wireFormat = arg3
        elif isinstance(arg4, WireFormat):
            wireFormat = arg4
        elif isinstance(arg5, WireFormat):
            wireFormat = arg5
        else:
            wireFormat = WireFormat.getDefaultWireFormat()

        return { 'pendingInterestId': self._node.getNextEntryId(),
          'interestCopy': interestCopy, 'onData': onData, 'onTimeout': onTimeout,
          'onNetworkNack': onNetworkNack, 'wireFormat': wireFormat }
예제 #38
0
class Node(object):
    """
    Create a new Node for communication with an NDN hub with the given Transport
    object and connectionInfo.
    
    :param Transport transport: An object of a subclass of Transport used for 
      communication.
    :param Transport.ConnectionInfo connectionInfo: An object of a subclass of 
      Transport.ConnectionInfo to be used to connect to the transport.
    """
    def __init__(self, transport, connectionInfo):
        self._transport = transport
        self._connectionInfo = connectionInfo
        # An array of PendintInterest
        self._pendingInterestTable = []
        # An array of RegisteredPrefix
        self._registeredPrefixTable = []
        self._ndndIdFetcherInterest = Interest(
            Name("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY"))
        self._ndndIdFetcherInterest.setInterestLifetimeMilliseconds(4000.0)
        self._ndndId = None
        self._commandInterestGenerator = CommandInterestGenerator()

    def expressInterest(self, interest, onData, onTimeout, wireFormat):
        """
        Send the Interest through the transport, read the entire response and 
        call onData(interest, data).
        
        :param Interest interest: The Interest which is NOT copied for this 
          internal Node method.  The Face expressInterest is reponsible for 
          making a copy for Node to use.
        :param onData: A function object to call when a matching data packet is 
          received.
        :type onData: function object
        :param onTimeout: A function object to call if the interest times out. 
          If onTimeout is None, this does not use it.
        :type onTimeout: function object
        :param wireFormat: A WireFormat object used to encode the message.
        :type wireFormat: a subclass of WireFormat
        """
        # TODO: Properly check if we are already connected to the expected host.
        if not self._transport.getIsConnected():
            self._transport.connect(self._connectionInfo, self)

        pendingInterestId = Node._PendingInterest.getNextPendingInterestId()
        self._pendingInterestTable.append(
            Node._PendingInterest(pendingInterestId, interest, onData,
                                  onTimeout))

        self._transport.send(interest.wireEncode(wireFormat).toBuffer())
        return pendingInterestId

    def removePendingInterest(self, pendingInterestId):
        """
        Remove the pending interest entry with the pendingInterestId from the 
        pending interest table. This does not affect another pending interest 
        with a different pendingInterestId, even if it has the same interest 
        name. If there is no entry with the pendingInterestId, do nothing.
        
        :param int pendingInterestId: The ID returned from expressInterest.
        """
        # Go backwards through the list so we can erase entries.
        # Remove all entries even though pendingInterestId should be unique.
        i = len(self._pendingInterestTable) - 1
        while i >= 0:
            if (self._pendingInterestTable[i].getPendingInterestId() ==
                    pendingInterestId):
                self._pendingInterestTable.pop(i)
            i -= 1

    def makeCommandInterest(self, interest, keyChain, certificateName,
                            wireFormat):
        """
        Append a timestamp component and a random value component to interest's
        name. Then use the keyChain and certificateName to sign the interest. 
        If the interest lifetime is not set, this sets it.

        :param Interest interest: The interest whose name is append with 
          components.
        :param KeyChain keyChain: The KeyChain for calling sign.
        :param Name certificateName: The certificate name of the key to use for 
          signing.
        :param wireFormat: (optional) A WireFormat object used to encode this 
           ControlParameters.
        :type wireFormat: A subclass of WireFormat
        """
        self._commandInterestGenerator.generate(interest, keyChain,
                                                certificateName, wireFormat)

    def registerPrefix(self, prefix, onInterest, onRegisterFailed, flags,
                       wireFormat, commandKeyChain, commandCertificateName):
        """
        Register prefix with the connected NDN hub and call onInterest when a 
        matching interest is received.
          
        :param Name prefix: The Name for the prefix to register which is NOT 
          copied for this internal Node method. The Face registerPrefix is 
          reponsible for making a copy for Node to use..
        :param onInterest: A function object to call when a matching interest is
          received.
        :type onInterest: function object
        :param onRegisterFailed: A function object to call if failed to retrieve
          the connected hub's ID or failed to register the prefix.
        :type onRegisterFailed: function object
        :param ForwardingFlags flags: The flags for finer control of which 
          interests are forwardedto the application.
        :param wireFormat: A WireFormat object used to encode the message.
        :type wireFormat: a subclass of WireFormat
        :param KeyChain commandKeyChain: The KeyChain object for signing 
          interests. If null, assume we are connected to a legacy NDNx forwarder.
        :param Name commandCertificateName: The certificate name for signing 
          interests.
        """
        # Get the registeredPrefixId now so we can return it to the caller.
        registeredPrefixId = Node._RegisteredPrefix.getNextRegisteredPrefixId()

        # If we have an _ndndId, we know we already connected to NDNx.
        if self._ndndId != None or commandKeyChain == None:
            # Assume we are connected to a legacy NDNx server.

            if self._ndndId == None:
                # First fetch the ndndId of the connected hub.
                fetcher = Node._NdndIdFetcher(self, registeredPrefixId, prefix,
                                              onInterest, onRegisterFailed,
                                              flags, wireFormat)
                # We send the interest using the given wire format so that the hub
                # receives (and sends) in the application's desired wire format.
                self.expressInterest(self._ndndIdFetcherInterest,
                                     fetcher.onData, fetcher.onTimeout,
                                     wireFormat)
            else:
                _registerPrefixHelper(registeredPrefixId, Name(prefix),
                                      onInterest, onRegisterFailed, flags,
                                      wireFormat)
        else:
            # The application set the KeyChain for signing NFD interests.
            self._nfdRegisterPrefix(registeredPrefixId, Name(prefix),
                                    onInterest, onRegisterFailed, flags,
                                    commandKeyChain, commandCertificateName)

        return registeredPrefixId

    def removeRegisteredPrefix(self, registeredPrefixId):
        """
        Remove the registered prefix entry with the registeredPrefixId from the
        registered prefix table. This does not affect another registered prefix 
        with a different registeredPrefixId, even if it has the same prefix 
        name. If there is no entry with the registeredPrefixId, do nothing.
        
        :param int registeredPrefixId: The ID returned from registerPrefix.
        """
        # Go backwards through the list so we can erase entries.
        # Remove all entries even though registeredPrefixId should be unique.
        i = len(self._registeredPrefixTable) - 1
        while i >= 0:
            if (self._registeredPrefixTable[i].getRegisteredPrefixId() ==
                    registeredPrefixId):
                self._registeredPrefixTable.pop(i)
            i -= 1

    def processEvents(self):
        """
        Process any packets to receive and call callbacks such as onData, 
        onInterest or onTimeout. This returns immediately if there is no data to 
        receive. This blocks while calling the callbacks. You should repeatedly 
        call this from an event loop, with calls to sleep as needed so that the 
        loop doesn't use 100% of the CPU. Since processEvents modifies the pending 
        interest table, your application should make sure that it calls 
        processEvents in the same thread as expressInterest (which also modifies 
        the pending interest table).
        
        :raises: This may raise an exception for reading data or in the callback
          for processing the data.  If you call this from an main event loop, 
          you may want to catch and log/disregard all exceptions.
        """
        self._transport.processEvents()

        # Check for PIT entry timeouts. Go backwards through the list so we can
        #   erase entries.
        nowMilliseconds = Common.getNowMilliseconds()
        i = len(self._pendingInterestTable) - 1
        while i >= 0:
            if self._pendingInterestTable[i].isTimedOut(nowMilliseconds):
                # Save the PendingInterest and remove it from the PIT.  Then
                #   call the callback.
                pendingInterest = self._pendingInterestTable[i]
                self._pendingInterestTable.pop(i)
                pendingInterest.callTimeout()

                # Refresh now since the timeout callback might have delayed.
                nowMilliseconds = Common.getNowMilliseconds()

            i -= 1

    def getTransport(self):
        """
        Get the transport object given to the constructor.
        
        :return: The transport object.
        :rtype: Transport
        """
        return self._transport

    def getConnectionInfo(self):
        """
        Get the connectionInfo object given to the constructor.
        
        :return: The connectionInfo object.
        :rtype: Transport.ConnectionInfo
        """
        return self._connectionInfo

    def onReceivedElement(self, element):
        """
        This is called by the transport's ElementReader to process an
        entire received Data or Interest element.
        
        :param element: The bytes of the incoming element.
        :type element: An array type with int elements
        """
        # The type codes for TLV Interest and Data packets are chosen to not
        #   conflict with the first byte of a binary XML packet, so we canjust
        #   look at the first byte.
        if not (element[0] == Tlv.Interest or element[0] == Tlv.Data):
            # Ignore non-TLV elements.
            return

        # First, decode as Interest or Data.
        interest = None
        data = None
        decoder = TlvDecoder(element)
        if decoder.peekType(Tlv.Interest, len(element)):
            interest = Interest()
            interest.wireDecode(element, TlvWireFormat.get())
        elif decoder.peekType(Tlv.Data, len(element)):
            data = Data()
            data.wireDecode(element, TlvWireFormat.get())

        # Now process as Interest or Data.
        if interest != None:
            entry = self._getEntryForRegisteredPrefix(interest.getName())
            if entry != None:
                entry.getOnInterest()(entry.getPrefix(), interest,
                                      self._transport,
                                      entry.getRegisteredPrefixId())
        elif data != None:
            pendingInterests = self._extractEntriesForExpressedInterest(
                data.getName())
            for pendingInterest in pendingInterests:
                pendingInterest.getOnData()(pendingInterest.getInterest(),
                                            data)

    def shutdown(self):
        """
        Call getTransport().close().
        """
        self._transport.close()

    def _extractEntriesForExpressedInterest(self, name):
        """
        Find all entries from the _pendingInterestTable where the name conforms 
        to the entry's interest selectors, remove the entries from the table
        and return them.
        
        :param Name name: The name to find the interest for (from the incoming 
          data packet).
        :return: The matching entries from the _pendingInterestTable, or []
          if none are found.
        :rtype: array of _PendingInterest
        """
        result = []

        # Go backwards through the list so we can erase entries.
        i = len(self._pendingInterestTable) - 1
        while i >= 0:
            if self._pendingInterestTable[i].getInterest().matchesName(name):
                result.append(self._pendingInterestTable[i])
                self._pendingInterestTable.pop(i)
            i -= 1

        return result

    def _getEntryForRegisteredPrefix(self, name):
        """
        Find the first entry from the _registeredPrefixTable where the entry 
        prefix is the longest that matches name.
        
        :param Name name: The name to find the RegisteredPrefix for (from the 
          incoming interest packet).
        :return: The registered prefix entry, or None of not found.
        :rtype: _RegisteredPrefix
        """
        iResult = -1

        for i in range(len(self._registeredPrefixTable)):
            if self._registeredPrefixTable[i].getPrefix().match(name):
                if (iResult < 0
                        or self._registeredPrefixTable[i].getPrefix().size() >
                        self._registeredPrefixTable[iResult].getPrefix().size(
                        )):
                    # Update to the longer match.
                    iResult = i

        if iResult >= 0:
            return self._registeredPrefixTable[iResult]
        else:
            return None

    def _registerPrefixHelper(self, registeredPrefixId, prefix, onInterest,
                              onRegisterFailed, flags, wireFormat):
        """
        Do the work of registerPrefix to register with NDNx once we have an 
        ndndId_.
        
        :param int registeredPrefixId: The 
          _RegisteredPrefix.getNextRegisteredPrefixId() which registerPrefix got
          so it could return it to the caller. If this is 0, then don't add to 
          registeredPrefixTable_ (assuming it has already been done).  
        """
        # Create a ForwardingEntry.
        # Note: ndnd ignores any freshness that is larger than 3600 seconds and
        #   sets 300 seconds instead. To register "forever", (=2000000000 sec),
        #   the freshness period must be omitted.
        forwardingEntry = ForwardingEntry()
        forwardingEntry.setAction("selfreg")
        forwardingEntry.setPrefix(prefix)
        forwardingEntry.setForwardingFlags(flags)
        content = forwardingEntry.wireEncode(wireFormat)

        # Set the ForwardingEntry as the content of a Data packet and sign.
        data = Data()
        data.setContent(content)
        # Set the name to a random value so that each request is unique.
        nonce = bytearray(4)
        for i in range(len(nonce)):
            nonce[i] = _systemRandom.randint(0, 0xff)
        data.getName().append(nonce)
        # The ndnd ignores the signature, so set to blank values.
        data.getSignature().getKeyLocator().setType(
            KeyLocatorType.KEY_LOCATOR_DIGEST)
        data.getSignature().getKeyLocator().setKeyData(
            Blob(bytearray(32), False))
        data.getSignature().setSignature(Blob(bytearray(128), False))
        encodedData = data.wireEncode(wireFormat)

        # Create an interest where the name has the encoded Data packet.
        interestName = Name().append("ndnx").append(
            self._ndndId).append("selfreg").append(encodedData)

        interest = Interest(interestName)
        interest.setInterestLifetimeMilliseconds(4000.0)
        interest.setScope(1)
        encodedInterest = interest.wireEncode(wireFormat)

        if registeredPrefixId != 0:
            # Save the onInterest callback and send the registration interest.
            self._registeredPrefixTable.append(
                Node._RegisteredPrefix(registeredPrefixId, prefix, onInterest))

        response = Node._RegisterResponse(self, prefix, onInterest,
                                          onRegisterFailed, flags, wireFormat,
                                          False)
        self.expressInterest(interest, response.onData, response.onTimeout,
                             wireFormat)

    def _nfdRegisterPrefix(self, registeredPrefixId, prefix, onInterest,
                           onRegisterFailed, flags, commandKeyChain,
                           commandCertificateName):
        """
        Do the work of registerPrefix to register with NFD.
        
        :param int registeredPrefixId: The 
          _RegisteredPrefix.getNextRegisteredPrefixId() which registerPrefix got
          so it could return it to the caller. If this is 0, then don't add to 
          registeredPrefixTable_ (assuming it has already been done).  
        """
        if commandKeyChain == None:
            raise RuntimeError(
                "registerPrefix: The command KeyChain has not been set. You must call setCommandSigningInfo."
            )
        if commandCertificateName.size() == 0:
            raise RuntimeError(
                "registerPrefix: The command certificate name has not been set. You must call setCommandSigningInfo."
            )

        controlParameters = ControlParameters()
        controlParameters.setName(prefix)

        commandInterest = Interest(Name("/localhost/nfd/rib/register"))
        # NFD only accepts TlvWireFormat packets.
        commandInterest.getName().append(
            controlParameters.wireEncode(TlvWireFormat.get()))
        self.makeCommandInterest(commandInterest, commandKeyChain,
                                 commandCertificateName, TlvWireFormat.get())
        # The interest is answered by the local host, so set a short timeout.
        commandInterest.setInterestLifetimeMilliseconds(2000.0)

        if registeredPrefixId != 0:
            # Save the onInterest callback and send the registration interest.
            self._registeredPrefixTable.append(
                Node._RegisteredPrefix(registeredPrefixId, prefix, onInterest))

        response = Node._RegisterResponse(self, prefix, onInterest,
                                          onRegisterFailed, flags,
                                          TlvWireFormat.get(), True)
        self.expressInterest(commandInterest, response.onData,
                             response.onTimeout, TlvWireFormat.get())

    class _PendingInterest(object):
        """
        _PendingInterest is a private class for the members of the 
        _pendingInterestTable.  Create a new PendingInterest and set the 
        _timeoutTime based on the current time and the interest lifetime.
        
        :param int pendingInterestId: A unique ID for this entry, which you 
          should get with getNextPendingInteresId().
        :param Interest interest: The interest.
        :param onData: A function object to call when a matching data packet is 
          received.
        :type onData: function object
        :param onTimeout: A function object to call if the interest times out.  
          If onTimeout is None, this does not use it.
        :type onTimeout: function object
        """
        def __init__(self, pendingInterestId, interest, onData, onTimeout):
            self._pendingInterestId = pendingInterestId
            self._interest = interest
            self._onData = onData
            self._onTimeout = onTimeout

            # Set up _timeoutTimeMilliseconds.
            if (self._interest.getInterestLifetimeMilliseconds() != None and
                    self._interest.getInterestLifetimeMilliseconds() >= 0.0):
                self._timeoutTimeMilliseconds = (
                    Common.getNowMilliseconds() +
                    self._interest.getInterestLifetimeMilliseconds())
            else:
                # No timeout.
                self._timeoutTimeMilliseconds = None

        _lastPendingInterestId = 0

        @staticmethod
        def getNextPendingInterestId():
            """
            Get the next unique pending interest ID.
            
            :return: The next pending interest ID.
            :rtype: int
            """
            Node._PendingInterest._lastPendingInterestId += 1
            return Node._PendingInterest._lastPendingInterestId

        def getPendingInterestId(self):
            """
            Get the pendingInterestId given to the constructor.
            
            :return: The pending interest ID.
            :rtype: int
            """
            return self._pendingInterestId

        def getInterest(self):
            """
            Get the interest given to the constructor.
            
            :return: The interest.
            :rtype: int
            """
            return self._interest

        def getOnData(self):
            """
            Get the onData function object given to the constructor.
            
            :return: The onData function object.
            :rtype: function object
            """
            return self._onData

        def isTimedOut(self, nowMilliseconds):
            """
            Check if this interest is timed out.
            
            :param float nowMilliseconds: The current time in milliseconds from 
              Common.getNowMilliseconds().
            :return: True if this interest timed out, otherwise False.
            :rtype: bool
            """
            return (self._timeoutTimeMilliseconds != None
                    and nowMilliseconds >= self._timeoutTimeMilliseconds)

        def callTimeout(self):
            """
            Call _onTimeout (if defined).  This ignores exceptions from 
            _onTimeout.
            """
            if self._onTimeout:
                # Ignore all exceptions.
                try:
                    self._onTimeout(self._interest)
                except:
                    pass

    class _RegisteredPrefix(object):
        """
        _RegisteredPrefix is a private class for the members of the
        _registeredPrefixTable. Create a new RegisteredPrefix with the
        given values.

        :param int registeredPrefixId: A unique ID for this entry, which you
          should get with getNextRegisteredPrefixId().
        :param Name prefix: The name prefix.
        :param onInterest: A function object to call when a matching data packet
          is received.
        :type onInterest: function object
        """
        def __init__(self, registeredPrefixId, prefix, onInterest):
            self._registeredPrefixId = registeredPrefixId
            self._prefix = prefix
            self._onInterest = onInterest

        _lastRegisteredPrefixId = 0

        @staticmethod
        def getNextRegisteredPrefixId():
            """
            Get the next unique registered prefix ID.
            
            :return: The next registered prefix ID.
            :rtype: int
            """
            Node._RegisteredPrefix._lastRegisteredPrefixId += 1
            return Node._RegisteredPrefix._lastRegisteredPrefixId

        def getRegisteredPrefixId(self):
            """
            Get the registeredPrefixId given to the constructor.
            
            :return: The registered prefix ID.
            :rtype: int
            """
            return self._registeredPrefixId

        def getPrefix(self):
            """
            Get the name prefix to the constructor.
            
            :return: The name prefix.
            :rtype: Name
            """
            return self._prefix

        def getOnInterest(self):
            """
            Get the onInterest function object given to the constructor.
            
            :return: The onInterest function object.
            :rtype: function object
            """
            return self._onInterest

    class _NdndIdFetcher(object):
        """
        An _NdndIdFetcher receives the Data packet with the publisher public key 
        digest for the connected NDN hub.
        """
        def __init__(self, node, registeredPrefixId, prefix, onInterest,
                     onRegisterFailed, flags, wireFormat):
            self._node = node
            self._registeredPrefixId = registeredPrefixId
            self._prefix = prefix
            self._onInterest = onInterest
            self._onRegisterFailed = onRegisterFailed
            self._flags = flags
            self._wireFormat = wireFormat

        def onData(self, interest, ndndIdData):
            """
            We received the ndnd ID.
            """
            # Assume that the content is a DER encoded public key of the ndnd.
            #   Do a quick check that the first byte is for DER encoding.
            if (ndndIdData.getContent().size() < 1
                    or ndndIdData.getContent().buf()[0] != 0x30):
                self._onRegisterFailed(self._prefix)
                return

            # Get the digest of the public key.
            digest = bytearray(
                hashlib.sha256(ndndIdData.getContent().toBuffer()).digest())

            # Set the _ndndId and continue.
            # TODO: If there are multiple connected hubs, the NDN ID is really
            #   stored per connected hub.
            self._node._ndndId = Blob(digest, False)
            self._node._registerPrefixHelper(self._registeredPrefixId,
                                             self._prefix, self._onInterest,
                                             self._onRegisterFailed,
                                             self._flags, self._wireFormat)

        def onTimeout(self, interest):
            """
            We timed out fetching the ndnd ID.
            """
            self._onRegisterFailed(self._prefix)

    class _RegisterResponse(object):
        """
        A _RegisterResponse receives the response Data packet from the register
        prefix interest sent to the connected NDN hub. If this gets a bad 
        response or a timeout, call onRegisterFailed.
        """
        def __init__(self, node, prefix, onInterest, onRegisterFailed, flags,
                     wireFormat, isNfdCommand):
            self._node = node
            self._prefix = prefix
            self._onInterest = onInterest
            self._onRegisterFailed = onRegisterFailed
            self._flags = flags
            self._wireFormat = wireFormat
            self._isNfdCommand = isNfdCommand

        def onData(self, interest, responseData):
            """
            We received the response. Do a quick check of expected name 
            components.
            """
            if self._isNfdCommand:
                # Decode responseData->getContent() and check for a success code.
                # TODO: Move this into the TLV code.
                statusCode = None
                try:
                    decoder = TlvDecoder(responseData.getContent().buf())
                    decoder.readNestedTlvsStart(Tlv.NfdCommand_ControlResponse)
                    statusCode = decoder.readNonNegativeIntegerTlv(
                        Tlv.NfdCommand_StatusCode)
                except ValueError:
                    # Error decoding the ControlResponse.
                    self._onRegisterFailed_(self._prefix)
                    return

                # Status code 200 is "OK".
                if statusCode != 200:
                    self._onRegisterFailed_(self._prefix)

                # Otherwise, silently succeed.
            else:
                expectedName = Name("/ndnx/.../selfreg")
                if (responseData.getName().size() < 4
                        or responseData.getName()[0] != expectedName[0]
                        or responseData.getName()[2] != expectedName[2]):
                    self._onRegisterFailed(self._prefix)
                    return

                # Otherwise, silently succeed.

        def onTimeout(self, interest):
            """
            We timed out waiting for the response.
            """
            if self._isNfdCommand:
                # The application set the commandKeyChain, but we may be
                #   connected to NDNx.
                if self._node._ndndId == None:
                    # First fetch the ndndId of the connected hub.
                    # Pass 0 for registeredPrefixId since the entry was already added to
                    #   registeredPrefixTable_ on the first try.
                    fetcher = Node._NdndIdFetcher(self._node, 0, self._prefix,
                                                  self._onInterest,
                                                  self._onRegisterFailed,
                                                  self._flags,
                                                  self._wireFormat)
                    # We send the interest using the given wire format so that the hub
                    # receives (and sends) in the application's desired wire format.
                    self._node.expressInterest(
                        self._node._ndndIdFetcherInterest, fetcher.onData,
                        fetcher.onTimeout, self._wireFormat)
                else:
                    # Pass 0 for registeredPrefixId since the entry was already
                    #   added to registeredPrefixTable_ on the first try.
                    self._node.registerPrefixHelper(0, self._prefix,
                                                    self._onInterest,
                                                    self._onRegisterFailed,
                                                    self._flags,
                                                    self._wireFormat)
            else:
                # An NDNx command was sent because there is no commandKeyChain,
                #   so we can't try an NFD command. Or it was sent from this
                #   callback after trying an NFD command. Fail.
                self._onRegisterFailed(self._prefix)