blob: 21256d8639b91fa157031be201aa9e688b82c0c1 [file] [log] [blame]
Wentao Shangbd63e462012-12-03 16:19:33 -08001/**
Jeff Thompson146d7de2012-11-17 16:15:28 -08002 * @author: Meki Cheraoui
Jeff Thompson745026e2012-10-13 12:49:20 -07003 * See COPYING for copyright and distribution information.
Meki Cherkaouif3d8f692012-05-18 15:44:28 -07004 * This class represents Face Instances
5 */
Meki Cherkaoui09c15572012-04-28 14:42:18 -07006
Meki Cherkaoui8f173612012-06-06 01:05:40 -07007var NetworkProtocol = { TCP:6, UDP:17};
Meki Cherkaoui09c15572012-04-28 14:42:18 -07008
Jeff Thompson2b14c7e2013-07-29 15:09:56 -07009/**
10 * @constructor
11 */
12var FaceInstance = function FaceInstance(action, publisherPublicKeyDigest, faceID, ipProto, host, port, multicastInterface,
13 multicastTTL, freshnessSeconds) {
14 this.action = action;
15 this.publisherPublicKeyDigest = publisherPublicKeyDigest;
16 this.faceID = faceID;
17 this.ipProto = ipProto;
18 this.host = host;
19 this.Port = port;
20 this.multicastInterface =multicastInterface;
21 this.multicastTTL =multicastTTL;
22 this.freshnessSeconds = freshnessSeconds;
Meki Cherkaoui09c15572012-04-28 14:42:18 -070023};
24
25/**
26 * Used by NetworkObject to decode the object from a network stream.
Meki Cherkaoui09c15572012-04-28 14:42:18 -070027 */
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070028FaceInstance.prototype.from_ndnb = function(//XMLDecoder
Meki Cherkaoui09c15572012-04-28 14:42:18 -070029 decoder) {
30
31 decoder.readStartElement(this.getElementLabel());
32
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070033 if (decoder.peekStartElement(NDNProtocolDTags.Action)) {
Meki Cherkaoui09c15572012-04-28 14:42:18 -070034
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070035 this.action = decoder.readUTF8Element(NDNProtocolDTags.Action);
Meki Cherkaoui09c15572012-04-28 14:42:18 -070036
37 }
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070038 if (decoder.peekStartElement(NDNProtocolDTags.PublisherPublicKeyDigest)) {
Meki Cherkaoui09c15572012-04-28 14:42:18 -070039
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070040 this.publisherPublicKeyDigest = new PublisherPublicKeyDigest();
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070041 this.publisherPublicKeyDigest.from_ndnb(decoder);
Meki Cherkaoui09c15572012-04-28 14:42:18 -070042
43 }
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070044 if (decoder.peekStartElement(NDNProtocolDTags.FaceID)) {
Meki Cherkaoui09c15572012-04-28 14:42:18 -070045
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070046 this.faceID = decoder.readIntegerElement(NDNProtocolDTags.FaceID);
Meki Cherkaoui09c15572012-04-28 14:42:18 -070047
48 }
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070049 if (decoder.peekStartElement(NDNProtocolDTags.IPProto)) {
Meki Cherkaoui09c15572012-04-28 14:42:18 -070050
51 //int
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070052 var pI = decoder.readIntegerElement(NDNProtocolDTags.IPProto);
Meki Cherkaoui09c15572012-04-28 14:42:18 -070053
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070054 this.ipProto = null;
Meki Cherkaoui09c15572012-04-28 14:42:18 -070055
Meki Cherkaoui8f173612012-06-06 01:05:40 -070056 if (NetworkProtocol.TCP == pI) {
Meki Cherkaoui09c15572012-04-28 14:42:18 -070057
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070058 this.ipProto = NetworkProtocol.TCP;
Meki Cherkaoui09c15572012-04-28 14:42:18 -070059
Meki Cherkaoui8f173612012-06-06 01:05:40 -070060 } else if (NetworkProtocol.UDP == pI) {
Meki Cherkaoui09c15572012-04-28 14:42:18 -070061
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070062 this.ipProto = NetworkProtocol.UDP;
Meki Cherkaoui09c15572012-04-28 14:42:18 -070063
64 } else {
65
Jeff Thompson34a2ec02012-09-29 21:47:05 -070066 throw new Error("FaceInstance.decoder. Invalid " +
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070067 NDNProtocolDTags.tagToString(NDNProtocolDTags.IPProto) + " field: " + pI);
Meki Cherkaoui09c15572012-04-28 14:42:18 -070068
69 }
70 }
71
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070072 if (decoder.peekStartElement(NDNProtocolDTags.Host)) {
Meki Cherkaoui09c15572012-04-28 14:42:18 -070073
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070074 this.host = decoder.readUTF8Element(NDNProtocolDTags.Host);
Meki Cherkaoui09c15572012-04-28 14:42:18 -070075
76 }
77
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070078 if (decoder.peekStartElement(NDNProtocolDTags.Port)) {
79 this.Port = decoder.readIntegerElement(NDNProtocolDTags.Port);
Meki Cherkaoui09c15572012-04-28 14:42:18 -070080 }
81
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070082 if (decoder.peekStartElement(NDNProtocolDTags.MulticastInterface)) {
83 this.multicastInterface = decoder.readUTF8Element(NDNProtocolDTags.MulticastInterface);
Meki Cherkaoui09c15572012-04-28 14:42:18 -070084 }
85
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070086 if (decoder.peekStartElement(NDNProtocolDTags.MulticastTTL)) {
87 this.multicastTTL = decoder.readIntegerElement(NDNProtocolDTags.MulticastTTL);
Meki Cherkaoui09c15572012-04-28 14:42:18 -070088 }
89
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070090 if (decoder.peekStartElement(NDNProtocolDTags.FreshnessSeconds)) {
91 this.freshnessSeconds = decoder.readIntegerElement(NDNProtocolDTags.FreshnessSeconds);
Meki Cherkaoui09c15572012-04-28 14:42:18 -070092 }
Meki Cherkaoui09c15572012-04-28 14:42:18 -070093 decoder.readEndElement();
94}
95
96/**
97 * Used by NetworkObject to encode the object to a network stream.
Meki Cherkaoui09c15572012-04-28 14:42:18 -070098 */
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070099FaceInstance.prototype.to_ndnb = function(//XMLEncoder
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700100 encoder){
101
102 //if (!this.validate()) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -0700103 //throw new Error("Cannot encode : field values missing.");
104 //throw new Error("")
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700105 //}
106 encoder.writeStartElement(this.getElementLabel());
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700107
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700108 if (null != this.action && this.action.length != 0)
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700109 encoder.writeElement(NDNProtocolDTags.Action, this.action);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700110
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700111 if (null != this.publisherPublicKeyDigest) {
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700112 this.publisherPublicKeyDigest.to_ndnb(encoder);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700113 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700114 if (null != this.faceID) {
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700115 encoder.writeElement(NDNProtocolDTags.FaceID, this.faceID);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700116 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700117 if (null != this.ipProto) {
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700118 //encoder.writeElement(NDNProtocolDTags.IPProto, this.IpProto.value());
119 encoder.writeElement(NDNProtocolDTags.IPProto, this.ipProto);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700120 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700121 if (null != this.host && this.host.length != 0) {
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700122 encoder.writeElement(NDNProtocolDTags.Host, this.host);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700123 }
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700124 if (null != this.Port) {
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700125 encoder.writeElement(NDNProtocolDTags.Port, this.Port);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700126 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700127 if (null != this.multicastInterface && this.multicastInterface.length != 0) {
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700128 encoder.writeElement(NDNProtocolDTags.MulticastInterface, this.multicastInterface);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700129 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700130 if (null != this.multicastTTL) {
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700131 encoder.writeElement(NDNProtocolDTags.MulticastTTL, this.multicastTTL);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700132 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700133 if (null != this.freshnessSeconds) {
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700134 encoder.writeElement(NDNProtocolDTags.FreshnessSeconds, this.freshnessSeconds);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700135 }
136 encoder.writeEndElement();
137}
138
139
Jeff Thompson2b14c7e2013-07-29 15:09:56 -0700140FaceInstance.prototype.getElementLabel = function() {
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700141 return NDNProtocolDTags.FaceInstance;
Jeff Thompson2b14c7e2013-07-29 15:09:56 -0700142};
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700143