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( |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 9 | _action, |
| 10 | _publisherPublicKeyDigest, |
| 11 | _faceID, |
| 12 | _ipProto, |
| 13 | _host, |
| 14 | _port, |
| 15 | _multicastInterface, |
| 16 | _multicastTTL, |
| 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 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 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 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 30 | //action ::= ("newface" | "destroyface" | "queryface") |
| 31 | //publisherPublicKeyDigest ::= SHA-256 digest |
| 32 | //faceID ::= nonNegativeInteger |
| 33 | //ipProto ::= nonNegativeInteger [IANA protocol number, 6=TCP, 17=UDP] |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 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] |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 38 | //freshnessSeconds ::= nonNegativeInteger |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 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 | */ |
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 | |
| 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 | |
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. |
| 116 | * @see org.ccnx.ccn.impl.encoding.XMLEncodable |
| 117 | */ |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 118 | FaceInstance.prototype.to_ccnb = function(//XMLEncoder |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 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 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21: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 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 130 | if (null != this.publisherPublicKeyDigest) { |
| 131 | this.publisherPublicKeyDigest.to_ccnb(encoder); |
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.faceID) { |
| 134 | encoder.writeElement(CCNProtocolDTags.FaceID, this.faceID); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 135 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 136 | if (null != this.ipProto) { |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 137 | //encoder.writeElement(CCNProtocolDTags.IPProto, this.IpProto.value()); |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 138 | encoder.writeElement(CCNProtocolDTags.IPProto, this.ipProto); |
Meki Cherkaoui | 09c1557 | 2012-04-28 14:42:18 -0700 | [diff] [blame] | 139 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21: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 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21: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 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21: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 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21: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 | |