def _decodeControlParameters(controlParameters, decoder): controlParameters.clear() endOffset = decoder.readNestedTlvsStart( Tlv.ControlParameters_ControlParameters) # decode name if decoder.peekType(Tlv.Name, endOffset): name = Name() Tlv0_1_1WireFormat._decodeName(name, decoder) controlParameters.setName(name) # decode face ID controlParameters.setFaceId(decoder.readOptionalNonNegativeIntegerTlv (Tlv.ControlParameters_FaceId, endOffset)) # decode URI if decoder.peekType(Tlv.ControlParameters_Uri, endOffset): uri = Blob( decoder.readOptionalBlobTlv(Tlv.ControlParameters_Uri, endOffset), False) controlParameters.setUri(str(uri)) # decode integers controlParameters.setLocalControlFeature( decoder.readOptionalNonNegativeIntegerTlv (Tlv.ControlParameters_LocalControlFeature, endOffset)) controlParameters.setOrigin( decoder.readOptionalNonNegativeIntegerTlv (Tlv.ControlParameters_Origin, endOffset)) controlParameters.setCost( decoder.readOptionalNonNegativeIntegerTlv (Tlv.ControlParameters_Cost, endOffset)) # set forwarding flags if decoder.peekType(Tlv.ControlParameters_Flags, endOffset): flags = ForwardingFlags() flags.setNfdForwardingFlags( decoder.readNonNegativeIntegerTlv(Tlv.ControlParameters_Flags)) controlParameters.setForwardingFlags(flags) # decode strategy if decoder.peekType(Tlv.ControlParameters_Strategy, endOffset): strategyEndOffset = decoder.readNestedTlvsStart( Tlv.ControlParameters_Strategy) Tlv0_1_1WireFormat._decodeName(controlParameters.getStrategy(), decoder) decoder.finishNestedTlvs(strategyEndOffset) # decode expiration period controlParameters.setExpirationPeriod( decoder.readOptionalNonNegativeIntegerTlv( Tlv.ControlParameters_ExpirationPeriod, endOffset)) decoder.finishNestedTlvs(endOffset)
def setForwardingFlags(self, forwardingFlags): """ Set the ForwardingFlags object to a copy of forwardingFlags. You can use getForwardingFlags() and change the existing ForwardingFlags object. :param ForwardingFlags forwardingFlags: The new ForwardingFlace object. """ self._forwardingFlags = (ForwardingFlags(forwardingFlags) if type(forwardingFlags) is ForwardingFlags else ForwardingFlags())
def clear(self): self._name = None self._faceId = None self._uri = "" self._localControlFeature = None self._origin = None self._cost = None self._forwardingFlags = ForwardingFlags() self._strategy = Name() self._expirationPeriod = None
def __init__(self): self._name = Name() self._faceId = None # TODO: Add "Uri" string. self._localControlFeature = None self._origin = None self._cost = None self._forwardingFlags = ForwardingFlags() # TODO: Add "Strategy" name. self._expirationPeriod = None
def __init__(self): if not WireFormat.ENABLE_NDNX: raise RuntimeError( "ForwardingEntry is for NDNx and is deprecated. To enable while you upgrade your code to use NFD, set WireFormat.ENABLE_NDNX = True" ) self._action = None self._prefix = Name() self._faceId = None self._forwardingFlags = ForwardingFlags() self._freshnessPeriod = None
def __init__(self, value=None): if type(value) is ControlParameters: # Make a deep copy. self._name = None if value._name == None else Name(value._name) self._faceId = value._faceId self._uri = value._uri self._localControlFeature = value._localControlFeature self._origin = value._origin self._cost = value._cost self._forwardingFlags = ForwardingFlags(value._forwardingFlags) self._strategy = Name(value._strategy) self._expirationPeriod = value._expirationPeriod else: self._name = None self._faceId = None self._uri = "" self._localControlFeature = None self._origin = None self._cost = None self._forwardingFlags = ForwardingFlags() self._strategy = Name() self._expirationPeriod = None
def encodeControlParameters(self, controlParameters): """ Encode controlParameters and return the encoding. :param controlParameters: The ControlParameters object to encode. :type controlParameters: ControlParameters :return: A Blob containing the encoding. :rtype: Blob """ encoder = TlvEncoder(256) saveLength = len(encoder) # Encode backwards. encoder.writeOptionalNonNegativeIntegerTlvFromFloat( Tlv.ControlParameters_ExpirationPeriod, controlParameters.getExpirationPeriod()) if controlParameters.getStrategy().size() > 0: strategySaveLength = len(encoder) self._encodeName(controlParameters.getStrategy(), encoder) encoder.writeTypeAndLength(Tlv.ControlParameters_Strategy, len(encoder) - strategySaveLength) flags = controlParameters.getForwardingFlags().getNfdForwardingFlags() if (flags != ForwardingFlags().getNfdForwardingFlags()): # The flags are not the default value. encoder.writeNonNegativeIntegerTlv(Tlv.ControlParameters_Flags, flags) encoder.writeOptionalNonNegativeIntegerTlv(Tlv.ControlParameters_Cost, controlParameters.getCost()) encoder.writeOptionalNonNegativeIntegerTlv( Tlv.ControlParameters_Origin, controlParameters.getOrigin()) encoder.writeOptionalNonNegativeIntegerTlv( Tlv.ControlParameters_LocalControlFeature, controlParameters.getLocalControlFeature()) if len(controlParameters.getUri()) != 0: encoder.writeBlobTlv(Tlv.ControlParameters_Uri, Blob(controlParameters.getUri()).buf()) encoder.writeOptionalNonNegativeIntegerTlv( Tlv.ControlParameters_FaceId, controlParameters.getFaceId()) if controlParameters.getName() != None: self._encodeName(controlParameters.getName(), encoder) encoder.writeTypeAndLength(Tlv.ControlParameters_ControlParameters, len(encoder) - saveLength) return Blob(encoder.getOutput(), False)
def _registerPrefixHelper(self, registeredPrefixId, prefixCopy, onInterest, onRegisterFailed, arg5=None, arg6=None, arg7=None): """ This is a protected helper method to do the work of registerPrefix to resolve the different overloaded forms. The registeredPrefixId is from getNextEntryId(). This has no return value and can be used in a callback. """ # arg5, arg6, arg7 may be: # OnRegisterSuccess, ForwardingFlags, WireFormat # OnRegisterSuccess, ForwardingFlags, None # OnRegisterSuccess, WireFormat, None # OnRegisterSuccess, None, None # ForwardingFlags, WireFormat, None # ForwardingFlags, None, None # WireFormat, None, None # None, None, None if isinstance(arg5, collections.Callable): onRegisterSuccess = arg5 else: onRegisterSuccess = None if isinstance(arg5, ForwardingFlags): flags = arg5 elif isinstance(arg6, ForwardingFlags): flags = arg6 else: flags = ForwardingFlags() if isinstance(arg5, WireFormat): wireFormat = arg5 elif isinstance(arg6, WireFormat): wireFormat = arg6 elif isinstance(arg7, WireFormat): wireFormat = arg7 else: # Don't use a default argument since getDefaultWireFormat can change. wireFormat = WireFormat.getDefaultWireFormat() return self._node.registerPrefix(registeredPrefixId, prefixCopy, onInterest, onRegisterFailed, onRegisterSuccess, flags, wireFormat, self._commandKeyChain, self._commandCertificateName, self)
def _registerPrefixHelper( self, registeredPrefixId, prefixCopy, onInterest, onRegisterFailed, flags = None, wireFormat = None): """ This is a protected helper method to do the work of registerPrefix to resolve the different overloaded forms. The registeredPrefixId is from getNextEntryId(). This has no return value and can be used in a callback. """ if flags == None: flags = ForwardingFlags() if wireFormat == None: # Don't use a default argument since getDefaultWireFormat can change. wireFormat = WireFormat.getDefaultWireFormat() return self._node.registerPrefix( registeredPrefixId, prefixCopy, onInterest, onRegisterFailed, flags, wireFormat, self._commandKeyChain, self._commandCertificateName, self)
def decodeForwardingEntry(self, forwardingEntry, input): """ Decode input as an forwardingEntry and set the fields of the forwardingEntry object. :param forwardingEntry: The ForwardingEntry object whose fields are updated. :type forwardingEntry: ForwardingEntry :param input: The array with the bytes to decode. :type input: An array type with int elements """ decoder = TlvDecoder(input) endOffset = decoder.readNestedTlvsStart(Tlv.ForwardingEntry) actionBytes = decoder.readOptionalBlobTlv(Tlv.Action, endOffset) if actionBytes != None: # Convert bytes to a str. forwardingEntry.setAction("".join(map(chr, actionBytes))) else: forwardingEntry.setAction(None) if decoder.peekType(Tlv.Name, endOffset): self._decodeName(forwardingEntry.getPrefix(), decoder) else: forwardingEntry.getPrefix().clear() forwardingEntry.setFaceId( decoder.readOptionalNonNegativeIntegerTlv(Tlv.FaceID, endOffset)) forwardingEntryFlags = decoder.readOptionalNonNegativeIntegerTlv( Tlv.ForwardingFlags, endOffset) if forwardingEntryFlags != None: forwardingEntry.getForwardingFlags().setForwardingEntryFlags( forwardingEntryFlags) else: # This sets the default flags. forwardingEntry.setForwardingFlags(ForwardingFlags()) forwardingEntry.setFreshnessPeriod( decoder.readOptionalNonNegativeIntegerTlvAsFloat( Tlv.FreshnessPeriod, endOffset)) decoder.finishNestedTlvs(endOffset)
def registerPrefix( self, prefix, onInterest, onRegisterFailed, flags = None, wireFormat = None): """ Register prefix with the connected NDN hub and call onInterest when a matching interest is received. If you have not called setCommandSigningInfo, this assumes you are connecting to NDNx. If you have called setCommandSigningInfo, this first sends an NFD registration request, and if that times out then this sends an NDNx registration request. If need to register a prefix with NFD, you must first call setCommandSigningInfo. :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: When an interest is received which matches the name prefix, this calls onInterest(prefix, interest, transport, registeredPrefixId). NOTE: You must not change the prefix object - if you need to change it then make a copy. :type onInterest: function object :param onRegisterFailed: If register prefix fails for any reason, this calls onRegisterFailed(prefix). :type onRegisterFailed: function object :param ForwardingFlags flags: The flags for finer control of which interests are forwardedto the application. :param wireFormat: (optional) A WireFormat object used to encode this ControlParameters. If omitted, use WireFormat.getDefaultWireFormat(). :type wireFormat: A subclass of WireFormat :raises: This raises an exception if setCommandSigningInfo has not been called to set the KeyChain, etc. for signing the command interest. """ if flags == None: flags = ForwardingFlags() if wireFormat == None: # Don't use a default argument since getDefaultWireFormat can change. wireFormat = WireFormat.getDefaultWireFormat() # Node.expressInterest requires a copy of the prefix. self._node.registerPrefix( prefix, onInterest, onRegisterFailed, flags, wireFormat, self._commandKeyChain, self._commandCertificateName)
if v2 != v: return (False, type(obj).__name__, "Property set then property get failed", p, v, v2) except Exception as e: #exc_type, exc_value, exc_traceback = sys.exc_info() #traceback.print_tb(exc_traceback, limit=1, file=sys.stdout) return (False, type(obj).__name__, "Exception raised", e) return (True, type(obj).__name__, vals) ## To Do: Add value checks # ControlParameters # from pyndn.name import Name from pyndn.control_parameters import ControlParameters from pyndn.forwarding_flags import ForwardingFlags for p in [("name",Name("yes"),Name("another")), ("faceId", 32, None), ("localControlFeature", 1, None), ("origin", 2, 9), ("cost", 1, None), ("forwardingFlags", ForwardingFlags(), ForwardingFlags()), ("expirationPeriod", 1000.1, None)]: res = testPropertyRW( ControlParameters(), p[0], [p[1],p[2]]) if not res[0]: print(res) # Data # from pyndn.data import Data from pyndn.name import Name from pyndn.meta_info import MetaInfo from pyndn.signature import Signature from pyndn.util.blob import Blob # We do not test the signature property because clone is not yet implemented for it. # for p in [("name",Name("yes"),Name("another")), ("metaInfo", MetaInfo(), MetaInfo()), ("content", Blob("foo"), Blob("bar"))]: res = testPropertyRW( Data(), p[0], [p[1],p[2]]) if not res[0]: print(res)
return (False, type(obj).__name__, "Exception raised", e) return (True, type(obj).__name__, vals) ## To Do: Add value checks # ControlParameters # from pyndn.name import Name from pyndn.control_parameters import ControlParameters from pyndn.forwarding_flags import ForwardingFlags for p in [("name", Name("yes"), Name("another")), ("faceId", 32, None), ("localControlFeature", 1, None), ("origin", 2, 9), ("cost", 1, None), ("forwardingFlags", ForwardingFlags(), ForwardingFlags()), ("expirationPeriod", 1000.1, None)]: res = testPropertyRW(ControlParameters(), p[0], [p[1], p[2]]) if not res[0]: print(res) # Data # from pyndn.data import Data from pyndn.name import Name from pyndn.meta_info import MetaInfo from pyndn.signature import Signature from pyndn.util.blob import Blob # We do not test the signature property because clone is not yet implemented for it. # for p in [("name", Name("yes"), Name("another")), ("metaInfo", MetaInfo(), MetaInfo()),
def registerPrefix(self, prefix, onRegisterFailed, onRegisterSuccess=None, onDataNotFound=None, flags=None, wireFormat=None): """ Call registerPrefix on the Face given to the constructor so that this MemoryContentCache will answer interests whose name has the prefix. Alternatively, if the Face's registerPrefix has already been called, then you can call this object's setInterestFilter. :param Name prefix: The Name for the prefix to register. This copies the Name. :param onRegisterFailed: If this fails to register the prefix for any reason, this calls onRegisterFailed(prefix) where prefix is the prefix given to registerPrefix. NOTE: The library will log any exceptions raised by this callback, but for better error handling the callback should catch and properly handle any exceptions. :type onRegisterFailed: function object :param onRegisterSuccess: (optional) This calls onRegisterSuccess[0](prefix, registeredPrefixId) when this receives a success message from the forwarder. If onRegisterSuccess is omitted or [None], this does not use it. (As a special case, this optional parameter is supplied as a list of one function object, instead of just a function object, in order to detect when it is used instead of the following optional onDataNotFound function object.) NOTE: The library will log any exceptions raised by this callback, but for better error handling the callback should catch and properly handle any exceptions. :type onRegisterSuccess: list of one function object :param onDataNotFound: (optional) If a data packet for an interest is not found in the cache, this forwards the interest by calling onDataNotFound(prefix, interest, face, interestFilterId, filter). Your callback can find the Data packet for the interest and call face.putData(data). If your callback cannot find the Data packet, it can optionally call storePendingInterest(interest, face) to store the pending interest in this object to be satisfied by a later call to add(data). If you want to automatically store all pending interests, you can simply use getStorePendingInterest() for onDataNotFound. If onDataNotFound is omitted or None, this does not use it. NOTE: The library will log any exceptions raised by this callback, but for better error handling the callback should catch and properly handle any exceptions. :type onDataNotFound: function object :param ForwardingFlags flags: (optional) See Face.registerPrefix. :param wireFormat: (optional) See Face.registerPrefix. :type wireFormat: A subclass of WireFormat """ arg3 = onRegisterSuccess arg4 = onDataNotFound arg5 = flags arg6 = wireFormat # arg3, arg4, arg5, arg6 may be: # [OnRegisterSuccess], OnDataNotFound, ForwardingFlags, WireFormat # [OnRegisterSuccess], OnDataNotFound, ForwardingFlags, None # [OnRegisterSuccess], OnDataNotFound, WireFormat, None # [OnRegisterSuccess], OnDataNotFound, None, None # [OnRegisterSuccess], ForwardingFlags, WireFormat, None # [OnRegisterSuccess], ForwardingFlags, None, None # [OnRegisterSuccess], WireFormat, None, None # [OnRegisterSuccess], None, None, None # OnDataNotFound, ForwardingFlags, WireFormat, None # OnDataNotFound, ForwardingFlags, None, None # OnDataNotFound, WireFormat, None, None # OnDataNotFound, None, None, None # ForwardingFlags, WireFormat, None, None # ForwardingFlags, None, None, None # WireFormat, None, None, None # None, None, None, None if type(arg3) is list and len(arg3) == 1: onRegisterSuccess = arg3[0] else: onRegisterSuccess = None if isinstance(arg3, collections.Callable): onDataNotFound = arg3 elif isinstance(arg4, collections.Callable): onDataNotFound = arg4 else: onDataNotFound = None if isinstance(arg3, ForwardingFlags): flags = arg3 elif isinstance(arg4, ForwardingFlags): flags = arg4 elif isinstance(arg5, ForwardingFlags): flags = arg5 else: flags = ForwardingFlags() if isinstance(arg3, WireFormat): wireFormat = arg3 elif isinstance(arg4, WireFormat): wireFormat = arg4 elif isinstance(arg5, WireFormat): wireFormat = arg5 elif isinstance(arg6, WireFormat): wireFormat = arg6 else: # Don't use a default argument since getDefaultWireFormat can change. wireFormat = WireFormat.getDefaultWireFormat() if onDataNotFound != None: self._onDataNotFoundForPrefix[prefix.toUri()] = onDataNotFound registeredPrefixId = self._face.registerPrefix(prefix, self._onInterest, onRegisterFailed, onRegisterSuccess, flags, wireFormat) self._registeredPrefixIdList.append(registeredPrefixId)
def __init__(self): self._action = None self._prefix = Name() self._faceId = None self._forwardingFlags = ForwardingFlags() self._freshnessPeriod = None
def decodeControlParameters(self, controlParameters, input): """ Decode input as an NDN-TLV ControlParameters and set the fields of the controlParameters object. :param ControlParameters controlParameters: The ControlParameters object whose fields are updated. :param input: The array with the bytes to decode. :type input: An array type with int elements """ controlParameters.clear() decoder = TlvDecoder(input) endOffset = decoder.readNestedTlvsStart( Tlv.ControlParameters_ControlParameters) # decode name if decoder.peekType(Tlv.Name, endOffset): name = Name() self._decodeName(name, decoder) controlParameters.setName(name) # decode face ID controlParameters.setFaceId( decoder.readOptionalNonNegativeIntegerTlv( Tlv.ControlParameters_FaceId, endOffset)) # decode URI if decoder.peekType(Tlv.ControlParameters_Uri, endOffset): uri = Blob( decoder.readOptionalBlobTlv(Tlv.ControlParameters_Uri, endOffset), False) controlParameters.setUri(str(uri)) # decode integers controlParameters.setLocalControlFeature( decoder.readOptionalNonNegativeIntegerTlv( Tlv.ControlParameters_LocalControlFeature, endOffset)) controlParameters.setOrigin( decoder.readOptionalNonNegativeIntegerTlv( Tlv.ControlParameters_Origin, endOffset)) controlParameters.setCost( decoder.readOptionalNonNegativeIntegerTlv( Tlv.ControlParameters_Cost, endOffset)) # set forwarding flags if decoder.peekType(Tlv.ControlParameters_Flags, endOffset): flags = ForwardingFlags() flags.setNfdForwardingFlags( decoder.readNonNegativeIntegerTlv(Tlv.ControlParameters_Flags)) controlParameters.setForwardingFlags(flags) # decode strategy if decoder.peekType(Tlv.ControlParameters_Strategy, endOffset): strategyEndOffset = decoder.readNestedTlvsStart( Tlv.ControlParameters_Strategy) self._decodeName(controlParameters.getStrategy(), decoder) decoder.finishNestedTlvs(strategyEndOffset) # decode expiration period controlParameters.setExpirationPeriod( decoder.readOptionalNonNegativeIntegerTlv( Tlv.ControlParameters_ExpirationPeriod, endOffset)) decoder.finishNestedTlvs(endOffset)