Publish code submitted
CLASSES ADDED: Forwarding Entry and FaceInstance.
NOTE: the CCNx daemon doesn't accept the FaceInstance it receives.
Instead,
diff --git a/.DS_Store b/.DS_Store
index d2205a4..a7da695 100644
--- a/.DS_Store
+++ b/.DS_Store
Binary files differ
diff --git a/java/JavaSocketBridge.java b/java/JavaSocketBridge.java
index fb029e1..a8f064d 100644
--- a/java/JavaSocketBridge.java
+++ b/java/JavaSocketBridge.java
@@ -8,14 +8,16 @@
import java.net.*;
import java.security.AccessController;
import java.security.PrivilegedAction;
+import java.util.concurrent.ConcurrentHashMap;
import java.io.*;
public class JavaSocketBridge extends JApplet {
private final static int PACKETSIZE = 3000 ;
// Instance variables
- JSObject browser = null; // The browser
+ static JSObject browser = null; // The browser
+ static ConcurrentHashMap hm = null;
// Initialize
public void init(){
@@ -38,7 +40,7 @@
// Construct the socket
socket = new DatagramSocket() ;
-
+
byte [] data = hex2Byte(interest);
DatagramPacket packet = new DatagramPacket( data, data.length, host, port ) ;
@@ -46,7 +48,7 @@
socket.send( packet ) ;
// Set a receive timeout, 2000 milliseconds
- socket.setSoTimeout( 2000 ) ;
+ socket.setSoTimeout( 4000 ) ;
// Prepare the packet for receive
packet.setData( new byte[PACKETSIZE] ) ;
@@ -78,6 +80,51 @@
);
}
+
+ public String sendContentObject(final String ip, final int por, final String interest){
+ return AccessController.doPrivileged(
+ new PrivilegedAction<String>() {
+ public String run() {
+
+ DatagramSocket socket = null ;
+ byte[] output = null;
+ try
+ {
+ // Convert the arguments first, to ensure that they are valid
+ InetAddress host = InetAddress.getByName( ip ) ;
+ int port = por ;
+
+ // Construct the socket
+ socket = new DatagramSocket() ;
+
+
+ byte [] data = hex2Byte(interest);
+ DatagramPacket packet = new DatagramPacket( data, data.length, host, port ) ;
+
+ // Send it
+ socket.send( packet );
+ }
+ catch( Exception e )
+ {
+ error(e.toString());
+ System.out.println( e ) ;
+ }
+ finally
+ {
+ if( socket != null )
+ socket.close() ;
+ }
+
+ if(output!=null)
+ return byte2hex(output);
+ else
+ return "";
+ }
+ }
+ );
+
+ }
+
public static byte[] hex2Byte(String str)
{
byte[] bytes = new byte[str.length() / 2];
@@ -126,14 +173,14 @@
browser.call("java_socket_bridge_ready", null);
} catch (JSException e) {
// TODO Auto-generated catch block
- e.printStackTrace();
+ error(e.getMessage());
}
}
// Report an error
- public void error(String message){
+ public static void error(String message){
message = "Java Socket Bridge ERROR: " + message;
Object[] arguments = new Object[1];
arguments[0] = message;
@@ -145,5 +192,88 @@
}
}
+ // Report an error
+ public static void receivedInterest(String IP,int port, byte[] data){
+
+
+ Object[] arguments = new Object[3];
+ arguments[0] = IP;
+ arguments[1] = port;
+ arguments[2] = byte2hex( data );
+
+ try {
+ browser.call("on_socket_received_interest", arguments);
+ } catch (JSException e) {
+ // TODO Auto-generated catch block
+ error(e.getMessage());
+ }
+
+ }
+
+
+ public static void connectAndStartAndPublish()
+ {
+
+ AccessController.doPrivileged(
+ new PrivilegedAction<String>() {
+ public String run() {
+ Thread t = new Thread( new Runnable(){
+ public void run(){
+
+
+ String message;
+ try{
+ DatagramSocket serverSocket = new DatagramSocket(9876);
+ byte[] receiveData = new byte[1024];
+ byte[] sendData = new byte[1024];
+ while(true)
+ {
+ DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
+ serverSocket.receive(receivePacket);
+
+ //String sentence = new String( receivePacket.getData());
+
+ //System.out.println("RECEIVED: " + sentence);
+
+
+ InetAddress IPAddress = receivePacket.getAddress();
+
+ int port = receivePacket.getPort();
+
+ byte[] receivedData = receivePacket.getData();
+
+ receivedInterest( IPAddress.getHostAddress() , port, receivedData);
+
+ //String capitalizedSentence = sentence.toUpperCase();
+
+ //DATA PACKET HERE!
+
+
+ //sendData = capitalizedSentence.getBytes();
+
+ //DatagramPacket sendPacket =
+ // new DatagramPacket(sendData, sendData.length, IPAddress, port);
+ //serverSocket.send(sendPacket);
+ }
+ }
+ catch(Exception e){
+ error("Exception " + e + "FAILURE, ERROR IN SOCKET CONECTION");
+ }
+
+
+ }
+ });
+
+ t.start();
+ return "SUCCESS";
+
+
+ }
+
+
+ }
+ );
+
+ }
}
\ No newline at end of file
diff --git a/js/.DS_Store b/js/.DS_Store
index b17090f..a58cca9 100644
--- a/js/.DS_Store
+++ b/js/.DS_Store
Binary files differ
diff --git a/js/FaceInstance.js b/js/FaceInstance.js
index 6a709fe..48d9e5f 100644
--- a/js/FaceInstance.js
+++ b/js/FaceInstance.js
@@ -10,19 +10,18 @@
_Port,
_MulticastInterface,
_MulticastTTL,
- _FreshnessSeconds,){
-
+ _FreshnessSeconds){
- this.Action = _Action,
- this.PublisherPublicKeyDigest = _PublisherPublicKeyDigest,
- this.FaceID = _FaceID,
- this.IPProto = _IPProto,
- this.Host = _Host,
- this.Port = _Port,
- this.MulticastInterface =_MulticastInterface,
- this.MulticastTTL =_MulticastTTL,
- this.FreshnessSeconds = _FreshnessSeconds,
-
+
+ this.Action = _Action;
+ this.PublisherPublicKeyDigest = _PublisherPublicKeyDigest;
+ this.FaceID = _FaceID;
+ this.IPProto = _IPProto;
+ this.Host = _Host;
+ this.Port = _Port;
+ this.MulticastInterface =_MulticastInterface;
+ this.MulticastTTL =_MulticastTTL;
+ this.FreshnessSeconds = _FreshnessSeconds;
//Action ::= ("newface" | "destroyface" | "queryface")
//PublisherPublicKeyDigest ::= SHA-256 digest
@@ -40,7 +39,7 @@
* Used by NetworkObject to decode the object from a network stream.
* @see org.ccnx.ccn.impl.encoding.XMLEncodable
*/
-FaceInstance.prototype.decode(//XMLDecoder
+FaceInstance.prototype.decode = function(//XMLDecoder
decoder) {
decoder.readStartElement(this.getElementLabel());
@@ -113,40 +112,46 @@
* Used by NetworkObject to encode the object to a network stream.
* @see org.ccnx.ccn.impl.encoding.XMLEncodable
*/
-public void encode(XMLEncoder encoder) throws ContentEncodingException {
- if (!validate()) {
- throw new ContentEncodingException("Cannot encode " + this.getClass().getName() + ": field values missing.");
+FaceInstance.prototype.encode = function(//XMLEncoder
+ encoder){
+
+ //if (!this.validate()) {
+ //throw new Exception("Cannot encode : field values missing.");
+ //throw new Exception("")
+ //}
+ encoder.writeStartElement(this.getElementLabel());
+ if (null != this.Action && this.Action.length != 0)
+ encoder.writeElement(CCNProtocolDTags.Action, this.Action);
+ if (null != this.PublisherPublicKeyDigest) {
+ this.PublisherPublicKeyDigest.encode(encoder);
}
- encoder.writeStartElement(getElementLabel());
- if (null != _action && _action.length() != 0)
- encoder.writeElement(CCNProtocolDTags.Action, _action);
- if (null != _ccndID) {
- _ccndID.encode(encoder);
+ if (null != this.FaceID) {
+ encoder.writeElement(CCNProtocolDTags.FaceID, this.FaceID);
}
- if (null != _faceID) {
- encoder.writeElement(CCNProtocolDTags.FaceID, _faceID);
+ if (null != this.IPProto) {
+ //encoder.writeElement(CCNProtocolDTags.IPProto, this.IpProto.value());
+ encoder.writeElement(CCNProtocolDTags.IPProto, this.IPProto);
}
- if (null != _ipProto) {
- encoder.writeElement(CCNProtocolDTags.IPProto, _ipProto.value());
+ if (null != this.Host && this.Host.length != 0) {
+ encoder.writeElement(CCNProtocolDTags.Host, this.Host);
}
- if (null != _host && _host.length() != 0) {
- encoder.writeElement(CCNProtocolDTags.Host, _host);
+ if (null != this.Port) {
+ encoder.writeElement(CCNProtocolDTags.Port, this.Port);
}
- if (null != _port) {
- encoder.writeElement(CCNProtocolDTags.Port, _port);
+ if (null != this.MulticastInterface && this.MulticastInterface.length != 0) {
+ encoder.writeElement(CCNProtocolDTags.MulticastInterface, this.MulticastInterface);
}
- if (null != _multicastInterface && _multicastInterface.length() != 0) {
- encoder.writeElement(CCNProtocolDTags.MulticastInterface, _multicastInterface);
+ if (null != this.MulticastTTL) {
+ encoder.writeElement(CCNProtocolDTags.MulticastTTL, this.MulticastTTL);
}
- if (null != _multicastTTL) {
- encoder.writeElement(CCNProtocolDTags.MulticastTTL, _multicastTTL);
- }
- if (null != _lifetime) {
- encoder.writeElement(CCNProtocolDTags.FreshnessSeconds, _lifetime);
+ if (null != this.FreshnessSeconds) {
+ encoder.writeElement(CCNProtocolDTags.FreshnessSeconds, this.FreshnessSeconds);
}
encoder.writeEndElement();
}
+
+
FaceInstance.prototype.getElementLabel= function(){return CCNProtocolDTags.FaceInstance;};
diff --git a/js/ForwardingEntry.js b/js/ForwardingEntry.js
new file mode 100644
index 0000000..6702f39
--- /dev/null
+++ b/js/ForwardingEntry.js
@@ -0,0 +1,97 @@
+
+
+var ForwardingEntry = function ForwardingEntry(
+ //ActionType
+ _action,
+ //ContentName
+ _prefixName,
+ //PublisherPublicKeyDigest
+ _ccndId,
+ //Integer
+ _faceID,
+ //Integer
+ _flags,
+ //Integer
+ _lifetime){
+
+
+
+ //String
+ this.Action = _action;
+ //ContentName\
+ this.PrefixName = _prefixName;
+ //PublisherPublicKeyDigest
+ this.CCNID = _ccndId;
+ //Integer
+ this.FaceID = _faceID;
+ //Integer
+ this.Flags = _flags;
+ //Integer
+ this.Lifetime = _lifetime; // in seconds
+
+};
+
+ForwardingEntry.prototype.decode =function(
+ //XMLDecoder
+ decoder)
+ //throws ContentDecodingException
+ {
+ decoder.readStartElement(this.getElementLabel());
+ if (decoder.peekStartElement(CCNProtocolDTags.Action)) {
+ this.Action = decoder.readUTF8Element(CCNProtocolDTags.Action);
+ }
+ if (decoder.peekStartElement(CCNProtocolDTags.Name)) {
+ this.PrefixName = new ContentName();
+ this.PrefixName.decode(decoder) ;
+ }
+ if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) {
+ this.CcndId = new PublisherPublicKeyDigest();
+ this.CcndId.decode(decoder);
+ }
+ if (decoder.peekStartElement(CCNProtocolDTags.FaceID)) {
+ this.FaceID = decoder.readIntegerElement(CCNProtocolDTags.FaceID);
+ }
+ if (decoder.peekStartElement(CCNProtocolDTags.ForwardingFlags)) {
+ this.Flags = decoder.readIntegerElement(CCNProtocolDTags.ForwardingFlags);
+ }
+ if (decoder.peekStartElement(CCNProtocolDTags.FreshnessSeconds)) {
+ this.Lifetime = decoder.readIntegerElement(CCNProtocolDTags.FreshnessSeconds);
+ }
+ decoder.readEndElement();
+ };
+
+ /**
+ * Used by NetworkObject to encode the object to a network stream.
+ * @see org.ccnx.ccn.impl.encoding.XMLEncodable
+ */
+ForwardingEntry.prototype.encode =function(
+ //XMLEncoder
+encoder)
+{
+
+
+ //if (!validate()) {
+ //throw new ContentEncodingException("Cannot encode " + this.getClass().getName() + ": field values missing.");
+ //}
+ encoder.writeStartElement(this.getElementLabel());
+ if (null != this.Action && this.Action.length != 0)
+ encoder.writeElement(CCNProtocolDTags.Action, this.Action);
+ if (null != this.PrefixName) {
+ this.PrefixName.encode(encoder);
+ }
+ if (null != this.CcndId) {
+ this.CcndId.encode(encoder);
+ }
+ if (null != this.FaceID) {
+ encoder.writeElement(CCNProtocolDTags.FaceID, this.FaceID);
+ }
+ if (null != this.Flags) {
+ encoder.writeElement(CCNProtocolDTags.ForwardingFlags, this.Flags);
+ }
+ if (null != this.Lifetime) {
+ encoder.writeElement(CCNProtocolDTags.FreshnessSeconds, this.Lifetime);
+ }
+ encoder.writeEndElement();
+ };
+
+ForwardingEntry.prototype.getElementLabel = function() { return CCNProtocolDTags.ForwardingEntry; }
diff --git a/js/Interest.js b/js/Interest.js
deleted file mode 100644
index c2df753..0000000
--- a/js/Interest.js
+++ /dev/null
@@ -1,125 +0,0 @@
-/*
- * @author: ucla-cs
- * This class represents Interest Objects
- */
-
-var Interest = function Interest(_Name,_MinSuffixComponents,_MaxSuffixComponents,_PublisherPublicKeyDigest, _Exclude, _ChildSelector,_AnswerOriginKind,_Scope,_InterestLifetime,_Nonce){
-
- this.Name = _Name;
- this.MaxSuffixComponents = _MaxSuffixComponents;
- this.MinSuffixComponents = _MinSuffixComponents;
-
- this.PublisherKeyDigest = _PublisherPublicKeyDigest;
- this.Exclude = _Exclude;
- this.ChildSelector = _ChildSelector;
- this.AnswerOriginKind = _AnswerOriginKind;
- this.Scope = _Scope;
- this.InterestLifetime = null; // For now we don't have the ability to set an interest lifetime
- this.Nonce = _Nonce;
-
-
- this.RECURSIVE_POSTFIX = "*";
-
- this.CHILD_SELECTOR_LEFT = 0;
- this.CHILD_SELECTOR_RIGHT = 1;
- this.ANSWER_CONTENT_STORE = 1;
- this.ANSWER_GENERATED = 2;
- this.ANSWER_STALE = 4; // Stale answer OK
- this.MARK_STALE = 16; // Must have Scope 0. Michael calls this a "hack"
-
- this.DEFAULT_ANSWER_ORIGIN_KIND = this.ANSWER_CONTENT_STORE | this.ANSWER_GENERATED;
-
-};
-
-Interest.prototype.decode = function(/*XMLDecoder*/ decoder) {
-
- decoder.readStartElement(CCNProtocolDTags.Interest);
-
- this.Name = new ContentName();
- this.Name.decode(decoder);
-
- if (decoder.peekStartElement(CCNProtocolDTags.MinSuffixComponents)) {
- this.MinSuffixComponents = decoder.readIntegerElement(CCNProtocolDTags.MinSuffixComponents);
- }
-
- if (decoder.peekStartElement(CCNProtocolDTags.MaxSuffixComponents)) {
- this.MaxSuffixComponents = decoder.readIntegerElement(CCNProtocolDTags.MaxSuffixComponents);
- }
-
- //TODO decode PublisherID
- /*if (PublisherID.peek(decoder)) {
- this.Publisher = new PublisherID();
- this.Publisher.decode(decoder);
- }*/
-
- if (decoder.peekStartElement(CCNProtocolDTags.Exclude)) {
- this.Exclude = new Exclude();
- this.Exclude.decode(decoder);
- }
-
- if (decoder.peekStartElement(CCNProtocolDTags.ChildSelector)) {
- this.ChildSelector = decoder.readIntegerElement(CCNProtocolDTags.ChildSelector);
- }
-
- if (decoder.peekStartElement(CCNProtocolDTags.AnswerOriginKind)) {
- // call setter to handle defaulting
- this.AnswerOriginKind = decoder.readIntegerElement(CCNProtocolDTags.AnswerOriginKind);
- }
-
- if (decoder.peekStartElement(CCNProtocolDTags.Scope)) {
- this.Scope = decoder.readIntegerElement(CCNProtocolDTags.Scope);
- }
-
- if (decoder.peekStartElement(CCNProtocolDTags.InterestLifetime)) {
- this.InterestLifetime = decoder.readBinaryElement(CCNProtocolDTags.InterestLifetime);
- }
-
- if (decoder.peekStartElement(CCNProtocolDTags.Nonce)) {
- this.Nonce = decoder.readBinaryElement(CCNProtocolDTags.Nonce);
- }
-
- decoder.readEndElement();
-};
-
-Interest.prototype.encode = function(/*XMLEncoder*/ encoder){
- /*if (!validate()) {
- throw new ContentEncodingException("Cannot encode " + this.getClass().getName() + ": field values missing.");
- }*/
-
- encoder.writeStartElement(CCNProtocolDTags.Interest);
-
- this.Name.encode(encoder);
-
- if (null != this.MinSuffixComponents)
- encoder.writeElement(CCNProtocolDTags.MinSuffixComponents, this.MinSuffixComponents);
-
- if (null != this.MaxSuffixComponents)
- encoder.writeElement(CCNProtocolDTags.MaxSuffixComponents, this.MaxSuffixComponents);
-
- //TODO Encode PublisherID
-
- /*if (null != this.PublisherID)
- publisherID().encode(encoder);*/
-
- //TODO Encode Exclude
-
- //if (null != this.Exclude)
- //exclude().encode(encoder);
-
- if (null != this.ChildSelector)
- encoder.writeElement(CCNProtocolDTags.ChildSelector, this.ChildSelector);
-
- //TODO Encode OriginKind
- if (this.DEFAULT_ANSWER_ORIGIN_KIND != this.AnswerOriginKind && this.AnswerOriginKind!=null)
- //encoder.writeElement(CCNProtocolDTags.AnswerOriginKind, this.AnswerOriginKind);
-
- if (null != this.Scope)
- encoder.writeElement(CCNProtocolDTags.Scope, this.Scope);
-
- if (null != this.Nonce)
- encoder.writeElement(CCNProtocolDTags.Nonce, this.Nonce);
-
- encoder.writeEndElement();
-
-};
-
diff --git a/js/KeyLocator.js b/js/KeyLocator.js
index ff5edaa..68adcba 100644
--- a/js/KeyLocator.js
+++ b/js/KeyLocator.js
@@ -23,7 +23,7 @@
else if(_Type==KeyLocatorType.CERTIFICATE){
this.Certificate = _Input;
}
-
+
};
KeyLocator.prototype.decode = function(decoder) {
@@ -105,7 +105,7 @@
encoder.writeEndElement();
};
-
+
KeyLocator.prototype.getElementLabel = function() {
return CCNProtocolDTags.KeyLocator;
};
diff --git a/js/encoding/BinaryXMLDecoder.js b/js/encoding/BinaryXMLDecoder.js
index f70cca4..92e81f1 100644
--- a/js/encoding/BinaryXMLDecoder.js
+++ b/js/encoding/BinaryXMLDecoder.js
@@ -641,7 +641,6 @@
var TypeAndVal = function TypeAndVal(_type,_val) {
this.t = _type;
this.v = _val;
-
};
TypeAndVal.prototype.type = function(){
diff --git a/js/encoding/BinaryXMLEncoder.js b/js/encoding/BinaryXMLEncoder.js
index f05a869..a4217ff 100644
--- a/js/encoding/BinaryXMLEncoder.js
+++ b/js/encoding/BinaryXMLEncoder.js
@@ -21,7 +21,7 @@
var XML_CLOSE = 0x0;
var XML_SUBTYPE_PROCESSING_INSTRUCTIONS = 16;
-
+
var XML_TT_BITS = 3;
var XML_TT_MASK = ((1 << XML_TT_BITS) - 1);
@@ -57,7 +57,9 @@
BinaryXMLEncoder.prototype.writeBlob = function(/*byte []*/ binaryContent
//, /*int*/ offset, /*int*/ length
) {
- console.log(binaryContent);
+
+ if(LOG >3) console.log(binaryContent);
+
this.encodeBlob(this.ostream, binaryContent, this.offset, binaryContent.length);
};
@@ -144,13 +146,30 @@
//long
tag,
//byte[]
- binaryContent,
+ Content,
//TreeMap<String, String>
attributes) {
this.writeStartElement(tag, attributes);
// Will omit if 0-length
- this.writeBlob(binaryContent);
+ if(typeof Content === 'number') {
+ if(LOG>4) console.log('GOING TO WRITE THE NUMBER ' +Content );
+ this.writeBlob(Content.toString());
+ //whatever
+
+ }
+
+ else{
+ //else if(typeof Content === 'string'){
+ console.log('went here');
+ //this.writeBlob(Content);
+ //}
+
+ //else if(typeof Content === 'object'){
+ this.writeBlob(Content);
+ //}
+ }
+
this.writeEndElement();
}
@@ -314,11 +333,19 @@
//CCNTime
offset) {
-
- for (var i = 0; i < input.length; i++) {
- this.ostream[this.offset+i] = (input.charCodeAt(i));
+ if(typeof input === 'string'){
+ //console.log('went here');
+ if(LOG>4) console.log('GOING TO WRITE A STRING');
+
+ for (var i = 0; i < input.length; i++) {
+ this.ostream[this.offset+i] = (input.charCodeAt(i));
+ }
}
-
+
+ else if(typeof input === 'object'){
+ this.writeBlobArray(input);
+ }
+
};
BinaryXMLEncoder.prototype.writeBlobArray = function(
@@ -327,11 +354,16 @@
//CCNTime
offset) {
+ if(LOG>4) console.log('GOING TO WRITE A BLOB');
+
for (var i = 0; i < Blob.length; i++) {
this.ostream[this.offset+i] = Blob[i];
}
};
+
+
+
BinaryXMLEncoder.prototype.getReducedOstream = function() {
return this.ostream.slice(0,this.offset);
diff --git a/js/encoding/DataUtils.js b/js/encoding/DataUtils.js
index 92a4257..635cac7 100644
--- a/js/encoding/DataUtils.js
+++ b/js/encoding/DataUtils.js
@@ -97,10 +97,6 @@
return unescape(output);
};
-
-
-
-
//byte []
DataUtils.prototype.unsignedLongToByteArray= function( value) {
@@ -133,5 +129,29 @@
}
+var utf8 = {}
+
+utf8.toByteArray = function(str) {
+ var byteArray = [];
+ for (var i = 0; i < str.length; i++)
+ if (str.charCodeAt(i) <= 0x7F)
+ byteArray.push(str.charCodeAt(i));
+ else {
+ var h = encodeURIComponent(str.charAt(i)).substr(1).split('%');
+ for (var j = 0; j < h.length; j++)
+ byteArray.push(parseInt(h[j], 16));
+ }
+ return byteArray;
+};
+
+utf8.parse = function(byteArray) {
+ var str = '';
+ for (var i = 0; i < byteArray.length; i++)
+ str += byteArray[i] <= 0x7F?
+ byteArray[i] === 0x25 ? "%25" : // %
+ String.fromCharCode(byteArray[i]) :
+ "%" + byteArray[i].toString(16).toUpperCase();
+ return decodeURIComponent(str);
+};
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
diff --git a/js/publish-data.html b/js/publish-data.html
index 84fb9ac..96ed252 100644
--- a/js/publish-data.html
+++ b/js/publish-data.html
@@ -37,7 +37,10 @@
<script type="text/javascript" src="PublisherPublicKeyDigest.js"></script>
+ <script type="text/javascript" src="FaceInstance.js"></script>
+ <script type="text/javascript" src="ForwardingEntry.js"></script>
+
<script type="text/javascript" src="encoding/BinaryXMLEncoder.js"></script>
<script type="text/javascript" src="encoding/BinaryXMLCodec.js"></script>
@@ -53,6 +56,12 @@
var Content = document.getElementById('content').value;
+ //createRoute('borges.metwi.ucla.edu', 9695);
+
+ createRoute('localhost', 9695);
+
+ registerPrefix(ContentName, Content);
+
///////////////////////////////////////
diff --git a/js/request-data.html b/js/request-data.html
index f00510a..1e58e2c 100644
--- a/js/request-data.html
+++ b/js/request-data.html
@@ -37,7 +37,8 @@
<script type="text/javascript" src="PublisherPublicKeyDigest.js"></script>
-
+ <script type="text/javascript" src="FaceInstance.js"></script>
+
<script type="text/javascript" src="encoding/BinaryXMLEncoder.js"></script>
<script type="text/javascript" src="encoding/BinaryXMLCodec.js"></script>
@@ -52,9 +53,11 @@
var ContentName = document.getElementById('contentname').value;
///////////////////////////////////////
- //createRoute('localhost', 9695);
-
- createRoute('131.179.141.15', 9695);
+ createRoute('localhost', 9695);
+
+
+ /// /ndn/ucla.edu/apps/hydra/mainvideo
+ //createRoute('131.179.141.15', 9695);
//createRoute('borges.metwi.ucla.edu', 9695);
//content object