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