Wentao Shang | bd63e46 | 2012-12-03 16:19:33 -0800 | [diff] [blame] | 1 | /** |
Jeff Thompson | 146d7de | 2012-11-17 16:15:28 -0800 | [diff] [blame] | 2 | * @author: Meki Cheraoui |
Jeff Thompson | 745026e | 2012-10-13 12:49:20 -0700 | [diff] [blame] | 3 | * See COPYING for copyright and distribution information. |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 4 | * This class represents Face Instances |
| 5 | */ |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 6 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 7 | var NetworkProtocol = { TCP:6, UDP:17}; |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 8 | |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame^] | 9 | /** |
| 10 | * @constructor |
| 11 | */ |
| 12 | var 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 Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | /** |
| 26 | * Used by NetworkObject to decode the object from a network stream. |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 27 | */ |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 28 | FaceInstance.prototype.from_ccnb = function(//XMLDecoder |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 29 | decoder) { |
| 30 | |
| 31 | decoder.readStartElement(this.getElementLabel()); |
| 32 | |
| 33 | if (decoder.peekStartElement(CCNProtocolDTags.Action)) { |
| 34 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 35 | this.action = decoder.readUTF8Element(CCNProtocolDTags.Action); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 36 | |
| 37 | } |
| 38 | if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) { |
| 39 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 40 | this.publisherPublicKeyDigest = new PublisherPublicKeyDigest(); |
| 41 | this.publisherPublicKeyDigest.from_ccnb(decoder); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 42 | |
| 43 | } |
| 44 | if (decoder.peekStartElement(CCNProtocolDTags.FaceID)) { |
| 45 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 46 | this.faceID = decoder.readIntegerElement(CCNProtocolDTags.FaceID); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 47 | |
| 48 | } |
| 49 | if (decoder.peekStartElement(CCNProtocolDTags.IPProto)) { |
| 50 | |
| 51 | //int |
| 52 | var pI = decoder.readIntegerElement(CCNProtocolDTags.IPProto); |
| 53 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 54 | this.ipProto = null; |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 55 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 56 | if (NetworkProtocol.TCP == pI) { |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 57 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 58 | this.ipProto = NetworkProtocol.TCP; |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 59 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 60 | } else if (NetworkProtocol.UDP == pI) { |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 61 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 62 | this.ipProto = NetworkProtocol.UDP; |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 63 | |
| 64 | } else { |
| 65 | |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 66 | throw new Error("FaceInstance.decoder. Invalid " + |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 67 | CCNProtocolDTags.tagToString(CCNProtocolDTags.IPProto) + " field: " + pI); |
| 68 | |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | if (decoder.peekStartElement(CCNProtocolDTags.Host)) { |
| 73 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 74 | this.host = decoder.readUTF8Element(CCNProtocolDTags.Host); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 75 | |
| 76 | } |
| 77 | |
| 78 | if (decoder.peekStartElement(CCNProtocolDTags.Port)) { |
| 79 | this.Port = decoder.readIntegerElement(CCNProtocolDTags.Port); |
| 80 | } |
| 81 | |
| 82 | if (decoder.peekStartElement(CCNProtocolDTags.MulticastInterface)) { |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 83 | this.multicastInterface = decoder.readUTF8Element(CCNProtocolDTags.MulticastInterface); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | if (decoder.peekStartElement(CCNProtocolDTags.MulticastTTL)) { |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 87 | this.multicastTTL = decoder.readIntegerElement(CCNProtocolDTags.MulticastTTL); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | if (decoder.peekStartElement(CCNProtocolDTags.FreshnessSeconds)) { |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 91 | this.freshnessSeconds = decoder.readIntegerElement(CCNProtocolDTags.FreshnessSeconds); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 92 | } |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 93 | decoder.readEndElement(); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Used by NetworkObject to encode the object to a network stream. |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 98 | */ |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 99 | FaceInstance.prototype.to_ccnb = function(//XMLEncoder |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 100 | encoder){ |
| 101 | |
| 102 | //if (!this.validate()) { |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 103 | //throw new Error("Cannot encode : field values missing."); |
| 104 | //throw new Error("") |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 105 | //} |
| 106 | encoder.writeStartElement(this.getElementLabel()); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 107 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 108 | if (null != this.action && this.action.length != 0) |
| 109 | encoder.writeElement(CCNProtocolDTags.Action, this.action); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 110 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 111 | if (null != this.publisherPublicKeyDigest) { |
| 112 | this.publisherPublicKeyDigest.to_ccnb(encoder); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 113 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 114 | if (null != this.faceID) { |
| 115 | encoder.writeElement(CCNProtocolDTags.FaceID, this.faceID); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 116 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 117 | if (null != this.ipProto) { |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 118 | //encoder.writeElement(CCNProtocolDTags.IPProto, this.IpProto.value()); |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 119 | encoder.writeElement(CCNProtocolDTags.IPProto, this.ipProto); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 120 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 121 | if (null != this.host && this.host.length != 0) { |
| 122 | encoder.writeElement(CCNProtocolDTags.Host, this.host); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 123 | } |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 124 | if (null != this.Port) { |
| 125 | encoder.writeElement(CCNProtocolDTags.Port, this.Port); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 126 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 127 | if (null != this.multicastInterface && this.multicastInterface.length != 0) { |
| 128 | encoder.writeElement(CCNProtocolDTags.MulticastInterface, this.multicastInterface); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 129 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 130 | if (null != this.multicastTTL) { |
| 131 | encoder.writeElement(CCNProtocolDTags.MulticastTTL, this.multicastTTL); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 132 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 133 | if (null != this.freshnessSeconds) { |
| 134 | encoder.writeElement(CCNProtocolDTags.FreshnessSeconds, this.freshnessSeconds); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 135 | } |
| 136 | encoder.writeEndElement(); |
| 137 | } |
| 138 | |
| 139 | |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame^] | 140 | FaceInstance.prototype.getElementLabel = function() { |
| 141 | return CCNProtocolDTags.FaceInstance; |
| 142 | }; |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 143 | |