Jeff Thompson | 745026e | 2012-10-13 12:49:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * @author: ucla-cs |
| 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 | e06b31e | 2012-09-30 17:19:19 -0700 | [diff] [blame] | 7 | /** |
| 8 | * host is default '127.0.0.1'. |
| 9 | * port is default 9695. |
| 10 | */ |
| 11 | var NDN = function NDN(host, port){ |
| 12 | this.host = (host || '127.0.0.1'); |
| 13 | this.port = (port || 9695); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 14 | }; |
| 15 | |
Jeff Thompson | e06b31e | 2012-09-30 17:19:19 -0700 | [diff] [blame] | 16 | NDN.prototype.createRoute = function(host,port){ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 17 | this.host=host; |
| 18 | this.port=port; |
| 19 | } |
| 20 | |
Jeff Thompson | e06b31e | 2012-09-30 17:19:19 -0700 | [diff] [blame] | 21 | NDN.prototype.get = function(message){ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 22 | if(this.host!=null && this.port!=null){ |
| 23 | var output =''; |
| 24 | message = message.trim(); |
| 25 | if(message==null || message =="" ){ |
| 26 | console.log('INVALID INPUT TO GET'); |
| 27 | return null; |
| 28 | } |
| 29 | |
| 30 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 31 | //var array = Name.createNameArray(message); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 32 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 33 | int = new Interest(new Name(message)); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 34 | |
| 35 | int.InterestLifetime = 4200; |
| 36 | |
| 37 | var hex = encodeToHexInterest(int); |
| 38 | |
| 39 | //var result = get_java_socket_bridge().connectAndStart(ndnurl,ndnport,hex); |
| 40 | |
| 41 | var result = get(this.host,this.port, hex); |
| 42 | |
| 43 | |
| 44 | if(LOG>0)console.log('BINARY RESPONSE IS ' +result); |
| 45 | |
| 46 | if(result==null || result==undefined || result =="" ){ |
| 47 | /*if(result[0] != '0'||result[1]!='4') { |
| 48 | if(LOG>2)console.log('INVALID ANSWER'); |
| 49 | }*/ |
| 50 | return null; |
| 51 | } |
| 52 | |
| 53 | else{ |
| 54 | |
| 55 | co = decodeHexContentObject(result); |
| 56 | |
| 57 | if(LOG>2) { |
| 58 | console.log('DECODED CONTENT OBJECT'); |
| 59 | console.log(co); |
| 60 | } |
| 61 | return co; |
| 62 | } |
| 63 | } |
| 64 | else{ |
| 65 | |
| 66 | console.log('ERROR URL OR PORT NOT SET'); |
| 67 | |
| 68 | return null; |
| 69 | |
| 70 | } |
| 71 | |
| 72 | |
| 73 | } |
| 74 | |
Jeff Thompson | e06b31e | 2012-09-30 17:19:19 -0700 | [diff] [blame] | 75 | NDN.prototype.put = function(name,content){ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 76 | if(this.host!=null && this.port!=null){ |
| 77 | |
Axel Colin de Verdiere | d363e63 | 2012-06-06 05:16:43 -0700 | [diff] [blame] | 78 | var co = this.get("/%C1.M.S.localhost/%C1.M.SRV/ccnd"); |
| 79 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 80 | 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] | 81 | alert("Cannot contact router"); |
| 82 | |
| 83 | return null; |
| 84 | } |
| 85 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 86 | var ccnxnodename = co.signedInfo.publisher.publisherPublicKeyDigest; |
Axel Colin de Verdiere | d363e63 | 2012-06-06 05:16:43 -0700 | [diff] [blame] | 87 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 88 | name = name.trim(); |
| 89 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 90 | var fe = new ForwardingEntry('selfreg',new Name(name),null, null, 3,2147483647); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 91 | |
| 92 | var bytes = encodeForwardingEntry(fe); |
| 93 | |
| 94 | |
| 95 | var si = new SignedInfo(); |
| 96 | si.setFields(); |
| 97 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 98 | var co = new ContentObject(new Name(),si,bytes,new Signature()); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 99 | co.sign(); |
| 100 | |
| 101 | var coBinary = encodeToBinaryContentObject(co); |
| 102 | |
Axel Colin de Verdiere | d363e63 | 2012-06-06 05:16:43 -0700 | [diff] [blame] | 103 | //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'); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 104 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 105 | var interestName = new Name(['ccnx',ccnxnodename,'selfreg',coBinary]); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 106 | |
| 107 | int = new Interest(interestName); |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 108 | int.scope = 1; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 109 | |
| 110 | var hex = encodeToHexInterest(int); |
| 111 | |
| 112 | console.log('GOING TO PUT INTEREST OBJECT'); |
| 113 | |
| 114 | console.log(hex); |
| 115 | |
Meki Cherkaoui | 81bfc28 | 2012-06-06 03:23:25 -0700 | [diff] [blame] | 116 | //var result = put(this.host,this.port, hex,name); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 117 | |
Meki Cherkaoui | 81bfc28 | 2012-06-06 03:23:25 -0700 | [diff] [blame] | 118 | |
| 119 | //if(LOG>3)console.log('received interest'); //from host'+ host +':'+port+' with name '+name); |
| 120 | |
| 121 | //if(LOG>3)console.log('DATA '); |
| 122 | |
| 123 | //if(LOG>3)console.log(result); |
| 124 | |
| 125 | //interest = decodeHexInterest(result); |
| 126 | |
| 127 | //console.log('SUCCESSFULLY PARSED INTEREST'); |
| 128 | |
| 129 | console.log('CREATING ANSWER'); |
| 130 | var si = new SignedInfo(); |
| 131 | si.setFields(); |
| 132 | |
| 133 | var answer = DataUtils.toNumbersFromString(content); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 134 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 135 | var co = new ContentObject(new Name(name),si,answer,new Signature()); |
Meki Cherkaoui | 81bfc28 | 2012-06-06 03:23:25 -0700 | [diff] [blame] | 136 | co.sign(); |
| 137 | |
| 138 | |
| 139 | var outputHex = encodeToHexContentObject(co); |
| 140 | |
| 141 | //console.log('SENDING ANSWER'); |
| 142 | |
| 143 | //return get_java_socket_bridge().putAnswer(outputHex,name); |
| 144 | |
Meki Cherkaoui | 81bfc28 | 2012-06-06 03:23:25 -0700 | [diff] [blame] | 145 | var result = put(this.host,this.port, hex,name,outputHex); |
| 146 | |
| 147 | |
| 148 | return result; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 149 | } |
| 150 | else{ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 151 | console.log('ERROR URL OR PORT NOT SET'); |
| 152 | |
| 153 | return null; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 154 | } |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 155 | } |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 156 | |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 157 | /** Encode name as an Interest. If template is not null, use its attributes. |
| 158 | * Send the interest to host:port, read the entire response and call |
| 159 | * closure.upcall(Closure.UPCALL_CONTENT (or Closure.UPCALL_CONTENT_UNVERIFIED), |
Jeff Thompson | 97f2743 | 2012-10-16 00:28:03 -0700 | [diff] [blame] | 160 | * new UpcallInfo(this, interest, 0, contentObject)). |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 161 | */ |
| 162 | NDN.prototype.expressInterest = function( |
| 163 | // Name |
| 164 | name, |
| 165 | // Closure |
| 166 | closure, |
| 167 | // Interest |
| 168 | template) { |
| 169 | if (this.host == null || this.port == null) { |
| 170 | dump('ERROR host OR port NOT SET\n'); |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | interest = new Interest(name); |
| 175 | if (template != null) { |
Jeff Thompson | 4404ab5 | 2012-10-21 10:29:48 -0700 | [diff] [blame^] | 176 | interest.minSuffixComponents = template.minSuffixComponents; |
| 177 | interest.maxSuffixComponents = template.maxSuffixComponents; |
| 178 | interest.publisherPublicKeyDigest = template.publisherPublicKeyDigest; |
| 179 | interest.exclude = template.exclude; |
| 180 | interest.childSelector = template.childSelector; |
| 181 | interest.answerOriginKind = template.answerOriginKind; |
| 182 | interest.scope = template.scope; |
| 183 | interest.interestLifetime = template.interestLifetime; |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 184 | } |
| 185 | else |
Jeff Thompson | 741108b | 2012-10-15 23:07:09 -0700 | [diff] [blame] | 186 | interest.interestLifetime = 4200; |
Jeff Thompson | 97f2743 | 2012-10-16 00:28:03 -0700 | [diff] [blame] | 187 | |
| 188 | var encoder = new BinaryXMLEncoder(); |
| 189 | interest.to_ccnb(encoder); |
| 190 | var outputData = DataUtils.toString(encoder.getReducedOstream()); |
| 191 | encoder = null; |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 192 | |
| 193 | var dataListener = { |
| 194 | onReceivedData : function(result) { |
| 195 | if (result == null || result == undefined || result.length == 0) |
| 196 | listener.onReceivedContentObject(null); |
| 197 | else { |
| 198 | var decoder = new BinaryXMLDecoder(result); |
| 199 | var co = new ContentObject(); |
| 200 | co.from_ccnb(decoder); |
| 201 | |
| 202 | if(LOG>2) { |
| 203 | dump('DECODED CONTENT OBJECT\n'); |
| 204 | dump(co); |
| 205 | dump('\n'); |
| 206 | } |
| 207 | |
| 208 | // TODO: verify the content object and set kind to UPCALL_CONTENT. |
Jeff Thompson | 741108b | 2012-10-15 23:07:09 -0700 | [diff] [blame] | 209 | var result = closure.upcall(Closure.UPCALL_CONTENT_UNVERIFIED, |
| 210 | new UpcallInfo(this, interest, 0, co)); |
| 211 | // TODO: Check result for Closure.RESULT_OK, etc. |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
Jeff Thompson | 97f2743 | 2012-10-16 00:28:03 -0700 | [diff] [blame] | 216 | // The application includes a source file that defines readAllFromSocket |
| 217 | // according to the application's communication method. |
| 218 | readAllFromSocket(this.host, this.port, outputData, dataListener); |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 219 | }; |
| 220 | |