blob: 91b8780e37cfd10d07e86353a97b9a32860a6f12 [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
9var FaceInstance = function FaceInstance(
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070010 _action,
11 _publisherPublicKeyDigest,
12 _faceID,
13 _ipProto,
14 _host,
15 _port,
16 _multicastInterface,
17 _multicastTTL,
18 _freshnessSeconds){
Meki Cherkaoui09c15572012-04-28 14:42:18 -070019
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070020
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070021 this.action = _action;
22 this.publisherPublicKeyDigest = _publisherPublicKeyDigest;
23 this.faceID = _faceID;
24 this.ipProto = _ipProto;
25 this.host = _host;
26 this.Port = _port;
27 this.multicastInterface =_multicastInterface;
28 this.multicastTTL =_multicastTTL;
29 this.freshnessSeconds = _freshnessSeconds;
Meki Cherkaoui09c15572012-04-28 14:42:18 -070030
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070031 //action ::= ("newface" | "destroyface" | "queryface")
32 //publisherPublicKeyDigest ::= SHA-256 digest
33 //faceID ::= nonNegativeInteger
34 //ipProto ::= nonNegativeInteger [IANA protocol number, 6=TCP, 17=UDP]
Meki Cherkaoui09c15572012-04-28 14:42:18 -070035 //Host ::= textual representation of numeric IPv4 or IPv6 address
36 //Port ::= nonNegativeInteger [1..65535]
37 //MulticastInterface ::= textual representation of numeric IPv4 or IPv6 address
38 //MulticastTTL ::= nonNegativeInteger [1..255]
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070039 //freshnessSeconds ::= nonNegativeInteger
Meki Cherkaoui09c15572012-04-28 14:42:18 -070040
41};
42
43/**
44 * Used by NetworkObject to decode the object from a network stream.
Meki Cherkaoui09c15572012-04-28 14:42:18 -070045 */
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070046FaceInstance.prototype.from_ccnb = function(//XMLDecoder
Meki Cherkaoui09c15572012-04-28 14:42:18 -070047 decoder) {
48
49 decoder.readStartElement(this.getElementLabel());
50
51 if (decoder.peekStartElement(CCNProtocolDTags.Action)) {
52
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070053 this.action = decoder.readUTF8Element(CCNProtocolDTags.Action);
Meki Cherkaoui09c15572012-04-28 14:42:18 -070054
55 }
56 if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) {
57
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070058 this.publisherPublicKeyDigest = new PublisherPublicKeyDigest();
59 this.publisherPublicKeyDigest.from_ccnb(decoder);
Meki Cherkaoui09c15572012-04-28 14:42:18 -070060
61 }
62 if (decoder.peekStartElement(CCNProtocolDTags.FaceID)) {
63
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070064 this.faceID = decoder.readIntegerElement(CCNProtocolDTags.FaceID);
Meki Cherkaoui09c15572012-04-28 14:42:18 -070065
66 }
67 if (decoder.peekStartElement(CCNProtocolDTags.IPProto)) {
68
69 //int
70 var pI = decoder.readIntegerElement(CCNProtocolDTags.IPProto);
71
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070072 this.ipProto = null;
Meki Cherkaoui09c15572012-04-28 14:42:18 -070073
Meki Cherkaoui8f173612012-06-06 01:05:40 -070074 if (NetworkProtocol.TCP == pI) {
Meki Cherkaoui09c15572012-04-28 14:42:18 -070075
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070076 this.ipProto = NetworkProtocol.TCP;
Meki Cherkaoui09c15572012-04-28 14:42:18 -070077
Meki Cherkaoui8f173612012-06-06 01:05:40 -070078 } else if (NetworkProtocol.UDP == pI) {
Meki Cherkaoui09c15572012-04-28 14:42:18 -070079
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070080 this.ipProto = NetworkProtocol.UDP;
Meki Cherkaoui09c15572012-04-28 14:42:18 -070081
82 } else {
83
Jeff Thompson34a2ec02012-09-29 21:47:05 -070084 throw new Error("FaceInstance.decoder. Invalid " +
Meki Cherkaoui09c15572012-04-28 14:42:18 -070085 CCNProtocolDTags.tagToString(CCNProtocolDTags.IPProto) + " field: " + pI);
86
87 }
88 }
89
90 if (decoder.peekStartElement(CCNProtocolDTags.Host)) {
91
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070092 this.host = decoder.readUTF8Element(CCNProtocolDTags.Host);
Meki Cherkaoui09c15572012-04-28 14:42:18 -070093
94 }
95
96 if (decoder.peekStartElement(CCNProtocolDTags.Port)) {
97 this.Port = decoder.readIntegerElement(CCNProtocolDTags.Port);
98 }
99
100 if (decoder.peekStartElement(CCNProtocolDTags.MulticastInterface)) {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700101 this.multicastInterface = decoder.readUTF8Element(CCNProtocolDTags.MulticastInterface);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700102 }
103
104 if (decoder.peekStartElement(CCNProtocolDTags.MulticastTTL)) {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700105 this.multicastTTL = decoder.readIntegerElement(CCNProtocolDTags.MulticastTTL);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700106 }
107
108 if (decoder.peekStartElement(CCNProtocolDTags.FreshnessSeconds)) {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700109 this.freshnessSeconds = decoder.readIntegerElement(CCNProtocolDTags.FreshnessSeconds);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700110 }
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700111 decoder.readEndElement();
112}
113
114/**
115 * Used by NetworkObject to encode the object to a network stream.
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700116 */
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700117FaceInstance.prototype.to_ccnb = function(//XMLEncoder
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700118 encoder){
119
120 //if (!this.validate()) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -0700121 //throw new Error("Cannot encode : field values missing.");
122 //throw new Error("")
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700123 //}
124 encoder.writeStartElement(this.getElementLabel());
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700125
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700126 if (null != this.action && this.action.length != 0)
127 encoder.writeElement(CCNProtocolDTags.Action, this.action);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700128
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700129 if (null != this.publisherPublicKeyDigest) {
130 this.publisherPublicKeyDigest.to_ccnb(encoder);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700131 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700132 if (null != this.faceID) {
133 encoder.writeElement(CCNProtocolDTags.FaceID, this.faceID);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700134 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700135 if (null != this.ipProto) {
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700136 //encoder.writeElement(CCNProtocolDTags.IPProto, this.IpProto.value());
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700137 encoder.writeElement(CCNProtocolDTags.IPProto, this.ipProto);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700138 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700139 if (null != this.host && this.host.length != 0) {
140 encoder.writeElement(CCNProtocolDTags.Host, this.host);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700141 }
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700142 if (null != this.Port) {
143 encoder.writeElement(CCNProtocolDTags.Port, this.Port);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700144 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700145 if (null != this.multicastInterface && this.multicastInterface.length != 0) {
146 encoder.writeElement(CCNProtocolDTags.MulticastInterface, this.multicastInterface);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700147 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700148 if (null != this.multicastTTL) {
149 encoder.writeElement(CCNProtocolDTags.MulticastTTL, this.multicastTTL);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700150 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700151 if (null != this.freshnessSeconds) {
152 encoder.writeElement(CCNProtocolDTags.FreshnessSeconds, this.freshnessSeconds);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700153 }
154 encoder.writeEndElement();
155}
156
157
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700158FaceInstance.prototype.getElementLabel= function(){return CCNProtocolDTags.FaceInstance;};
159