Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [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 | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 7 | var LOG = 0; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 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(); } |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame^] | 15 | * onopen: function() { if (LOG > 3) console.log("NDN connection established."); } |
| 16 | * onclose: function() { if (LOG > 3) console.log("NDN connection closed."); } |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 17 | * } |
Jeff Thompson | e06b31e | 2012-09-30 17:19:19 -0700 | [diff] [blame] | 18 | */ |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 19 | var NDN = function NDN(settings) { |
| 20 | settings = (settings || {}); |
| 21 | this.host = (settings.host || "localhost"); |
| 22 | this.port = (settings.port || 9696); |
| 23 | var getTransport = (settings.getTransport || function() { return new WebSocketTransport(); }); |
Wentao Shang | 0e291c8 | 2012-12-02 23:36:29 -0800 | [diff] [blame] | 24 | this.transport = getTransport(); |
| 25 | this.readyStatus = NDN.UNOPEN; |
| 26 | // Event handler |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame^] | 27 | this.onopen = (settings.onopen || function() { if (LOG > 3) console.log("NDN connection established."); }); |
| 28 | this.onclose = (settings.onclose || function() { if (LOG > 3) console.log("NDN connection closed."); }); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 29 | }; |
| 30 | |
Wentao Shang | 0e291c8 | 2012-12-02 23:36:29 -0800 | [diff] [blame] | 31 | NDN.UNOPEN = 0; // created but not opened yet |
| 32 | NDN.OPENED = 1; // connection to ccnd opened |
| 33 | NDN.CLOSED = 2; // connection to ccnd closed |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 34 | |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame^] | 35 | NDN.InterestTimeOut = 5000; // 5000 ms timeout for pending interest |
| 36 | |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 37 | /* Java Socket Bridge and XPCOM transport */ |
| 38 | |
Jeff Thompson | e06b31e | 2012-09-30 17:19:19 -0700 | [diff] [blame] | 39 | NDN.prototype.createRoute = function(host,port){ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 40 | this.host=host; |
| 41 | this.port=port; |
| 42 | } |
| 43 | |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 44 | /** Encode name as an Interest. If template is not null, use its attributes. |
| 45 | * Send the interest to host:port, read the entire response and call |
| 46 | * closure.upcall(Closure.UPCALL_CONTENT (or Closure.UPCALL_CONTENT_UNVERIFIED), |
Jeff Thompson | 97f2743 | 2012-10-16 00:28:03 -0700 | [diff] [blame] | 47 | * new UpcallInfo(this, interest, 0, contentObject)). |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 48 | */ |
| 49 | NDN.prototype.expressInterest = function( |
| 50 | // Name |
| 51 | name, |
| 52 | // Closure |
| 53 | closure, |
| 54 | // Interest |
| 55 | template) { |
| 56 | if (this.host == null || this.port == null) { |
| 57 | dump('ERROR host OR port NOT SET\n'); |
| 58 | return; |
| 59 | } |
| 60 | |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 61 | var interest = new Interest(name); |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 62 | if (template != null) { |
Jeff Thompson | 4404ab5 | 2012-10-21 10:29:48 -0700 | [diff] [blame] | 63 | interest.minSuffixComponents = template.minSuffixComponents; |
| 64 | interest.maxSuffixComponents = template.maxSuffixComponents; |
| 65 | interest.publisherPublicKeyDigest = template.publisherPublicKeyDigest; |
| 66 | interest.exclude = template.exclude; |
| 67 | interest.childSelector = template.childSelector; |
| 68 | interest.answerOriginKind = template.answerOriginKind; |
| 69 | interest.scope = template.scope; |
| 70 | interest.interestLifetime = template.interestLifetime; |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 71 | } |
| 72 | else |
Jeff Thompson | 741108b | 2012-10-15 23:07:09 -0700 | [diff] [blame] | 73 | interest.interestLifetime = 4200; |
Jeff Thompson | 97f2743 | 2012-10-16 00:28:03 -0700 | [diff] [blame] | 74 | |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 75 | this.transport.expressInterest(this, interest, closure); |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 78 | |
| 79 | NDN.prototype.registerPrefix = function(name, closure, flag) { |
| 80 | return this.transport.registerPrefix(this, name, closure, flag); |
| 81 | } |