Move expressInterestHelpler from WebSocketTransport up to NDN.
diff --git a/js/NDN.js b/js/NDN.js
index 31aa8a7..5f1c746 100644
--- a/js/NDN.js
+++ b/js/NDN.js
@@ -175,6 +175,43 @@
this.transport.expressInterest(this, interest, closure);
};
+/*
+ * Do the work of expressInterest once we know we are connected. Set the PITTable and call
+ * this.transport.send to send the interest.
+ */
+NDN.prototype.expressInterestHelper = function(interest, closure) {
+ //TODO: check local content store first
+ if (closure != null) {
+ var pitEntry = new PITEntry(interest, closure);
+ // TODO: This needs to be a single thread-safe transaction on a global object.
+ NDN.PITTable.push(pitEntry);
+ closure.pitEntry = pitEntry;
+ }
+
+ // Set interest timer
+ var thisNDN = this;
+ if (closure != null) {
+ pitEntry.timerID = setTimeout(function() {
+ if (LOG > 3) console.log("Interest time out.");
+
+ // Remove PIT entry from NDN.PITTable.
+ // TODO: Make this a thread-safe operation on the global PITTable.
+ var index = NDN.PITTable.indexOf(pitEntry);
+ //console.log(NDN.PITTable);
+ if (index >= 0)
+ NDN.PITTable.splice(index, 1);
+ //console.log(NDN.PITTable);
+ //console.log(pitEntry.interest.name.getName());
+
+ // Raise closure callback
+ closure.upcall(Closure.UPCALL_INTEREST_TIMED_OUT, new UpcallInfo(thisNDN, interest, 0, null));
+ }, interest.interestLifetime); // interestLifetime is in milliseconds.
+ //console.log(closure.timerID);
+ }
+
+ this.transport.send(encodeToBinaryInterest(interest));
+};
+
NDN.prototype.registerPrefix = function(name, closure, flag) {
var thisNDN = this;
var onConnected = function() {
@@ -242,7 +279,9 @@
return Closure.RESULT_OK;
};
-// Do the work of registerPrefix once we know we are connected with a ccndid.
+/*
+ * Do the work of registerPrefix once we know we are connected with a ccndid.
+ */
NDN.prototype.registerPrefixHelper = function(name, closure, flag) {
var fe = new ForwardingEntry('selfreg', name, null, null, 3, 2147483647);
var bytes = encodeForwardingEntry(fe);
diff --git a/js/WebSocketTransport.js b/js/WebSocketTransport.js
index 316a1f0..15862f4 100644
--- a/js/WebSocketTransport.js
+++ b/js/WebSocketTransport.js
@@ -96,42 +96,8 @@
}
WebSocketTransport.prototype.expressInterest = function(ndn, interest, closure) {
- if (this.ws == null || this.connectedHost != ndn.host || this.connectedPort != ndn.port) {
- var self = this;
- this.connect(ndn, function() { self.expressInterestHelper(ndn, interest, closure); });
- }
+ if (this.ws == null || this.connectedHost != ndn.host || this.connectedPort != ndn.port)
+ this.connect(ndn, function() { ndn.expressInterestHelper(interest, closure); });
else
- this.expressInterestHelper(ndn, interest, closure);
-};
-
-WebSocketTransport.prototype.expressInterestHelper = function(ndn, interest, closure) {
- //TODO: check local content store first
- if (closure != null) {
- var pitEntry = new PITEntry(interest, closure);
- // TODO: This needs to be a single thread-safe transaction on a global object.
- NDN.PITTable.push(pitEntry);
- closure.pitEntry = pitEntry;
- }
-
- // Set interest timer
- if (closure != null) {
- pitEntry.timerID = setTimeout(function() {
- if (LOG > 3) console.log("Interest time out.");
-
- // Remove PIT entry from NDN.PITTable.
- // TODO: Make this a thread-safe operation on the global PITTable.
- var index = NDN.PITTable.indexOf(pitEntry);
- //console.log(NDN.PITTable);
- if (index >= 0)
- NDN.PITTable.splice(index, 1);
- //console.log(NDN.PITTable);
- //console.log(pitEntry.interest.name.getName());
-
- // Raise closure callback
- closure.upcall(Closure.UPCALL_INTEREST_TIMED_OUT, new UpcallInfo(ndn, interest, 0, null));
- }, interest.interestLifetime); // interestLifetime is in milliseconds.
- //console.log(closure.timerID);
- }
-
- this.send(encodeToBinaryInterest(interest));
+ ndn.expressInterestHelper(interest, closure);
};
diff --git a/js/tools/build/ndn-js-uncomp.js b/js/tools/build/ndn-js-uncomp.js
index a6c2c4d..77b45dc 100644
--- a/js/tools/build/ndn-js-uncomp.js
+++ b/js/tools/build/ndn-js-uncomp.js
@@ -161,44 +161,10 @@
}
WebSocketTransport.prototype.expressInterest = function(ndn, interest, closure) {
- if (this.ws == null || this.connectedHost != ndn.host || this.connectedPort != ndn.port) {
- var self = this;
- this.connect(ndn, function() { self.expressInterestHelper(ndn, interest, closure); });
- }
+ if (this.ws == null || this.connectedHost != ndn.host || this.connectedPort != ndn.port)
+ this.connect(ndn, function() { ndn.expressInterestHelper(interest, closure); });
else
- this.expressInterestHelper(ndn, interest, closure);
-};
-
-WebSocketTransport.prototype.expressInterestHelper = function(ndn, interest, closure) {
- //TODO: check local content store first
- if (closure != null) {
- var pitEntry = new PITEntry(interest, closure);
- // TODO: This needs to be a single thread-safe transaction on a global object.
- NDN.PITTable.push(pitEntry);
- closure.pitEntry = pitEntry;
- }
-
- // Set interest timer
- if (closure != null) {
- pitEntry.timerID = setTimeout(function() {
- if (LOG > 3) console.log("Interest time out.");
-
- // Remove PIT entry from NDN.PITTable.
- // TODO: Make this a thread-safe operation on the global PITTable.
- var index = NDN.PITTable.indexOf(pitEntry);
- //console.log(NDN.PITTable);
- if (index >= 0)
- NDN.PITTable.splice(index, 1);
- //console.log(NDN.PITTable);
- //console.log(pitEntry.interest.name.getName());
-
- // Raise closure callback
- closure.upcall(Closure.UPCALL_INTEREST_TIMED_OUT, new UpcallInfo(ndn, interest, 0, null));
- }, interest.interestLifetime); // interestLifetime is in milliseconds.
- //console.log(closure.timerID);
- }
-
- this.send(encodeToBinaryInterest(interest));
+ ndn.expressInterestHelper(interest, closure);
};
/**
* @author: Meki Cheraoui
@@ -7681,6 +7647,43 @@
this.transport.expressInterest(this, interest, closure);
};
+/*
+ * Do the work of expressInterest once we know we are connected. Set the PITTable and call
+ * this.transport.send to send the interest.
+ */
+NDN.prototype.expressInterestHelper = function(interest, closure) {
+ //TODO: check local content store first
+ if (closure != null) {
+ var pitEntry = new PITEntry(interest, closure);
+ // TODO: This needs to be a single thread-safe transaction on a global object.
+ NDN.PITTable.push(pitEntry);
+ closure.pitEntry = pitEntry;
+ }
+
+ // Set interest timer
+ var thisNDN = this;
+ if (closure != null) {
+ pitEntry.timerID = setTimeout(function() {
+ if (LOG > 3) console.log("Interest time out.");
+
+ // Remove PIT entry from NDN.PITTable.
+ // TODO: Make this a thread-safe operation on the global PITTable.
+ var index = NDN.PITTable.indexOf(pitEntry);
+ //console.log(NDN.PITTable);
+ if (index >= 0)
+ NDN.PITTable.splice(index, 1);
+ //console.log(NDN.PITTable);
+ //console.log(pitEntry.interest.name.getName());
+
+ // Raise closure callback
+ closure.upcall(Closure.UPCALL_INTEREST_TIMED_OUT, new UpcallInfo(thisNDN, interest, 0, null));
+ }, interest.interestLifetime); // interestLifetime is in milliseconds.
+ //console.log(closure.timerID);
+ }
+
+ this.transport.send(encodeToBinaryInterest(interest));
+};
+
NDN.prototype.registerPrefix = function(name, closure, flag) {
var thisNDN = this;
var onConnected = function() {
@@ -7748,7 +7751,9 @@
return Closure.RESULT_OK;
};
-// Do the work of registerPrefix once we know we are connected with a ccndid.
+/*
+ * Do the work of registerPrefix once we know we are connected with a ccndid.
+ */
NDN.prototype.registerPrefixHelper = function(name, closure, flag) {
var fe = new ForwardingEntry('selfreg', name, null, null, 3, 2147483647);
var bytes = encodeForwardingEntry(fe);
@@ -8022,11 +8027,16 @@
if (this.structureDecoder.findElementEnd(rawData)) {
// Got the remainder of an object. Report to the caller.
this.dataParts.push(rawData.subarray(0, this.structureDecoder.offset));
- this.elementListener.onReceivedElement(DataUtils.concatArrays(this.dataParts));
+ var element = DataUtils.concatArrays(this.dataParts);
+ this.dataParts = [];
+ try {
+ this.elementListener.onReceivedElement(element);
+ } catch (ex) {
+ console.log("BinaryXmlElementReader: ignoring exception from onReceivedElement: " + ex);
+ }
// Need to read a new object.
rawData = rawData.subarray(this.structureDecoder.offset, rawData.length);
- this.dataParts = [];
this.structureDecoder = new BinaryXMLStructureDecoder();
if (rawData.length == 0)
// No more data in the packet.
diff --git a/js/tools/build/ndn-js.js b/js/tools/build/ndn-js.js
index 3aacc7e..b833539 100644
--- a/js/tools/build/ndn-js.js
+++ b/js/tools/build/ndn-js.js
@@ -3,9 +3,7 @@
var WebSocketTransport=function(){this.elementReader=this.connectedPort=this.connectedHost=this.ws=null;this.defaultGetHostAndPort=NDN.makeShuffledGetHostAndPort(["A.ws.ndn.ucla.edu","B.ws.ndn.ucla.edu","C.ws.ndn.ucla.edu","D.ws.ndn.ucla.edu","E.ws.ndn.ucla.edu"],9696)};
WebSocketTransport.prototype.connect=function(a,b){null!=this.ws&&delete this.ws;this.ws=new WebSocket("ws://"+a.host+":"+a.port);0<LOG&&console.log("ws connection created.");this.connectedHost=a.host;this.connectedPort=a.port;this.ws.binaryType="arraybuffer";this.elementReader=new BinaryXmlElementReader(a);var c=this;this.ws.onmessage=function(a){a=a.data;if(null==a||void 0==a||""==a)console.log("INVALID ANSWER");else if(a instanceof ArrayBuffer){a=new Uint8Array(a);3<LOG&&console.log("BINARY RESPONSE IS "+
DataUtils.toHex(a));try{c.elementReader.onReceivedData(a)}catch(b){console.log("NDN.ws.onmessage exception: "+b)}}};this.ws.onopen=function(a){3<LOG&&console.log(a);3<LOG&&console.log("ws.onopen: WebSocket connection opened.");3<LOG&&console.log("ws.onopen: ReadyState: "+this.readyState);b()};this.ws.onerror=function(a){console.log("ws.onerror: ReadyState: "+this.readyState);console.log(a);console.log("ws.onerror: WebSocket error: "+a.data)};this.ws.onclose=function(){console.log("ws.onclose: WebSocket connection closed.");
-c.ws=null;a.readyStatus=NDN.CLOSED;a.onclose()}};WebSocketTransport.prototype.send=function(a){if(null!=this.ws){var b=new Uint8Array(a.length);b.set(a);this.ws.send(b.buffer);3<LOG&&console.log("ws.send() returned.")}else console.log("WebSocket connection is not established.")};
-WebSocketTransport.prototype.expressInterest=function(a,b,c){if(null==this.ws||this.connectedHost!=a.host||this.connectedPort!=a.port){var d=this;this.connect(a,function(){d.expressInterestHelper(a,b,c)})}else this.expressInterestHelper(a,b,c)};
-WebSocketTransport.prototype.expressInterestHelper=function(a,b,c){if(null!=c){var d=new PITEntry(b,c);NDN.PITTable.push(d);c.pitEntry=d}null!=c&&(d.timerID=setTimeout(function(){3<LOG&&console.log("Interest time out.");var e=NDN.PITTable.indexOf(d);0<=e&&NDN.PITTable.splice(e,1);c.upcall(Closure.UPCALL_INTEREST_TIMED_OUT,new UpcallInfo(a,b,0,null))},b.interestLifetime));this.send(encodeToBinaryInterest(b))};
+c.ws=null;a.readyStatus=NDN.CLOSED;a.onclose()}};WebSocketTransport.prototype.send=function(a){if(null!=this.ws){var b=new Uint8Array(a.length);b.set(a);this.ws.send(b.buffer);3<LOG&&console.log("ws.send() returned.")}else console.log("WebSocket connection is not established.")};WebSocketTransport.prototype.expressInterest=function(a,b,c){null==this.ws||this.connectedHost!=a.host||this.connectedPort!=a.port?this.connect(a,function(){a.expressInterestHelper(b,c)}):a.expressInterestHelper(b,c)};
var CCNProtocolDTags={Any:13,Name:14,Component:15,Certificate:16,Collection:17,CompleteName:18,Content:19,SignedInfo:20,ContentDigest:21,ContentHash:22,Count:24,Header:25,Interest:26,Key:27,KeyLocator:28,KeyName:29,Length:30,Link:31,LinkAuthenticator:32,NameComponentCount:33,RootDigest:36,Signature:37,Start:38,Timestamp:39,Type:40,Nonce:41,Scope:42,Exclude:43,Bloom:44,BloomSeed:45,AnswerOriginKind:47,InterestLifetime:48,Witness:53,SignatureBits:54,DigestAlgorithm:55,BlockSize:56,FreshnessSeconds:58,
FinalBlockID:59,PublisherPublicKeyDigest:60,PublisherCertificateDigest:61,PublisherIssuerKeyDigest:62,PublisherIssuerCertificateDigest:63,ContentObject:64,WrappedKey:65,WrappingKeyIdentifier:66,WrapAlgorithm:67,KeyAlgorithm:68,Label:69,EncryptedKey:70,EncryptedNonceKey:71,WrappingKeyName:72,Action:73,FaceID:74,IPProto:75,Host:76,Port:77,MulticastInterface:78,ForwardingFlags:79,FaceInstance:80,ForwardingEntry:81,MulticastTTL:82,MinSuffixComponents:83,MaxSuffixComponents:84,ChildSelector:85,RepositoryInfo:86,
Version:87,RepositoryVersion:88,GlobalPrefix:89,LocalName:90,Policy:91,Namespace:92,GlobalPrefixName:93,PolicyVersion:94,KeyValueSet:95,KeyValuePair:96,IntegerValue:97,DecimalValue:98,StringValue:99,BinaryValue:100,NameValue:101,Entry:102,ACL:103,ParameterizedName:104,Prefix:105,Suffix:106,Root:107,ProfileName:108,Parameters:109,InfoString:110,StatusResponse:112,StatusCode:113,StatusText:114,SyncNode:115,SyncNodeKind:116,SyncNodeElement:117,SyncVersion:118,SyncNodeElements:119,SyncContentHash:120,
@@ -257,7 +255,7 @@
NDN.getEntryForExpressedInterest=function(a){for(var b=null,c=0;c<NDN.PITTable.length;c++)if(NDN.PITTable[c].interest.matches_name(a)&&(null==b||NDN.PITTable[c].interest.name.components.length>b.interest.name.components.length))b=NDN.PITTable[c];return b};NDN.CSTable=[];var CSEntry=function(a,b){this.name=a;this.closure=b};function getEntryForRegisteredPrefix(a){for(var b=0;b<NDN.CSTable.length;b++)if(null!=NDN.CSTable[b].name.match(a))return NDN.CSTable[b];return null}
NDN.makeShuffledGetHostAndPort=function(a,b){a=a.slice(0,a.length);DataUtils.shuffle(a);return function(){return 0==a.length?null:{host:a.splice(0,1)[0],port:b}}};
NDN.prototype.expressInterest=function(a,b,c){var d=new Interest(a);null!=c?(d.minSuffixComponents=c.minSuffixComponents,d.maxSuffixComponents=c.maxSuffixComponents,d.publisherPublicKeyDigest=c.publisherPublicKeyDigest,d.exclude=c.exclude,d.childSelector=c.childSelector,d.answerOriginKind=c.answerOriginKind,d.scope=c.scope,d.interestLifetime=c.interestLifetime):d.interestLifetime=4E3;if(null==this.host||null==this.port)if(null==this.getHostAndPort)console.log("ERROR: host OR port NOT SET");else{var e=
-this;this.connectAndExecute(function(){e.transport.expressInterest(e,d,b)})}else this.transport.expressInterest(this,d,b)};
+this;this.connectAndExecute(function(){e.transport.expressInterest(e,d,b)})}else this.transport.expressInterest(this,d,b)};NDN.prototype.expressInterestHelper=function(a,b){if(null!=b){var c=new PITEntry(a,b);NDN.PITTable.push(c);b.pitEntry=c}var d=this;null!=b&&(c.timerID=setTimeout(function(){3<LOG&&console.log("Interest time out.");var e=NDN.PITTable.indexOf(c);0<=e&&NDN.PITTable.splice(e,1);b.upcall(Closure.UPCALL_INTEREST_TIMED_OUT,new UpcallInfo(d,a,0,null))},a.interestLifetime));this.transport.send(encodeToBinaryInterest(a))};
NDN.prototype.registerPrefix=function(a,b,c){var d=this,e=function(){if(null==d.ccndid){var e=new Interest(NDN.ccndIdFetcher);e.interestLifetime=4E3;3<LOG&&console.log("Expressing interest for ccndid from ccnd.");d.transport.expressInterest(d,e,new NDN.FetchCcndidClosure(d,a,b,c))}else d.registerPrefixHelper(a,b,c)};null==this.host||null==this.port?null==this.getHostAndPort?console.log("ERROR: host OR port NOT SET"):this.connectAndExecute(e):e()};
NDN.FetchCcndidClosure=function(a,b,c,d){Closure.call(this);this.ndn=a;this.name=b;this.callerClosure=c;this.flag=d};
NDN.FetchCcndidClosure.prototype.upcall=function(a,b){if(a==Closure.UPCALL_INTEREST_TIMED_OUT)return console.log("Timeout while requesting the ccndid. Cannot registerPrefix for "+this.name.to_uri()+" ."),Closure.RESULT_OK;if(!(a==Closure.UPCALL_CONTENT||a==Closure.UPCALL_CONTENT_UNVERIFIED))return Closure.RESULT_ERR;var c=b.contentObject;!c.signedInfo||!c.signedInfo.publisher||!c.signedInfo.publisher.publisherPublicKeyDigest?console.log("ContentObject doesn't have a publisherPublicKeyDigest. Cannot set ccndid and registerPrefix for "+
@@ -273,5 +271,5 @@
NDN.prototype.connectAndExecute=function(a){var b=this.getHostAndPort();if(null==b)console.log("ERROR: No more hosts from getHostAndPort"),this.host=null;else if(b.host==this.host&&b.port==this.port)console.log("ERROR: The host returned by getHostAndPort is not alive: "+this.host+":"+this.port);else{this.host=b.host;this.port=b.port;3<LOG&&console.log("Connect: trying host from getHostAndPort: "+this.host);b=new Interest(new Name("/"));b.interestLifetime=4E3;var c=this,d=setTimeout(function(){3<LOG&&
console.log("Connect: timeout waiting for host "+c.host);c.connectAndExecute(a)},3E3);this.transport.expressInterest(this,b,new NDN.ConnectClosure(this,a,d))}};NDN.ConnectClosure=function(a,b,c){Closure.call(this);this.ndn=a;this.onConnected=b;this.timerID=c};NDN.ConnectClosure.prototype.upcall=function(a){if(!(a==Closure.UPCALL_CONTENT||a==Closure.UPCALL_CONTENT_UNVERIFIED))return Closure.RESULT_ERR;clearTimeout(this.timerID);this.ndn.readyStatus=NDN.OPENED;this.ndn.onopen();this.onConnected();return Closure.RESULT_OK};
var BinaryXmlElementReader=function(a){this.elementListener=a;this.dataParts=[];this.structureDecoder=new BinaryXMLStructureDecoder};
-BinaryXmlElementReader.prototype.onReceivedData=function(a){for(;;)if(this.structureDecoder.seek(0),this.structureDecoder.findElementEnd(a)){if(this.dataParts.push(a.subarray(0,this.structureDecoder.offset)),this.elementListener.onReceivedElement(DataUtils.concatArrays(this.dataParts)),a=a.subarray(this.structureDecoder.offset,a.length),this.dataParts=[],this.structureDecoder=new BinaryXMLStructureDecoder,0==a.length)break}else{this.dataParts.push(a);3<LOG&&console.log("Incomplete packet received. Length "+
-a.length+". Wait for more input.");break}};
+BinaryXmlElementReader.prototype.onReceivedData=function(a){for(;;)if(this.structureDecoder.seek(0),this.structureDecoder.findElementEnd(a)){this.dataParts.push(a.subarray(0,this.structureDecoder.offset));var b=DataUtils.concatArrays(this.dataParts);this.dataParts=[];try{this.elementListener.onReceivedElement(b)}catch(c){console.log("BinaryXmlElementReader: ignoring exception from onReceivedElement: "+c)}a=a.subarray(this.structureDecoder.offset,a.length);this.structureDecoder=new BinaryXMLStructureDecoder;
+if(0==a.length)break}else{this.dataParts.push(a);3<LOG&&console.log("Incomplete packet received. Length "+a.length+". Wait for more input.");break}};