Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 1 | /*
|
| 2 | * @author: ucla-cs
|
Jeff Thompson | 745026e | 2012-10-13 12:49:20 -0700 | [diff] [blame^] | 3 | * See COPYING for copyright and distribution information.
|
| 4 | * This file provides access to the JavaSocketBridge applet.
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 5 | */
|
| 6 |
|
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 7 |
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 8 | //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] | 9 |
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 10 |
|
| 11 | var LOG = 0;
|
| 12 |
|
| 13 |
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 14 | var java_socket_bridge_ready_flag = false;
|
| 15 |
|
| 16 | var ndnport =null;
|
| 17 | var ndnurl=null;
|
| 18 |
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 19 | var registeredPrefixes ={};
|
| 20 |
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 21 | /**
|
| 22 | * Add a trim funnction for Strings
|
| 23 | */
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 24 | String.prototype.trim = function() {
|
| 25 | return this.replace(/^\s+|\s+$/g, "");
|
| 26 | };
|
| 27 |
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 28 |
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 29 | // Applet reports it is ready to use
|
| 30 | function java_socket_bridge_ready(){
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 31 | console.log('APPLET LOADED');
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 32 | java_socket_bridge_ready_flag = true;
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 33 |
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 34 | }
|
| 35 |
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 36 | // Send Test Interest
|
| 37 | function get(host,port,data){
|
| 38 | if(java_socket_bridge_ready_flag){
|
| 39 | return get_java_socket_bridge().get(host,port,data,1000);
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 40 | }
|
| 41 | else{
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 42 | 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] | 43 | }
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 44 | }
|
| 45 |
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 46 |
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 47 | // Send Test Interest
|
Meki Cherkaoui | 81bfc28 | 2012-06-06 03:23:25 -0700 | [diff] [blame] | 48 | function put(host,port,data,name,toReturn){
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 49 |
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 50 | if(java_socket_bridge_ready_flag){
|
Meki Cherkaoui | 81bfc28 | 2012-06-06 03:23:25 -0700 | [diff] [blame] | 51 | return get_java_socket_bridge().put(host,port,data,name,toReturn);
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 52 | }
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 53 | else{
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 54 | 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] | 55 | }
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 56 | }
|
| 57 |
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 58 | function on_socket_received_interest(hex,name){
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 59 |
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 60 | 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] | 61 |
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 62 | if(LOG>3)console.log('DATA ');
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 63 |
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 64 | if(LOG>3)console.log(hex);
|
| 65 |
|
| 66 | interest = decodeHexInterest(hex);
|
| 67 |
|
| 68 | console.log('SUCCESSFULLY PARSED INTEREST');
|
| 69 |
|
| 70 | console.log('CREATING ANSWER');
|
| 71 | var si = new SignedInfo();
|
| 72 | si.setFields();
|
| 73 |
|
| 74 | var answer = toNumbersFromString('WORLD');
|
| 75 |
|
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 76 | var co = new ContentObject(new Name(name),si,answer,new Signature());
|
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 77 | co.sign();
|
| 78 |
|
| 79 |
|
| 80 | var outputHex = encodeToHexContentObject(co);
|
| 81 |
|
| 82 | console.log('SENDING ANSWER');
|
| 83 |
|
| 84 | return get_java_socket_bridge().putAnswer(outputHex,name);
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 85 | }
|
| 86 |
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 87 | // Get something from the socket
|
| 88 | function on_socket_get(message){}
|
| 89 |
|
| 90 | // Report an error
|
| 91 | function on_socket_error(message){
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 92 | alert('Received error message \n' +message);
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 93 | }
|
| 94 |
|
| 95 | // Get the applet object
|
| 96 | function get_java_socket_bridge(){
|
| 97 | return document.getElementById('JavaSocketBridge');
|
| 98 | } |