blob: 5ecb3433bf3245ea0ae8818248ea1a98323c8365 [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
32function createRoute(url, port){
33 ndnport = port;
34 ndnurl=url;
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070035
36 console.log(new BinaryXMLDecoder());
37
38 //SEND INTERST TO CCNX NODE
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070039
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070040
41 //Now Start the receiving thread
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070042
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070043}
44
45// Connect to a given url and port
46//Error -1 No countent found
47//Error -2 Empty query
48function queryPrefix(message){
49 if(ndnport!=null && ndnurl!=null){
50 var newMessage ='';
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070051
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070052
53 message = message.trim();
54
55
56
57 if(message==null || message =="" || message=="/"){
58 return -2;
59 }
60
61 //message = decodeURIComponent(message);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070062
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070063 var array = createNameArray(message);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070064
65 //console.log('ARRAY IS '+ array);
66
67 enc = new BinaryXMLEncoder();
68
69 int = new Interest(new ContentName(array));
70
71 int.encode(enc);
72
73 var hex = toHex(enc.getReducedOstream());
74
75
76
77 //console.log('Connecting and start '+ ndnurl +':'+ndnport+'-'+message);
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070078
79
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070080 var result = get_java_socket_bridge().connectAndStart(ndnurl,ndnport,hex);
81
82 console.log('BINARY RESPONSE IS ' +result);
83
84
85 //result[0] and result[1] should be 0 and 4 if there is a content object found
86 if(result==null || result==undefined || result =="" || result[0] != '0'||result[1]!='4'){
87 return -1;
88 }
89
90 else{
91
92 var numbers = toNumbers(result);
93
94 console.log('HEX RESPONSE IS \n'+numbers);
95 decoder = new BinaryXMLDecoder(numbers);
96
97
98 co = new ContentObject();
99
100 co.decode(decoder);
101
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700102 if(LOG>2) console.log(co);
103
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700104 return co;
105
106
107 }
108
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700109
110 }
111
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700112
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700113 else{
114
115 alert('ERROR URL OR PORT NOT SET');
116
117 return -3;
118
119 }
120
121}
122
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700123var registerStarted = false;
124function registerPrefix(name, content){
125
126 registeredPrefixes[name] = content ;
127
128 if(registerStarted == false){
129 var result = get_java_socket_bridge().connectAndStartAndPublish();
130
131 startRegisterPrefix();
132
133 registerStarted = true;
134 }
135 sendForwardingEntry(10);
136}
137
138
139function unRegisterPrefix(name){
140
141 delete registeredPrefixes[name];
142
143}
144
145
146
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700147
148function on_socket_received_interest(IP, port, interestBinary){
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700149 console.log('WOOOO RECEIVED STUFF' );
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700150 var interest = decodeHexInterest(interestBinary);
151
152 console.log('WOOO received interest' + interest.Name.Components);
153
154 var stringName = "";
155
156 for(var i=0;i<interest.Name.Components.length;i++){
157 stringName += "/"+ interest.Name.Components[i];
158 }
159
160 if(registeredPrefix[stringName]!=null){
161 if(LOG>1)console.log("CANNOT FIND THE OBJECT OF NAME " + stringName );
162 }
163 else{
164 var co = new ContentObject(interest.Name, null,registeredPrefix[stringName],null );
165
166 var hex = encodeToHexContentObject(co);
167
168 get_java_socket_bridge().sendContentObject(IP,port,hex);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700169
170
171 }
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700172}
173
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700174
175
176
177// Connect to a given url and port
178//Error -1 No countent found
179//Error -2 Empty query
180function startRegisterPrefix(){
181 if(LOG>2) console.log('START REGISTER PREFIX');
182
183 if(ndnport!=null && ndnurl!=null){
184 var newMessage ='';
185
186
187
188 name = name.trim();
189
190
191
192 ///////////////////////
193 var face = new FaceInstance('newface',null,null, 17, '127.0.0.1',9876,null,null,null);
194
195 var encoder1 = new BinaryXMLEncoder();
196
197 face.encode(encoder1);
198
199 var faceInstanceBinary = encoder1.getReducedOstream();
200
201
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700202 var si = new SignedInfo();
203 si.setFields();
204
205 var co = new ContentObject(new ContentName(),si,faceInstanceBinary,new Signature());
206 co.sign();
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700207
208 var encoder2 = new BinaryXMLEncoder();
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700209
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700210 co.encode(encoder2);
211
212 var coBinary = encoder2.getReducedOstream();
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700213
214 //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'));
215
216 //var interestName = new ContentName(['ccnx',co.SignedInfo.Publisher.PublisherPublicKeyDigest,'newface',coBinary]);
217 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]);
218 //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 -0700219
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700220 //var interestName = new ContentName(['ccnx','1234','newface',coBinary]);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700221 //var interestName = new ContentName(['ccnx',co.SignedInfo.Publisher.PublisherPublicKeyDigest,'newface',coBinary]);
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700222 int = new Interest(interestName,face);
223
224 var hex = encodeToHexInterest(int);
225 /////////////////
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700226
227
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700228
229 if(LOG>4)console.log('Interst name of Conntection Message is '+ interestName);
230
231
232 if(LOG>4) console.log('Connecting and start '+ ndnurl +':'+ndnport+'-'+hex);
233 //console.log('Connecting and start '+ ndnurl +':'+ndnport+'-'+message);
234
235 var result = get_java_socket_bridge().connectAndStart(ndnurl,ndnport,hex);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700236
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700237
238 //TODO MOVE THIS
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700239
240 //result[0] and result[1] should be 0 and 4 if there is a content object found
241 if(result==null || result==undefined || result =="" || result[0] != '0'||result[1]!='4'){
242 return -1;
243 }
244
245 if(LOG>4) console.log('RECEIVED THE FOLLOWING DATA: ' +co.Content);
246
247 else{
248
249 co = decodeHexContentObject(result);
250
251 if(LOG>4) console.log('RECEIVED THE FOLLOWING DATA: ' +co.Content);
252
253 return co;
254 }
255 }
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700256 else{
257
258 alert('ERROR URL OR PORT NOT SET');
259
260 return -3;
261
262 }
263
264}
265
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700266
267// Connect to a given url and port
268//Error -1 No countent found
269//Error -2 Empty query
270function sendForwardingEntry(faceID){
271 if(LOG>2) console.log('START REGISTER PREFIX');
272
273 if(ndnport!=null && ndnurl!=null){
274 var newMessage ='';
275
276
277
278 name = name.trim();
279
280
281
282 ///////////////////////
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700283 var face = new ForwardingEntry('prefixreg',new ContentName(['helloworld']),null, faceID, 1,null);
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700284
285 var encoder1 = new BinaryXMLEncoder();
286
287 face.encode(encoder1);
288
289 var faceInstanceBinary = encoder1.getReducedOstream();
290
291
292
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700293 var si = new SignedInfo();
294 si.setFields();
295
296 var co = new ContentObject(new ContentName(),si,faceInstanceBinary,new Signature());
297 co.sign();
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700298
299 var encoder2 = new BinaryXMLEncoder();
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700300
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700301 co.encode(encoder2);
302
303 var coBinary = encoder2.getReducedOstream();
304
305
306
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700307 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]);
308 //var interestName = new ContentName(['ccnx',co.SignedInfo.Publisher.PublisherPublicKeyDigest,'newface',coBinary]);
309 //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]);
310
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700311 //var interestName = new ContentName(['ccnx','1234','newface',coBinary]);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700312 //var interestName = new ContentName(['ccnx',co.SignedInfo.Publisher.PublisherPublicKeyDigest,'prefixreg',coBinary]);
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700313
314 int = new Interest(interestName,face);
315
316 var hex = encodeToHexInterest(int);
317 /////////////////
318
319
320
321 if(LOG>4)console.log('Interst name of Conntection Message is '+ interestName);
322
323
324 if(LOG>4) console.log('Connecting and start '+ ndnurl +':'+ndnport+'-'+hex);
325 //console.log('Connecting and start '+ ndnurl +':'+ndnport+'-'+message);
326
327 var result = get_java_socket_bridge().connectAndStart(ndnurl,ndnport,hex);
328
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700329 if(LOG>3)console.log('BINARY RESPONSE IS ' +result);
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700330
331
332 //result[0] and result[1] should be 0 and 4 if there is a content object found
333 if(result==null || result==undefined || result =="" || result[0] != '0'||result[1]!='4'){
334 return -1;
335 }
336
337 if(LOG>4) console.log('RECEIVED THE FOLLOWING DATA: ' +co.Content);
338
339 else{
340
341 co = decodeHexContentObject(result);
342
343 if(LOG>4) console.log('RECEIVED THE FOLLOWING DATA: ' +co.Content);
344
345 return co;
346 }
347 }
348 else{
349
350 alert('ERROR URL OR PORT NOT SET');
351
352 return -3;
353
354 }
355
356}
357
358
359
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700360
361function encodeToHexInterest(int){
362
363 var enc = new BinaryXMLEncoder();
364
365 int.encode(enc);
366
367 var hex = toHex(enc.getReducedOstream());
368
369 return hex;
370
371
372}
373
374
375function encodeToHexContentObject(co){
376 var enc = new BinaryXMLEncoder();
377
378 co.encode(enc);
379
380 var hex = toHex(enc.getReducedOstream());
381
382 return hex;
383
384
385}
386
387function decodeHexInterest(result){
388 var numbers = toNumbers(result);
389
390
391 decoder = new BinaryXMLDecoder(numbers);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700392 if(LOG>3)console.log('DECODED HEX INTERST \n'+numbers);
393
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700394 i = new Interest();
395
396 i.decode(decoder);
397
398 return i;
399
400}
401
402function decodeHexContentObject(result){
403 var numbers = toNumbers(result);
404
405 decoder = new BinaryXMLDecoder(numbers);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700406 if(LOG>3)console.log('DECODED HEX CONTENT OBJECT \n'+numbers);
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700407
408 co = new ContentObject();
409
410 co.decode(decoder);
411
412 return co;
413
414}
415
416
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700417
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700418
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700419// Get something from the socket
420function on_socket_get(message){}
421
422// Report an error
423function on_socket_error(message){
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700424 alert('Received error message \n' +message);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700425}
426
427// Get the applet object
428function get_java_socket_bridge(){
429 return document.getElementById('JavaSocketBridge');
430}