blob: 5f617b6c9888d96b1754ddf937a66fb9d687a0e5 [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 Cherkaoui8f173612012-06-06 01:05:40 -070035// Send Test Interest
36function get(host,port,data){
37 if(java_socket_bridge_ready_flag){
38 return get_java_socket_bridge().get(host,port,data,1000);
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070039 }
40 else{
Meki Cherkaoui8f173612012-06-06 01:05:40 -070041 on_socket_error("Java Socket Bridge send Interest until the applet has loaded");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070042 }
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070043}
44
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070045
Meki Cherkaoui8f173612012-06-06 01:05:40 -070046// Send Test Interest
Meki Cherkaoui81bfc282012-06-06 03:23:25 -070047function put(host,port,data,name,toReturn){
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070048
Meki Cherkaoui8f173612012-06-06 01:05:40 -070049 if(java_socket_bridge_ready_flag){
Meki Cherkaoui81bfc282012-06-06 03:23:25 -070050 return get_java_socket_bridge().put(host,port,data,name,toReturn);
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070051 }
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070052 else{
Meki Cherkaoui8f173612012-06-06 01:05:40 -070053 on_socket_error("Java Socket Bridge send Interest until the applet has loaded");
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070054 }
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070055}
56
Meki Cherkaoui8f173612012-06-06 01:05:40 -070057function on_socket_received_interest(hex,name){
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070058
Meki Cherkaoui8f173612012-06-06 01:05:40 -070059 if(LOG>3)console.log('received interest from host'+ host +':'+port+' with name '+name);
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070060
Meki Cherkaoui8f173612012-06-06 01:05:40 -070061 if(LOG>3)console.log('DATA ');
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070062
Meki Cherkaoui8f173612012-06-06 01:05:40 -070063 if(LOG>3)console.log(hex);
64
65 interest = decodeHexInterest(hex);
66
67 console.log('SUCCESSFULLY PARSED INTEREST');
68
69 console.log('CREATING ANSWER');
70 var si = new SignedInfo();
71 si.setFields();
72
73 var answer = toNumbersFromString('WORLD');
74
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070075 var co = new ContentObject(new Name(name),si,answer,new Signature());
Meki Cherkaoui8f173612012-06-06 01:05:40 -070076 co.sign();
77
78
79 var outputHex = encodeToHexContentObject(co);
80
81 console.log('SENDING ANSWER');
82
83 return get_java_socket_bridge().putAnswer(outputHex,name);
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070084}
85
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070086// Get something from the socket
87function on_socket_get(message){}
88
89// Report an error
90function on_socket_error(message){
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070091 alert('Received error message \n' +message);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070092}
93
94// Get the applet object
95function get_java_socket_bridge(){
96 return document.getElementById('JavaSocketBridge');
97}