blob: 3d91de5230942c78029925fcb708d8496ac1a231 [file] [log] [blame]
/**
* @author: Meki Cheraoui
* See COPYING for copyright and distribution information.
* This class represents Interest Objects
*/
// _interestLifetime is in milliseconds.
var Interest = function Interest(_name,_faceInstance,_minSuffixComponents,_maxSuffixComponents,_publisherPublicKeyDigest, _exclude, _childSelector,_answerOriginKind,_scope,_interestLifetime,_nonce){
this.name = _name;
this.faceInstance = _faceInstance;
this.maxSuffixComponents = _maxSuffixComponents;
this.minSuffixComponents = _minSuffixComponents;
this.publisherPublicKeyDigest = _publisherPublicKeyDigest;
this.exclude = _exclude;
this.childSelector = _childSelector;
this.answerOriginKind = _answerOriginKind;
this.scope = _scope;
this.interestLifetime = _interestLifetime; // milli seconds
this.nonce = _nonce;
};
Interest.RECURSIVE_POSTFIX = "*";
Interest.CHILD_SELECTOR_LEFT = 0;
Interest.CHILD_SELECTOR_RIGHT = 1;
Interest.ANSWER_CONTENT_STORE = 1;
Interest.ANSWER_GENERATED = 2;
Interest.ANSWER_STALE = 4; // Stale answer OK
Interest.MARK_STALE = 16; // Must have scope 0. Michael calls this a "hack"
Interest.DEFAULT_ANSWER_ORIGIN_KIND = Interest.ANSWER_CONTENT_STORE | Interest.ANSWER_GENERATED;
Interest.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) {
decoder.readStartElement(CCNProtocolDTags.Interest);
this.name = new Name();
this.name.from_ccnb(decoder);
if (decoder.peekStartElement(CCNProtocolDTags.MinSuffixComponents))
this.minSuffixComponents = decoder.readIntegerElement(CCNProtocolDTags.MinSuffixComponents);
if (decoder.peekStartElement(CCNProtocolDTags.MaxSuffixComponents))
this.maxSuffixComponents = decoder.readIntegerElement(CCNProtocolDTags.MaxSuffixComponents);
if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) {
this.publisherPublicKeyDigest = new PublisherPublicKeyDigest();
this.publisherPublicKeyDigest.from_ccnb(decoder);
}
if (decoder.peekStartElement(CCNProtocolDTags.Exclude)) {
this.exclude = new Exclude();
this.exclude.from_ccnb(decoder);
}
if (decoder.peekStartElement(CCNProtocolDTags.ChildSelector))
this.childSelector = decoder.readIntegerElement(CCNProtocolDTags.ChildSelector);
if (decoder.peekStartElement(CCNProtocolDTags.AnswerOriginKind))
this.answerOriginKind = decoder.readIntegerElement(CCNProtocolDTags.AnswerOriginKind);
if (decoder.peekStartElement(CCNProtocolDTags.Scope))
this.scope = decoder.readIntegerElement(CCNProtocolDTags.Scope);
if (decoder.peekStartElement(CCNProtocolDTags.InterestLifetime))
this.interestLifetime = 1000.0 * DataUtils.bigEndianToUnsignedInt
(decoder.readBinaryElement(CCNProtocolDTags.InterestLifetime)) / 4096;
if (decoder.peekStartElement(CCNProtocolDTags.Nonce))
this.nonce = decoder.readBinaryElement(CCNProtocolDTags.Nonce);
decoder.readEndElement();
};
Interest.prototype.to_ccnb = function(/*XMLEncoder*/ encoder){
//Could check if name is present
encoder.writeStartElement(CCNProtocolDTags.Interest);
this.name.to_ccnb(encoder);
if (null != this.minSuffixComponents)
encoder.writeElement(CCNProtocolDTags.MinSuffixComponents, this.minSuffixComponents);
if (null != this.maxSuffixComponents)
encoder.writeElement(CCNProtocolDTags.MaxSuffixComponents, this.maxSuffixComponents);
if (null != this.publisherPublicKeyDigest)
this.publisherPublicKeyDigest.to_ccnb(encoder);
if (null != this.exclude)
this.exclude.to_ccnb(encoder);
if (null != this.childSelector)
encoder.writeElement(CCNProtocolDTags.ChildSelector, this.childSelector);
if (this.DEFAULT_ANSWER_ORIGIN_KIND != this.answerOriginKind && this.answerOriginKind!=null)
encoder.writeElement(CCNProtocolDTags.AnswerOriginKind, this.answerOriginKind);
if (null != this.scope)
encoder.writeElement(CCNProtocolDTags.Scope, this.scope);
if (null != this.interestLifetime)
encoder.writeElement(CCNProtocolDTags.InterestLifetime,
DataUtils.nonNegativeIntToBigEndian((this.interestLifetime / 1000.0) * 4096));
if (null != this.nonce)
encoder.writeElement(CCNProtocolDTags.Nonce, this.nonce);
encoder.writeEndElement();
};
Interest.prototype.matches_name = function(/*Name*/ name) {
return this.name.match(name);
}
/*
* Handle the interest Exclude element.
* _values is an array where each element is either Uint8Array component or Exclude.ANY.
*/
var Exclude = function Exclude(_values) {
this.values = (_values || []);
}
Exclude.ANY = "*";
Exclude.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) {
decoder.readStartElement(CCNProtocolDTags.Exclude);
while (true) {
if (decoder.peekStartElement(CCNProtocolDTags.Component))
this.values.push(decoder.readBinaryElement(CCNProtocolDTags.Component));
else if (decoder.peekStartElement(CCNProtocolDTags.Any)) {
decoder.readStartElement(CCNProtocolDTags.Any);
decoder.readEndElement();
this.values.push(Exclude.ANY);
}
else if (decoder.peekStartElement(CCNProtocolDTags.Bloom)) {
// Skip the Bloom and treat it as Any.
decoder.readBinaryElement(CCNProtocolDTags.Bloom);
this.values.push(Exclude.ANY);
}
else
break;
}
decoder.readEndElement();
};
Exclude.prototype.to_ccnb = function(/*XMLEncoder*/ encoder) {
if (this.values == null || this.values.length == 0)
return;
encoder.writeStartElement(CCNProtocolDTags.Exclude);
// TODO: Do we want to order the components (except for ANY)?
for (var i = 0; i < this.values.length; ++i) {
if (this.values[i] == Exclude.ANY) {
encoder.writeStartElement(CCNProtocolDTags.Any);
encoder.writeEndElement();
}
else
encoder.writeElement(CCNProtocolDTags.Component, this.values[i]);
}
encoder.writeEndElement();
};
// Return a string with elements separated by "," and Exclude.ANY shown as "*".
Exclude.prototype.to_uri = function() {
if (this.values == null || this.values.length == 0)
return "";
var result = "";
for (var i = 0; i < this.values.length; ++i) {
if (i > 0)
result += ",";
if (this.values[i] == Exclude.ANY)
result += "*";
else
result += Name.toEscapedString(this.values[i]);
}
return result;
};