Security implemented + Partial publish
diff --git a/js/java_socket_bridge.js b/js/java_socket_bridge.js
index 3eccdfc..f2ee60d 100644
--- a/js/java_socket_bridge.js
+++ b/js/java_socket_bridge.js
@@ -3,12 +3,13 @@
  * This class represents Interest Objects

  */

 

-//var ccndAddr = 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 ccndAddrHex = '%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 LOG = 5;

+//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');

 

-// Global variables

+

+var LOG = 0;

+

+

 var java_socket_bridge_ready_flag = false;

 

 var ndnport =null;

@@ -16,411 +17,74 @@
 

 var registeredPrefixes ={};

 

+/**

+ * Add a trim funnction for Strings

+ */

 String.prototype.trim = function() {

 	return this.replace(/^\s+|\s+$/g, "");

 };

 

+

 // Applet reports it is ready to use

 function java_socket_bridge_ready(){

+	console.log('APPLET LOADED');

 	java_socket_bridge_ready_flag = true;

-}

-

-

-

-

-//Sets the route to ccnx router

-/**

- * Setup the router to use

- * @url the url of the remote NDN router

- * @port the port of the remote NDN router

- */

-function createRoute(url, port){

-	ndnport = port;

-	ndnurl=url;

-	

-	console.log(new BinaryXMLDecoder());

-

-	//SEND INTERST TO CCNX NODE

-

-	

-	//Now Start the receiving thread

 	

 }

 

-// Connect to a given url and port

-//Error -1 No countent found

-//Error -2 Empty query

-function queryPrefix(message){

-	if(ndnport!=null && ndnurl!=null){

-		var newMessage ='';

 

 

-		message = message.trim();

-		

-

-		

-		if(message==null || message =="" || message=="/"){

-			return -2;

-		}

-		

-		//message = decodeURIComponent(message);

-		

-		var array = createNameArray(message);

-		

-		//console.log('ARRAY IS '+ array);

-		

-		enc = new BinaryXMLEncoder();

- 

-		int = new Interest(new ContentName(array));

-

-		int.encode(enc);

-		

-		var hex = toHex(enc.getReducedOstream());

-		

-		

-		

-		//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;

-		}

-		

-		else{

-			

-			var numbers = toNumbers(result);

-			

-			console.log('HEX RESPONSE IS \n'+numbers);

-			decoder = new BinaryXMLDecoder(numbers);

-			

-			

-			co = new ContentObject();

-        

-			co.decode(decoder);

-

-			if(LOG>2) console.log(co);

-

-			return co;

-			

-

-		}

-		

-

-	}

-

-

-	else{

-

-		alert('ERROR URL OR PORT NOT SET');

-

-		return -3;

-

-	}

-

-}

-

-var registerStarted = false;

-function registerPrefix(name, content){

-	

-	registeredPrefixes[name] = content ;

-	

-	if(registerStarted == false){

-		var result = get_java_socket_bridge().connectAndStartAndPublish();

-		

-		startRegisterPrefix();

-		

-		registerStarted = true;

-	}

-	sendForwardingEntry(10);

-}

-

-

-function unRegisterPrefix(name){

-	

-	delete registeredPrefixes[name];

-

-}

-

-

-

-

-function on_socket_received_interest(IP, port, interestBinary){

-	console.log('WOOOO RECEIVED STUFF' );

-	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 );

+// Send Test Interest

+function get(host,port,data){

+	if(java_socket_bridge_ready_flag){

+		return get_java_socket_bridge().get(host,port,data,1000);

 	}

 	else{

-		var co = new ContentObject(interest.Name, null,registeredPrefix[stringName],null );

-		

-		var hex = encodeToHexContentObject(co);

-		

-		get_java_socket_bridge().sendContentObject(IP,port,hex);

-		

-		

+		on_socket_error("Java Socket Bridge send Interest until the applet has loaded");

 	}

 }

 

 

-

-

-// 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');

+// Send Test Interest

+function put(host,port,data,name){

 	

-	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 si = new SignedInfo();

-		si.setFields();

-		

-		var co = new ContentObject(new ContentName(),si,faceInstanceBinary,new Signature()); 

-		co.sign();

-		

-		var encoder2 = new BinaryXMLEncoder();

-

-		co.encode(encoder2);

-

-		var coBinary = encoder2.getReducedOstream();

-		

-		//if(LOG>3)console.log('ADDESS OF CCND IS'+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 ContentName(['ccnx',co.SignedInfo.Publisher.PublisherPublicKeyDigest,'newface',coBinary]);

-		var interestName = new ContentName(['ccnx',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'),'newface',coBinary]);

-		//var interestName = new ContentName(['ccnx','%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','newface',coBinary]);

-

-		//var interestName = new ContentName(['ccnx','1234','newface',coBinary]);

-		//var interestName = new ContentName(['ccnx',co.SignedInfo.Publisher.PublisherPublicKeyDigest,'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);

-

-		

-		//TODO MOVE THIS

-		

-		//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;

-		}

+	if(java_socket_bridge_ready_flag){ 

+		return get_java_socket_bridge().put(host,port,data,name);

 	}

 	else{

-

-		alert('ERROR URL OR PORT NOT SET');

-

-		return -3;

-

-	}	

-

-}

-

-

-// 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 si = new SignedInfo();

-		si.setFields();

-		

-		var co = new ContentObject(new ContentName(),si,faceInstanceBinary,new Signature()); 

-		co.sign();

-		

-		var encoder2 = new BinaryXMLEncoder();

-

-		co.encode(encoder2);

-

-		var coBinary = encoder2.getReducedOstream();

-

-

-

-		var interestName = new ContentName(['ccnx',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'),'prefixreg',coBinary]);

-		//var interestName = new ContentName(['ccnx',co.SignedInfo.Publisher.PublisherPublicKeyDigest,'newface',coBinary]);

-		//var interestName = new ContentName(['ccnx','%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','newface',coBinary]);

-

-		//var interestName = new ContentName(['ccnx','1234','newface',coBinary]);

-		//var interestName = new ContentName(['ccnx',co.SignedInfo.Publisher.PublisherPublicKeyDigest,'prefixreg',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);

-		

-		if(LOG>3)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;

-		}

+		on_socket_error("Java Socket Bridge send Interest until the applet has loaded");

 	}

-	else{

-

-		alert('ERROR URL OR PORT NOT SET');

-

-		return -3;

-

-	}	

-

 }

 

-

-

-

-function encodeToHexInterest(int){

+function on_socket_received_interest(hex,name){

 	

-	var enc = new BinaryXMLEncoder();

- 

-	int.encode(enc);

+	if(LOG>3)console.log('received interest from host'+ host +':'+port+' with name '+name);

 	

-	var hex = toHex(enc.getReducedOstream());

-

-	return hex;

-

+	if(LOG>3)console.log('DATA ');

 	

+	if(LOG>3)console.log(hex);

+	

+	interest = decodeHexInterest(hex);

+	

+	console.log('SUCCESSFULLY PARSED INTEREST');

+	

+	console.log('CREATING ANSWER');

+	var si = new SignedInfo();

+	si.setFields();

+	

+	var answer = toNumbersFromString('WORLD');

+

+	var co = new ContentObject(new ContentName(name),si,answer,new Signature()); 

+	co.sign();

+	

+	

+	var outputHex = encodeToHexContentObject(co);

+	

+	console.log('SENDING ANSWER');

+

+	return get_java_socket_bridge().putAnswer(outputHex,name);

 }

 

-

-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);

-	if(LOG>3)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);

-	if(LOG>3)console.log('DECODED HEX CONTENT OBJECT \n'+numbers);

-	

-	co = new ContentObject();

-

-	co.decode(decoder);

-

-	return co;

-	

-}

-

-

-

-

 // Get something from the socket

 function on_socket_get(message){}