blob: 877f1b952c82dc4d036fec269295bd2ce54444bb [file] [log] [blame]
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07001// JavaSocketBridge.java
2// by Stephen Ware
3// April 25, 2009
4//
5// Part of the JavaSocketBridge project.
6// This applet provides an interface for using true sockets in JavaScript.
7//
8// Note: You will need to have the Java Plugin archive in your classpath to compile this.
9// For me, that's C:\Program Files\Java\jre6\lib\plugin.jar
10// Note: You will need to jar this class and Listener.class into a signed jar file if
11// you want your sockets to access domains other than the one this is running on.
12// Note: Apparently, when you grant permissions to Java applets in Java 6, you only grant
13// them to the main applet thread. That's the reason for all the confusing stuff
14// in the connect methods... so that connections always happen on the main thread.
15
16import java.applet.*;
17import javax.swing.*;
18import netscape.javascript.*;
19
20import java.net.*;
21import java.security.AccessController;
22import java.security.PrivilegedAction;
23import java.io.*;
24
25public class JavaSocketBridge extends JApplet {
26
27 private final static int PACKETSIZE = 3000 ;
28 // Instance variables
29 JSObject browser = null; // The browser
30
31
32 // Initialize
33 public void init(){
34 browser = JSObject.getWindow(this);
35 }
36
37 public String connectAndStart(final String ip, final int por, final String interest){
38 return AccessController.doPrivileged(
39 new PrivilegedAction<String>() {
40 public String run() {
41
42 DatagramSocket socket = null ;
43 byte[] output = null;
44 try
45 {
46 // Convert the arguments first, to ensure that they are valid
47 InetAddress host = InetAddress.getByName( ip ) ;
48 int port = por ;
49
50 // Construct the socket
51 socket = new DatagramSocket() ;
52
53
54 byte [] data = hex2Byte(interest);
55 DatagramPacket packet = new DatagramPacket( data, data.length, host, port ) ;
56
57 // Send it
58 socket.send( packet ) ;
59
60 // Set a receive timeout, 2000 milliseconds
61 socket.setSoTimeout( 2000 ) ;
62
63 // Prepare the packet for receive
64 packet.setData( new byte[PACKETSIZE] ) ;
65
66 // Wait for a response from the server
67 socket.receive( packet ) ;
68
69 // Print the response
70 output = packet.getData() ;
71
72 }
73 catch( Exception e )
74 {
75 error(e.toString());
76 System.out.println( e ) ;
77 }
78 finally
79 {
80 if( socket != null )
81 socket.close() ;
82 }
83
84 if(output!=null)
85 return byte2hex(output);
86 else
87 return "";
88 }
89 }
90 );
91
92 }
93 public static byte[] hex2Byte(String str)
94 {
95 byte[] bytes = new byte[str.length() / 2];
96 for (int i = 0; i < bytes.length; i++)
97 {
98 bytes[i] = (byte) Integer
99 .parseInt(str.substring(2 * i, 2 * i + 2), 16);
100 }
101
102 return bytes;
103 }
104 public static String byte2hex(byte[] b)
105 {
106 // String Buffer can be used instead
107 String hs = "";
108 String stmp = "";
109
110 for (int n = 0; n < b.length; n++)
111 {
112 stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
113
114 if (stmp.length() == 1)
115 {
116 hs = hs + "0" + stmp;
117 }
118 else
119 {
120 hs = hs + stmp;
121 }
122
123 if (n < b.length - 1)
124 {
125 hs = hs + "";
126 }
127 }
128
129 return hs;
130 }
131
132
133 // Main
134 // Note: This method loops over and over to handle requests becuase only
135 // this thread gets the elevated security policy. Java == stupid.
136 public void start(){
137 try {
138 browser.call("java_socket_bridge_ready", null);
139 } catch (JSException e) {
140 // TODO Auto-generated catch block
141 e.printStackTrace();
142 }
143
144 }
145
146
147 // Report an error
148 public void error(String message){
149 message = "Java Socket Bridge ERROR: " + message;
150 Object[] arguments = new Object[1];
151 arguments[0] = message;
152 try {
153 browser.call("on_socket_error", arguments);
154 } catch (JSException e) {
155 // TODO Auto-generated catch block
156 e.printStackTrace();
157 }
158 }
159
160
161}