blob: 48c9c4d15d62d766ea6bea84f8bed6377e7a8c97 [file] [log] [blame]
Meki Cherkaoui9eaf4142012-04-22 15:32:12 -07001// @author: Meki cherkaoui
2
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;
11import java.io.*;
12
13public class JavaSocketBridge extends JApplet {
14
15 private final static int PACKETSIZE = 3000 ;
16 // Instance variables
17 JSObject browser = null; // The browser
18
19
20 // Initialize
21 public void init(){
22 browser = JSObject.getWindow(this);
23 }
24
25 public String connectAndStart(final String ip, final int por, final String interest){
26 return AccessController.doPrivileged(
27 new PrivilegedAction<String>() {
28 public String run() {
29
30 DatagramSocket socket = null ;
31 byte[] output = null;
32 try
33 {
34 // Convert the arguments first, to ensure that they are valid
35 InetAddress host = InetAddress.getByName( ip ) ;
36 int port = por ;
37
38 // Construct the socket
39 socket = new DatagramSocket() ;
40
41
42 byte [] data = hex2Byte(interest);
43 DatagramPacket packet = new DatagramPacket( data, data.length, host, port ) ;
44
45 // Send it
46 socket.send( packet ) ;
47
48 // Set a receive timeout, 2000 milliseconds
49 socket.setSoTimeout( 2000 ) ;
50
51 // Prepare the packet for receive
52 packet.setData( new byte[PACKETSIZE] ) ;
53
54 // Wait for a response from the server
55 socket.receive( packet ) ;
56
57 // Print the response
58 output = packet.getData() ;
59
60 }
61 catch( Exception e )
62 {
63 error(e.toString());
64 System.out.println( e ) ;
65 }
66 finally
67 {
68 if( socket != null )
69 socket.close() ;
70 }
71
72 if(output!=null)
73 return byte2hex(output);
74 else
75 return "";
76 }
77 }
78 );
79
80 }
81 public static byte[] hex2Byte(String str)
82 {
83 byte[] bytes = new byte[str.length() / 2];
84 for (int i = 0; i < bytes.length; i++)
85 {
86 bytes[i] = (byte) Integer
87 .parseInt(str.substring(2 * i, 2 * i + 2), 16);
88 }
89
90 return bytes;
91 }
92 public static String byte2hex(byte[] b)
93 {
94 // String Buffer can be used instead
95 String hs = "";
96 String stmp = "";
97
98 for (int n = 0; n < b.length; n++)
99 {
100 stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
101
102 if (stmp.length() == 1)
103 {
104 hs = hs + "0" + stmp;
105 }
106 else
107 {
108 hs = hs + stmp;
109 }
110
111 if (n < b.length - 1)
112 {
113 hs = hs + "";
114 }
115 }
116
117 return hs;
118 }
119
120
121 // Main
122 // Note: This method loops over and over to handle requests becuase only
123 // this thread gets the elevated security policy. Java == stupid.
124 public void start(){
125 try {
126 browser.call("java_socket_bridge_ready", null);
127 } catch (JSException e) {
128 // TODO Auto-generated catch block
129 e.printStackTrace();
130 }
131
132 }
133
134
135 // Report an error
136 public void error(String message){
137 message = "Java Socket Bridge ERROR: " + message;
138 Object[] arguments = new Object[1];
139 arguments[0] = message;
140 try {
141 browser.call("on_socket_error", arguments);
142 } catch (JSException e) {
143 // TODO Auto-generated catch block
144 e.printStackTrace();
145 }
146 }
147
148
149}