blob: bf4c3ac4da597a2db923279d8baae0507b3307f8 [file] [log] [blame]
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07001/*
2 * @author: ucla-cs
3 * This class represents Interest Objects
4 */
5
6// Global variables
7var java_socket_bridge_ready_flag = false;
8
9var ndnport =null;
10var ndnurl=null;
11
12// Applet reports it is ready to use
13function java_socket_bridge_ready(){
14 java_socket_bridge_ready_flag = true;
15}
16
17//Sets the route to ccnx router
18function createRoute(url, port){
19 ndnport = port;
20 ndnurl=url;
21}
22
23// Connect to a given url and port
24//Error -1 No countent found
25//Error -2 Empty query
26function queryPrefix(message){
27 if(ndnport!=null && ndnurl!=null){
28 var newMessage ='';
29
30 String.prototype.trim = function() {
31 return this.replace(/^\s+|\s+$/g, "");
32 };
33
34 message = message.trim();
35
36
37
38 if(message==null || message =="" || message=="/"){
39 return -2;
40 }
41
42 //message = decodeURIComponent(message);
43 message = unescape(message);
44
45 var array = message.split('/');
46
47
48 if(message[0]=="/")
49 array=array.slice(1,array.length);
50
51 if(message[message.length-1]=="/")
52 array=array.slice(0,array.length-1);
53
54
55 //console.log('ARRAY IS '+ array);
56
57 enc = new BinaryXMLEncoder();
58
59 int = new Interest(new ContentName(array));
60
61 int.encode(enc);
62
63 var hex = toHex(enc.getReducedOstream());
64
65
66
67 //console.log('Connecting and start '+ ndnurl +':'+ndnport+'-'+message);
68
69
70
71 var result = get_java_socket_bridge().connectAndStart(ndnurl,ndnport,hex);
72
73 console.log('BINARY RESPONSE IS ' +result);
74
75
76 //result[0] and result[1] should be 0 and 4 if there is a content object found
77 if(result==null || result==undefined || result =="" || result[0] != '0'||result[1]!='4'){
78 return -1;
79 }
80
81 else{
82
83 var numbers = toNumbers(result);
84
85 console.log('HEX RESPONSE IS \n'+numbers);
86 decoder = new BinaryXMLDecoder(numbers);
87
88
89 co = new ContentObject();
90
91 co.decode(decoder);
92
93 //console.log(co);
94
95 return co;
96
97
98 }
99
100
101
102 }
103
104 else{
105
106 alert('ERROR URL OR PORT NOT SET');
107
108 return -3;
109
110 }
111
112}
113
114//http://ejohn.org/blog/numbers-hex-and-colors/
115function toHex(arguments){
116 //console.log(arguments);
117 var ret = "";
118 for ( var i = 0; i < arguments.length; i++ )
119 ret += (arguments[i] < 16 ? "0" : "") + arguments[i].toString(16);
120 return ret.toUpperCase();
121}
122
123function toString(arguments){
124 //console.log(arguments);
125 var ret = "";
126 for ( var i = 0; i < arguments.length; i++ )
127 ret += String.fromCharCode(arguments[i]);
128 return ret;
129}
130
131function toNumbers( str ){
132 var ret = [];
133 str.replace(/(..)/g, function(str){
134 ret.push( parseInt( str, 16 ) );
135 });
136 return ret;
137}
138
139
140// Get something from the socket
141function on_socket_get(message){}
142
143// Report an error
144function on_socket_error(message){
145 alert('NO CONTENT FOUND\nERROR MESSAGE:' +message);
146}
147
148// Get the applet object
149function get_java_socket_bridge(){
150 return document.getElementById('JavaSocketBridge');
151}