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 | |
| 9 | var FaceInstance = function FaceInstance( |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 10 | _action, |
| 11 | _publisherPublicKeyDigest, |
| 12 | _faceID, |
| 13 | _ipProto, |
| 14 | _host, |
| 15 | _port, |
| 16 | _multicastInterface, |
| 17 | _multicastTTL, |
| 18 | _freshnessSeconds){ |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 19 | |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 20 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 21 | this.action = _action; |
| 22 | this.publisherPublicKeyDigest = _publisherPublicKeyDigest; |
| 23 | this.faceID = _faceID; |
| 24 | this.ipProto = _ipProto; |
| 25 | this.host = _host; |
| 26 | this.Port = _port; |
| 27 | this.multicastInterface =_multicastInterface; |
| 28 | this.multicastTTL =_multicastTTL; |
| 29 | this.freshnessSeconds = _freshnessSeconds; |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 30 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 31 | //action ::= ("newface" | "destroyface" | "queryface") |
| 32 | //publisherPublicKeyDigest ::= SHA-256 digest |
| 33 | //faceID ::= nonNegativeInteger |
| 34 | //ipProto ::= nonNegativeInteger [IANA protocol number, 6=TCP, 17=UDP] |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 35 | //Host ::= textual representation of numeric IPv4 or IPv6 address |
| 36 | //Port ::= nonNegativeInteger [1..65535] |
| 37 | //MulticastInterface ::= textual representation of numeric IPv4 or IPv6 address |
| 38 | //MulticastTTL ::= nonNegativeInteger [1..255] |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 39 | //freshnessSeconds ::= nonNegativeInteger |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 40 | |
| 41 | }; |
| 42 | |
| 43 | /** |
| 44 | * Used by NetworkObject to decode the object from a network stream. |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 45 | */ |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 46 | FaceInstance.prototype.from_ccnb = function(//XMLDecoder |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 47 | decoder) { |
| 48 | |
| 49 | decoder.readStartElement(this.getElementLabel()); |
| 50 | |
| 51 | if (decoder.peekStartElement(CCNProtocolDTags.Action)) { |
| 52 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 53 | this.action = decoder.readUTF8Element(CCNProtocolDTags.Action); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 54 | |
| 55 | } |
| 56 | if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) { |
| 57 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 58 | this.publisherPublicKeyDigest = new PublisherPublicKeyDigest(); |
| 59 | this.publisherPublicKeyDigest.from_ccnb(decoder); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 60 | |
| 61 | } |
| 62 | if (decoder.peekStartElement(CCNProtocolDTags.FaceID)) { |
| 63 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 64 | this.faceID = decoder.readIntegerElement(CCNProtocolDTags.FaceID); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 65 | |
| 66 | } |
| 67 | if (decoder.peekStartElement(CCNProtocolDTags.IPProto)) { |
| 68 | |
| 69 | //int |
| 70 | var pI = decoder.readIntegerElement(CCNProtocolDTags.IPProto); |
| 71 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 72 | this.ipProto = null; |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 73 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 74 | if (NetworkProtocol.TCP == pI) { |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 75 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 76 | this.ipProto = NetworkProtocol.TCP; |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 77 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 78 | } else if (NetworkProtocol.UDP == pI) { |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 79 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 80 | this.ipProto = NetworkProtocol.UDP; |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 81 | |
| 82 | } else { |
| 83 | |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 84 | throw new Error("FaceInstance.decoder. Invalid " + |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 85 | CCNProtocolDTags.tagToString(CCNProtocolDTags.IPProto) + " field: " + pI); |
| 86 | |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if (decoder.peekStartElement(CCNProtocolDTags.Host)) { |
| 91 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 92 | this.host = decoder.readUTF8Element(CCNProtocolDTags.Host); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 93 | |
| 94 | } |
| 95 | |
| 96 | if (decoder.peekStartElement(CCNProtocolDTags.Port)) { |
| 97 | this.Port = decoder.readIntegerElement(CCNProtocolDTags.Port); |
| 98 | } |
| 99 | |
| 100 | if (decoder.peekStartElement(CCNProtocolDTags.MulticastInterface)) { |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 101 | this.multicastInterface = decoder.readUTF8Element(CCNProtocolDTags.MulticastInterface); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | if (decoder.peekStartElement(CCNProtocolDTags.MulticastTTL)) { |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 105 | this.multicastTTL = decoder.readIntegerElement(CCNProtocolDTags.MulticastTTL); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | if (decoder.peekStartElement(CCNProtocolDTags.FreshnessSeconds)) { |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 109 | this.freshnessSeconds = decoder.readIntegerElement(CCNProtocolDTags.FreshnessSeconds); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 110 | } |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 111 | decoder.readEndElement(); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Used by NetworkObject to encode the object to a network stream. |
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 | FaceInstance.prototype.to_ccnb = function(//XMLEncoder |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 118 | encoder){ |
| 119 | |
| 120 | //if (!this.validate()) { |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 121 | //throw new Error("Cannot encode : field values missing."); |
| 122 | //throw new Error("") |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 123 | //} |
| 124 | encoder.writeStartElement(this.getElementLabel()); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 125 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 126 | if (null != this.action && this.action.length != 0) |
| 127 | encoder.writeElement(CCNProtocolDTags.Action, this.action); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 128 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 129 | if (null != this.publisherPublicKeyDigest) { |
| 130 | this.publisherPublicKeyDigest.to_ccnb(encoder); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 131 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 132 | if (null != this.faceID) { |
| 133 | encoder.writeElement(CCNProtocolDTags.FaceID, this.faceID); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 134 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 135 | if (null != this.ipProto) { |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 136 | //encoder.writeElement(CCNProtocolDTags.IPProto, this.IpProto.value()); |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 137 | encoder.writeElement(CCNProtocolDTags.IPProto, this.ipProto); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 138 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 139 | if (null != this.host && this.host.length != 0) { |
| 140 | encoder.writeElement(CCNProtocolDTags.Host, this.host); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 141 | } |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 142 | if (null != this.Port) { |
| 143 | encoder.writeElement(CCNProtocolDTags.Port, this.Port); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 144 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 145 | if (null != this.multicastInterface && this.multicastInterface.length != 0) { |
| 146 | encoder.writeElement(CCNProtocolDTags.MulticastInterface, this.multicastInterface); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 147 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 148 | if (null != this.multicastTTL) { |
| 149 | encoder.writeElement(CCNProtocolDTags.MulticastTTL, this.multicastTTL); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 150 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 151 | if (null != this.freshnessSeconds) { |
| 152 | encoder.writeElement(CCNProtocolDTags.FreshnessSeconds, this.freshnessSeconds); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 153 | } |
| 154 | encoder.writeEndElement(); |
| 155 | } |
| 156 | |
| 157 | |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 158 | FaceInstance.prototype.getElementLabel= function(){return CCNProtocolDTags.FaceInstance;}; |
| 159 | |