blob: dfc44a6955fc22d994ed8f3bc0bc9119562e6236 [file] [log] [blame]
Meki Cherkaouif3d8f692012-05-18 15:44:28 -07001/*
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.
45 * @see org.ccnx.ccn.impl.encoding.XMLEncodable
46 */
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070047FaceInstance.prototype.from_ccnb = function(//XMLDecoder
Meki Cherkaoui09c15572012-04-28 14:42:18 -070048 decoder) {
49
50 decoder.readStartElement(this.getElementLabel());
51
52 if (decoder.peekStartElement(CCNProtocolDTags.Action)) {
53
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070054 this.action = decoder.readUTF8Element(CCNProtocolDTags.Action);
Meki Cherkaoui09c15572012-04-28 14:42:18 -070055
56 }
57 if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) {
58
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070059 this.publisherPublicKeyDigest = new PublisherPublicKeyDigest();
60 this.publisherPublicKeyDigest.from_ccnb(decoder);
Meki Cherkaoui09c15572012-04-28 14:42:18 -070061
62 }
63 if (decoder.peekStartElement(CCNProtocolDTags.FaceID)) {
64
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070065 this.faceID = decoder.readIntegerElement(CCNProtocolDTags.FaceID);
Meki Cherkaoui09c15572012-04-28 14:42:18 -070066
67 }
68 if (decoder.peekStartElement(CCNProtocolDTags.IPProto)) {
69
70 //int
71 var pI = decoder.readIntegerElement(CCNProtocolDTags.IPProto);
72
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070073 this.ipProto = null;
Meki Cherkaoui09c15572012-04-28 14:42:18 -070074
Meki Cherkaoui8f173612012-06-06 01:05:40 -070075 if (NetworkProtocol.TCP == pI) {
Meki Cherkaoui09c15572012-04-28 14:42:18 -070076
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070077 this.ipProto = NetworkProtocol.TCP;
Meki Cherkaoui09c15572012-04-28 14:42:18 -070078
Meki Cherkaoui8f173612012-06-06 01:05:40 -070079 } else if (NetworkProtocol.UDP == pI) {
Meki Cherkaoui09c15572012-04-28 14:42:18 -070080
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070081 this.ipProto = NetworkProtocol.UDP;
Meki Cherkaoui09c15572012-04-28 14:42:18 -070082
83 } else {
84
Jeff Thompson34a2ec02012-09-29 21:47:05 -070085 throw new Error("FaceInstance.decoder. Invalid " +
Meki Cherkaoui09c15572012-04-28 14:42:18 -070086 CCNProtocolDTags.tagToString(CCNProtocolDTags.IPProto) + " field: " + pI);
87
88 }
89 }
90
91 if (decoder.peekStartElement(CCNProtocolDTags.Host)) {
92
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070093 this.host = decoder.readUTF8Element(CCNProtocolDTags.Host);
Meki Cherkaoui09c15572012-04-28 14:42:18 -070094
95 }
96
97 if (decoder.peekStartElement(CCNProtocolDTags.Port)) {
98 this.Port = decoder.readIntegerElement(CCNProtocolDTags.Port);
99 }
100
101 if (decoder.peekStartElement(CCNProtocolDTags.MulticastInterface)) {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700102 this.multicastInterface = decoder.readUTF8Element(CCNProtocolDTags.MulticastInterface);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700103 }
104
105 if (decoder.peekStartElement(CCNProtocolDTags.MulticastTTL)) {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700106 this.multicastTTL = decoder.readIntegerElement(CCNProtocolDTags.MulticastTTL);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700107 }
108
109 if (decoder.peekStartElement(CCNProtocolDTags.FreshnessSeconds)) {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700110 this.freshnessSeconds = decoder.readIntegerElement(CCNProtocolDTags.FreshnessSeconds);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700111 }
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700112 decoder.readEndElement();
113}
114
115/**
116 * Used by NetworkObject to encode the object to a network stream.
117 * @see org.ccnx.ccn.impl.encoding.XMLEncodable
118 */
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700119FaceInstance.prototype.to_ccnb = function(//XMLEncoder
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700120 encoder){
121
122 //if (!this.validate()) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -0700123 //throw new Error("Cannot encode : field values missing.");
124 //throw new Error("")
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700125 //}
126 encoder.writeStartElement(this.getElementLabel());
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700127
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700128 if (null != this.action && this.action.length != 0)
129 encoder.writeElement(CCNProtocolDTags.Action, this.action);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700130
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700131 if (null != this.publisherPublicKeyDigest) {
132 this.publisherPublicKeyDigest.to_ccnb(encoder);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700133 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700134 if (null != this.faceID) {
135 encoder.writeElement(CCNProtocolDTags.FaceID, this.faceID);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700136 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700137 if (null != this.ipProto) {
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700138 //encoder.writeElement(CCNProtocolDTags.IPProto, this.IpProto.value());
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700139 encoder.writeElement(CCNProtocolDTags.IPProto, this.ipProto);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700140 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700141 if (null != this.host && this.host.length != 0) {
142 encoder.writeElement(CCNProtocolDTags.Host, this.host);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700143 }
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700144 if (null != this.Port) {
145 encoder.writeElement(CCNProtocolDTags.Port, this.Port);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700146 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700147 if (null != this.multicastInterface && this.multicastInterface.length != 0) {
148 encoder.writeElement(CCNProtocolDTags.MulticastInterface, this.multicastInterface);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700149 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700150 if (null != this.multicastTTL) {
151 encoder.writeElement(CCNProtocolDTags.MulticastTTL, this.multicastTTL);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700152 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700153 if (null != this.freshnessSeconds) {
154 encoder.writeElement(CCNProtocolDTags.FreshnessSeconds, this.freshnessSeconds);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700155 }
156 encoder.writeEndElement();
157}
158
159
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700160FaceInstance.prototype.getElementLabel= function(){return CCNProtocolDTags.FaceInstance;};
161