Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [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. |
| 45 | * @see org.ccnx.ccn.impl.encoding.XMLEncodable |
| 46 | */ |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 47 | FaceInstance.prototype.from_ccnb = function(//XMLDecoder |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 48 | decoder) { |
| 49 | |
| 50 | decoder.readStartElement(this.getElementLabel()); |
| 51 | |
| 52 | if (decoder.peekStartElement(CCNProtocolDTags.Action)) { |
| 53 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 54 | this.action = decoder.readUTF8Element(CCNProtocolDTags.Action); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 55 | |
| 56 | } |
| 57 | if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) { |
| 58 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 59 | this.publisherPublicKeyDigest = new PublisherPublicKeyDigest(); |
| 60 | this.publisherPublicKeyDigest.from_ccnb(decoder); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 61 | |
| 62 | } |
| 63 | if (decoder.peekStartElement(CCNProtocolDTags.FaceID)) { |
| 64 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 65 | this.faceID = decoder.readIntegerElement(CCNProtocolDTags.FaceID); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 66 | |
| 67 | } |
| 68 | if (decoder.peekStartElement(CCNProtocolDTags.IPProto)) { |
| 69 | |
| 70 | //int |
| 71 | var pI = decoder.readIntegerElement(CCNProtocolDTags.IPProto); |
| 72 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 73 | this.ipProto = null; |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 74 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 75 | if (NetworkProtocol.TCP == pI) { |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 76 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 77 | this.ipProto = NetworkProtocol.TCP; |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 78 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 79 | } else if (NetworkProtocol.UDP == pI) { |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 80 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 81 | this.ipProto = NetworkProtocol.UDP; |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 82 | |
| 83 | } else { |
| 84 | |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 85 | throw new Error("FaceInstance.decoder. Invalid " + |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 86 | CCNProtocolDTags.tagToString(CCNProtocolDTags.IPProto) + " field: " + pI); |
| 87 | |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | if (decoder.peekStartElement(CCNProtocolDTags.Host)) { |
| 92 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 93 | this.host = decoder.readUTF8Element(CCNProtocolDTags.Host); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 94 | |
| 95 | } |
| 96 | |
| 97 | if (decoder.peekStartElement(CCNProtocolDTags.Port)) { |
| 98 | this.Port = decoder.readIntegerElement(CCNProtocolDTags.Port); |
| 99 | } |
| 100 | |
| 101 | if (decoder.peekStartElement(CCNProtocolDTags.MulticastInterface)) { |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 102 | this.multicastInterface = decoder.readUTF8Element(CCNProtocolDTags.MulticastInterface); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | if (decoder.peekStartElement(CCNProtocolDTags.MulticastTTL)) { |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 106 | this.multicastTTL = decoder.readIntegerElement(CCNProtocolDTags.MulticastTTL); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | if (decoder.peekStartElement(CCNProtocolDTags.FreshnessSeconds)) { |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 110 | this.freshnessSeconds = decoder.readIntegerElement(CCNProtocolDTags.FreshnessSeconds); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 111 | } |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 112 | decoder.readEndElement(); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Used by NetworkObject to encode the object to a network stream. |
| 117 | * @see org.ccnx.ccn.impl.encoding.XMLEncodable |
| 118 | */ |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 119 | FaceInstance.prototype.to_ccnb = function(//XMLEncoder |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 120 | encoder){ |
| 121 | |
| 122 | //if (!this.validate()) { |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 123 | //throw new Error("Cannot encode : field values missing."); |
| 124 | //throw new Error("") |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 125 | //} |
| 126 | encoder.writeStartElement(this.getElementLabel()); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 127 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 128 | if (null != this.action && this.action.length != 0) |
| 129 | encoder.writeElement(CCNProtocolDTags.Action, this.action); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 130 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 131 | if (null != this.publisherPublicKeyDigest) { |
| 132 | this.publisherPublicKeyDigest.to_ccnb(encoder); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 133 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 134 | if (null != this.faceID) { |
| 135 | encoder.writeElement(CCNProtocolDTags.FaceID, this.faceID); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 136 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 137 | if (null != this.ipProto) { |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 138 | //encoder.writeElement(CCNProtocolDTags.IPProto, this.IpProto.value()); |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 139 | encoder.writeElement(CCNProtocolDTags.IPProto, this.ipProto); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 140 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 141 | if (null != this.host && this.host.length != 0) { |
| 142 | encoder.writeElement(CCNProtocolDTags.Host, this.host); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 143 | } |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 144 | if (null != this.Port) { |
| 145 | encoder.writeElement(CCNProtocolDTags.Port, this.Port); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 146 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 147 | if (null != this.multicastInterface && this.multicastInterface.length != 0) { |
| 148 | encoder.writeElement(CCNProtocolDTags.MulticastInterface, this.multicastInterface); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 149 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 150 | if (null != this.multicastTTL) { |
| 151 | encoder.writeElement(CCNProtocolDTags.MulticastTTL, this.multicastTTL); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 152 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 153 | if (null != this.freshnessSeconds) { |
| 154 | encoder.writeElement(CCNProtocolDTags.FreshnessSeconds, this.freshnessSeconds); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 155 | } |
| 156 | encoder.writeEndElement(); |
| 157 | } |
| 158 | |
| 159 | |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 160 | FaceInstance.prototype.getElementLabel= function(){return CCNProtocolDTags.FaceInstance;}; |
| 161 | |