Change connectAndExpressInterest to connectAndExecute to be used by registerPrefix too.
diff --git a/js/NDN.js b/js/NDN.js
index a796931..30d5d3f 100644
--- a/js/NDN.js
+++ b/js/NDN.js
@@ -165,8 +165,11 @@
if (this.host == null || this.port == null) {
if (this.getHostAndPort == null)
console.log('ERROR: host OR port NOT SET');
- else
- this.connectAndExpressInterest(interest, closure);
+ else {
+ var thisNDN = this;
+ this.connectAndExecute
+ (function() { thisNDN.transport.expressInterest(thisNDN, interest, closure); });
+ }
}
else
this.transport.expressInterest(this, interest, closure);
@@ -397,9 +400,9 @@
/*
* Assume this.getHostAndPort is not null. This is called when this.host is null or its host
- * is not alive. Get a host and port, connect, then express callerInterest with callerClosure.
+ * is not alive. Get a host and port, connect, then execute onConnected().
*/
-NDN.prototype.connectAndExpressInterest = function(callerInterest, callerClosure) {
+NDN.prototype.connectAndExecute = function(onConnected) {
var hostAndPort = this.getHostAndPort();
if (hostAndPort == null) {
console.log('ERROR: No more hosts from getHostAndPort');
@@ -415,7 +418,7 @@
this.host = hostAndPort.host;
this.port = hostAndPort.port;
- if (LOG>3) console.log("Trying host from getHostAndPort: " + this.host);
+ if (LOG>3) console.log("Connect: trying host from getHostAndPort: " + this.host);
// Fetch any content.
var interest = new Interest(new Name("/"));
@@ -423,22 +426,21 @@
var thisNDN = this;
var timerID = setTimeout(function() {
- if (LOG>3) console.log("Timeout waiting for host " + thisNDN.host);
+ if (LOG>3) console.log("Connect: timeout waiting for host " + thisNDN.host);
// Try again.
- thisNDN.connectAndExpressInterest(callerInterest, callerClosure);
+ thisNDN.connectAndExecute(onConnected);
}, 3000);
this.transport.expressInterest
- (this, interest, new NDN.ConnectClosure(this, callerInterest, callerClosure, timerID));
+ (this, interest, new NDN.ConnectClosure(this, onConnected, timerID));
};
-NDN.ConnectClosure = function ConnectClosure(ndn, callerInterest, callerClosure, timerID) {
+NDN.ConnectClosure = function ConnectClosure(ndn, onConnected, timerID) {
// Inherit from Closure.
Closure.call(this);
this.ndn = ndn;
- this.callerInterest = callerInterest;
- this.callerClosure = callerClosure;
+ this.onConnected = onConnected;
this.timerID = timerID;
};
@@ -448,10 +450,9 @@
// The upcall is not for us.
return Closure.RESULT_ERR;
- // The host is alive, so cancel the timeout and issue the caller's interest.
+ // The host is alive, so cancel the timeout and continue with onConnected().
clearTimeout(this.timerID);
- if (LOG>3) console.log(this.ndn.host + ": Host is alive. Fetching callerInterest.");
- this.ndn.transport.expressInterest(this.ndn, this.callerInterest, this.callerClosure);
+ this.onConnected();
return Closure.RESULT_OK;
};