blob: 5cac6bcddc1f2296de0711b5b73b43f01e14a684 [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(
9 _Action,
10 _PublisherPublicKeyDigest,
11 _FaceID,
12 _IPProto,
13 _Host,
14 _Port,
15 _MulticastInterface,
16 _MulticastTTL,
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070017 _FreshnessSeconds){
Meki Cherkaoui09c15572012-04-28 14:42:18 -070018
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070019
20 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
30 //Action ::= ("newface" | "destroyface" | "queryface")
31 //PublisherPublicKeyDigest ::= SHA-256 digest
32 //FaceID ::= nonNegativeInteger
33 //IPProto ::= nonNegativeInteger [IANA protocol number, 6=TCP, 17=UDP]
34 //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]
38 //FreshnessSeconds ::= nonNegativeInteger
39
40};
41
42/**
43 * Used by NetworkObject to decode the object from a network stream.
44 * @see org.ccnx.ccn.impl.encoding.XMLEncodable
45 */
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070046FaceInstance.prototype.decode = 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
53 this.Action = decoder.readUTF8Element(CCNProtocolDTags.Action);
54
55 }
56 if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) {
57
58 this.PublisherPublicKeyDigest = new PublisherPublicKeyDigest();
59 this.PublisherPublicKeyDigest.decode(decoder);
60
61 }
62 if (decoder.peekStartElement(CCNProtocolDTags.FaceID)) {
63
64 this.FaceID = decoder.readIntegerElement(CCNProtocolDTags.FaceID);
65
66 }
67 if (decoder.peekStartElement(CCNProtocolDTags.IPProto)) {
68
69 //int
70 var pI = decoder.readIntegerElement(CCNProtocolDTags.IPProto);
71
72 this.IPProto = null;
73
Meki Cherkaoui8f173612012-06-06 01:05:40 -070074 if (NetworkProtocol.TCP == pI) {
Meki Cherkaoui09c15572012-04-28 14:42:18 -070075
76 this.IPProto = NetworkProtocol.TCP;
77
Meki Cherkaoui8f173612012-06-06 01:05:40 -070078 } else if (NetworkProtocol.UDP == pI) {
Meki Cherkaoui09c15572012-04-28 14:42:18 -070079
80 this.IPProto = NetworkProtocol.UDP;
81
82 } else {
83
84 throw new Exception("FaceInstance.decoder. Invalid " +
85 CCNProtocolDTags.tagToString(CCNProtocolDTags.IPProto) + " field: " + pI);
86
87 }
88 }
89
90 if (decoder.peekStartElement(CCNProtocolDTags.Host)) {
91
92 this.Host = decoder.readUTF8Element(CCNProtocolDTags.Host);
93
94 }
95
96 if (decoder.peekStartElement(CCNProtocolDTags.Port)) {
97 this.Port = decoder.readIntegerElement(CCNProtocolDTags.Port);
98 }
99
100 if (decoder.peekStartElement(CCNProtocolDTags.MulticastInterface)) {
101 this.MulticastInterface = decoder.readUTF8Element(CCNProtocolDTags.MulticastInterface);
102 }
103
104 if (decoder.peekStartElement(CCNProtocolDTags.MulticastTTL)) {
105 this.MulticastTTL = decoder.readIntegerElement(CCNProtocolDTags.MulticastTTL);
106 }
107
108 if (decoder.peekStartElement(CCNProtocolDTags.FreshnessSeconds)) {
109 this.FreshnessSeconds = decoder.readIntegerElement(CCNProtocolDTags.FreshnessSeconds);
110 }
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 */
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700118FaceInstance.prototype.encode = function(//XMLEncoder
119 encoder){
120
121 //if (!this.validate()) {
122 //throw new Exception("Cannot encode : field values missing.");
123 //throw new Exception("")
124 //}
125 encoder.writeStartElement(this.getElementLabel());
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700126
Meki Cherkaouiabb973b2012-05-09 14:25: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
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700130 if (null != this.PublisherPublicKeyDigest) {
131 this.PublisherPublicKeyDigest.encode(encoder);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700132 }
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700133 if (null != this.FaceID) {
134 encoder.writeElement(CCNProtocolDTags.FaceID, this.FaceID);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700135 }
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700136 if (null != this.IPProto) {
137 //encoder.writeElement(CCNProtocolDTags.IPProto, this.IpProto.value());
138 encoder.writeElement(CCNProtocolDTags.IPProto, this.IPProto);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700139 }
Meki Cherkaouiabb973b2012-05-09 14:25: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 }
Meki Cherkaouiabb973b2012-05-09 14:25: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 }
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700149 if (null != this.MulticastTTL) {
150 encoder.writeElement(CCNProtocolDTags.MulticastTTL, this.MulticastTTL);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700151 }
Meki Cherkaouiabb973b2012-05-09 14:25: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