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