blob: f2ee60da98dd40788c540bba6bc55ad8acd2e616 [file] [log] [blame]
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07001/*
2 * @author: ucla-cs
3 * This class represents Interest Objects
4 */
5
Meki Cherkaouif3d8f692012-05-18 15:44:28 -07006
Meki Cherkaoui8f173612012-06-06 01:05:40 -07007//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');
Meki Cherkaouiabb973b2012-05-09 14:25:57 -07008
Meki Cherkaoui8f173612012-06-06 01:05:40 -07009
10var LOG = 0;
11
12
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070013var java_socket_bridge_ready_flag = false;
14
15var ndnport =null;
16var ndnurl=null;
17
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070018var registeredPrefixes ={};
19
Meki Cherkaoui8f173612012-06-06 01:05:40 -070020/**
21 * Add a trim funnction for Strings
22 */
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070023String.prototype.trim = function() {
24 return this.replace(/^\s+|\s+$/g, "");
25};
26
Meki Cherkaoui8f173612012-06-06 01:05:40 -070027
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070028// Applet reports it is ready to use
29function java_socket_bridge_ready(){
Meki Cherkaoui8f173612012-06-06 01:05:40 -070030 console.log('APPLET LOADED');
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070031 java_socket_bridge_ready_flag = true;
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070032
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070033}
34
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070035
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070036
Meki Cherkaoui8f173612012-06-06 01:05:40 -070037// Send Test Interest
38function get(host,port,data){
39 if(java_socket_bridge_ready_flag){
40 return get_java_socket_bridge().get(host,port,data,1000);
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070041 }
42 else{
Meki Cherkaoui8f173612012-06-06 01:05:40 -070043 on_socket_error("Java Socket Bridge send Interest until the applet has loaded");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070044 }
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070045}
46
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070047
Meki Cherkaoui8f173612012-06-06 01:05:40 -070048// Send Test Interest
49function put(host,port,data,name){
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070050
Meki Cherkaoui8f173612012-06-06 01:05:40 -070051 if(java_socket_bridge_ready_flag){
52 return get_java_socket_bridge().put(host,port,data,name);
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070053 }
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070054 else{
Meki Cherkaoui8f173612012-06-06 01:05:40 -070055 on_socket_error("Java Socket Bridge send Interest until the applet has loaded");
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070056 }
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070057}
58
Meki Cherkaoui8f173612012-06-06 01:05:40 -070059function on_socket_received_interest(hex,name){
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070060
Meki Cherkaoui8f173612012-06-06 01:05:40 -070061 if(LOG>3)console.log('received interest from host'+ host +':'+port+' with name '+name);
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070062
Meki Cherkaoui8f173612012-06-06 01:05:40 -070063 if(LOG>3)console.log('DATA ');
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070064
Meki Cherkaoui8f173612012-06-06 01:05:40 -070065 if(LOG>3)console.log(hex);
66
67 interest = decodeHexInterest(hex);
68
69 console.log('SUCCESSFULLY PARSED INTEREST');
70
71 console.log('CREATING ANSWER');
72 var si = new SignedInfo();
73 si.setFields();
74
75 var answer = toNumbersFromString('WORLD');
76
77 var co = new ContentObject(new ContentName(name),si,answer,new Signature());
78 co.sign();
79
80
81 var outputHex = encodeToHexContentObject(co);
82
83 console.log('SENDING ANSWER');
84
85 return get_java_socket_bridge().putAnswer(outputHex,name);
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070086}
87
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070088// Get something from the socket
89function on_socket_get(message){}
90
91// Report an error
92function on_socket_error(message){
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070093 alert('Received error message \n' +message);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070094}
95
96// Get the applet object
97function get_java_socket_bridge(){
98 return document.getElementById('JavaSocketBridge');
99}