Meki Cherkaoui | 4910fbc | 2012-04-22 15:51:46 -0700 | [diff] [blame] | 1 | // @author: ucla-cs
|
Meki Cherkaoui | 9eaf414 | 2012-04-22 15:32:12 -0700 | [diff] [blame] | 2 |
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 3 |
|
| 4 | import java.applet.*;
|
| 5 | import javax.swing.*;
|
| 6 | import netscape.javascript.*;
|
| 7 |
|
| 8 | import java.net.*;
|
| 9 | import java.security.AccessController;
|
| 10 | import java.security.PrivilegedAction;
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame^] | 11 | import java.util.concurrent.ConcurrentHashMap;
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 12 | import java.io.*;
|
| 13 |
|
| 14 | public class JavaSocketBridge extends JApplet {
|
| 15 |
|
| 16 | private final static int PACKETSIZE = 3000 ;
|
| 17 | // Instance variables
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame^] | 18 | static JSObject browser = null; // The browser
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 19 |
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame^] | 20 | static ConcurrentHashMap hm = null;
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 21 |
|
| 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 Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame^] | 43 |
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 44 | 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 Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame^] | 51 | socket.setSoTimeout( 4000 ) ;
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 52 |
|
| 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 Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame^] | 83 |
|
| 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 Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 128 | 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 Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame^] | 176 | error(e.getMessage());
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 177 | }
|
| 178 |
|
| 179 | }
|
| 180 |
|
| 181 |
|
| 182 | // Report an error
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame^] | 183 | public static void error(String message){
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 184 | 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 Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame^] | 195 | // 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 Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 278 |
|
| 279 | } |