/// <summary> /// Create a new interest as a deep copy of the given interest. /// </summary> /// /// <param name="interest">The interest to copy.</param> public Interest(Interest interest) { this.name_ = new ChangeCounter(new Name()); this.minSuffixComponents_ = -1; this.maxSuffixComponents_ = -1; this.keyLocator_ = new ChangeCounter( new KeyLocator()); this.exclude_ = new ChangeCounter(new Exclude()); this.childSelector_ = -1; this.mustBeFresh_ = true; this.interestLifetimeMilliseconds_ = -1; this.nonce_ = new Blob(); this.getNonceChangeCount_ = 0; this.lpPacket_ = null; this.linkWireEncoding_ = new Blob(); this.linkWireEncodingFormat_ = null; this.forwardingHint_ = new ChangeCounter( new DelegationSet()); this.parameters_ = new Blob(); this.link_ = new ChangeCounter(null); this.selectedDelegationIndex_ = -1; this.defaultWireEncoding_ = new SignedBlob(); this.getDefaultWireEncodingChangeCount_ = 0; this.changeCount_ = 0; name_.set(new Name(interest.getName())); minSuffixComponents_ = interest.minSuffixComponents_; maxSuffixComponents_ = interest.maxSuffixComponents_; keyLocator_.set(new KeyLocator(interest.getKeyLocator())); exclude_.set(new Exclude(interest.getExclude())); childSelector_ = interest.childSelector_; mustBeFresh_ = interest.mustBeFresh_; interestLifetimeMilliseconds_ = interest.interestLifetimeMilliseconds_; nonce_ = interest.getNonce(); forwardingHint_.set(new DelegationSet(interest.getForwardingHint())); parameters_ = interest.parameters_; linkWireEncoding_ = interest.linkWireEncoding_; linkWireEncodingFormat_ = interest.linkWireEncodingFormat_; if (interest.link_.get() != null) { link_.set(new Link((Link)interest.link_.get())); } selectedDelegationIndex_ = interest.selectedDelegationIndex_; setDefaultWireEncoding(interest.getDefaultWireEncoding(), interest.defaultWireEncodingFormat_); }
static void dumpInterest(Interest interest) { Console.Out.WriteLine("name: " + interest.getName().toUri()); Console.Out.WriteLine("minSuffixComponents: " + (interest.getMinSuffixComponents() >= 0 ? "" + interest.getMinSuffixComponents() : "<none>")); Console.Out.WriteLine("maxSuffixComponents: " + (interest.getMaxSuffixComponents() >= 0 ? "" + interest.getMaxSuffixComponents() : "<none>")); Console.Out.Write("keyLocator: "); if (interest.getKeyLocator().getType() == KeyLocatorType.NONE) Console.Out.WriteLine("<none>"); else if (interest.getKeyLocator().getType() ==KeyLocatorType.KEY_LOCATOR_DIGEST) Console.Out.WriteLine("KeyLocatorDigest: " + interest.getKeyLocator().getKeyData().toHex()); else if (interest.getKeyLocator().getType() == KeyLocatorType.KEYNAME) Console.Out.WriteLine("KeyName: " + interest.getKeyLocator().getKeyName().toUri()); else Console.Out.WriteLine("<unrecognized ndn_KeyLocatorType>"); Console.Out.WriteLine ("exclude: " + (interest.getExclude().size() > 0 ? interest.getExclude().toUri() : "<none>")); Console.Out.WriteLine("lifetimeMilliseconds: " + (interest.getInterestLifetimeMilliseconds() >= 0 ? "" + interest.getInterestLifetimeMilliseconds() : "<none>")); Console.Out.WriteLine("childSelector: " + (interest.getChildSelector() >= 0 ? "" + interest.getChildSelector() : "<none>")); Console.Out.WriteLine("mustBeFresh: " + interest.getMustBeFresh()); Console.Out.WriteLine("nonce: " + (interest.getNonce().size() > 0 ? "" + interest.getNonce().toHex() : "<none>")); }
/// <summary> /// Create a new interest as a deep copy of the given interest. /// </summary> /// /// <param name="interest">The interest to copy.</param> public Interest(Interest interest) { this.name_ = new ChangeCounter(new Name()); this.minSuffixComponents_ = -1; this.maxSuffixComponents_ = -1; this.keyLocator_ = new ChangeCounter( new KeyLocator()); this.exclude_ = new ChangeCounter(new Exclude()); this.childSelector_ = -1; this.mustBeFresh_ = true; this.interestLifetimeMilliseconds_ = -1; this.nonce_ = new Blob(); this.getNonceChangeCount_ = 0; this.lpPacket_ = null; this.linkWireEncoding_ = new Blob(); this.linkWireEncodingFormat_ = null; this.link_ = new ChangeCounter(null); this.selectedDelegationIndex_ = -1; this.defaultWireEncoding_ = new SignedBlob(); this.getDefaultWireEncodingChangeCount_ = 0; this.changeCount_ = 0; name_.set(new Name(interest.getName())); minSuffixComponents_ = interest.minSuffixComponents_; maxSuffixComponents_ = interest.maxSuffixComponents_; keyLocator_.set(new KeyLocator(interest.getKeyLocator())); exclude_.set(new Exclude(interest.getExclude())); childSelector_ = interest.childSelector_; mustBeFresh_ = interest.mustBeFresh_; interestLifetimeMilliseconds_ = interest.interestLifetimeMilliseconds_; nonce_ = interest.getNonce(); linkWireEncoding_ = interest.linkWireEncoding_; linkWireEncodingFormat_ = interest.linkWireEncodingFormat_; if (interest.link_.get() != null) link_.set(new Link((Link) interest.link_.get())); selectedDelegationIndex_ = interest.selectedDelegationIndex_; setDefaultWireEncoding(interest.getDefaultWireEncoding(), interest.defaultWireEncodingFormat_); }
/// <summary> /// Encode interest using NDN-TLV and return the encoding. /// </summary> /// /// <param name="interest">The Interest object to encode.</param> /// <param name="signedPortionBeginOffset">name component and ends just before the final name component (which is assumed to be a signature for a signed interest).</param> /// <param name="signedPortionEndOffset">name component and ends just before the final name component (which is assumed to be a signature for a signed interest).</param> /// <returns>A Blob containing the encoding.</returns> public override Blob encodeInterest(Interest interest, int[] signedPortionBeginOffset, int[] signedPortionEndOffset) { TlvEncoder encoder = new TlvEncoder(); int saveLength = encoder.getLength(); // Encode backwards. encoder.writeOptionalNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.SelectedDelegation, interest.getSelectedDelegationIndex()); try { Blob linkWireEncoding = interest.getLinkWireEncoding(this); if (!linkWireEncoding.isNull()) // Encode the entire link as is. encoder.writeBuffer(linkWireEncoding.buf()); } catch (EncodingException ex) { throw new Exception(ex.Message); } encoder.writeOptionalNonNegativeIntegerTlvFromDouble( net.named_data.jndn.encoding.tlv.Tlv.InterestLifetime, interest.getInterestLifetimeMilliseconds()); // Encode the Nonce as 4 bytes. if (interest.getNonce().size() == 0) { // This is the most common case. Generate a nonce. ByteBuffer nonce = ILOG.J2CsMapping.NIO.ByteBuffer.allocate(4); random_.nextBytes(nonce.array()); encoder.writeBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.Nonce, nonce); } else if (interest.getNonce().size() < 4) { ByteBuffer nonce_0 = ILOG.J2CsMapping.NIO.ByteBuffer.allocate(4); // Copy existing nonce bytes. nonce_0.put(interest.getNonce().buf()); // Generate random bytes for remaining bytes in the nonce. for (int i = 0; i < 4 - interest.getNonce().size(); ++i) nonce_0.put((byte) random_.Next()); nonce_0.flip(); encoder.writeBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.Nonce, nonce_0); } else if (interest.getNonce().size() == 4) // Use the nonce as-is. encoder.writeBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.Nonce, interest.getNonce().buf()); else { // Truncate. ByteBuffer nonce_1 = interest.getNonce().buf(); // buf() returns a new ByteBuffer, so we can change its limit. nonce_1.limit(nonce_1.position() + 4); encoder.writeBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.Nonce, nonce_1); } encodeSelectors(interest, encoder); int[] tempSignedPortionBeginOffset = new int[1]; int[] tempSignedPortionEndOffset = new int[1]; encodeName(interest.getName(), tempSignedPortionBeginOffset, tempSignedPortionEndOffset, encoder); int signedPortionBeginOffsetFromBack = encoder.getLength() - tempSignedPortionBeginOffset[0]; int signedPortionEndOffsetFromBack = encoder.getLength() - tempSignedPortionEndOffset[0]; encoder.writeTypeAndLength(net.named_data.jndn.encoding.tlv.Tlv.Interest, encoder.getLength() - saveLength); signedPortionBeginOffset[0] = encoder.getLength() - signedPortionBeginOffsetFromBack; signedPortionEndOffset[0] = encoder.getLength() - signedPortionEndOffsetFromBack; return new Blob(encoder.getOutput(), false); }
private static ArrayList dumpInterest(Interest interest) { ArrayList result = new ArrayList(); ILOG.J2CsMapping.Collections.Collections.Add(result,dump("name:", interest.getName().toUri())); ILOG.J2CsMapping.Collections.Collections.Add(result,dump( "minSuffixComponents:", (interest.getMinSuffixComponents() >= 0) ? (Object) (interest.getMinSuffixComponents()) : (Object) ("<none>"))); ILOG.J2CsMapping.Collections.Collections.Add(result,dump( "maxSuffixComponents:", (interest.getMaxSuffixComponents() >= 0) ? (Object) (interest.getMaxSuffixComponents()) : (Object) ("<none>"))); if (interest.getKeyLocator().getType() != net.named_data.jndn.KeyLocatorType.NONE) { if (interest.getKeyLocator().getType() == net.named_data.jndn.KeyLocatorType.KEY_LOCATOR_DIGEST) ILOG.J2CsMapping.Collections.Collections.Add(result,dump("keyLocator: KeyLocatorDigest:", interest .getKeyLocator().getKeyData().toHex())); else if (interest.getKeyLocator().getType() == net.named_data.jndn.KeyLocatorType.KEYNAME) ILOG.J2CsMapping.Collections.Collections.Add(result,dump("keyLocator: KeyName:", interest .getKeyLocator().getKeyName().toUri())); else ILOG.J2CsMapping.Collections.Collections.Add(result,dump("keyLocator: <unrecognized KeyLocatorType")); } else ILOG.J2CsMapping.Collections.Collections.Add(result,dump("keyLocator: <none>")); ILOG.J2CsMapping.Collections.Collections.Add(result,dump("exclude:", (interest.getExclude().size() > 0) ? interest .getExclude().toUri() : "<none>")); ILOG.J2CsMapping.Collections.Collections.Add(result,dump("childSelector:", (interest.getChildSelector() >= 0) ? (Object) (interest.getChildSelector()) : (Object) ("<none>"))); ILOG.J2CsMapping.Collections.Collections.Add(result,dump("mustBeFresh:", (interest.getMustBeFresh()) ? "true" : "false")); ILOG.J2CsMapping.Collections.Collections.Add(result,dump("nonce:", (interest.getNonce().size() == 0) ? "<none>" : interest.getNonce().toHex())); ILOG.J2CsMapping.Collections.Collections.Add(result,dump("lifetimeMilliseconds:", (interest.getInterestLifetimeMilliseconds() < 0) ? "<none>" : "" + (long) interest.getInterestLifetimeMilliseconds())); return result; }
public void testSetRemovesNonce() { // Ensure that changing a value on an interest clears the nonce. Assert.AssertFalse(referenceInterest.getNonce().isNull()); Interest interest = new Interest(referenceInterest); // Change a child object. interest.getExclude().clear(); Assert.AssertTrue("Interest should not have a nonce after changing fields", interest.getNonce().isNull()); }
public void testRefreshNonce() { Interest interest = new Interest(referenceInterest); Blob oldNonce = interest.getNonce(); Assert.AssertEquals(4, oldNonce.size()); interest.refreshNonce(); Assert.AssertEquals("The refreshed nonce should be the same size", oldNonce.size(), interest.getNonce().size()); Assert.AssertFalse("The refreshed nonce should be different", interest .getNonce().equals(oldNonce)); }