blob: 621e5ec340d71c4eca5dfb2bee4260217fe7a4a6 [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
Jeff Thompson2b14c7e2013-07-29 15:09:56 -07009/**
10 * @constructor
11 */
12var FaceInstance = function FaceInstance(action, publisherPublicKeyDigest, faceID, ipProto, host, port, multicastInterface,
13 multicastTTL, freshnessSeconds) {
14 this.action = action;
15 this.publisherPublicKeyDigest = publisherPublicKeyDigest;
16 this.faceID = faceID;
17 this.ipProto = ipProto;
18 this.host = host;
19 this.Port = port;
20 this.multicastInterface =multicastInterface;
21 this.multicastTTL =multicastTTL;
22 this.freshnessSeconds = freshnessSeconds;
Meki Cherkaoui09c15572012-04-28 14:42:18 -070023};
24
25/**
26 * Used by NetworkObject to decode the object from a network stream.
Meki Cherkaoui09c15572012-04-28 14:42:18 -070027 */
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070028FaceInstance.prototype.from_ccnb = function(//XMLDecoder
Meki Cherkaoui09c15572012-04-28 14:42:18 -070029 decoder) {
30
31 decoder.readStartElement(this.getElementLabel());
32
33 if (decoder.peekStartElement(CCNProtocolDTags.Action)) {
34
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070035 this.action = decoder.readUTF8Element(CCNProtocolDTags.Action);
Meki Cherkaoui09c15572012-04-28 14:42:18 -070036
37 }
38 if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) {
39
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070040 this.publisherPublicKeyDigest = new PublisherPublicKeyDigest();
41 this.publisherPublicKeyDigest.from_ccnb(decoder);
Meki Cherkaoui09c15572012-04-28 14:42:18 -070042
43 }
44 if (decoder.peekStartElement(CCNProtocolDTags.FaceID)) {
45
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070046 this.faceID = decoder.readIntegerElement(CCNProtocolDTags.FaceID);
Meki Cherkaoui09c15572012-04-28 14:42:18 -070047
48 }
49 if (decoder.peekStartElement(CCNProtocolDTags.IPProto)) {
50
51 //int
52 var pI = decoder.readIntegerElement(CCNProtocolDTags.IPProto);
53
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070054 this.ipProto = null;
Meki Cherkaoui09c15572012-04-28 14:42:18 -070055
Meki Cherkaoui8f173612012-06-06 01:05:40 -070056 if (NetworkProtocol.TCP == pI) {
Meki Cherkaoui09c15572012-04-28 14:42:18 -070057
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070058 this.ipProto = NetworkProtocol.TCP;
Meki Cherkaoui09c15572012-04-28 14:42:18 -070059
Meki Cherkaoui8f173612012-06-06 01:05:40 -070060 } else if (NetworkProtocol.UDP == pI) {
Meki Cherkaoui09c15572012-04-28 14:42:18 -070061
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070062 this.ipProto = NetworkProtocol.UDP;
Meki Cherkaoui09c15572012-04-28 14:42:18 -070063
64 } else {
65
Jeff Thompson34a2ec02012-09-29 21:47:05 -070066 throw new Error("FaceInstance.decoder. Invalid " +
Meki Cherkaoui09c15572012-04-28 14:42:18 -070067 CCNProtocolDTags.tagToString(CCNProtocolDTags.IPProto) + " field: " + pI);
68
69 }
70 }
71
72 if (decoder.peekStartElement(CCNProtocolDTags.Host)) {
73
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070074 this.host = decoder.readUTF8Element(CCNProtocolDTags.Host);
Meki Cherkaoui09c15572012-04-28 14:42:18 -070075
76 }
77
78 if (decoder.peekStartElement(CCNProtocolDTags.Port)) {
79 this.Port = decoder.readIntegerElement(CCNProtocolDTags.Port);
80 }
81
82 if (decoder.peekStartElement(CCNProtocolDTags.MulticastInterface)) {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070083 this.multicastInterface = decoder.readUTF8Element(CCNProtocolDTags.MulticastInterface);
Meki Cherkaoui09c15572012-04-28 14:42:18 -070084 }
85
86 if (decoder.peekStartElement(CCNProtocolDTags.MulticastTTL)) {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070087 this.multicastTTL = decoder.readIntegerElement(CCNProtocolDTags.MulticastTTL);
Meki Cherkaoui09c15572012-04-28 14:42:18 -070088 }
89
90 if (decoder.peekStartElement(CCNProtocolDTags.FreshnessSeconds)) {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070091 this.freshnessSeconds = decoder.readIntegerElement(CCNProtocolDTags.FreshnessSeconds);
Meki Cherkaoui09c15572012-04-28 14:42:18 -070092 }
Meki Cherkaoui09c15572012-04-28 14:42:18 -070093 decoder.readEndElement();
94}
95
96/**
97 * Used by NetworkObject to encode the object to a network stream.
Meki Cherkaoui09c15572012-04-28 14:42:18 -070098 */
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070099FaceInstance.prototype.to_ccnb = function(//XMLEncoder
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700100 encoder){
101
102 //if (!this.validate()) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -0700103 //throw new Error("Cannot encode : field values missing.");
104 //throw new Error("")
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700105 //}
106 encoder.writeStartElement(this.getElementLabel());
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700107
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700108 if (null != this.action && this.action.length != 0)
109 encoder.writeElement(CCNProtocolDTags.Action, this.action);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700110
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700111 if (null != this.publisherPublicKeyDigest) {
112 this.publisherPublicKeyDigest.to_ccnb(encoder);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700113 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700114 if (null != this.faceID) {
115 encoder.writeElement(CCNProtocolDTags.FaceID, this.faceID);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700116 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700117 if (null != this.ipProto) {
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700118 //encoder.writeElement(CCNProtocolDTags.IPProto, this.IpProto.value());
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700119 encoder.writeElement(CCNProtocolDTags.IPProto, this.ipProto);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700120 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700121 if (null != this.host && this.host.length != 0) {
122 encoder.writeElement(CCNProtocolDTags.Host, this.host);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700123 }
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700124 if (null != this.Port) {
125 encoder.writeElement(CCNProtocolDTags.Port, this.Port);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700126 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700127 if (null != this.multicastInterface && this.multicastInterface.length != 0) {
128 encoder.writeElement(CCNProtocolDTags.MulticastInterface, this.multicastInterface);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700129 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700130 if (null != this.multicastTTL) {
131 encoder.writeElement(CCNProtocolDTags.MulticastTTL, this.multicastTTL);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700132 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700133 if (null != this.freshnessSeconds) {
134 encoder.writeElement(CCNProtocolDTags.FreshnessSeconds, this.freshnessSeconds);
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700135 }
136 encoder.writeEndElement();
137}
138
139
Jeff Thompson2b14c7e2013-07-29 15:09:56 -0700140FaceInstance.prototype.getElementLabel = function() {
141 return CCNProtocolDTags.FaceInstance;
142};
Meki Cherkaoui09c15572012-04-28 14:42:18 -0700143