blob: 5cac6bcddc1f2296de0711b5b73b43f01e14a684 [file] [log] [blame]
/*
* @author: ucla-cs
* This class represents Face Instances
*/
var NetworkProtocol = { TCP:6, UDP:17};
var FaceInstance = function FaceInstance(
_Action,
_PublisherPublicKeyDigest,
_FaceID,
_IPProto,
_Host,
_Port,
_MulticastInterface,
_MulticastTTL,
_FreshnessSeconds){
this.Action = _Action;
this.PublisherPublicKeyDigest = _PublisherPublicKeyDigest;
this.FaceID = _FaceID;
this.IPProto = _IPProto;
this.Host = _Host;
this.Port = _Port;
this.MulticastInterface =_MulticastInterface;
this.MulticastTTL =_MulticastTTL;
this.FreshnessSeconds = _FreshnessSeconds;
//Action ::= ("newface" | "destroyface" | "queryface")
//PublisherPublicKeyDigest ::= SHA-256 digest
//FaceID ::= nonNegativeInteger
//IPProto ::= nonNegativeInteger [IANA protocol number, 6=TCP, 17=UDP]
//Host ::= textual representation of numeric IPv4 or IPv6 address
//Port ::= nonNegativeInteger [1..65535]
//MulticastInterface ::= textual representation of numeric IPv4 or IPv6 address
//MulticastTTL ::= nonNegativeInteger [1..255]
//FreshnessSeconds ::= nonNegativeInteger
};
/**
* Used by NetworkObject to decode the object from a network stream.
* @see org.ccnx.ccn.impl.encoding.XMLEncodable
*/
FaceInstance.prototype.decode = function(//XMLDecoder
decoder) {
decoder.readStartElement(this.getElementLabel());
if (decoder.peekStartElement(CCNProtocolDTags.Action)) {
this.Action = decoder.readUTF8Element(CCNProtocolDTags.Action);
}
if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) {
this.PublisherPublicKeyDigest = new PublisherPublicKeyDigest();
this.PublisherPublicKeyDigest.decode(decoder);
}
if (decoder.peekStartElement(CCNProtocolDTags.FaceID)) {
this.FaceID = decoder.readIntegerElement(CCNProtocolDTags.FaceID);
}
if (decoder.peekStartElement(CCNProtocolDTags.IPProto)) {
//int
var pI = decoder.readIntegerElement(CCNProtocolDTags.IPProto);
this.IPProto = null;
if (NetworkProtocol.TCP == pI) {
this.IPProto = NetworkProtocol.TCP;
} else if (NetworkProtocol.UDP == pI) {
this.IPProto = NetworkProtocol.UDP;
} else {
throw new Exception("FaceInstance.decoder. Invalid " +
CCNProtocolDTags.tagToString(CCNProtocolDTags.IPProto) + " field: " + pI);
}
}
if (decoder.peekStartElement(CCNProtocolDTags.Host)) {
this.Host = decoder.readUTF8Element(CCNProtocolDTags.Host);
}
if (decoder.peekStartElement(CCNProtocolDTags.Port)) {
this.Port = decoder.readIntegerElement(CCNProtocolDTags.Port);
}
if (decoder.peekStartElement(CCNProtocolDTags.MulticastInterface)) {
this.MulticastInterface = decoder.readUTF8Element(CCNProtocolDTags.MulticastInterface);
}
if (decoder.peekStartElement(CCNProtocolDTags.MulticastTTL)) {
this.MulticastTTL = decoder.readIntegerElement(CCNProtocolDTags.MulticastTTL);
}
if (decoder.peekStartElement(CCNProtocolDTags.FreshnessSeconds)) {
this.FreshnessSeconds = decoder.readIntegerElement(CCNProtocolDTags.FreshnessSeconds);
}
decoder.readEndElement();
}
/**
* Used by NetworkObject to encode the object to a network stream.
* @see org.ccnx.ccn.impl.encoding.XMLEncodable
*/
FaceInstance.prototype.encode = function(//XMLEncoder
encoder){
//if (!this.validate()) {
//throw new Exception("Cannot encode : field values missing.");
//throw new Exception("")
//}
encoder.writeStartElement(this.getElementLabel());
if (null != this.Action && this.Action.length != 0)
encoder.writeElement(CCNProtocolDTags.Action, this.Action);
if (null != this.PublisherPublicKeyDigest) {
this.PublisherPublicKeyDigest.encode(encoder);
}
if (null != this.FaceID) {
encoder.writeElement(CCNProtocolDTags.FaceID, this.FaceID);
}
if (null != this.IPProto) {
//encoder.writeElement(CCNProtocolDTags.IPProto, this.IpProto.value());
encoder.writeElement(CCNProtocolDTags.IPProto, this.IPProto);
}
if (null != this.Host && this.Host.length != 0) {
encoder.writeElement(CCNProtocolDTags.Host, this.Host);
}
if (null != this.Port) {
encoder.writeElement(CCNProtocolDTags.Port, this.Port);
}
if (null != this.MulticastInterface && this.MulticastInterface.length != 0) {
encoder.writeElement(CCNProtocolDTags.MulticastInterface, this.MulticastInterface);
}
if (null != this.MulticastTTL) {
encoder.writeElement(CCNProtocolDTags.MulticastTTL, this.MulticastTTL);
}
if (null != this.FreshnessSeconds) {
encoder.writeElement(CCNProtocolDTags.FreshnessSeconds, this.FreshnessSeconds);
}
encoder.writeEndElement();
}
FaceInstance.prototype.getElementLabel= function(){return CCNProtocolDTags.FaceInstance;};