blob: a8f064d52455504fbdb8215f37284d137439dfb1 [file] [log] [blame]
Meki Cherkaoui4910fbc2012-04-22 15:51:46 -07001// @author: ucla-cs
Meki Cherkaoui9eaf4142012-04-22 15:32:12 -07002
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07003
4import java.applet.*;
5import javax.swing.*;
6import netscape.javascript.*;
7
8import java.net.*;
9import java.security.AccessController;
10import java.security.PrivilegedAction;
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070011import java.util.concurrent.ConcurrentHashMap;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070012import java.io.*;
13
14public class JavaSocketBridge extends JApplet {
15
16 private final static int PACKETSIZE = 3000 ;
17 // Instance variables
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070018 static JSObject browser = null; // The browser
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070019
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070020 static ConcurrentHashMap hm = null;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070021
22 // Initialize
23 public void init(){
24 browser = JSObject.getWindow(this);
25 }
26
27 public String connectAndStart(final String ip, final int por, final String interest){
28 return AccessController.doPrivileged(
29 new PrivilegedAction<String>() {
30 public String run() {
31
32 DatagramSocket socket = null ;
33 byte[] output = null;
34 try
35 {
36 // Convert the arguments first, to ensure that they are valid
37 InetAddress host = InetAddress.getByName( ip ) ;
38 int port = por ;
39
40 // Construct the socket
41 socket = new DatagramSocket() ;
42
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070043
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070044 byte [] data = hex2Byte(interest);
45 DatagramPacket packet = new DatagramPacket( data, data.length, host, port ) ;
46
47 // Send it
48 socket.send( packet ) ;
49
50 // Set a receive timeout, 2000 milliseconds
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070051 socket.setSoTimeout( 4000 ) ;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070052
53 // Prepare the packet for receive
54 packet.setData( new byte[PACKETSIZE] ) ;
55
56 // Wait for a response from the server
57 socket.receive( packet ) ;
58
59 // Print the response
60 output = packet.getData() ;
61
62 }
63 catch( Exception e )
64 {
65 error(e.toString());
66 System.out.println( e ) ;
67 }
68 finally
69 {
70 if( socket != null )
71 socket.close() ;
72 }
73
74 if(output!=null)
75 return byte2hex(output);
76 else
77 return "";
78 }
79 }
80 );
81
82 }
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070083
84 public String sendContentObject(final String ip, final int por, final String interest){
85 return AccessController.doPrivileged(
86 new PrivilegedAction<String>() {
87 public String run() {
88
89 DatagramSocket socket = null ;
90 byte[] output = null;
91 try
92 {
93 // Convert the arguments first, to ensure that they are valid
94 InetAddress host = InetAddress.getByName( ip ) ;
95 int port = por ;
96
97 // Construct the socket
98 socket = new DatagramSocket() ;
99
100
101 byte [] data = hex2Byte(interest);
102 DatagramPacket packet = new DatagramPacket( data, data.length, host, port ) ;
103
104 // Send it
105 socket.send( packet );
106 }
107 catch( Exception e )
108 {
109 error(e.toString());
110 System.out.println( e ) ;
111 }
112 finally
113 {
114 if( socket != null )
115 socket.close() ;
116 }
117
118 if(output!=null)
119 return byte2hex(output);
120 else
121 return "";
122 }
123 }
124 );
125
126 }
127
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700128 public static byte[] hex2Byte(String str)
129 {
130 byte[] bytes = new byte[str.length() / 2];
131 for (int i = 0; i < bytes.length; i++)
132 {
133 bytes[i] = (byte) Integer
134 .parseInt(str.substring(2 * i, 2 * i + 2), 16);
135 }
136
137 return bytes;
138 }
139 public static String byte2hex(byte[] b)
140 {
141 // String Buffer can be used instead
142 String hs = "";
143 String stmp = "";
144
145 for (int n = 0; n < b.length; n++)
146 {
147 stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
148
149 if (stmp.length() == 1)
150 {
151 hs = hs + "0" + stmp;
152 }
153 else
154 {
155 hs = hs + stmp;
156 }
157
158 if (n < b.length - 1)
159 {
160 hs = hs + "";
161 }
162 }
163
164 return hs;
165 }
166
167
168 // Main
169 // Note: This method loops over and over to handle requests becuase only
170 // this thread gets the elevated security policy. Java == stupid.
171 public void start(){
172 try {
173 browser.call("java_socket_bridge_ready", null);
174 } catch (JSException e) {
175 // TODO Auto-generated catch block
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700176 error(e.getMessage());
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700177 }
178
179 }
180
181
182 // Report an error
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700183 public static void error(String message){
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700184 message = "Java Socket Bridge ERROR: " + message;
185 Object[] arguments = new Object[1];
186 arguments[0] = message;
187 try {
188 browser.call("on_socket_error", arguments);
189 } catch (JSException e) {
190 // TODO Auto-generated catch block
191 e.printStackTrace();
192 }
193 }
194
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700195 // Report an error
196 public static void receivedInterest(String IP,int port, byte[] data){
197
198
199 Object[] arguments = new Object[3];
200 arguments[0] = IP;
201 arguments[1] = port;
202 arguments[2] = byte2hex( data );
203
204 try {
205 browser.call("on_socket_received_interest", arguments);
206 } catch (JSException e) {
207 // TODO Auto-generated catch block
208 error(e.getMessage());
209 }
210
211 }
212
213
214 public static void connectAndStartAndPublish()
215 {
216
217 AccessController.doPrivileged(
218 new PrivilegedAction<String>() {
219 public String run() {
220 Thread t = new Thread( new Runnable(){
221 public void run(){
222
223
224 String message;
225 try{
226 DatagramSocket serverSocket = new DatagramSocket(9876);
227 byte[] receiveData = new byte[1024];
228 byte[] sendData = new byte[1024];
229 while(true)
230 {
231 DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
232 serverSocket.receive(receivePacket);
233
234 //String sentence = new String( receivePacket.getData());
235
236 //System.out.println("RECEIVED: " + sentence);
237
238
239 InetAddress IPAddress = receivePacket.getAddress();
240
241 int port = receivePacket.getPort();
242
243 byte[] receivedData = receivePacket.getData();
244
245 receivedInterest( IPAddress.getHostAddress() , port, receivedData);
246
247 //String capitalizedSentence = sentence.toUpperCase();
248
249 //DATA PACKET HERE!
250
251
252 //sendData = capitalizedSentence.getBytes();
253
254 //DatagramPacket sendPacket =
255 // new DatagramPacket(sendData, sendData.length, IPAddress, port);
256 //serverSocket.send(sendPacket);
257 }
258 }
259 catch(Exception e){
260 error("Exception " + e + "FAILURE, ERROR IN SOCKET CONECTION");
261 }
262
263
264 }
265 });
266
267 t.start();
268 return "SUCCESS";
269
270
271 }
272
273
274 }
275 );
276
277 }
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700278
279}