blob: 9f32ade2ab6c23e643b86dae2f23dc57d27b8f9a [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
6
7var FaceInstance = function FaceInstance(
8 _Action,
9 _PublisherPublicKeyDigest,
10 _FaceID,
11 _IPProto,
12 _Host,
13 _Port,
14 _MulticastInterface,
15 _MulticastTTL,
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070016 _FreshnessSeconds){
Meki Cherkaoui09c15572012-04-28 14:42:18 -070017
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070018
19 this.Action = _Action;
20 this.PublisherPublicKeyDigest = _PublisherPublicKeyDigest;
21 this.FaceID = _FaceID;
22 this.IPProto = _IPProto;
23 this.Host = _Host;
24 this.Port = _Port;
25 this.MulticastInterface =_MulticastInterface;
26 this.MulticastTTL =_MulticastTTL;
27 this.FreshnessSeconds = _FreshnessSeconds;
Meki Cherkaoui09c15572012-04-28 14:42:18 -070028
29 //Action ::= ("newface" | "destroyface" | "queryface")
30 //PublisherPublicKeyDigest ::= SHA-256 digest
31 //FaceID ::= nonNegativeInteger
32 //IPProto ::= nonNegativeInteger [IANA protocol number, 6=TCP, 17=UDP]
33 //Host ::= textual representation of numeric IPv4 or IPv6 address
34 //Port ::= nonNegativeInteger [1..65535]
35 //MulticastInterface ::= textual representation of numeric IPv4 or IPv6 address
36 //MulticastTTL ::= nonNegativeInteger [1..255]
37 //FreshnessSeconds ::= nonNegativeInteger
38
39};
40
41/**
42 * Used by NetworkObject to decode the object from a network stream.
43 * @see org.ccnx.ccn.impl.encoding.XMLEncodable
44 */
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070045FaceInstance.prototype.decode = function(//XMLDecoder
Meki Cherkaoui09c15572012-04-28 14:42:18 -070046 decoder) {
47
48 decoder.readStartElement(this.getElementLabel());
49
50 if (decoder.peekStartElement(CCNProtocolDTags.Action)) {
51
52 this.Action = decoder.readUTF8Element(CCNProtocolDTags.Action);
53
54 }
55 if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) {
56
57 this.PublisherPublicKeyDigest = new PublisherPublicKeyDigest();
58 this.PublisherPublicKeyDigest.decode(decoder);
59
60 }
61 if (decoder.peekStartElement(CCNProtocolDTags.FaceID)) {
62
63 this.FaceID = decoder.readIntegerElement(CCNProtocolDTags.FaceID);
64
65 }
66 if (decoder.peekStartElement(CCNProtocolDTags.IPProto)) {
67
68 //int
69 var pI = decoder.readIntegerElement(CCNProtocolDTags.IPProto);
70
71 this.IPProto = null;
72
73 if (NetworkProtocol.TCP.value().intValue() == pI) {
74
75 this.IPProto = NetworkProtocol.TCP;
76
77 } else if (NetworkProtocol.UDP.value().intValue() == pI) {
78
79 this.IPProto = NetworkProtocol.UDP;
80
81 } else {
82
83 throw new Exception("FaceInstance.decoder. Invalid " +
84 CCNProtocolDTags.tagToString(CCNProtocolDTags.IPProto) + " field: " + pI);
85
86 }
87 }
88
89 if (decoder.peekStartElement(CCNProtocolDTags.Host)) {
90
91 this.Host = decoder.readUTF8Element(CCNProtocolDTags.Host);
92
93 }
94
95 if (decoder.peekStartElement(CCNProtocolDTags.Port)) {
96 this.Port = decoder.readIntegerElement(CCNProtocolDTags.Port);
97 }
98
99 if (decoder.peekStartElement(CCNProtocolDTags.MulticastInterface)) {
100 this.MulticastInterface = decoder.readUTF8Element(CCNProtocolDTags.MulticastInterface);
101 }
102
103 if (decoder.peekStartElement(CCNProtocolDTags.MulticastTTL)) {
104 this.MulticastTTL = decoder.readIntegerElement(CCNProtocolDTags.MulticastTTL);
105 }
106
107 if (decoder.peekStartElement(CCNProtocolDTags.FreshnessSeconds)) {
108 this.FreshnessSeconds = decoder.readIntegerElement(CCNProtocolDTags.FreshnessSeconds);
109 }
110
111 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());
126 if (null != this.Action && this.Action.length != 0)
127 encoder.writeElement(CCNProtocolDTags.Action, this.Action);
128 if (null != this.PublisherPublicKeyDigest) {
129 this.PublisherPublicKeyDigest.encode(encoder);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700130 }
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700131 if (null != this.FaceID) {
132 encoder.writeElement(CCNProtocolDTags.FaceID, this.FaceID);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700133 }
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700134 if (null != this.IPProto) {
135 //encoder.writeElement(CCNProtocolDTags.IPProto, this.IpProto.value());
136 encoder.writeElement(CCNProtocolDTags.IPProto, this.IPProto);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700137 }
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700138 if (null != this.Host && this.Host.length != 0) {
139 encoder.writeElement(CCNProtocolDTags.Host, this.Host);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700140 }
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700141 if (null != this.Port) {
142 encoder.writeElement(CCNProtocolDTags.Port, this.Port);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700143 }
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700144 if (null != this.MulticastInterface && this.MulticastInterface.length != 0) {
145 encoder.writeElement(CCNProtocolDTags.MulticastInterface, this.MulticastInterface);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700146 }
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700147 if (null != this.MulticastTTL) {
148 encoder.writeElement(CCNProtocolDTags.MulticastTTL, this.MulticastTTL);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700149 }
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700150 if (null != this.FreshnessSeconds) {
151 encoder.writeElement(CCNProtocolDTags.FreshnessSeconds, this.FreshnessSeconds);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700152 }
153 encoder.writeEndElement();
154}
155
156
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700157FaceInstance.prototype.getElementLabel= function(){return CCNProtocolDTags.FaceInstance;};
158