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 | |
Jeff Thompson | bd25df2 | 2012-12-13 21:50:13 -0800 | [diff] [blame] | 35 | NDN.ccndIdFetcher = '/%C1.M.S.localhost/%C1.M.SRV/ccnd/KEY'; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 36 | |
Jeff Thompson | e06b31e | 2012-09-30 17:19:19 -0700 | [diff] [blame] | 37 | NDN.prototype.createRoute = function(host,port){ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 38 | this.host=host; |
| 39 | this.port=port; |
| 40 | } |
| 41 | |
Jeff Thompson | be85be6 | 2012-12-13 22:32:01 -0800 | [diff] [blame^] | 42 | // For fetching data |
| 43 | NDN.PITTable = new Array(); |
| 44 | |
| 45 | var PITEntry = function PITEntry(interest, closure) { |
| 46 | this.interest = interest; // Interest |
| 47 | this.closure = closure; // Closure |
| 48 | }; |
| 49 | |
| 50 | // Return the longest entry from NDN.PITTable that matches name. |
| 51 | NDN.getEntryForExpressedInterest = function(/*Name*/ name) { |
| 52 | // TODO: handle multiple matches? Maybe not from registerPrefix because multiple ContentObject |
| 53 | // could be sent for one Interest? |
| 54 | var result = null; |
| 55 | |
| 56 | for (var i = 0; i < NDN.PITTable.length; i++) { |
| 57 | if (NDN.PITTable[i].interest.matches_name(name)) { |
| 58 | if (result == null || |
| 59 | NDN.PITTable[i].interest.name.components.length > result.interest.name.components.length) |
| 60 | result = NDN.PITTable[i]; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | return result; |
| 65 | }; |
| 66 | |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 67 | /** Encode name as an Interest. If template is not null, use its attributes. |
| 68 | * Send the interest to host:port, read the entire response and call |
| 69 | * closure.upcall(Closure.UPCALL_CONTENT (or Closure.UPCALL_CONTENT_UNVERIFIED), |
Jeff Thompson | 97f2743 | 2012-10-16 00:28:03 -0700 | [diff] [blame] | 70 | * new UpcallInfo(this, interest, 0, contentObject)). |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 71 | */ |
| 72 | NDN.prototype.expressInterest = function( |
| 73 | // Name |
| 74 | name, |
| 75 | // Closure |
| 76 | closure, |
| 77 | // Interest |
| 78 | template) { |
| 79 | if (this.host == null || this.port == null) { |
| 80 | dump('ERROR host OR port NOT SET\n'); |
| 81 | return; |
| 82 | } |
| 83 | |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 84 | var interest = new Interest(name); |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 85 | if (template != null) { |
Jeff Thompson | 4404ab5 | 2012-10-21 10:29:48 -0700 | [diff] [blame] | 86 | interest.minSuffixComponents = template.minSuffixComponents; |
| 87 | interest.maxSuffixComponents = template.maxSuffixComponents; |
| 88 | interest.publisherPublicKeyDigest = template.publisherPublicKeyDigest; |
| 89 | interest.exclude = template.exclude; |
| 90 | interest.childSelector = template.childSelector; |
| 91 | interest.answerOriginKind = template.answerOriginKind; |
| 92 | interest.scope = template.scope; |
| 93 | interest.interestLifetime = template.interestLifetime; |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 94 | } |
| 95 | else |
Jeff Thompson | 84db263 | 2012-12-09 22:31:39 -0800 | [diff] [blame] | 96 | interest.interestLifetime = 4.0; // default interest timeout value in seconds. |
Jeff Thompson | 97f2743 | 2012-10-16 00:28:03 -0700 | [diff] [blame] | 97 | |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 98 | this.transport.expressInterest(this, interest, closure); |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 101 | |
| 102 | NDN.prototype.registerPrefix = function(name, closure, flag) { |
| 103 | return this.transport.registerPrefix(this, name, closure, flag); |
| 104 | } |