Publish code submitted
CLASSES ADDED: Forwarding Entry and FaceInstance.
NOTE: the CCNx daemon doesn't accept the FaceInstance it receives.
Instead,
diff --git a/js/java_socket_bridge.js b/js/java_socket_bridge.js
index bf4c3ac..5ed0c9d 100644
--- a/js/java_socket_bridge.js
+++ b/js/java_socket_bridge.js
@@ -3,12 +3,20 @@
* This class represents Interest Objects
*/
+var LOG = 5;
+
// Global variables
var java_socket_bridge_ready_flag = false;
var ndnport =null;
var ndnurl=null;
+var registeredPrefixes ={};
+
+String.prototype.trim = function() {
+ return this.replace(/^\s+|\s+$/g, "");
+};
+
// Applet reports it is ready to use
function java_socket_bridge_ready(){
java_socket_bridge_ready_flag = true;
@@ -18,6 +26,16 @@
function createRoute(url, port){
ndnport = port;
ndnurl=url;
+
+ console.log(new BinaryXMLDecoder());
+
+ //SEND INTERST TO CCNX NODE
+ startRegisterPrefix();
+
+ //Now Start the receiving thread
+ var result = get_java_socket_bridge().connectAndStartAndPublish();
+
+
}
// Connect to a given url and port
@@ -26,10 +44,7 @@
function queryPrefix(message){
if(ndnport!=null && ndnurl!=null){
var newMessage ='';
-
- String.prototype.trim = function() {
- return this.replace(/^\s+|\s+$/g, "");
- };
+
message = message.trim();
@@ -67,7 +82,8 @@
//console.log('Connecting and start '+ ndnurl +':'+ndnport+'-'+message);
-
+
+
var result = get_java_socket_bridge().connectAndStart(ndnurl,ndnport,hex);
console.log('BINARY RESPONSE IS ' +result);
@@ -97,10 +113,137 @@
}
+
+ }
+
+ else{
+
+ alert('ERROR URL OR PORT NOT SET');
+
+ return -3;
+
+ }
+
+}
+
+
+function on_socket_received_interest(IP, port, interestBinary){
+ var interest = decodeHexInterest(interestBinary);
+
+ console.log('WOOO received interest' + interest.Name.Components);
+
+ var stringName = "";
+
+ for(var i=0;i<interest.Name.Components.length;i++){
+ stringName += "/"+ interest.Name.Components[i];
+ }
+
+ if(registeredPrefix[stringName]!=null){
+ if(LOG>1)console.log("CANNOT FIND THE OBJECT OF NAME " + stringName );
+ }
+ else{
+ var co = new ContentObject(interest.Name, null,registeredPrefix[stringName],null );
+
+ var hex = encodeToHexContentObject(co);
+
+ get_java_socket_bridge().sendContentObject(IP,port,hex);
}
+}
+
+function registerPrefix(name, content){
+ registeredPrefixes[name] = content ;
+
+}
+
+
+function unRegisterPrefix(name){
+
+ delete registeredPrefixes[name];
+
+}
+
+
+
+
+
+// Connect to a given url and port
+//Error -1 No countent found
+//Error -2 Empty query
+function startRegisterPrefix(){
+ if(LOG>2) console.log('START REGISTER PREFIX');
+
+ if(ndnport!=null && ndnurl!=null){
+ var newMessage ='';
+
+
+
+ name = name.trim();
+
+
+
+ ///////////////////////
+ var face = new FaceInstance('newface',null,null, 17, '127.0.0.1',9876,null,null,null);
+
+ var encoder1 = new BinaryXMLEncoder();
+
+ face.encode(encoder1);
+
+ var faceInstanceBinary = encoder1.getReducedOstream();
+
+
+
+ var co = new ContentObject(null,null,faceInstanceBinary,null);
+
+ var encoder2 = new BinaryXMLEncoder();
+
+ co.encode(encoder2);
+
+ var coBinary = encoder2.getReducedOstream();
+
+
+
+ var interestName = new ContentName(['ccnx','1234','newface',faceInstanceBinary]);
+ //var interestName = new ContentName(['ccnx','1234','newface',coBinary]);
+
+ int = new Interest(interestName,face);
+
+ var hex = encodeToHexInterest(int);
+ /////////////////
+
+
+
+ if(LOG>4)console.log('Interst name of Conntection Message is '+ interestName);
+
+
+ if(LOG>4) console.log('Connecting and start '+ ndnurl +':'+ndnport+'-'+hex);
+ //console.log('Connecting and start '+ ndnurl +':'+ndnport+'-'+message);
+
+ var result = get_java_socket_bridge().connectAndStart(ndnurl,ndnport,hex);
+
+ console.log('BINARY RESPONSE IS ' +result);
+
+ //TODO MOVE THIS
+ sendForwardingEntry(10);
+
+ //result[0] and result[1] should be 0 and 4 if there is a content object found
+ if(result==null || result==undefined || result =="" || result[0] != '0'||result[1]!='4'){
+ return -1;
+ }
+
+ if(LOG>4) console.log('RECEIVED THE FOLLOWING DATA: ' +co.Content);
+
+ else{
+
+ co = decodeHexContentObject(result);
+
+ if(LOG>4) console.log('RECEIVED THE FOLLOWING DATA: ' +co.Content);
+
+ return co;
+ }
+ }
else{
alert('ERROR URL OR PORT NOT SET');
@@ -111,6 +254,169 @@
}
+
+// Connect to a given url and port
+//Error -1 No countent found
+//Error -2 Empty query
+function sendForwardingEntry(faceID){
+ if(LOG>2) console.log('START REGISTER PREFIX');
+
+ if(ndnport!=null && ndnurl!=null){
+ var newMessage ='';
+
+
+
+ name = name.trim();
+
+
+
+ ///////////////////////
+ var face = new ForwardingEntry('prefixreg',new ContentName('helloworld'),null, faceID, 1,null);
+
+ var encoder1 = new BinaryXMLEncoder();
+
+ face.encode(encoder1);
+
+ var faceInstanceBinary = encoder1.getReducedOstream();
+
+
+
+ var co = new ContentObject(null,null,faceInstanceBinary,null);
+
+ var encoder2 = new BinaryXMLEncoder();
+
+ co.encode(encoder2);
+
+ var coBinary = encoder2.getReducedOstream();
+
+
+
+ var interestName = new ContentName(['ccnx','1234','prefixreg',faceInstanceBinary]);
+ //var interestName = new ContentName(['ccnx','1234','newface',coBinary]);
+
+ int = new Interest(interestName,face);
+
+ var hex = encodeToHexInterest(int);
+ /////////////////
+
+
+
+ if(LOG>4)console.log('Interst name of Conntection Message is '+ interestName);
+
+
+ if(LOG>4) console.log('Connecting and start '+ ndnurl +':'+ndnport+'-'+hex);
+ //console.log('Connecting and start '+ ndnurl +':'+ndnport+'-'+message);
+
+ var result = get_java_socket_bridge().connectAndStart(ndnurl,ndnport,hex);
+
+ console.log('BINARY RESPONSE IS ' +result);
+
+
+ //result[0] and result[1] should be 0 and 4 if there is a content object found
+ if(result==null || result==undefined || result =="" || result[0] != '0'||result[1]!='4'){
+ return -1;
+ }
+
+ if(LOG>4) console.log('RECEIVED THE FOLLOWING DATA: ' +co.Content);
+
+ else{
+
+ co = decodeHexContentObject(result);
+
+ if(LOG>4) console.log('RECEIVED THE FOLLOWING DATA: ' +co.Content);
+
+ return co;
+ }
+ }
+ else{
+
+ alert('ERROR URL OR PORT NOT SET');
+
+ return -3;
+
+ }
+
+}
+
+
+
+function createNameArray(name){
+ if(name==null || name =="" || name=="/"){
+ return -2;
+ }
+
+ //message = decodeURIComponent(message);
+ name = unescape(name);
+
+ var array = name.split('/');
+
+
+ if(name[0]=="/")
+ array=array.slice(1,array.length);
+
+ if(name[name.length-1]=="/")
+ array=array.slice(0,array.length-1);
+
+ return array;
+}
+
+function encodeToHexInterest(int){
+
+ var enc = new BinaryXMLEncoder();
+
+ int.encode(enc);
+
+ var hex = toHex(enc.getReducedOstream());
+
+ return hex;
+
+
+}
+
+
+function encodeToHexContentObject(co){
+ var enc = new BinaryXMLEncoder();
+
+ co.encode(enc);
+
+ var hex = toHex(enc.getReducedOstream());
+
+ return hex;
+
+
+}
+
+function decodeHexInterest(result){
+ var numbers = toNumbers(result);
+
+
+ decoder = new BinaryXMLDecoder(numbers);
+ console.log('DECODED HEX INTERST \n'+numbers);
+
+
+ i = new Interest();
+
+ i.decode(decoder);
+
+ return i;
+
+}
+
+function decodeHexContentObject(result){
+ var numbers = toNumbers(result);
+
+ decoder = new BinaryXMLDecoder(numbers);
+ console.log('DECODED HEX CONTENT OBJECT \n'+numbers);
+
+ co = new ContentObject();
+
+ co.decode(decoder);
+
+ return co;
+
+}
+
+
//http://ejohn.org/blog/numbers-hex-and-colors/
function toHex(arguments){
//console.log(arguments);
@@ -136,13 +442,12 @@
return ret;
}
-
// Get something from the socket
function on_socket_get(message){}
// Report an error
function on_socket_error(message){
- alert('NO CONTENT FOUND\nERROR MESSAGE:' +message);
+ alert('Received error message \n' +message);
}
// Get the applet object