blob: f19d523c60d24de1288bdb2e6ca6a4bb77ca1343 [file] [log] [blame]
Meki Cherkaouif3d8f692012-05-18 15:44:28 -07001/*
2 * @author: ucla-cs
3 * This class represents Face Instances
4 */
Meki Cherkaoui09c15572012-04-28 14:42:18 -07005
Meki Cherkaoui8f173612012-06-06 01:05:40 -07006var NetworkProtocol = { TCP:6, UDP:17};
Meki Cherkaoui09c15572012-04-28 14:42:18 -07007
8var FaceInstance = function FaceInstance(
Jeff Thompsone85ff1d2012-09-29 21:21:57 -07009 _action,
10 _publisherPublicKeyDigest,
11 _faceID,
12 _ipProto,
13 _host,
14 _port,
15 _multicastInterface,
16 _multicastTTL,
17 _freshnessSeconds){
Meki Cherkaoui09c15572012-04-28 14:42:18 -070018
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070019
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070020 this.action = _action;
21 this.publisherPublicKeyDigest = _publisherPublicKeyDigest;
22 this.faceID = _faceID;
23 this.ipProto = _ipProto;
24 this.host = _host;
25 this.Port = _port;
26 this.multicastInterface =_multicastInterface;
27 this.multicastTTL =_multicastTTL;
28 this.freshnessSeconds = _freshnessSeconds;
Meki Cherkaoui09c15572012-04-28 14:42:18 -070029
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070030 //action ::= ("newface" | "destroyface" | "queryface")
31 //publisherPublicKeyDigest ::= SHA-256 digest
32 //faceID ::= nonNegativeInteger
33 //ipProto ::= nonNegativeInteger [IANA protocol number, 6=TCP, 17=UDP]
Meki Cherkaoui09c15572012-04-28 14:42:18 -070034 //Host ::= textual representation of numeric IPv4 or IPv6 address
35 //Port ::= nonNegativeInteger [1..65535]
36 //MulticastInterface ::= textual representation of numeric IPv4 or IPv6 address
37 //MulticastTTL ::= nonNegativeInteger [1..255]
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070038 //freshnessSeconds ::= nonNegativeInteger
Meki Cherkaoui09c15572012-04-28 14:42:18 -070039
40};
41
42/**
43 * Used by NetworkObject to decode the object from a network stream.
44 * @see org.ccnx.ccn.impl.encoding.XMLEncodable
45 */
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.
116 * @see org.ccnx.ccn.impl.encoding.XMLEncodable
117 */
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700118FaceInstance.prototype.to_ccnb = function(//XMLEncoder
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700119 encoder){
120
121 //if (!this.validate()) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -0700122 //throw new Error("Cannot encode : field values missing.");
123 //throw new Error("")
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700124 //}
125 encoder.writeStartElement(this.getElementLabel());
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700126
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700127 if (null != this.action && this.action.length != 0)
128 encoder.writeElement(CCNProtocolDTags.Action, this.action);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700129
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700130 if (null != this.publisherPublicKeyDigest) {
131 this.publisherPublicKeyDigest.to_ccnb(encoder);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700132 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700133 if (null != this.faceID) {
134 encoder.writeElement(CCNProtocolDTags.FaceID, this.faceID);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700135 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700136 if (null != this.ipProto) {
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700137 //encoder.writeElement(CCNProtocolDTags.IPProto, this.IpProto.value());
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700138 encoder.writeElement(CCNProtocolDTags.IPProto, this.ipProto);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700139 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700140 if (null != this.host && this.host.length != 0) {
141 encoder.writeElement(CCNProtocolDTags.Host, this.host);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700142 }
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700143 if (null != this.Port) {
144 encoder.writeElement(CCNProtocolDTags.Port, this.Port);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700145 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700146 if (null != this.multicastInterface && this.multicastInterface.length != 0) {
147 encoder.writeElement(CCNProtocolDTags.MulticastInterface, this.multicastInterface);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700148 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700149 if (null != this.multicastTTL) {
150 encoder.writeElement(CCNProtocolDTags.MulticastTTL, this.multicastTTL);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700151 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700152 if (null != this.freshnessSeconds) {
153 encoder.writeElement(CCNProtocolDTags.FreshnessSeconds, this.freshnessSeconds);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700154 }
155 encoder.writeEndElement();
156}
157
158
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700159FaceInstance.prototype.getElementLabel= function(){return CCNProtocolDTags.FaceInstance;};
160