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 | * { |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 12 | * getTransport: function() { return new WebSocketTransport(); } |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 13 | * getHostAndPort: transport.defaultGetHostAndPort, |
| 14 | * host: 'localhost', // If null, use getHostAndPort when connecting. |
| 15 | * port: 9696, |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 16 | * onopen: function() { if (LOG > 3) console.log("NDN connection established."); } |
| 17 | * onclose: function() { if (LOG > 3) console.log("NDN connection closed."); } |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 18 | * } |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 19 | * |
| 20 | * getHostAndPort is a function, on each call it returns a new { host: host, port: port } or |
| 21 | * null if there are no more hosts. |
Jeff Thompson | e06b31e | 2012-09-30 17:19:19 -0700 | [diff] [blame] | 22 | */ |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 23 | var NDN = function NDN(settings) { |
| 24 | settings = (settings || {}); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 25 | var getTransport = (settings.getTransport || function() { return new WebSocketTransport(); }); |
Wentao Shang | 0e291c8 | 2012-12-02 23:36:29 -0800 | [diff] [blame] | 26 | this.transport = getTransport(); |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 27 | this.getHostAndPort = (settings.getHostAndPort || this.transport.defaultGetHostAndPort); |
| 28 | this.host = (settings.host !== undefined ? settings.host : 'localhost'); |
| 29 | this.port = (settings.port || 9696); |
Wentao Shang | 0e291c8 | 2012-12-02 23:36:29 -0800 | [diff] [blame] | 30 | this.readyStatus = NDN.UNOPEN; |
| 31 | // Event handler |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 32 | this.onopen = (settings.onopen || function() { if (LOG > 3) console.log("NDN connection established."); }); |
| 33 | 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] | 34 | }; |
| 35 | |
Wentao Shang | 0e291c8 | 2012-12-02 23:36:29 -0800 | [diff] [blame] | 36 | NDN.UNOPEN = 0; // created but not opened yet |
| 37 | NDN.OPENED = 1; // connection to ccnd opened |
| 38 | NDN.CLOSED = 2; // connection to ccnd closed |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 39 | |
Jeff Thompson | bd25df2 | 2012-12-13 21:50:13 -0800 | [diff] [blame] | 40 | NDN.ccndIdFetcher = '/%C1.M.S.localhost/%C1.M.SRV/ccnd/KEY'; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 41 | |
Jeff Thompson | e06b31e | 2012-09-30 17:19:19 -0700 | [diff] [blame] | 42 | NDN.prototype.createRoute = function(host,port){ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 43 | this.host=host; |
| 44 | this.port=port; |
| 45 | } |
| 46 | |
Jeff Thompson | be85be6 | 2012-12-13 22:32:01 -0800 | [diff] [blame] | 47 | // For fetching data |
| 48 | NDN.PITTable = new Array(); |
| 49 | |
| 50 | var PITEntry = function PITEntry(interest, closure) { |
| 51 | this.interest = interest; // Interest |
| 52 | this.closure = closure; // Closure |
| 53 | }; |
| 54 | |
| 55 | // Return the longest entry from NDN.PITTable that matches name. |
| 56 | NDN.getEntryForExpressedInterest = function(/*Name*/ name) { |
| 57 | // TODO: handle multiple matches? Maybe not from registerPrefix because multiple ContentObject |
| 58 | // could be sent for one Interest? |
| 59 | var result = null; |
| 60 | |
| 61 | for (var i = 0; i < NDN.PITTable.length; i++) { |
| 62 | if (NDN.PITTable[i].interest.matches_name(name)) { |
| 63 | if (result == null || |
| 64 | NDN.PITTable[i].interest.name.components.length > result.interest.name.components.length) |
| 65 | result = NDN.PITTable[i]; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return result; |
| 70 | }; |
| 71 | |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 72 | /* |
| 73 | * Return a function that selects a host at random from hostList and returns { host: host, port: port }. |
| 74 | * If no more hosts remain, return null. |
| 75 | */ |
| 76 | NDN.makeShuffledGetHostAndPort = function(hostList, port) { |
| 77 | // Make a copy. |
| 78 | hostList = hostList.slice(0, hostList.length); |
| 79 | DataUtils.shuffle(hostList); |
| 80 | |
| 81 | return function() { |
| 82 | if (hostList.length == 0) |
| 83 | return null; |
| 84 | |
| 85 | return { host: hostList.splice(0, 1)[0], port: port }; |
| 86 | }; |
| 87 | }; |
| 88 | |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 89 | /** Encode name as an Interest. If template is not null, use its attributes. |
| 90 | * Send the interest to host:port, read the entire response and call |
| 91 | * closure.upcall(Closure.UPCALL_CONTENT (or Closure.UPCALL_CONTENT_UNVERIFIED), |
Jeff Thompson | 97f2743 | 2012-10-16 00:28:03 -0700 | [diff] [blame] | 92 | * new UpcallInfo(this, interest, 0, contentObject)). |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 93 | */ |
| 94 | NDN.prototype.expressInterest = function( |
| 95 | // Name |
| 96 | name, |
| 97 | // Closure |
| 98 | closure, |
| 99 | // Interest |
| 100 | template) { |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 101 | var interest = new Interest(name); |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 102 | if (template != null) { |
Jeff Thompson | 4404ab5 | 2012-10-21 10:29:48 -0700 | [diff] [blame] | 103 | interest.minSuffixComponents = template.minSuffixComponents; |
| 104 | interest.maxSuffixComponents = template.maxSuffixComponents; |
| 105 | interest.publisherPublicKeyDigest = template.publisherPublicKeyDigest; |
| 106 | interest.exclude = template.exclude; |
| 107 | interest.childSelector = template.childSelector; |
| 108 | interest.answerOriginKind = template.answerOriginKind; |
| 109 | interest.scope = template.scope; |
| 110 | interest.interestLifetime = template.interestLifetime; |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 111 | } |
| 112 | else |
Jeff Thompson | 84db263 | 2012-12-09 22:31:39 -0800 | [diff] [blame] | 113 | interest.interestLifetime = 4.0; // default interest timeout value in seconds. |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 114 | |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 115 | if (this.host == null || this.port == null) { |
| 116 | if (this.getHostAndPort == null) |
| 117 | console.log('ERROR: host OR port NOT SET'); |
| 118 | else |
| 119 | this.connectAndExpressInterest(interest, closure); |
| 120 | } |
| 121 | else |
| 122 | this.transport.expressInterest(this, interest, closure); |
| 123 | }; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 124 | |
| 125 | NDN.prototype.registerPrefix = function(name, closure, flag) { |
| 126 | return this.transport.registerPrefix(this, name, closure, flag); |
| 127 | } |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 128 | |
| 129 | /* |
| 130 | * Assume this.getHostAndPort is not null. This is called when this.host is null or its host |
| 131 | * is not alive. Get a host and port, connect, then express callerInterest with callerClosure. |
| 132 | */ |
| 133 | NDN.prototype.connectAndExpressInterest = function(callerInterest, callerClosure) { |
| 134 | var hostAndPort = this.getHostAndPort(); |
| 135 | if (hostAndPort == null) { |
| 136 | console.log('ERROR: No more hosts from getHostAndPort'); |
| 137 | this.host = null; |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | if (hostAndPort.host == this.host && hostAndPort.port == this.port) { |
| 142 | console.log('ERROR: The host returned by getHostAndPort is not alive: ' + |
| 143 | this.host + ":" + this.port); |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | this.host = hostAndPort.host; |
| 148 | this.port = hostAndPort.port; |
| 149 | console.log("Trying host from getHostAndPort: " + this.host); |
| 150 | |
| 151 | // Fetch the ccndId. |
| 152 | var interest = new Interest(new Name(NDN.ccndIdFetcher)); |
| 153 | interest.interestLifetime = 4.0; // seconds |
| 154 | |
| 155 | var thisNDN = this; |
| 156 | var timerID = setTimeout(function() { |
| 157 | console.log("Timeout waiting for host " + thisNDN.host); |
| 158 | // Try again. |
| 159 | thisNDN.connectAndExpressInterest(callerInterest, callerClosure); |
| 160 | }, 3000); |
| 161 | |
| 162 | this.transport.expressInterest |
| 163 | (this, interest, new NDN.ConnectClosure(this, callerInterest, callerClosure, timerID)); |
| 164 | } |
| 165 | |
| 166 | NDN.ConnectClosure = function ConnectClosure(ndn, callerInterest, callerClosure, timerID) { |
| 167 | // Inherit from Closure. |
| 168 | Closure.call(this); |
| 169 | |
| 170 | this.ndn = ndn; |
| 171 | this.callerInterest = callerInterest; |
| 172 | this.callerClosure = callerClosure; |
| 173 | this.timerID = timerID; |
| 174 | }; |
| 175 | |
| 176 | NDN.ConnectClosure.prototype.upcall = function(kind, upcallInfo) { |
| 177 | if (!(kind == Closure.UPCALL_CONTENT || |
| 178 | kind == Closure.UPCALL_CONTENT_UNVERIFIED || |
| 179 | kind == Closure.UPCALL_INTEREST)) |
| 180 | // The upcall is not for us. |
| 181 | return Closure.RESULT_ERR; |
| 182 | |
| 183 | // The host is alive, so cancel the timeout and issue the caller's interest. |
| 184 | clearTimeout(this.timerID); |
| 185 | console.log(this.ndn.host + ": Host is alive. Fetching callerInterest."); |
| 186 | this.ndn.transport.expressInterest(this.ndn, this.callerInterest, this.callerClosure); |
| 187 | |
| 188 | return Closure.RESULT_OK; |
| 189 | }; |
| 190 | |