blob: 2b36d5b003d763db8e96748ed49a1b8c117d3613 [file] [log] [blame]
Wentao Shangc0311e52012-12-03 10:38:23 -08001/**
Jeff Thompson5b265a72012-11-12 01:13:08 -08002 * @author: Meki Cherkaoui, Jeff Thompson, Wentao Shang
Jeff Thompson745026e2012-10-13 12:49:20 -07003 * See COPYING for copyright and distribution information.
4 * This class represents the top-level object for communicating with an NDN host.
5 */
Meki Cherkaoui8f173612012-06-06 01:05:40 -07006
Jeff Thompson3c263812012-12-01 17:20:28 -08007var LOG = 0;
Jeff Thompson5b265a72012-11-12 01:13:08 -08008
Jeff Thompsone06b31e2012-09-30 17:19:19 -07009/**
Jeff Thompson5b265a72012-11-12 01:13:08 -080010 * settings is an associative array with the following defaults:
11 * {
Jeff Thompson5b265a72012-11-12 01:13:08 -080012 * getTransport: function() { return new WebSocketTransport(); }
Jeff Thompsond3a80dc2012-12-16 17:52:43 -080013 * getHostAndPort: transport.defaultGetHostAndPort,
14 * host: 'localhost', // If null, use getHostAndPort when connecting.
15 * port: 9696,
Wentao Shangc0311e52012-12-03 10:38:23 -080016 * onopen: function() { if (LOG > 3) console.log("NDN connection established."); }
17 * onclose: function() { if (LOG > 3) console.log("NDN connection closed."); }
Jeff Thompson5b265a72012-11-12 01:13:08 -080018 * }
Jeff Thompsond3a80dc2012-12-16 17:52:43 -080019 *
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 Thompsone06b31e2012-09-30 17:19:19 -070022 */
Jeff Thompson5b265a72012-11-12 01:13:08 -080023var NDN = function NDN(settings) {
24 settings = (settings || {});
Jeff Thompson5b265a72012-11-12 01:13:08 -080025 var getTransport = (settings.getTransport || function() { return new WebSocketTransport(); });
Wentao Shang0e291c82012-12-02 23:36:29 -080026 this.transport = getTransport();
Jeff Thompsond3a80dc2012-12-16 17:52:43 -080027 this.getHostAndPort = (settings.getHostAndPort || this.transport.defaultGetHostAndPort);
28 this.host = (settings.host !== undefined ? settings.host : 'localhost');
29 this.port = (settings.port || 9696);
Wentao Shang0e291c82012-12-02 23:36:29 -080030 this.readyStatus = NDN.UNOPEN;
31 // Event handler
Wentao Shangc0311e52012-12-03 10:38:23 -080032 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 Cherkaoui8f173612012-06-06 01:05:40 -070034};
35
Wentao Shang0e291c82012-12-02 23:36:29 -080036NDN.UNOPEN = 0; // created but not opened yet
37NDN.OPENED = 1; // connection to ccnd opened
38NDN.CLOSED = 2; // connection to ccnd closed
Jeff Thompson5b265a72012-11-12 01:13:08 -080039
Jeff Thompsonbd25df22012-12-13 21:50:13 -080040NDN.ccndIdFetcher = '/%C1.M.S.localhost/%C1.M.SRV/ccnd/KEY';
Jeff Thompson5b265a72012-11-12 01:13:08 -080041
Jeff Thompsone06b31e2012-09-30 17:19:19 -070042NDN.prototype.createRoute = function(host,port){
Meki Cherkaoui8f173612012-06-06 01:05:40 -070043 this.host=host;
44 this.port=port;
45}
46
Jeff Thompsonbe85be62012-12-13 22:32:01 -080047// For fetching data
48NDN.PITTable = new Array();
49
50var 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.
56NDN.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 Thompsond3a80dc2012-12-16 17:52:43 -080072/*
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 */
76NDN.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 Thompson34419762012-10-15 22:24:12 -070089/** 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 Thompson97f27432012-10-16 00:28:03 -070092 * new UpcallInfo(this, interest, 0, contentObject)).
Jeff Thompson34419762012-10-15 22:24:12 -070093 */
94NDN.prototype.expressInterest = function(
95 // Name
96 name,
97 // Closure
98 closure,
99 // Interest
100 template) {
Jeff Thompson5b265a72012-11-12 01:13:08 -0800101 var interest = new Interest(name);
Jeff Thompson34419762012-10-15 22:24:12 -0700102 if (template != null) {
Jeff Thompson4404ab52012-10-21 10:29:48 -0700103 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 Thompson34419762012-10-15 22:24:12 -0700111 }
112 else
Jeff Thompson42806a12012-12-29 18:19:39 -0800113 interest.interestLifetime = 4000; // default interest timeout value in milliseconds.
Jeff Thompson34419762012-10-15 22:24:12 -0700114
Jeff Thompsond3a80dc2012-12-16 17:52:43 -0800115 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 Thompson5b265a72012-11-12 01:13:08 -0800124
125NDN.prototype.registerPrefix = function(name, closure, flag) {
126 return this.transport.registerPrefix(this, name, closure, flag);
127}
Jeff Thompsond3a80dc2012-12-16 17:52:43 -0800128
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 */
133NDN.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));
Jeff Thompson42806a12012-12-29 18:19:39 -0800153 interest.interestLifetime = 4000; // milliseconds
Jeff Thompsond3a80dc2012-12-16 17:52:43 -0800154
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
166NDN.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
176NDN.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