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 |
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 8 | import java.lang.reflect.InvocationTargetException;
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 9 | import java.net.*;
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 10 | import java.nio.ByteBuffer;
|
| 11 | import java.nio.channels.ClosedChannelException;
|
| 12 | import java.nio.channels.DatagramChannel;
|
| 13 | import java.nio.channels.SelectionKey;
|
| 14 | import java.nio.channels.Selector;
|
| 15 | import java.nio.channels.ServerSocketChannel;
|
| 16 | import java.nio.channels.SocketChannel;
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 17 | import java.security.AccessController;
|
| 18 | import java.security.PrivilegedAction;
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 19 | import java.util.Hashtable;
|
| 20 | import java.util.Iterator;
|
| 21 | import java.util.Set;
|
| 22 | import java.util.Timer;
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 23 | import java.util.concurrent.ConcurrentHashMap;
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 24 | import java.util.concurrent.atomic.AtomicInteger;
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 25 | import java.io.*;
|
| 26 |
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 27 |
|
| 28 |
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 29 | public class JavaSocketBridge extends JApplet {
|
| 30 |
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 31 | static String protocol = "TCP";
|
| 32 |
|
| 33 | static volatile Hashtable<String, SocketChannel> connections = new Hashtable<String, SocketChannel>();
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 | volatile String result = "";
|
| 38 |
|
| 39 | //static ConcurrentHashMap hm = null;
|
| 40 | static boolean isBrowser = true;
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 | private final static int PACKETSIZE = 2000 ;
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 46 | // Instance variables
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 47 | static JSObject browser = null; // The browser
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 48 |
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 49 | protected Selector _ncReadSelector = null;
|
| 50 | protected Selector _ncWriteSelector = null;
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 51 |
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 52 |
|
| 53 | // Initialize automatically called by browser
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 54 | public void init(){
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 55 |
|
| 56 | try{
|
| 57 | browser = JSObject.getWindow(this);
|
| 58 | }
|
| 59 | catch(Exception e){
|
| 60 | error( "ERROR IN INIT" + e.toString());
|
| 61 | }
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 62 | }
|
| 63 |
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 64 | //start automatically called by browser
|
| 65 | public void start(){
|
| 66 | try {
|
| 67 | browser.call("java_socket_bridge_ready", null);
|
| 68 | } catch (Exception e) {
|
| 69 | // TODO Auto-generated catch block
|
| 70 | error("ERROR IN START" +e.toString());
|
| 71 | }
|
| 72 |
|
| 73 | }
|
| 74 |
|
| 75 | public String getOLD(final String h, final int p, final String interest, final int timeout){
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 76 | return AccessController.doPrivileged(
|
| 77 | new PrivilegedAction<String>() {
|
| 78 | public String run() {
|
| 79 |
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 80 | Socket sock=null;
|
| 81 | OutputStream out=null;
|
| 82 | InputStream in=null;
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 83 |
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 84 | try{
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 85 |
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 86 |
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 87 | sock=new Socket(h,p);
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 88 |
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 89 |
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 90 | String word= interest;
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 91 |
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 92 |
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 93 | in = sock.getInputStream();
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 94 |
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 95 | out=sock.getOutputStream();
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 96 |
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 97 |
|
| 98 | System.out.println("Your string is"+word);
|
| 99 |
|
| 100 | out.write(hex2Bytes(word));
|
| 101 | out.flush();
|
| 102 | sock.shutdownOutput();
|
| 103 |
|
| 104 | ByteArrayOutputStream serverinput=new ByteArrayOutputStream();
|
| 105 |
|
| 106 | int len=0;
|
| 107 | byte[] buf=new byte[PACKETSIZE];
|
| 108 | while ((len = in.read(buf))>=0) {
|
| 109 | serverinput.write(buf, 0, len);
|
| 110 | }
|
| 111 |
|
| 112 | String outputString = bytes2Hex(buf);
|
| 113 |
|
| 114 | System.out.println("Your string is "+word+" converted to byte "+outputString);
|
| 115 |
|
| 116 | return outputString;
|
| 117 |
|
| 118 | } catch (Exception e) {
|
| 119 | // TODO Auto-generated catch block
|
| 120 | error(e);
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 121 | }
|
| 122 |
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 123 |
|
| 124 | finally{
|
| 125 |
|
| 126 | try {
|
| 127 | sock.close();
|
| 128 | } catch (IOException e) {
|
| 129 | // TODO Auto-generated catch block
|
| 130 | error(e);
|
| 131 | }
|
| 132 |
|
| 133 | }
|
| 134 |
|
| 135 | return "ERROR";
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 136 | }
|
| 137 | }
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 138 |
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 139 | );
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 140 | }
|
| 141 |
|
| 142 | public String get(final String h, final int p, final String interest,final int timeout){
|
| 143 | return AccessController.doPrivileged(
|
| 144 | new PrivilegedAction<String>() {
|
| 145 | public String run() {
|
| 146 | Thread t = new Thread( new Runnable(){
|
| 147 | public void run(){
|
| 148 |
|
| 149 | SocketChannel sChannelCloser = null;
|
| 150 | try{
|
| 151 |
|
| 152 | final SocketChannel sChannel = open(h,p);
|
| 153 |
|
| 154 | sChannelCloser=sChannel;
|
| 155 |
|
| 156 | sChannel.socket().setSoTimeout(timeout);
|
| 157 |
|
| 158 | ByteBuffer buffer = ByteBuffer.wrap(hex2Bytes(interest));
|
| 159 |
|
| 160 | System.out.println("WRITING BYTES:"+interest);
|
| 161 |
|
| 162 | while(buffer.hasRemaining())
|
| 163 | sChannel.write(buffer);
|
| 164 |
|
| 165 | ByteBuffer bufferReceiver = ByteBuffer.allocate(PACKETSIZE);
|
| 166 |
|
| 167 | buffer.clear();
|
| 168 | sChannel.read(bufferReceiver);
|
| 169 |
|
jeff | fe2cdeb | 2012-09-09 17:46:10 -0700 | [diff] [blame] | 170 | String output = bytes2Hex(bufferReceiver.array(), bufferReceiver.position());
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 171 | System.out.println("RECEIVED BYTES:" +output);
|
| 172 |
|
| 173 | result=output;
|
| 174 |
|
| 175 | return ;
|
| 176 |
|
| 177 | } catch (Exception e) {
|
| 178 | // TODO Auto-generated catch block
|
| 179 | error(e);
|
| 180 | }
|
| 181 | finally{
|
| 182 |
|
| 183 | try {
|
| 184 | sChannelCloser.close();
|
| 185 | } catch (IOException e) {
|
| 186 | // TODO Auto-generated catch block
|
| 187 | error(e);
|
| 188 | }
|
| 189 |
|
| 190 | }
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 | //return "STARTED GETTING";
|
| 195 |
|
| 196 | }
|
| 197 | }
|
| 198 |
|
| 199 | );
|
| 200 |
|
| 201 | t.start();
|
| 202 |
|
| 203 | Long start = System.currentTimeMillis();
|
| 204 | Long current = System.currentTimeMillis();
|
| 205 |
|
| 206 | while(t.isAlive() && current-start < timeout ){
|
| 207 | try {
|
| 208 | Thread.sleep(100);
|
| 209 | } catch (InterruptedException e) {
|
| 210 | error(e);
|
| 211 | }
|
| 212 | current = System.currentTimeMillis();
|
| 213 | }
|
| 214 | if(current-start >= timeout){
|
| 215 | t.stop();
|
| 216 | return "TIMEOUT EXPIRED";
|
| 217 | }
|
| 218 | else{
|
| 219 | return result;
|
| 220 |
|
| 221 | }
|
| 222 |
|
| 223 |
|
| 224 | }
|
| 225 | }
|
| 226 |
|
| 227 | );
|
| 228 | }
|
| 229 |
|
| 230 |
|
| 231 | // Report an error
|
| 232 | public static void receivedInterest( String data,String name){
|
| 233 |
|
| 234 | Object[] arguments = new Object[2];
|
| 235 | arguments[0] = data;
|
| 236 | arguments[1] = name;
|
| 237 |
|
| 238 |
|
| 239 | try {
|
| 240 | browser.call("on_socket_received_interest", arguments);
|
| 241 | } catch (JSException e) {
|
| 242 | // TODO Auto-generated catch block
|
| 243 | error(e.getMessage());
|
| 244 | }
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 245 |
|
| 246 | }
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 247 |
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 248 | public String put(final String h, final int p, final String interest,final String name, final String toReturn){
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 249 | return AccessController.doPrivileged(
|
| 250 | new PrivilegedAction<String>() {
|
| 251 | public String run() {
|
| 252 |
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 253 |
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 254 | SocketChannel sChannelCloser = null;
|
| 255 | try{
|
| 256 |
|
| 257 | final SocketChannel sChannel = open(h,p);
|
| 258 | sChannelCloser=sChannel;
|
| 259 |
|
| 260 | //FIRST TIME
|
| 261 | //ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
|
| 262 |
|
| 263 | ByteBuffer buffer = ByteBuffer.wrap(hex2Bytes(interest));
|
| 264 |
|
| 265 | System.out.println("WRITING BYTES:"+interest);
|
| 266 | while(buffer.hasRemaining())
|
| 267 | sChannel.write(buffer);
|
| 268 |
|
| 269 | ByteBuffer bufferReceiver = ByteBuffer.allocate(PACKETSIZE);
|
| 270 |
|
| 271 | buffer.clear();
|
| 272 | sChannel.read(bufferReceiver);
|
| 273 |
|
| 274 | System.out.println("RECEIVED BYTES:" + bytes2Hex(bufferReceiver.array()));
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 275 |
|
| 276 |
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 277 |
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 278 |
|
| 279 | Thread t = new Thread( new Runnable(){
|
| 280 | public void run(){
|
| 281 | //sChannel.
|
| 282 | while(true){
|
| 283 |
|
| 284 | try{
|
| 285 |
|
| 286 | ByteBuffer bufferReceiver = ByteBuffer.allocate(PACKETSIZE);
|
| 287 |
|
| 288 | bufferReceiver.clear();
|
| 289 | sChannel.read(bufferReceiver);
|
| 290 |
|
| 291 | String receivedHexBytes = bytes2Hex(bufferReceiver.array());
|
| 292 |
|
| 293 | System.out.println("RECEIVED BYTES:" + receivedHexBytes);
|
| 294 |
|
| 295 |
|
| 296 | System.out.println("name is "+name);
|
| 297 |
|
| 298 | //connections.put(name, sChannel);
|
| 299 |
|
| 300 | //System.out.println("name is "+name);
|
| 301 |
|
| 302 | //receivedInterest(receivedHexBytes,name);
|
| 303 |
|
| 304 |
|
| 305 | //byte[] output = hex2Bytes("048202aa03b208855dea25cca4d3967c774cc6e709a140d91f9d74e97a665cbb106568ee94f998d8b22fbe2b8d2fc43bd9363b501a50f2d770a7882aaf0792c20359f54da27d4a5b5a29be7d349c656e60fd37c92cf4e4aae256af04161e561aa4323512b38799c43d7a2ec4e35c3e19599b12c5ee3a5f0994837461c05c294a8b596f960e9287520000f2faad726f63636f000001a203e2028548398bad0eeb58e9cc64720e84f4947fe1c1eb310055181ed0b4c2d920ef6e590002bab504fcc5336e9c0001e201da0a9530819f300d06092a864886f70d010101050003818d003081890281810092e67426906bae66a3f4910bade5a2d24e2b7ed0d7a3e16f368c258e848f30115f6e95f5e0ee64505ed3da08be6ee599d395f611ffb096a9c258171518f0c6b3612a94681d29e44619227ac268e5d450abac89820d96188f507d15fb40d8780ccb76a012f7ce4f4efe2b8ba431ef993e31da867adffb069003808bceef9ff4d10203010001000000019abd424f4e4a4f55520000");
|
| 306 |
|
| 307 | byte[] output = hex2Bytes(toReturn);
|
| 308 |
|
| 309 |
|
| 310 | ByteBuffer outputBuffer = ByteBuffer.wrap(output);
|
| 311 |
|
| 312 | //byte[] output = hex2Bytes("048202aa03b208857d7f003e50fc79aca1563842832db26c5e313bec0940ce795dd8adc34e7fd2cadee7b44b28737d59c061240da60d0733e2bcc760c7656a0f03b20a987c1a1fb94bb93648243c48fde222bc21b85062f186ffdc15f637cfe83f35ab11e3564e7e83a26de39a0faf3991f469f0f376fca535fb1be28ede72b433547b4977f0f3000000f2faa574657374000001a203e20285189f9df9814d134883758f9c5541ba957a4464d8756f34870cf981143f56779a0002bab504fcbf24f6f60001e201da0a9530819f300d06092a864886f70d010101050003818d0030818902818100c0a2c68770c339ed3152b90cde701ba588652f358854460b36b866c6e76272013232df351f10841ac49e35a6bc644f9c9caacd9aa0cd0e1835a34162c9208049d3f1f893d0b9566854133a763756df45297328d595ba6b6459fd311d5e1c97ce5278fa076dde765090c7221670ad54689958cc5fb46699482c5ac16c301dba1f0203010001000000019abd424f4e4a4f55520000");
|
| 313 |
|
| 314 | while(outputBuffer.hasRemaining())
|
| 315 | sChannel.write(outputBuffer);
|
| 316 |
|
| 317 | System.out.println("SENT BACK SOME DATA");
|
| 318 |
|
| 319 |
|
| 320 | }
|
| 321 | catch(Exception e){
|
| 322 | error(e);
|
| 323 | try {
|
| 324 | sChannel.close();
|
| 325 | } catch (IOException e1) {
|
| 326 | // TODO Auto-generated catch block
|
| 327 | error(e1);
|
| 328 | }
|
| 329 | System.exit(0);
|
| 330 |
|
| 331 | }
|
| 332 | }
|
| 333 |
|
| 334 | }});
|
| 335 |
|
| 336 | t.start();
|
| 337 |
|
| 338 | return "STARTED PUBLISHING";
|
| 339 |
|
| 340 | //return receivedHexBytes;
|
| 341 |
|
| 342 | } catch (Exception e) {
|
| 343 | // TODO Auto-generated catch block
|
| 344 | error(e);
|
| 345 |
|
| 346 | try {
|
| 347 | System.out.println("CLOSING THE CONNECTION");
|
| 348 | sChannelCloser.close();
|
| 349 | } catch (IOException ex) {
|
| 350 | // TODO Auto-generated catch block
|
| 351 | error(ex);
|
| 352 |
|
| 353 | }
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 354 | }
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 355 | return "FAILURE";
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 356 |
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 357 |
|
| 358 |
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 359 | }
|
| 360 | }
|
| 361 | );
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 362 | }
|
| 363 |
|
| 364 |
|
| 365 |
|
Axel Colin de Verdiere | d363e63 | 2012-06-06 05:16:43 -0700 | [diff] [blame] | 366 | /**
|
| 367 | * Not used
|
| 368 | */
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 369 | public String putAnswer(final String s, final String dataBack){
|
| 370 |
|
| 371 | final SocketChannel sChannel = connections.get(s);
|
| 372 |
|
| 373 |
|
| 374 | byte[] output = hex2Bytes(dataBack);
|
| 375 |
|
| 376 | ByteBuffer outputBuffer = ByteBuffer.wrap(output);
|
| 377 |
|
| 378 | //byte[] output = hex2Bytes("048202aa03b208857d7f003e50fc79aca1563842832db26c5e313bec0940ce795dd8adc34e7fd2cadee7b44b28737d59c061240da60d0733e2bcc760c7656a0f03b20a987c1a1fb94bb93648243c48fde222bc21b85062f186ffdc15f637cfe83f35ab11e3564e7e83a26de39a0faf3991f469f0f376fca535fb1be28ede72b433547b4977f0f3000000f2faa574657374000001a203e20285189f9df9814d134883758f9c5541ba957a4464d8756f34870cf981143f56779a0002bab504fcbf24f6f60001e201da0a9530819f300d06092a864886f70d010101050003818d0030818902818100c0a2c68770c339ed3152b90cde701ba588652f358854460b36b866c6e76272013232df351f10841ac49e35a6bc644f9c9caacd9aa0cd0e1835a34162c9208049d3f1f893d0b9566854133a763756df45297328d595ba6b6459fd311d5e1c97ce5278fa076dde765090c7221670ad54689958cc5fb46699482c5ac16c301dba1f0203010001000000019abd424f4e4a4f55520000");
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 | try{
|
| 383 | while(outputBuffer.hasRemaining())
|
| 384 | sChannel.write(outputBuffer);
|
| 385 |
|
| 386 |
|
| 387 | ByteBuffer bufferReceiver = ByteBuffer.allocate(PACKETSIZE);
|
| 388 |
|
| 389 | bufferReceiver.clear();
|
| 390 | sChannel.read(bufferReceiver);
|
| 391 |
|
| 392 | String receivedHexBytes = bytes2Hex(bufferReceiver.array());
|
| 393 | System.out.println("RECEIVED BYTES:" + receivedHexBytes);
|
| 394 |
|
| 395 |
|
| 396 | receivedInterest(receivedHexBytes,s);
|
| 397 |
|
| 398 | }
|
| 399 | catch (Exception e) {
|
| 400 | // TODO Auto-generated catch block
|
| 401 | error(e);
|
| 402 |
|
| 403 | try {
|
| 404 | System.out.println("CLOSING THE CONNECTION");
|
| 405 | sChannel.close();
|
| 406 | } catch (IOException ex) {
|
| 407 | // TODO Auto-generated catch block
|
| 408 | error(ex);
|
| 409 | return "FAILURE";
|
| 410 | }
|
| 411 | }
|
| 412 |
|
| 413 | return "SUCCESS";
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 414 |
|
| 415 | }
|
| 416 |
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 417 |
|
| 418 | public SocketChannel open(String host, int ip) throws IOException{
|
| 419 | // Create a non-blocking socket channel
|
| 420 | SocketChannel sChannel = SocketChannel.open();
|
| 421 |
|
| 422 | //sChannel.configureBlocking(false);
|
| 423 |
|
| 424 | // Send a connection request to the server; this method is non-blocking
|
| 425 | sChannel.connect(new InetSocketAddress(host, ip));
|
| 426 |
|
| 427 | // Before the socket is usable, the connection must be completed
|
| 428 | // by calling finishConnect(), which is non-blocking
|
| 429 |
|
| 430 | _ncReadSelector = Selector.open();
|
| 431 | //sChannel.configureBlocking(false);
|
| 432 | //sChannel.register(_ncReadSelector, SelectionKey.OP_READ);
|
| 433 | _ncWriteSelector = Selector.open();
|
| 434 | //sChannel.register(_ncWriteSelector, SelectionKey.OP_WRITE);
|
| 435 |
|
| 436 |
|
| 437 | while (!sChannel.finishConnect()) {
|
| 438 | // Do something else
|
| 439 | System.out.println("TRYING TO CONNECT");
|
| 440 | }
|
| 441 | System.out.println("CONNECTED TO " +host +":"+ip);
|
| 442 |
|
| 443 | return sChannel;
|
| 444 | }
|
| 445 |
|
| 446 |
|
| 447 | public static void error(Exception ex){
|
| 448 | StringWriter sw = new StringWriter();
|
| 449 | ex.printStackTrace(new PrintWriter(sw));
|
| 450 | String exceptionAsStrting = sw.toString();
|
| 451 |
|
| 452 | error(exceptionAsStrting );
|
| 453 | }
|
| 454 | // Report an error
|
| 455 | public static void error(String message){
|
| 456 |
|
| 457 | System.out.println("RECEIVED AN ERROR");
|
| 458 | message = "Java Socket Bridge ERROR: " + message;
|
| 459 | Object[] arguments = new Object[1];
|
| 460 | arguments[0] = message;
|
| 461 |
|
| 462 | if(isBrowser){
|
| 463 | try {
|
| 464 | browser.call("on_socket_error", arguments);
|
| 465 | } catch (JSException e) {
|
| 466 | // TODO Auto-generated catch block
|
| 467 | e.printStackTrace();
|
| 468 | }
|
| 469 | }
|
| 470 | else{
|
| 471 | System.out.println(message);
|
| 472 | }
|
| 473 | }
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 | // Log something
|
| 479 | public void log(String message){
|
| 480 | System.out.println(message);
|
| 481 | }
|
| 482 |
|
| 483 |
|
| 484 | public static byte[] hex2Bytes(String str)
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 485 | {
|
| 486 | byte[] bytes = new byte[str.length() / 2];
|
| 487 | for (int i = 0; i < bytes.length; i++)
|
| 488 | {
|
| 489 | bytes[i] = (byte) Integer
|
| 490 | .parseInt(str.substring(2 * i, 2 * i + 2), 16);
|
| 491 | }
|
| 492 |
|
| 493 | return bytes;
|
| 494 | }
|
jeff | fe2cdeb | 2012-09-09 17:46:10 -0700 | [diff] [blame] | 495 | public static String bytes2Hex(byte[] b, int length)
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 496 | {
|
| 497 | // String Buffer can be used instead
|
| 498 | String hs = "";
|
| 499 | String stmp = "";
|
| 500 |
|
jeff | fe2cdeb | 2012-09-09 17:46:10 -0700 | [diff] [blame] | 501 | for (int n = 0; n < length; n++)
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 502 | {
|
| 503 | stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
|
| 504 |
|
| 505 | if (stmp.length() == 1)
|
| 506 | {
|
| 507 | hs = hs + "0" + stmp;
|
| 508 | }
|
| 509 | else
|
| 510 | {
|
| 511 | hs = hs + stmp;
|
| 512 | }
|
| 513 |
|
jeff | fe2cdeb | 2012-09-09 17:46:10 -0700 | [diff] [blame] | 514 | if (n < length - 1)
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 515 | {
|
| 516 | hs = hs + "";
|
| 517 | }
|
| 518 | }
|
| 519 |
|
| 520 | return hs;
|
| 521 | }
|
jeff | fe2cdeb | 2012-09-09 17:46:10 -0700 | [diff] [blame] | 522 | public static String bytes2Hex(byte[] b)
|
| 523 | {
|
| 524 | return bytes2Hex(b, b.length);
|
| 525 | }
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 526 |
|
| 527 |
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 528 | public static void main(String[] args) throws IOException {
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 529 |
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 530 | JavaSocketBridge b = new JavaSocketBridge();
|
| 531 |
|
| 532 | //System.out.println( b.get("127.0.0.1",9695 ,"01D2F2FAA4657374000000", 1000));
|
| 533 |
|
| 534 | //System.out.println( b.get("127.0.0.1",9695 ,"01D2F2FAA574657374000000", 1000));
|
| 535 |
|
| 536 | //System.out.println( b.putSend("127.0.0.1",9695 ,"01d2f2faa563636e7800fa0285e0a01e093968f9740ce7f4361babf5bb05a4e55aaca5e58f73eddeb8e013aa8f00fabd73656c6672656700fa1bf5048202aa03b2088568cf069cdfdedad97ad9358f7e2446ca581eaa22ed9eb4e482fe616b35840533ba4f9d321155d6c34915dff7352de5e60bddb17e32f893cd89056cfd291011a5c3742312a083c2628fed4ddffb04cf51e6860eb1dbd43ff9b59736e62ec1a69218ce0acfdd9da896a617f609c12225f14a63876488b38d3a7b9fc1757d9058470000f20001a203e2028547ab87ece0e191c5e946f839507bc875c63c7032e42c347c135a952e7187c9300002bab504fcb2a6a0250001e201da0a9530819f300d06092a864886f70d010101050003818d003081890281810089b8f8b42d8aa31148d9f2a0c38d3fee7c73f60ea444d08fd886114a689cfe235c49bf9e256489390c19d961aabd5ee6d9e9e133282cd68b046609fe0a81be76c683cb150f3d035231b25745530fc887fbd137d6ef9c05d795fdb78f84eeab6f7dcbd1aa64b3920d96cfe941b66967bb2892baef1995bea231a4dc89c383e8550203010001000000019a0585058a04cabe73656c6672656700f2faad726f63636f000003e20285e0a01e093968f9740ce7f4361babf5bb05a4e55aaca5e58f73eddeb8e013aa8f0004fa8e330003d2d63231343734383336343700000000000002d28e310000",1000));
|
| 537 |
|
| 538 | //System.out.println( b.putSend("127.0.0.1",9695 ,"01D2F2FAA574657374000000",1000));
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 539 |
|
| 540 |
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 541 | //System.out.println( b.getSend("127.0.0.1",9695 ,"01D2F2FAA574657374000000", 1000));
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 542 |
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 543 | //System.out.println( b.get("localhost",9695 ,"01d2f2fafd",3000));
|
| 544 |
|
| 545 | //System.out.println( b.get("localhost",9695 ,"01D2F2FAA574657374000",3000));
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 546 |
|
| 547 |
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 548 | //System.out.println( b.getOLD("localhost",9695 ,"01d2f2fafdc12e4d2e532e6c6f63616c686f737400fabdc12e4d2e53525600faa563636e6400fa9d4b4559000002d28e310000",1000));
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 549 |
|
| 550 |
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 551 | //System.out.println( b.putSend("localhost",9695 ,"01d2f2faa563636e7800fa0285e0a01e093968f9740ce7f4361babf5bb05a4e55aaca5e58f73eddeb8e013aa8f00fabd73656c6672656700fa1bf5048202aa03b208854a18988c72aee624da28e2e1acbccb209b8e89429041985521ed68f95a1c546872fba3d854b1377dc249b6d8ec5935e5069256c97a7f6d8a62e86222ccd2cfe5097aed3fe5ede6732ce191a8680d78e39d0c5058a2b7bb0f0687994e9f045de346b66c46498547a08da1f2f0cdfafba3afdfe7107931935ede79040137ba94a90000f20001a203e202851a4860caa4991e829bcdc9429fb711d52440968d23560726606050bf147acffc0002bab504fcb3f03aa40001e201da0a9530819f300d06092a864886f70d010101050003818d00308189028181008ed27580e3d1c4c67672208665133a1ba12d8ebf5cad8e054571926b3ff0782a04c71703384021a6cefb6616b66cbd8a679b761d69d6373a851546e26f7105510b4c23be9a3c7f2e652e100ecc1471855730659f1477ce4e8504ad1fd8f44116baaeae2ff67eec33abba790157a79bf5039e5a528a471d9d67c94e70117ed7490203010001000000019a0585058a04cabe73656c6672656700f2faad726f63636f000003e20285e0a01e093968f9740ce7f4361babf5bb05a4e55aaca5e58f73eddeb8e013aa8f0004fa8e330003d2d63231343734383336343700000000000002d28e310000", 1000));
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 552 |
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 553 | //b.putSend("localhost",9695 ,"01d2f2faa563636e7800fa0285e0a01e093968f9740ce7f4361babf5bb05a4e55aaca5e58f73eddeb8e013aa8f00fabd73656c6672656700fa1bf5048202aa03b208854a18988c72aee624da28e2e1acbccb209b8e89429041985521ed68f95a1c546872fba3d854b1377dc249b6d8ec5935e5069256c97a7f6d8a62e86222ccd2cfe5097aed3fe5ede6732ce191a8680d78e39d0c5058a2b7bb0f0687994e9f045de346b66c46498547a08da1f2f0cdfafba3afdfe7107931935ede79040137ba94a90000f20001a203e202851a4860caa4991e829bcdc9429fb711d52440968d23560726606050bf147acffc0002bab504fcb3f03aa40001e201da0a9530819f300d06092a864886f70d010101050003818d00308189028181008ed27580e3d1c4c67672208665133a1ba12d8ebf5cad8e054571926b3ff0782a04c71703384021a6cefb6616b66cbd8a679b761d69d6373a851546e26f7105510b4c23be9a3c7f2e652e100ecc1471855730659f1477ce4e8504ad1fd8f44116baaeae2ff67eec33abba790157a79bf5039e5a528a471d9d67c94e70117ed7490203010001000000019a0585058a04cabe73656c6672656700f2faad726f63636f000003e20285e0a01e093968f9740ce7f4361babf5bb05a4e55aaca5e58f73eddeb8e013aa8f0004fa8e330003d2d63231343734383336343700000000000002d28e310000", 1000);
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 554 |
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 555 | //NewTest(54567);
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 556 |
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 557 | //System.out.println( b.put());
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 558 |
|
Meki Cherkaoui | 0984797 | 2012-06-06 03:26:30 -0700 | [diff] [blame] | 559 | //connections.put("meki", SocketChannel.open());
|
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 560 |
|
| 561 | }
|
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 562 |
|
| 563 | } |