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 | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 33 | /** Encode name as an Interest. If template is not null, use its attributes. |
| 34 | * Send the interest to host:port, read the entire response and call |
| 35 | * closure.upcall(Closure.UPCALL_CONTENT (or Closure.UPCALL_CONTENT_UNVERIFIED), |
Jeff Thompson | 97f2743 | 2012-10-16 00:28:03 -0700 | [diff] [blame] | 36 | * new UpcallInfo(this, interest, 0, contentObject)). |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 37 | */ |
| 38 | NDN.prototype.expressInterest = function( |
| 39 | // Name |
| 40 | name, |
| 41 | // Closure |
| 42 | closure, |
| 43 | // Interest |
| 44 | template) { |
| 45 | if (this.host == null || this.port == null) { |
| 46 | dump('ERROR host OR port NOT SET\n'); |
| 47 | return; |
| 48 | } |
| 49 | |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 50 | var interest = new Interest(name); |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 51 | if (template != null) { |
Jeff Thompson | 4404ab5 | 2012-10-21 10:29:48 -0700 | [diff] [blame] | 52 | interest.minSuffixComponents = template.minSuffixComponents; |
| 53 | interest.maxSuffixComponents = template.maxSuffixComponents; |
| 54 | interest.publisherPublicKeyDigest = template.publisherPublicKeyDigest; |
| 55 | interest.exclude = template.exclude; |
| 56 | interest.childSelector = template.childSelector; |
| 57 | interest.answerOriginKind = template.answerOriginKind; |
| 58 | interest.scope = template.scope; |
| 59 | interest.interestLifetime = template.interestLifetime; |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 60 | } |
| 61 | else |
Jeff Thompson | 741108b | 2012-10-15 23:07:09 -0700 | [diff] [blame] | 62 | interest.interestLifetime = 4200; |
Jeff Thompson | 97f2743 | 2012-10-16 00:28:03 -0700 | [diff] [blame] | 63 | |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 64 | this.transport.expressInterest(this, interest, closure); |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 67 | |
| 68 | NDN.prototype.registerPrefix = function(name, closure, flag) { |
| 69 | return this.transport.registerPrefix(this, name, closure, flag); |
| 70 | } |