Changed NDN to take getTransport and added WebSocketTransport.js which defines the default transport. Make NDN.expressInterest take the Name and call the specified transport's expressInterest.
diff --git a/js/NDN.js b/js/NDN.js
index 958a267..8dde5c3 100644
--- a/js/NDN.js
+++ b/js/NDN.js
@@ -1,18 +1,30 @@
/*
- * @author: ucla-cs
+ * @author: Meki Cherkaoui, Jeff Thompson, Wentao Shang
* See COPYING for copyright and distribution information.
* This class represents the top-level object for communicating with an NDN host.
*/
+var LOG = 3;
+
/**
- * host is default '127.0.0.1'.
- * port is default 9695.
+ * settings is an associative array with the following defaults:
+ * {
+ * host: 'localhost',
+ * port: 9696,
+ * getTransport: function() { return new WebSocketTransport(); }
+ * }
*/
-var NDN = function NDN(host, port){
- this.host = (host || '127.0.0.1');
- this.port = (port || 9695);
+var NDN = function NDN(settings) {
+ settings = (settings || {});
+ this.host = (settings.host || "localhost");
+ this.port = (settings.port || 9696);
+ var getTransport = (settings.getTransport || function() { return new WebSocketTransport(); });
+ this.transport = getTransport();
};
+
+/* Java Socket Bridge and XPCOM transport */
+
NDN.prototype.createRoute = function(host,port){
this.host=host;
this.port=port;
@@ -26,32 +38,32 @@
console.log('INVALID INPUT TO GET');
return null;
}
-
-
+
+
//var array = Name.createNameArray(message);
int = new Interest(new Name(message));
int.InterestLifetime = 4200;
-
+
var hex = encodeToHexInterest(int);
-
+
//var result = get_java_socket_bridge().connectAndStart(ndnurl,ndnport,hex);
-
+
var result = get(this.host,this.port, hex);
if(LOG>0)console.log('BINARY RESPONSE IS ' +result);
-
+
if(result==null || result==undefined || result =="" ){
/*if(result[0] != '0'||result[1]!='4') {
if(LOG>2)console.log('INVALID ANSWER');
}*/
return null;
}
-
+
else{
-
+
co = decodeHexContentObject(result);
if(LOG>2) {
@@ -68,76 +80,76 @@
return null;
}
-
+
}
NDN.prototype.put = function(name,content){
if(this.host!=null && this.port!=null){
-
+
var co = this.get("/%C1.M.S.localhost/%C1.M.SRV/ccnd");
-
+
if(!co || !co.signedInfo || !co.signedInfo.publisher || !co.signedInfo.publisher.publisherPublicKeyDigest){
alert("Cannot contact router");
-
+
return null;
}
-
+
var ccnxnodename = co.signedInfo.publisher.publisherPublicKeyDigest;
-
+
name = name.trim();
-
+
var fe = new ForwardingEntry('selfreg',new Name(name),null, null, 3,2147483647);
-
+
var bytes = encodeForwardingEntry(fe);
-
-
+
+
var si = new SignedInfo();
si.setFields();
-
+
var co = new ContentObject(new Name(),si,bytes,new Signature());
co.sign();
-
+
var coBinary = encodeToBinaryContentObject(co);
-
+
//var ccnxnodename = unescape('%E0%A0%1E%099h%F9t%0C%E7%F46%1B%AB%F5%BB%05%A4%E5Z%AC%A5%E5%8Fs%ED%DE%B8%E0%13%AA%8F');
-
+
var interestName = new Name(['ccnx',ccnxnodename,'selfreg',coBinary]);
int = new Interest(interestName);
int.scope = 1;
-
+
var hex = encodeToHexInterest(int);
console.log('GOING TO PUT INTEREST OBJECT');
-
+
console.log(hex);
-
+
//var result = put(this.host,this.port, hex,name);
-
+
//if(LOG>3)console.log('received interest'); //from host'+ host +':'+port+' with name '+name);
-
+
//if(LOG>3)console.log('DATA ');
-
+
//if(LOG>3)console.log(result);
-
+
//interest = decodeHexInterest(result);
-
+
//console.log('SUCCESSFULLY PARSED INTEREST');
-
+
console.log('CREATING ANSWER');
var si = new SignedInfo();
si.setFields();
-
+
var answer = DataUtils.toNumbersFromString(content);
var co = new ContentObject(new Name(name),si,answer,new Signature());
co.sign();
-
-
+
+
var outputHex = encodeToHexContentObject(co);
-
+
//console.log('SENDING ANSWER');
//return get_java_socket_bridge().putAnswer(outputHex,name);
@@ -171,7 +183,7 @@
return;
}
- interest = new Interest(name);
+ var interest = new Interest(name);
if (template != null) {
interest.minSuffixComponents = template.minSuffixComponents;
interest.maxSuffixComponents = template.maxSuffixComponents;
@@ -185,51 +197,10 @@
else
interest.interestLifetime = 4200;
- var encoder = new BinaryXMLEncoder();
- interest.to_ccnb(encoder);
- var outputData = encoder.getReducedOstream();
- encoder = null;
-
- // Make a local variable so it is not masked by an inner this.
- var ndn = this;
- var dataListener = {
- onReceivedData : function(data) {
- if (data == null || data == undefined || data.length == 0)
- dump("NDN.expressInterest: received empty data from socket.\n");
- else {
- var decoder = new BinaryXMLDecoder(data);
- var co = new ContentObject();
- co.from_ccnb(decoder);
-
- if(LOG>2) {
- dump("DECODED CONTENT OBJECT\n");
- dump(co);
- dump("\n");
- }
-
- // TODO: verify the content object and set kind to UPCALL_CONTENT.
- var result = closure.upcall(Closure.UPCALL_CONTENT_UNVERIFIED,
- new UpcallInfo(ndn, interest, 0, co));
- if (result == Closure.RESULT_OK) {
- // success
- }
- else if (result == Closure.RESULT_ERR)
- dump("NDN.expressInterest: upcall returned RESULT_ERR.\n");
- else if (result == Closure.RESULT_REEXPRESS)
- readAllFromSocket(ndn.host, ndn.port, outputData, dataListener);
- else if (result == Closure.RESULT_VERIFY) {
- // TODO: force verification of content.
- }
- else if (result == Closure.RESULT_FETCHKEY) {
- // TODO: get the key in the key locator and re-call the interest
- // with the key available in the local storage.
- }
- }
- }
- }
-
- // The application includes a source file that defines readAllFromSocket
- // according to the application's communication method.
- readAllFromSocket(this.host, this.port, outputData, dataListener);
+ this.transport.expressInterest(this, interest, closure);
};
+
+NDN.prototype.registerPrefix = function(name, closure, flag) {
+ return this.transport.registerPrefix(this, name, closure, flag);
+}