Jeff Thompson | 745026e | 2012-10-13 12:49:20 -0700 | [diff] [blame] | 1 | /* |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 2 | * @author: Meki Cherkaoui, Jeff Thompson, Wentao Shang |
Jeff Thompson | 745026e | 2012-10-13 12:49:20 -0700 | [diff] [blame] | 3 | * See COPYING for copyright and distribution information. |
| 4 | * This class represents the top-level object for communicating with an NDN host. |
| 5 | */ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 6 | |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 7 | var LOG = 3; |
| 8 | |
Jeff Thompson | e06b31e | 2012-09-30 17:19:19 -0700 | [diff] [blame] | 9 | /** |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 10 | * settings is an associative array with the following defaults: |
| 11 | * { |
| 12 | * host: 'localhost', |
| 13 | * port: 9696, |
| 14 | * getTransport: function() { return new WebSocketTransport(); } |
| 15 | * } |
Jeff Thompson | e06b31e | 2012-09-30 17:19:19 -0700 | [diff] [blame] | 16 | */ |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 17 | var NDN = function NDN(settings) { |
| 18 | settings = (settings || {}); |
| 19 | this.host = (settings.host || "localhost"); |
| 20 | this.port = (settings.port || 9696); |
| 21 | var getTransport = (settings.getTransport || function() { return new WebSocketTransport(); }); |
| 22 | this.transport = getTransport(); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 23 | }; |
| 24 | |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 25 | |
| 26 | /* Java Socket Bridge and XPCOM transport */ |
| 27 | |
Jeff Thompson | e06b31e | 2012-09-30 17:19:19 -0700 | [diff] [blame] | 28 | NDN.prototype.createRoute = function(host,port){ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 29 | this.host=host; |
| 30 | this.port=port; |
| 31 | } |
| 32 | |
Jeff Thompson | e06b31e | 2012-09-30 17:19:19 -0700 | [diff] [blame] | 33 | NDN.prototype.get = function(message){ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 34 | if(this.host!=null && this.port!=null){ |
| 35 | var output =''; |
| 36 | message = message.trim(); |
| 37 | if(message==null || message =="" ){ |
| 38 | console.log('INVALID INPUT TO GET'); |
| 39 | return null; |
| 40 | } |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 41 | |
| 42 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 43 | //var array = Name.createNameArray(message); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 44 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 45 | int = new Interest(new Name(message)); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 46 | |
| 47 | int.InterestLifetime = 4200; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 48 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 49 | var hex = encodeToHexInterest(int); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 50 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 51 | //var result = get_java_socket_bridge().connectAndStart(ndnurl,ndnport,hex); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 52 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 53 | var result = get(this.host,this.port, hex); |
| 54 | |
| 55 | |
| 56 | if(LOG>0)console.log('BINARY RESPONSE IS ' +result); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 57 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 58 | if(result==null || result==undefined || result =="" ){ |
| 59 | /*if(result[0] != '0'||result[1]!='4') { |
| 60 | if(LOG>2)console.log('INVALID ANSWER'); |
| 61 | }*/ |
| 62 | return null; |
| 63 | } |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 64 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 65 | else{ |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 66 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 67 | co = decodeHexContentObject(result); |
| 68 | |
| 69 | if(LOG>2) { |
| 70 | console.log('DECODED CONTENT OBJECT'); |
| 71 | console.log(co); |
| 72 | } |
| 73 | return co; |
| 74 | } |
| 75 | } |
| 76 | else{ |
| 77 | |
| 78 | console.log('ERROR URL OR PORT NOT SET'); |
| 79 | |
| 80 | return null; |
| 81 | |
| 82 | } |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 83 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 84 | |
| 85 | } |
| 86 | |
Jeff Thompson | e06b31e | 2012-09-30 17:19:19 -0700 | [diff] [blame] | 87 | NDN.prototype.put = function(name,content){ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 88 | if(this.host!=null && this.port!=null){ |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 89 | |
Axel Colin de Verdiere | d363e63 | 2012-06-06 05:16:43 -0700 | [diff] [blame] | 90 | var co = this.get("/%C1.M.S.localhost/%C1.M.SRV/ccnd"); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 91 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 92 | if(!co || !co.signedInfo || !co.signedInfo.publisher || !co.signedInfo.publisher.publisherPublicKeyDigest){ |
Axel Colin de Verdiere | d363e63 | 2012-06-06 05:16:43 -0700 | [diff] [blame] | 93 | alert("Cannot contact router"); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 94 | |
Axel Colin de Verdiere | d363e63 | 2012-06-06 05:16:43 -0700 | [diff] [blame] | 95 | return null; |
| 96 | } |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 97 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 98 | var ccnxnodename = co.signedInfo.publisher.publisherPublicKeyDigest; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 99 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 100 | name = name.trim(); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 101 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 102 | var fe = new ForwardingEntry('selfreg',new Name(name),null, null, 3,2147483647); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 103 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 104 | var bytes = encodeForwardingEntry(fe); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 105 | |
| 106 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 107 | var si = new SignedInfo(); |
| 108 | si.setFields(); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 109 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 110 | var co = new ContentObject(new Name(),si,bytes,new Signature()); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 111 | co.sign(); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 112 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 113 | var coBinary = encodeToBinaryContentObject(co); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 114 | |
Axel Colin de Verdiere | d363e63 | 2012-06-06 05:16:43 -0700 | [diff] [blame] | 115 | //var ccnxnodename = unescape('%E0%A0%1E%099h%F9t%0C%E7%F46%1B%AB%F5%BB%05%A4%E5Z%AC%A5%E5%8Fs%ED%DE%B8%E0%13%AA%8F'); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 116 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 117 | var interestName = new Name(['ccnx',ccnxnodename,'selfreg',coBinary]); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 118 | |
| 119 | int = new Interest(interestName); |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 120 | int.scope = 1; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 121 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 122 | var hex = encodeToHexInterest(int); |
| 123 | |
| 124 | console.log('GOING TO PUT INTEREST OBJECT'); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 125 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 126 | console.log(hex); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 127 | |
Meki Cherkaoui | 81bfc28 | 2012-06-06 03:23:25 -0700 | [diff] [blame] | 128 | //var result = put(this.host,this.port, hex,name); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 129 | |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 130 | |
Meki Cherkaoui | 81bfc28 | 2012-06-06 03:23:25 -0700 | [diff] [blame] | 131 | //if(LOG>3)console.log('received interest'); //from host'+ host +':'+port+' with name '+name); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 132 | |
Meki Cherkaoui | 81bfc28 | 2012-06-06 03:23:25 -0700 | [diff] [blame] | 133 | //if(LOG>3)console.log('DATA '); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 134 | |
Meki Cherkaoui | 81bfc28 | 2012-06-06 03:23:25 -0700 | [diff] [blame] | 135 | //if(LOG>3)console.log(result); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 136 | |
Meki Cherkaoui | 81bfc28 | 2012-06-06 03:23:25 -0700 | [diff] [blame] | 137 | //interest = decodeHexInterest(result); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 138 | |
Meki Cherkaoui | 81bfc28 | 2012-06-06 03:23:25 -0700 | [diff] [blame] | 139 | //console.log('SUCCESSFULLY PARSED INTEREST'); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 140 | |
Meki Cherkaoui | 81bfc28 | 2012-06-06 03:23:25 -0700 | [diff] [blame] | 141 | console.log('CREATING ANSWER'); |
| 142 | var si = new SignedInfo(); |
| 143 | si.setFields(); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 144 | |
Meki Cherkaoui | 81bfc28 | 2012-06-06 03:23:25 -0700 | [diff] [blame] | 145 | var answer = DataUtils.toNumbersFromString(content); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 146 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 147 | var co = new ContentObject(new Name(name),si,answer,new Signature()); |
Meki Cherkaoui | 81bfc28 | 2012-06-06 03:23:25 -0700 | [diff] [blame] | 148 | co.sign(); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 149 | |
| 150 | |
Meki Cherkaoui | 81bfc28 | 2012-06-06 03:23:25 -0700 | [diff] [blame] | 151 | var outputHex = encodeToHexContentObject(co); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 152 | |
Meki Cherkaoui | 81bfc28 | 2012-06-06 03:23:25 -0700 | [diff] [blame] | 153 | //console.log('SENDING ANSWER'); |
| 154 | |
| 155 | //return get_java_socket_bridge().putAnswer(outputHex,name); |
| 156 | |
Meki Cherkaoui | 81bfc28 | 2012-06-06 03:23:25 -0700 | [diff] [blame] | 157 | var result = put(this.host,this.port, hex,name,outputHex); |
| 158 | |
| 159 | |
| 160 | return result; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 161 | } |
| 162 | else{ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 163 | console.log('ERROR URL OR PORT NOT SET'); |
| 164 | |
| 165 | return null; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 166 | } |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 167 | } |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 168 | |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 169 | /** Encode name as an Interest. If template is not null, use its attributes. |
| 170 | * Send the interest to host:port, read the entire response and call |
| 171 | * closure.upcall(Closure.UPCALL_CONTENT (or Closure.UPCALL_CONTENT_UNVERIFIED), |
Jeff Thompson | 97f2743 | 2012-10-16 00:28:03 -0700 | [diff] [blame] | 172 | * new UpcallInfo(this, interest, 0, contentObject)). |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 173 | */ |
| 174 | NDN.prototype.expressInterest = function( |
| 175 | // Name |
| 176 | name, |
| 177 | // Closure |
| 178 | closure, |
| 179 | // Interest |
| 180 | template) { |
| 181 | if (this.host == null || this.port == null) { |
| 182 | dump('ERROR host OR port NOT SET\n'); |
| 183 | return; |
| 184 | } |
| 185 | |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 186 | var interest = new Interest(name); |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 187 | if (template != null) { |
Jeff Thompson | 4404ab5 | 2012-10-21 10:29:48 -0700 | [diff] [blame] | 188 | interest.minSuffixComponents = template.minSuffixComponents; |
| 189 | interest.maxSuffixComponents = template.maxSuffixComponents; |
| 190 | interest.publisherPublicKeyDigest = template.publisherPublicKeyDigest; |
| 191 | interest.exclude = template.exclude; |
| 192 | interest.childSelector = template.childSelector; |
| 193 | interest.answerOriginKind = template.answerOriginKind; |
| 194 | interest.scope = template.scope; |
| 195 | interest.interestLifetime = template.interestLifetime; |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 196 | } |
| 197 | else |
Jeff Thompson | 741108b | 2012-10-15 23:07:09 -0700 | [diff] [blame] | 198 | interest.interestLifetime = 4200; |
Jeff Thompson | 97f2743 | 2012-10-16 00:28:03 -0700 | [diff] [blame] | 199 | |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 200 | this.transport.expressInterest(this, interest, closure); |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 201 | }; |
| 202 | |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 203 | |
| 204 | NDN.prototype.registerPrefix = function(name, closure, flag) { |
| 205 | return this.transport.registerPrefix(this, name, closure, flag); |
| 206 | } |