Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 1 | /*
|
| 2 | * @author: ucla-cs
|
| 3 | * This class represents Interest Objects
|
| 4 | */
|
| 5 |
|
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 6 |
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 7 | //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 Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 8 |
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 9 |
|
| 10 | var LOG = 0;
|
| 11 |
|
| 12 |
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 13 | var java_socket_bridge_ready_flag = false;
|
| 14 |
|
| 15 | var ndnport =null;
|
| 16 | var ndnurl=null;
|
| 17 |
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 18 | var registeredPrefixes ={};
|
| 19 |
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 20 | /**
|
| 21 | * Add a trim funnction for Strings
|
| 22 | */
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 23 | String.prototype.trim = function() {
|
| 24 | return this.replace(/^\s+|\s+$/g, "");
|
| 25 | };
|
| 26 |
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 27 |
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 28 | // Applet reports it is ready to use
|
| 29 | function java_socket_bridge_ready(){
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 30 | console.log('APPLET LOADED');
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 31 | java_socket_bridge_ready_flag = true;
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 32 |
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 33 | }
|
| 34 |
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 35 | // Send Test Interest
|
| 36 | function get(host,port,data){
|
| 37 | if(java_socket_bridge_ready_flag){
|
| 38 | return get_java_socket_bridge().get(host,port,data,1000);
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 39 | }
|
| 40 | else{
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 41 | on_socket_error("Java Socket Bridge send Interest until the applet has loaded");
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 42 | }
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 43 | }
|
| 44 |
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 45 |
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 46 | // Send Test Interest
|
Meki Cherkaoui | 81bfc28 | 2012-06-06 03:23:25 -0700 | [diff] [blame] | 47 | function put(host,port,data,name,toReturn){
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 48 |
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 49 | if(java_socket_bridge_ready_flag){
|
Meki Cherkaoui | 81bfc28 | 2012-06-06 03:23:25 -0700 | [diff] [blame] | 50 | return get_java_socket_bridge().put(host,port,data,name,toReturn);
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 51 | }
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 52 | else{
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 53 | on_socket_error("Java Socket Bridge send Interest until the applet has loaded");
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 54 | }
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 55 | }
|
| 56 |
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 57 | function on_socket_received_interest(hex,name){
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 58 |
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 59 | if(LOG>3)console.log('received interest from host'+ host +':'+port+' with name '+name);
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 60 |
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 61 | if(LOG>3)console.log('DATA ');
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 62 |
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 63 | 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 |
|
| 75 | var co = new ContentObject(new ContentName(name),si,answer,new Signature());
|
| 76 | 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 Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 84 | }
|
| 85 |
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 86 | // Get something from the socket
|
| 87 | function on_socket_get(message){}
|
| 88 |
|
| 89 | // Report an error
|
| 90 | function on_socket_error(message){
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 91 | alert('Received error message \n' +message);
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 92 | }
|
| 93 |
|
| 94 | // Get the applet object
|
| 95 | function get_java_socket_bridge(){
|
| 96 | return document.getElementById('JavaSocketBridge');
|
| 97 | } |