Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * @author: ucla-cs |
| 3 | * This class represents Face Instances |
| 4 | */ |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 5 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 6 | var NetworkProtocol = { TCP:6, UDP:17}; |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 7 | |
| 8 | var FaceInstance = function FaceInstance( |
| 9 | _Action, |
| 10 | _PublisherPublicKeyDigest, |
| 11 | _FaceID, |
| 12 | _IPProto, |
| 13 | _Host, |
| 14 | _Port, |
| 15 | _MulticastInterface, |
| 16 | _MulticastTTL, |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 17 | _FreshnessSeconds){ |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 18 | |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 19 | |
| 20 | this.Action = _Action; |
| 21 | this.PublisherPublicKeyDigest = _PublisherPublicKeyDigest; |
| 22 | this.FaceID = _FaceID; |
| 23 | this.IPProto = _IPProto; |
| 24 | this.Host = _Host; |
| 25 | this.Port = _Port; |
| 26 | this.MulticastInterface =_MulticastInterface; |
| 27 | this.MulticastTTL =_MulticastTTL; |
| 28 | this.FreshnessSeconds = _FreshnessSeconds; |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 29 | |
| 30 | //Action ::= ("newface" | "destroyface" | "queryface") |
| 31 | //PublisherPublicKeyDigest ::= SHA-256 digest |
| 32 | //FaceID ::= nonNegativeInteger |
| 33 | //IPProto ::= nonNegativeInteger [IANA protocol number, 6=TCP, 17=UDP] |
| 34 | //Host ::= textual representation of numeric IPv4 or IPv6 address |
| 35 | //Port ::= nonNegativeInteger [1..65535] |
| 36 | //MulticastInterface ::= textual representation of numeric IPv4 or IPv6 address |
| 37 | //MulticastTTL ::= nonNegativeInteger [1..255] |
| 38 | //FreshnessSeconds ::= nonNegativeInteger |
| 39 | |
| 40 | }; |
| 41 | |
| 42 | /** |
| 43 | * Used by NetworkObject to decode the object from a network stream. |
| 44 | * @see org.ccnx.ccn.impl.encoding.XMLEncodable |
| 45 | */ |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 46 | FaceInstance.prototype.decode = 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 | |
| 53 | this.Action = decoder.readUTF8Element(CCNProtocolDTags.Action); |
| 54 | |
| 55 | } |
| 56 | if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) { |
| 57 | |
| 58 | this.PublisherPublicKeyDigest = new PublisherPublicKeyDigest(); |
| 59 | this.PublisherPublicKeyDigest.decode(decoder); |
| 60 | |
| 61 | } |
| 62 | if (decoder.peekStartElement(CCNProtocolDTags.FaceID)) { |
| 63 | |
| 64 | this.FaceID = decoder.readIntegerElement(CCNProtocolDTags.FaceID); |
| 65 | |
| 66 | } |
| 67 | if (decoder.peekStartElement(CCNProtocolDTags.IPProto)) { |
| 68 | |
| 69 | //int |
| 70 | var pI = decoder.readIntegerElement(CCNProtocolDTags.IPProto); |
| 71 | |
| 72 | this.IPProto = null; |
| 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 | |
| 76 | this.IPProto = NetworkProtocol.TCP; |
| 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 | |
| 80 | this.IPProto = NetworkProtocol.UDP; |
| 81 | |
| 82 | } else { |
| 83 | |
| 84 | throw new Exception("FaceInstance.decoder. Invalid " + |
| 85 | CCNProtocolDTags.tagToString(CCNProtocolDTags.IPProto) + " field: " + pI); |
| 86 | |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if (decoder.peekStartElement(CCNProtocolDTags.Host)) { |
| 91 | |
| 92 | this.Host = decoder.readUTF8Element(CCNProtocolDTags.Host); |
| 93 | |
| 94 | } |
| 95 | |
| 96 | if (decoder.peekStartElement(CCNProtocolDTags.Port)) { |
| 97 | this.Port = decoder.readIntegerElement(CCNProtocolDTags.Port); |
| 98 | } |
| 99 | |
| 100 | if (decoder.peekStartElement(CCNProtocolDTags.MulticastInterface)) { |
| 101 | this.MulticastInterface = decoder.readUTF8Element(CCNProtocolDTags.MulticastInterface); |
| 102 | } |
| 103 | |
| 104 | if (decoder.peekStartElement(CCNProtocolDTags.MulticastTTL)) { |
| 105 | this.MulticastTTL = decoder.readIntegerElement(CCNProtocolDTags.MulticastTTL); |
| 106 | } |
| 107 | |
| 108 | if (decoder.peekStartElement(CCNProtocolDTags.FreshnessSeconds)) { |
| 109 | this.FreshnessSeconds = decoder.readIntegerElement(CCNProtocolDTags.FreshnessSeconds); |
| 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. |
| 116 | * @see org.ccnx.ccn.impl.encoding.XMLEncodable |
| 117 | */ |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 118 | FaceInstance.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()); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 126 | |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 127 | if (null != this.Action && this.Action.length != 0) |
| 128 | encoder.writeElement(CCNProtocolDTags.Action, this.Action); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 129 | |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 130 | if (null != this.PublisherPublicKeyDigest) { |
| 131 | this.PublisherPublicKeyDigest.encode(encoder); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 132 | } |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 133 | if (null != this.FaceID) { |
| 134 | encoder.writeElement(CCNProtocolDTags.FaceID, this.FaceID); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 135 | } |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 136 | if (null != this.IPProto) { |
| 137 | //encoder.writeElement(CCNProtocolDTags.IPProto, this.IpProto.value()); |
| 138 | encoder.writeElement(CCNProtocolDTags.IPProto, this.IPProto); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 139 | } |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 140 | if (null != this.Host && this.Host.length != 0) { |
| 141 | encoder.writeElement(CCNProtocolDTags.Host, this.Host); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 142 | } |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 143 | if (null != this.Port) { |
| 144 | encoder.writeElement(CCNProtocolDTags.Port, this.Port); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 145 | } |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 146 | if (null != this.MulticastInterface && this.MulticastInterface.length != 0) { |
| 147 | encoder.writeElement(CCNProtocolDTags.MulticastInterface, this.MulticastInterface); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 148 | } |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 149 | if (null != this.MulticastTTL) { |
| 150 | encoder.writeElement(CCNProtocolDTags.MulticastTTL, this.MulticastTTL); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 151 | } |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 152 | if (null != this.FreshnessSeconds) { |
| 153 | encoder.writeElement(CCNProtocolDTags.FreshnessSeconds, this.FreshnessSeconds); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 154 | } |
| 155 | encoder.writeEndElement(); |
| 156 | } |
| 157 | |
| 158 | |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 159 | FaceInstance.prototype.getElementLabel= function(){return CCNProtocolDTags.FaceInstance;}; |
| 160 | |