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 | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 35 |
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 36 |
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame^] | 37 | // Send Test Interest
|
| 38 | function get(host,port,data){
|
| 39 | if(java_socket_bridge_ready_flag){
|
| 40 | return get_java_socket_bridge().get(host,port,data,1000);
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 41 | }
|
| 42 | else{
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame^] | 43 | 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] | 44 | }
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 45 | }
|
| 46 |
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 47 |
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame^] | 48 | // Send Test Interest
|
| 49 | function put(host,port,data,name){
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 50 |
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame^] | 51 | if(java_socket_bridge_ready_flag){
|
| 52 | return get_java_socket_bridge().put(host,port,data,name);
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 53 | }
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 54 | else{
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame^] | 55 | 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] | 56 | }
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 57 | }
|
| 58 |
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame^] | 59 | function on_socket_received_interest(hex,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('received interest from host'+ host +':'+port+' with name '+name);
|
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('DATA ');
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 64 |
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame^] | 65 | if(LOG>3)console.log(hex);
|
| 66 |
|
| 67 | interest = decodeHexInterest(hex);
|
| 68 |
|
| 69 | console.log('SUCCESSFULLY PARSED INTEREST');
|
| 70 |
|
| 71 | console.log('CREATING ANSWER');
|
| 72 | var si = new SignedInfo();
|
| 73 | si.setFields();
|
| 74 |
|
| 75 | var answer = toNumbersFromString('WORLD');
|
| 76 |
|
| 77 | var co = new ContentObject(new ContentName(name),si,answer,new Signature());
|
| 78 | co.sign();
|
| 79 |
|
| 80 |
|
| 81 | var outputHex = encodeToHexContentObject(co);
|
| 82 |
|
| 83 | console.log('SENDING ANSWER');
|
| 84 |
|
| 85 | return get_java_socket_bridge().putAnswer(outputHex,name);
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 86 | }
|
| 87 |
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 88 | // Get something from the socket
|
| 89 | function on_socket_get(message){}
|
| 90 |
|
| 91 | // Report an error
|
| 92 | function on_socket_error(message){
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 93 | alert('Received error message \n' +message);
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 94 | }
|
| 95 |
|
| 96 | // Get the applet object
|
| 97 | function get_java_socket_bridge(){
|
| 98 | return document.getElementById('JavaSocketBridge');
|
| 99 | } |