blob: a61603e4ef01ced1b9d24b051edac07b135b4070 [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
Wentao Shang82854bd2012-12-27 14:14:41 -080047
48NDN.KeyStore = new Array();
49
50var KeyStoreEntry = function KeyStoreEntry(name, key, rsa) {
51 this.keyName = name; // KeyName
52 this.keyHex = key; // Raw key hex string
53 this.rsaKey = rsa; // RSA key
54};
55
56NDN.getKeyByName = function(/* KeyName */ name) {
57 var result = null;
58
59 for (var i = 0; i < NDN.KeyStore.length; i++) {
60 if (NDN.KeyStore[i].keyName.matches_name(name.contentName)) {
61 if (result == null ||
62 NDN.KeyStore[i].keyName.contentName.components.length > result.keyName.contentName.components.length)
63 result = NDN.KeyStore[i];
64 }
65 }
66
67 return result;
68};
69
Jeff Thompsonbe85be62012-12-13 22:32:01 -080070// For fetching data
71NDN.PITTable = new Array();
72
73var PITEntry = function PITEntry(interest, closure) {
74 this.interest = interest; // Interest
75 this.closure = closure; // Closure
76};
77
78// Return the longest entry from NDN.PITTable that matches name.
79NDN.getEntryForExpressedInterest = function(/*Name*/ name) {
80 // TODO: handle multiple matches? Maybe not from registerPrefix because multiple ContentObject
81 // could be sent for one Interest?
82 var result = null;
83
84 for (var i = 0; i < NDN.PITTable.length; i++) {
85 if (NDN.PITTable[i].interest.matches_name(name)) {
86 if (result == null ||
87 NDN.PITTable[i].interest.name.components.length > result.interest.name.components.length)
88 result = NDN.PITTable[i];
89 }
90 }
91
92 return result;
93};
94
Jeff Thompsond3a80dc2012-12-16 17:52:43 -080095/*
96 * Return a function that selects a host at random from hostList and returns { host: host, port: port }.
97 * If no more hosts remain, return null.
98 */
99NDN.makeShuffledGetHostAndPort = function(hostList, port) {
100 // Make a copy.
101 hostList = hostList.slice(0, hostList.length);
102 DataUtils.shuffle(hostList);
103
104 return function() {
105 if (hostList.length == 0)
106 return null;
107
108 return { host: hostList.splice(0, 1)[0], port: port };
109 };
110};
111
Jeff Thompson34419762012-10-15 22:24:12 -0700112/** Encode name as an Interest. If template is not null, use its attributes.
113 * Send the interest to host:port, read the entire response and call
114 * closure.upcall(Closure.UPCALL_CONTENT (or Closure.UPCALL_CONTENT_UNVERIFIED),
Jeff Thompson97f27432012-10-16 00:28:03 -0700115 * new UpcallInfo(this, interest, 0, contentObject)).
Jeff Thompson34419762012-10-15 22:24:12 -0700116 */
117NDN.prototype.expressInterest = function(
118 // Name
119 name,
120 // Closure
121 closure,
122 // Interest
123 template) {
Jeff Thompson5b265a72012-11-12 01:13:08 -0800124 var interest = new Interest(name);
Jeff Thompson34419762012-10-15 22:24:12 -0700125 if (template != null) {
Jeff Thompson4404ab52012-10-21 10:29:48 -0700126 interest.minSuffixComponents = template.minSuffixComponents;
127 interest.maxSuffixComponents = template.maxSuffixComponents;
128 interest.publisherPublicKeyDigest = template.publisherPublicKeyDigest;
129 interest.exclude = template.exclude;
130 interest.childSelector = template.childSelector;
131 interest.answerOriginKind = template.answerOriginKind;
132 interest.scope = template.scope;
133 interest.interestLifetime = template.interestLifetime;
Jeff Thompson34419762012-10-15 22:24:12 -0700134 }
135 else
Jeff Thompson84db2632012-12-09 22:31:39 -0800136 interest.interestLifetime = 4.0; // default interest timeout value in seconds.
Jeff Thompson34419762012-10-15 22:24:12 -0700137
Jeff Thompsond3a80dc2012-12-16 17:52:43 -0800138 if (this.host == null || this.port == null) {
139 if (this.getHostAndPort == null)
140 console.log('ERROR: host OR port NOT SET');
141 else
142 this.connectAndExpressInterest(interest, closure);
143 }
144 else
145 this.transport.expressInterest(this, interest, closure);
146};
Jeff Thompson5b265a72012-11-12 01:13:08 -0800147
148NDN.prototype.registerPrefix = function(name, closure, flag) {
149 return this.transport.registerPrefix(this, name, closure, flag);
150}
Jeff Thompsond3a80dc2012-12-16 17:52:43 -0800151
152/*
153 * Assume this.getHostAndPort is not null. This is called when this.host is null or its host
154 * is not alive. Get a host and port, connect, then express callerInterest with callerClosure.
155 */
156NDN.prototype.connectAndExpressInterest = function(callerInterest, callerClosure) {
157 var hostAndPort = this.getHostAndPort();
158 if (hostAndPort == null) {
159 console.log('ERROR: No more hosts from getHostAndPort');
160 this.host = null;
161 return;
162 }
163
164 if (hostAndPort.host == this.host && hostAndPort.port == this.port) {
165 console.log('ERROR: The host returned by getHostAndPort is not alive: ' +
166 this.host + ":" + this.port);
167 return;
168 }
169
170 this.host = hostAndPort.host;
171 this.port = hostAndPort.port;
172 console.log("Trying host from getHostAndPort: " + this.host);
173
174 // Fetch the ccndId.
175 var interest = new Interest(new Name(NDN.ccndIdFetcher));
176 interest.interestLifetime = 4.0; // seconds
177
178 var thisNDN = this;
179 var timerID = setTimeout(function() {
180 console.log("Timeout waiting for host " + thisNDN.host);
181 // Try again.
182 thisNDN.connectAndExpressInterest(callerInterest, callerClosure);
183 }, 3000);
184
185 this.transport.expressInterest
186 (this, interest, new NDN.ConnectClosure(this, callerInterest, callerClosure, timerID));
187}
188
189NDN.ConnectClosure = function ConnectClosure(ndn, callerInterest, callerClosure, timerID) {
190 // Inherit from Closure.
191 Closure.call(this);
192
193 this.ndn = ndn;
194 this.callerInterest = callerInterest;
195 this.callerClosure = callerClosure;
196 this.timerID = timerID;
197};
198
199NDN.ConnectClosure.prototype.upcall = function(kind, upcallInfo) {
200 if (!(kind == Closure.UPCALL_CONTENT ||
201 kind == Closure.UPCALL_CONTENT_UNVERIFIED ||
202 kind == Closure.UPCALL_INTEREST))
203 // The upcall is not for us.
204 return Closure.RESULT_ERR;
205
206 // The host is alive, so cancel the timeout and issue the caller's interest.
207 clearTimeout(this.timerID);
208 console.log(this.ndn.host + ": Host is alive. Fetching callerInterest.");
209 this.ndn.transport.expressInterest(this.ndn, this.callerInterest, this.callerClosure);
210
211 return Closure.RESULT_OK;
212};
213