blob: 62f43b53d7bb6236d1cd16c07e632ae6159792a8 [file] [log] [blame]
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07001/*
2 * @author: ucla-cs
Jeff Thompson745026e2012-10-13 12:49:20 -07003 * See COPYING for copyright and distribution information.
4 * This file provides access to the JavaSocketBridge applet.
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07005 */
6
Meki Cherkaouif3d8f692012-05-18 15:44:28 -07007
Meki Cherkaoui8f173612012-06-06 01:05:40 -07008//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 Cherkaouiabb973b2012-05-09 14:25:57 -07009
Meki Cherkaoui8f173612012-06-06 01:05:40 -070010
11var LOG = 0;
12
13
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070014var java_socket_bridge_ready_flag = false;
15
16var ndnport =null;
17var ndnurl=null;
18
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070019var registeredPrefixes ={};
20
Meki Cherkaoui8f173612012-06-06 01:05:40 -070021/**
22 * Add a trim funnction for Strings
23 */
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070024String.prototype.trim = function() {
25 return this.replace(/^\s+|\s+$/g, "");
26};
27
Meki Cherkaoui8f173612012-06-06 01:05:40 -070028
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070029// Applet reports it is ready to use
30function java_socket_bridge_ready(){
Meki Cherkaoui8f173612012-06-06 01:05:40 -070031 console.log('APPLET LOADED');
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070032 java_socket_bridge_ready_flag = true;
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070033
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070034}
35
Meki Cherkaoui8f173612012-06-06 01:05:40 -070036// Send Test Interest
37function get(host,port,data){
38 if(java_socket_bridge_ready_flag){
39 return get_java_socket_bridge().get(host,port,data,1000);
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070040 }
41 else{
Meki Cherkaoui8f173612012-06-06 01:05:40 -070042 on_socket_error("Java Socket Bridge send Interest until the applet has loaded");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070043 }
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070044}
45
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070046
Meki Cherkaoui8f173612012-06-06 01:05:40 -070047// Send Test Interest
Meki Cherkaoui81bfc282012-06-06 03:23:25 -070048function put(host,port,data,name,toReturn){
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070049
Meki Cherkaoui8f173612012-06-06 01:05:40 -070050 if(java_socket_bridge_ready_flag){
Meki Cherkaoui81bfc282012-06-06 03:23:25 -070051 return get_java_socket_bridge().put(host,port,data,name,toReturn);
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070052 }
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070053 else{
Meki Cherkaoui8f173612012-06-06 01:05:40 -070054 on_socket_error("Java Socket Bridge send Interest until the applet has loaded");
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070055 }
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070056}
57
Meki Cherkaoui8f173612012-06-06 01:05:40 -070058function on_socket_received_interest(hex,name){
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070059
Meki Cherkaoui8f173612012-06-06 01:05:40 -070060 if(LOG>3)console.log('received interest from host'+ host +':'+port+' with name '+name);
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070061
Meki Cherkaoui8f173612012-06-06 01:05:40 -070062 if(LOG>3)console.log('DATA ');
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070063
Meki Cherkaoui8f173612012-06-06 01:05:40 -070064 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 Thompsonf3bd3592012-09-29 23:25:30 -070076 var co = new ContentObject(new Name(name),si,answer,new Signature());
Meki Cherkaoui8f173612012-06-06 01:05:40 -070077 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 Cherkaouiabb973b2012-05-09 14:25:57 -070085}
86
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070087// Get something from the socket
88function on_socket_get(message){}
89
90// Report an error
91function on_socket_error(message){
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070092 alert('Received error message \n' +message);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070093}
94
95// Get the applet object
96function get_java_socket_bridge(){
97 return document.getElementById('JavaSocketBridge');
98}