blob: 3eccdfc688b4ffa78f6105ee4c77f6f8e6b491e4 [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
9var LOG = 5;
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070010
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070011// Global variables
12var java_socket_bridge_ready_flag = false;
13
14var ndnport =null;
15var ndnurl=null;
16
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070017var registeredPrefixes ={};
18
19String.prototype.trim = function() {
20 return this.replace(/^\s+|\s+$/g, "");
21};
22
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070023// Applet reports it is ready to use
24function java_socket_bridge_ready(){
25 java_socket_bridge_ready_flag = true;
26}
27
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070028
29
30
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070031//Sets the route to ccnx router
Axel Colin de Verdieree2991cd2012-05-18 16:49:27 -070032/**
33 * Setup the router to use
34 * @url the url of the remote NDN router
35 * @port the port of the remote NDN router
36 */
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070037function createRoute(url, port){
38 ndnport = port;
39 ndnurl=url;
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070040
41 console.log(new BinaryXMLDecoder());
42
43 //SEND INTERST TO CCNX NODE
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070044
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070045
46 //Now Start the receiving thread
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070047
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070048}
49
50// Connect to a given url and port
51//Error -1 No countent found
52//Error -2 Empty query
53function queryPrefix(message){
54 if(ndnport!=null && ndnurl!=null){
55 var newMessage ='';
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070056
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070057
58 message = message.trim();
59
60
61
62 if(message==null || message =="" || message=="/"){
63 return -2;
64 }
65
66 //message = decodeURIComponent(message);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070067
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070068 var array = createNameArray(message);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070069
70 //console.log('ARRAY IS '+ array);
71
72 enc = new BinaryXMLEncoder();
73
74 int = new Interest(new ContentName(array));
75
76 int.encode(enc);
77
78 var hex = toHex(enc.getReducedOstream());
79
80
81
82 //console.log('Connecting and start '+ ndnurl +':'+ndnport+'-'+message);
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070083
84
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070085 var result = get_java_socket_bridge().connectAndStart(ndnurl,ndnport,hex);
86
87 console.log('BINARY RESPONSE IS ' +result);
88
89
90 //result[0] and result[1] should be 0 and 4 if there is a content object found
91 if(result==null || result==undefined || result =="" || result[0] != '0'||result[1]!='4'){
92 return -1;
93 }
94
95 else{
96
97 var numbers = toNumbers(result);
98
99 console.log('HEX RESPONSE IS \n'+numbers);
100 decoder = new BinaryXMLDecoder(numbers);
101
102
103 co = new ContentObject();
104
105 co.decode(decoder);
106
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700107 if(LOG>2) console.log(co);
108
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700109 return co;
110
111
112 }
113
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700114
115 }
116
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700117
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700118 else{
119
120 alert('ERROR URL OR PORT NOT SET');
121
122 return -3;
123
124 }
125
126}
127
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700128var registerStarted = false;
129function registerPrefix(name, content){
130
131 registeredPrefixes[name] = content ;
132
133 if(registerStarted == false){
134 var result = get_java_socket_bridge().connectAndStartAndPublish();
135
136 startRegisterPrefix();
137
138 registerStarted = true;
139 }
140 sendForwardingEntry(10);
141}
142
143
144function unRegisterPrefix(name){
145
146 delete registeredPrefixes[name];
147
148}
149
150
151
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700152
153function on_socket_received_interest(IP, port, interestBinary){
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700154 console.log('WOOOO RECEIVED STUFF' );
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700155 var interest = decodeHexInterest(interestBinary);
156
157 console.log('WOOO received interest' + interest.Name.Components);
158
159 var stringName = "";
160
161 for(var i=0;i<interest.Name.Components.length;i++){
162 stringName += "/"+ interest.Name.Components[i];
163 }
164
165 if(registeredPrefix[stringName]!=null){
166 if(LOG>1)console.log("CANNOT FIND THE OBJECT OF NAME " + stringName );
167 }
168 else{
169 var co = new ContentObject(interest.Name, null,registeredPrefix[stringName],null );
170
171 var hex = encodeToHexContentObject(co);
172
173 get_java_socket_bridge().sendContentObject(IP,port,hex);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700174
175
176 }
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700177}
178
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700179
180
181
182// Connect to a given url and port
183//Error -1 No countent found
184//Error -2 Empty query
185function startRegisterPrefix(){
186 if(LOG>2) console.log('START REGISTER PREFIX');
187
188 if(ndnport!=null && ndnurl!=null){
189 var newMessage ='';
190
191
192
193 name = name.trim();
194
195
196
197 ///////////////////////
198 var face = new FaceInstance('newface',null,null, 17, '127.0.0.1',9876,null,null,null);
199
200 var encoder1 = new BinaryXMLEncoder();
201
202 face.encode(encoder1);
203
204 var faceInstanceBinary = encoder1.getReducedOstream();
205
206
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700207 var si = new SignedInfo();
208 si.setFields();
209
210 var co = new ContentObject(new ContentName(),si,faceInstanceBinary,new Signature());
211 co.sign();
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700212
213 var encoder2 = new BinaryXMLEncoder();
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700214
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700215 co.encode(encoder2);
216
217 var coBinary = encoder2.getReducedOstream();
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700218
219 //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'));
220
221 //var interestName = new ContentName(['ccnx',co.SignedInfo.Publisher.PublisherPublicKeyDigest,'newface',coBinary]);
222 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]);
223 //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 -0700224
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700225 //var interestName = new ContentName(['ccnx','1234','newface',coBinary]);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700226 //var interestName = new ContentName(['ccnx',co.SignedInfo.Publisher.PublisherPublicKeyDigest,'newface',coBinary]);
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700227 int = new Interest(interestName,face);
228
229 var hex = encodeToHexInterest(int);
230 /////////////////
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700231
232
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700233
234 if(LOG>4)console.log('Interst name of Conntection Message is '+ interestName);
235
236
237 if(LOG>4) console.log('Connecting and start '+ ndnurl +':'+ndnport+'-'+hex);
238 //console.log('Connecting and start '+ ndnurl +':'+ndnport+'-'+message);
239
240 var result = get_java_socket_bridge().connectAndStart(ndnurl,ndnport,hex);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700241
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700242
243 //TODO MOVE THIS
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700244
245 //result[0] and result[1] should be 0 and 4 if there is a content object found
246 if(result==null || result==undefined || result =="" || result[0] != '0'||result[1]!='4'){
247 return -1;
248 }
249
250 if(LOG>4) console.log('RECEIVED THE FOLLOWING DATA: ' +co.Content);
251
252 else{
253
254 co = decodeHexContentObject(result);
255
256 if(LOG>4) console.log('RECEIVED THE FOLLOWING DATA: ' +co.Content);
257
258 return co;
259 }
260 }
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700261 else{
262
263 alert('ERROR URL OR PORT NOT SET');
264
265 return -3;
266
267 }
268
269}
270
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700271
272// Connect to a given url and port
273//Error -1 No countent found
274//Error -2 Empty query
275function sendForwardingEntry(faceID){
276 if(LOG>2) console.log('START REGISTER PREFIX');
277
278 if(ndnport!=null && ndnurl!=null){
279 var newMessage ='';
280
281
282
283 name = name.trim();
284
285
286
287 ///////////////////////
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700288 var face = new ForwardingEntry('prefixreg',new ContentName(['helloworld']),null, faceID, 1,null);
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700289
290 var encoder1 = new BinaryXMLEncoder();
291
292 face.encode(encoder1);
293
294 var faceInstanceBinary = encoder1.getReducedOstream();
295
296
297
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700298 var si = new SignedInfo();
299 si.setFields();
300
301 var co = new ContentObject(new ContentName(),si,faceInstanceBinary,new Signature());
302 co.sign();
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700303
304 var encoder2 = new BinaryXMLEncoder();
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700305
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700306 co.encode(encoder2);
307
308 var coBinary = encoder2.getReducedOstream();
309
310
311
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700312 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]);
313 //var interestName = new ContentName(['ccnx',co.SignedInfo.Publisher.PublisherPublicKeyDigest,'newface',coBinary]);
314 //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]);
315
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700316 //var interestName = new ContentName(['ccnx','1234','newface',coBinary]);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700317 //var interestName = new ContentName(['ccnx',co.SignedInfo.Publisher.PublisherPublicKeyDigest,'prefixreg',coBinary]);
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700318
319 int = new Interest(interestName,face);
320
321 var hex = encodeToHexInterest(int);
322 /////////////////
323
324
325
326 if(LOG>4)console.log('Interst name of Conntection Message is '+ interestName);
327
328
329 if(LOG>4) console.log('Connecting and start '+ ndnurl +':'+ndnport+'-'+hex);
330 //console.log('Connecting and start '+ ndnurl +':'+ndnport+'-'+message);
331
332 var result = get_java_socket_bridge().connectAndStart(ndnurl,ndnport,hex);
333
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700334 if(LOG>3)console.log('BINARY RESPONSE IS ' +result);
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700335
336
337 //result[0] and result[1] should be 0 and 4 if there is a content object found
338 if(result==null || result==undefined || result =="" || result[0] != '0'||result[1]!='4'){
339 return -1;
340 }
341
342 if(LOG>4) console.log('RECEIVED THE FOLLOWING DATA: ' +co.Content);
343
344 else{
345
346 co = decodeHexContentObject(result);
347
348 if(LOG>4) console.log('RECEIVED THE FOLLOWING DATA: ' +co.Content);
349
350 return co;
351 }
352 }
353 else{
354
355 alert('ERROR URL OR PORT NOT SET');
356
357 return -3;
358
359 }
360
361}
362
363
364
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700365
366function encodeToHexInterest(int){
367
368 var enc = new BinaryXMLEncoder();
369
370 int.encode(enc);
371
372 var hex = toHex(enc.getReducedOstream());
373
374 return hex;
375
376
377}
378
379
380function encodeToHexContentObject(co){
381 var enc = new BinaryXMLEncoder();
382
383 co.encode(enc);
384
385 var hex = toHex(enc.getReducedOstream());
386
387 return hex;
388
389
390}
391
392function decodeHexInterest(result){
393 var numbers = toNumbers(result);
394
395
396 decoder = new BinaryXMLDecoder(numbers);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700397 if(LOG>3)console.log('DECODED HEX INTERST \n'+numbers);
398
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700399 i = new Interest();
400
401 i.decode(decoder);
402
403 return i;
404
405}
406
407function decodeHexContentObject(result){
408 var numbers = toNumbers(result);
409
410 decoder = new BinaryXMLDecoder(numbers);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700411 if(LOG>3)console.log('DECODED HEX CONTENT OBJECT \n'+numbers);
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700412
413 co = new ContentObject();
414
415 co.decode(decoder);
416
417 return co;
418
419}
420
421
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700422
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700423
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700424// Get something from the socket
425function on_socket_get(message){}
426
427// Report an error
428function on_socket_error(message){
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700429 alert('Received error message \n' +message);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700430}
431
432// Get the applet object
433function get_java_socket_bridge(){
434 return document.getElementById('JavaSocketBridge');
435}