blob: 8e0e7c7cd601d4f4d7b9ad526e70ab8ac1140c27 [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//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);
7var 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';
8
Axel Colin de Verdieref049a762012-05-19 12:10:17 -07009//var LOG = 5;
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070010var LOG = 5;
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070011
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070012// Global variables
13var java_socket_bridge_ready_flag = false;
14
15var ndnport =null;
16var ndnurl=null;
17
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070018var registeredPrefixes ={};
19
20String.prototype.trim = function() {
21 return this.replace(/^\s+|\s+$/g, "");
22};
23
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070024// Applet reports it is ready to use
25function java_socket_bridge_ready(){
26 java_socket_bridge_ready_flag = true;
27}
28
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070029
30
31
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070032//Sets the route to ccnx router
Axel Colin de Verdieree2991cd2012-05-18 16:49:27 -070033/**
34 * Setup the router to use
35 * @url the url of the remote NDN router
36 * @port the port of the remote NDN router
37 */
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070038function createRoute(url, port){
39 ndnport = port;
40 ndnurl=url;
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070041
42 console.log(new BinaryXMLDecoder());
43
44 //SEND INTERST TO CCNX NODE
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070045
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070046
47 //Now Start the receiving thread
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070048
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070049}
50
51// Connect to a given url and port
52//Error -1 No countent found
53//Error -2 Empty query
Axel Colin de Verdieref049a762012-05-19 12:10:17 -070054/**
55 * Sends an interest for the given prefix and returns the response (java_socket_bridge.js)
56 * @message the prefix to query for
57 * @return -1 if no content is found,-2 is the array is empty, the content otherwise
58 */
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070059function queryPrefix(message){
60 if(ndnport!=null && ndnurl!=null){
61 var newMessage ='';
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070062
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070063
64 message = message.trim();
65
66
67
68 if(message==null || message =="" || message=="/"){
69 return -2;
70 }
71
72 //message = decodeURIComponent(message);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070073
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070074 var array = createNameArray(message);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070075
76 //console.log('ARRAY IS '+ array);
77
78 enc = new BinaryXMLEncoder();
79
80 int = new Interest(new ContentName(array));
81
82 int.encode(enc);
83
84 var hex = toHex(enc.getReducedOstream());
85
86
87
88 //console.log('Connecting and start '+ ndnurl +':'+ndnport+'-'+message);
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070089
90
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070091 var result = get_java_socket_bridge().connectAndStart(ndnurl,ndnport,hex);
92
93 console.log('BINARY RESPONSE IS ' +result);
94
95
96 //result[0] and result[1] should be 0 and 4 if there is a content object found
97 if(result==null || result==undefined || result =="" || result[0] != '0'||result[1]!='4'){
98 return -1;
99 }
100
101 else{
102
103 var numbers = toNumbers(result);
104
105 console.log('HEX RESPONSE IS \n'+numbers);
106 decoder = new BinaryXMLDecoder(numbers);
107
108
109 co = new ContentObject();
110
111 co.decode(decoder);
112
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700113 if(LOG>2) console.log(co);
114
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700115 return co;
116
117
118 }
119
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700120
121 }
122
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700123
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700124 else{
125
126 alert('ERROR URL OR PORT NOT SET');
127
128 return -3;
129
130 }
131
132}
133
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700134var registerStarted = false;
135function registerPrefix(name, content){
136
137 registeredPrefixes[name] = content ;
138
139 if(registerStarted == false){
140 var result = get_java_socket_bridge().connectAndStartAndPublish();
141
142 startRegisterPrefix();
143
144 registerStarted = true;
145 }
146 sendForwardingEntry(10);
147}
148
149
150function unRegisterPrefix(name){
151
152 delete registeredPrefixes[name];
153
154}
155
156
157
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700158
159function on_socket_received_interest(IP, port, interestBinary){
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700160 console.log('WOOOO RECEIVED STUFF' );
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700161 var interest = decodeHexInterest(interestBinary);
162
163 console.log('WOOO received interest' + interest.Name.Components);
164
165 var stringName = "";
166
167 for(var i=0;i<interest.Name.Components.length;i++){
168 stringName += "/"+ interest.Name.Components[i];
169 }
170
171 if(registeredPrefix[stringName]!=null){
172 if(LOG>1)console.log("CANNOT FIND THE OBJECT OF NAME " + stringName );
173 }
174 else{
175 var co = new ContentObject(interest.Name, null,registeredPrefix[stringName],null );
176
177 var hex = encodeToHexContentObject(co);
178
179 get_java_socket_bridge().sendContentObject(IP,port,hex);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700180
181
182 }
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700183}
184
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700185
186
187
188// Connect to a given url and port
189//Error -1 No countent found
190//Error -2 Empty query
191function startRegisterPrefix(){
192 if(LOG>2) console.log('START REGISTER PREFIX');
193
194 if(ndnport!=null && ndnurl!=null){
195 var newMessage ='';
196
197
198
199 name = name.trim();
200
201
202
203 ///////////////////////
204 var face = new FaceInstance('newface',null,null, 17, '127.0.0.1',9876,null,null,null);
205
206 var encoder1 = new BinaryXMLEncoder();
207
208 face.encode(encoder1);
209
210 var faceInstanceBinary = encoder1.getReducedOstream();
211
212
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700213 var si = new SignedInfo();
214 si.setFields();
215
216 var co = new ContentObject(new ContentName(),si,faceInstanceBinary,new Signature());
217 co.sign();
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700218
219 var encoder2 = new BinaryXMLEncoder();
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700220
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700221 co.encode(encoder2);
222
223 var coBinary = encoder2.getReducedOstream();
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700224
225 //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'));
226
227 //var interestName = new ContentName(['ccnx',co.SignedInfo.Publisher.PublisherPublicKeyDigest,'newface',coBinary]);
228 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]);
229 //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]);
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700230
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700231 //var interestName = new ContentName(['ccnx','1234','newface',coBinary]);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700232 //var interestName = new ContentName(['ccnx',co.SignedInfo.Publisher.PublisherPublicKeyDigest,'newface',coBinary]);
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700233 int = new Interest(interestName,face);
234
235 var hex = encodeToHexInterest(int);
236 /////////////////
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700237
238
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700239
240 if(LOG>4)console.log('Interst name of Conntection Message is '+ interestName);
241
242
243 if(LOG>4) console.log('Connecting and start '+ ndnurl +':'+ndnport+'-'+hex);
244 //console.log('Connecting and start '+ ndnurl +':'+ndnport+'-'+message);
245
246 var result = get_java_socket_bridge().connectAndStart(ndnurl,ndnport,hex);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700247
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700248
249 //TODO MOVE THIS
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700250
251 //result[0] and result[1] should be 0 and 4 if there is a content object found
252 if(result==null || result==undefined || result =="" || result[0] != '0'||result[1]!='4'){
253 return -1;
254 }
255
256 if(LOG>4) console.log('RECEIVED THE FOLLOWING DATA: ' +co.Content);
257
258 else{
259
260 co = decodeHexContentObject(result);
261
262 if(LOG>4) console.log('RECEIVED THE FOLLOWING DATA: ' +co.Content);
263
264 return co;
265 }
266 }
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700267 else{
268
269 alert('ERROR URL OR PORT NOT SET');
270
271 return -3;
272
273 }
274
275}
276
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700277
278// Connect to a given url and port
279//Error -1 No countent found
280//Error -2 Empty query
281function sendForwardingEntry(faceID){
282 if(LOG>2) console.log('START REGISTER PREFIX');
283
284 if(ndnport!=null && ndnurl!=null){
285 var newMessage ='';
286
287
288
289 name = name.trim();
290
291
292
293 ///////////////////////
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700294 var face = new ForwardingEntry('prefixreg',new ContentName(['helloworld']),null, faceID, 1,null);
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700295
296 var encoder1 = new BinaryXMLEncoder();
297
298 face.encode(encoder1);
299
300 var faceInstanceBinary = encoder1.getReducedOstream();
301
302
303
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700304 var si = new SignedInfo();
305 si.setFields();
306
307 var co = new ContentObject(new ContentName(),si,faceInstanceBinary,new Signature());
308 co.sign();
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700309
310 var encoder2 = new BinaryXMLEncoder();
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700311
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700312 co.encode(encoder2);
313
314 var coBinary = encoder2.getReducedOstream();
315
316
317
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700318 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]);
319 //var interestName = new ContentName(['ccnx',co.SignedInfo.Publisher.PublisherPublicKeyDigest,'newface',coBinary]);
320 //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]);
321
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700322 //var interestName = new ContentName(['ccnx','1234','newface',coBinary]);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700323 //var interestName = new ContentName(['ccnx',co.SignedInfo.Publisher.PublisherPublicKeyDigest,'prefixreg',coBinary]);
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700324
325 int = new Interest(interestName,face);
326
327 var hex = encodeToHexInterest(int);
328 /////////////////
329
330
331
332 if(LOG>4)console.log('Interst name of Conntection Message is '+ interestName);
333
334
335 if(LOG>4) console.log('Connecting and start '+ ndnurl +':'+ndnport+'-'+hex);
336 //console.log('Connecting and start '+ ndnurl +':'+ndnport+'-'+message);
337
338 var result = get_java_socket_bridge().connectAndStart(ndnurl,ndnport,hex);
339
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700340 if(LOG>3)console.log('BINARY RESPONSE IS ' +result);
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700341
342
343 //result[0] and result[1] should be 0 and 4 if there is a content object found
344 if(result==null || result==undefined || result =="" || result[0] != '0'||result[1]!='4'){
345 return -1;
346 }
347
348 if(LOG>4) console.log('RECEIVED THE FOLLOWING DATA: ' +co.Content);
349
350 else{
351
352 co = decodeHexContentObject(result);
353
354 if(LOG>4) console.log('RECEIVED THE FOLLOWING DATA: ' +co.Content);
355
356 return co;
357 }
358 }
359 else{
360
361 alert('ERROR URL OR PORT NOT SET');
362
363 return -3;
364
365 }
366
367}
368
369
Axel Colin de Verdieree5e666f2012-05-22 23:32:00 -0700370/**
371 * Computes the size in bytes of the ContentObject once encoded
372 * @param co the content object to encode
373 * @returns the size in bytes of the encoded ContentObject
374 */
375function getContentObjectSize(co){
376 var enc = new BinaryXMLEncoder();
377
378 co.encode(enc);
379
380 return enc.getReducedOstream().length;
381}
382
383
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700384
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700385
386function encodeToHexInterest(int){
387
388 var enc = new BinaryXMLEncoder();
389
390 int.encode(enc);
391
392 var hex = toHex(enc.getReducedOstream());
393
394 return hex;
395
396
397}
398
399
400function encodeToHexContentObject(co){
401 var enc = new BinaryXMLEncoder();
402
403 co.encode(enc);
404
Axel Colin de Verdieree5e666f2012-05-22 23:32:00 -0700405 var reducedOstream = enc.getReducedOstream();
406
407 if(LOG>3) console.log("In encodeToHexContentObject, reducedOstream: ", reducedOstream);
408
409 var hex = toHex(reducedOstream);
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700410
411 return hex;
412
413
414}
415
416function decodeHexInterest(result){
417 var numbers = toNumbers(result);
418
419
420 decoder = new BinaryXMLDecoder(numbers);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700421 if(LOG>3)console.log('DECODED HEX INTERST \n'+numbers);
422
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700423 i = new Interest();
424
425 i.decode(decoder);
426
427 return i;
428
429}
430
431function decodeHexContentObject(result){
432 var numbers = toNumbers(result);
433
434 decoder = new BinaryXMLDecoder(numbers);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700435 if(LOG>3)console.log('DECODED HEX CONTENT OBJECT \n'+numbers);
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700436
437 co = new ContentObject();
438
439 co.decode(decoder);
440
441 return co;
442
443}
444
445
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700446
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700447
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700448// Get something from the socket
449function on_socket_get(message){}
450
451// Report an error
452function on_socket_error(message){
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700453 alert('Received error message \n' +message);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700454}
455
456// Get the applet object
457function get_java_socket_bridge(){
458 return document.getElementById('JavaSocketBridge');
459}