diff --git a/src/android/src/cz/blocshop/socketsforcordova/SocketAdapter.java b/src/android/src/cz/blocshop/socketsforcordova/SocketAdapter.java index 4e29b6b..536a6db 100644 --- a/src/android/src/cz/blocshop/socketsforcordova/SocketAdapter.java +++ b/src/android/src/cz/blocshop/socketsforcordova/SocketAdapter.java @@ -5,12 +5,14 @@ import java.net.SocketException; public interface SocketAdapter { - public void open(String host, int port) throws Throwable; + public void open(String host, int port); public void write(byte[] data) throws IOException; public void shutdownWrite() throws IOException; public void close() throws IOException; public void setOptions(SocketAdapterOptions options) throws SocketException; + public void setOpenEventHandler(Consumer openEventHandler); + public void setOpenErrorEventHandler(Consumer openErrorEventHandler); public void setDataConsumer(Consumer dataConsumer); public void setCloseEventHandler(Consumer closeEventHandler); - public void setErrorHandler(Consumer errorHandler); + public void setErrorEventHandler(Consumer errorEventHandler); } \ No newline at end of file diff --git a/src/android/src/cz/blocshop/socketsforcordova/SocketAdapterImpl.java b/src/android/src/cz/blocshop/socketsforcordova/SocketAdapterImpl.java index bc11d3f..f5d7389 100644 --- a/src/android/src/cz/blocshop/socketsforcordova/SocketAdapterImpl.java +++ b/src/android/src/cz/blocshop/socketsforcordova/SocketAdapterImpl.java @@ -16,9 +16,11 @@ public class SocketAdapterImpl implements SocketAdapter { private final int INPUT_STREAM_BUFFER_SIZE = 16 * 1024; private final Socket socket; + private Consumer openEventHandler; + private Consumer openErrorEventHandler; private Consumer dataConsumer; private Consumer closeEventHandler; - private Consumer exceptionHandler; + private Consumer errorEventHandler; private ExecutorService executor; @@ -28,9 +30,20 @@ public class SocketAdapterImpl implements SocketAdapter { } @Override - public void open(String host, int port) throws Throwable { - this.openWithBackgroundThread(host, port); - this.submitReadTask(); + public void open(final String host, final int port) { + this.executor.submit(new Runnable() { + @Override + public void run() { + try { + socket.connect(new InetSocketAddress(host, port)); + invokeOpenEventHandler(); + submitReadTask(); + } catch (IOException e) { + Logging.Error(SocketAdapterImpl.class.getName(), "Error during connecting of socket", e.getCause()); + invokeOpenErrorEventHandler(e.getMessage()); + } + } + }); } @Override @@ -74,6 +87,16 @@ public class SocketAdapterImpl implements SocketAdapter { } } + @Override + public void setOpenEventHandler(Consumer openEventHandler) { + this.openEventHandler = openEventHandler; + } + + @Override + public void setOpenErrorEventHandler(Consumer openErrorEventHandler) { + this.openErrorEventHandler = openErrorEventHandler; + } + @Override public void setDataConsumer(Consumer dataConsumer) { this.dataConsumer = dataConsumer; @@ -85,31 +108,10 @@ public class SocketAdapterImpl implements SocketAdapter { } @Override - public void setErrorHandler(Consumer exceptionHandler) { - this.exceptionHandler = exceptionHandler; + public void setErrorEventHandler(Consumer errorEventHandler) { + this.errorEventHandler = errorEventHandler; } - - private void openWithBackgroundThread(final String host, final int port) throws Throwable { - Future future = this.executor.submit(new Runnable() { - @Override - public void run() { - try { - socket.connect(new InetSocketAddress(host, port)); - } catch (IOException e) { - Logging.Error(SocketAdapterImpl.class.getName(), "Error during connecting of socket", e.getCause()); - throw new RuntimeException(e); - } - } - }); - - try { - future.get(); - } - catch (ExecutionException e) { - throw e.getCause(); - } - } - + private void submitReadTask() { this.executor.submit(new Runnable() { @Override @@ -150,6 +152,18 @@ public class SocketAdapterImpl implements SocketAdapter { this.invokeDataConsumer(data); } } + + private void invokeOpenEventHandler() { + if (this.openEventHandler != null) { + this.openEventHandler.accept((Void)null); + } + } + + private void invokeOpenErrorEventHandler(String errorMessage) { + if (this.openErrorEventHandler != null) { + this.openErrorEventHandler.accept(errorMessage); + } + } private void invokeDataConsumer(byte[] data) { if (this.dataConsumer != null) { @@ -164,8 +178,8 @@ public class SocketAdapterImpl implements SocketAdapter { } private void invokeExceptionHandler(String errorMessage) { - if (this.exceptionHandler != null) { - this.exceptionHandler.accept(errorMessage); + if (this.errorEventHandler != null) { + this.errorEventHandler.accept(errorMessage); } } } diff --git a/src/android/src/cz/blocshop/socketsforcordova/SocketPlugin.java b/src/android/src/cz/blocshop/socketsforcordova/SocketPlugin.java index 369db58..f9b5cab 100644 --- a/src/android/src/cz/blocshop/socketsforcordova/SocketPlugin.java +++ b/src/android/src/cz/blocshop/socketsforcordova/SocketPlugin.java @@ -47,15 +47,11 @@ public class SocketPlugin extends CordovaPlugin { SocketAdapter socketAdapter = new SocketAdapterImpl(); socketAdapter.setCloseEventHandler(new CloseEventHandler(socketKey)); socketAdapter.setDataConsumer(new DataConsumer(socketKey)); - socketAdapter.setErrorHandler(new ErrorHandler(socketKey)); + socketAdapter.setErrorEventHandler(new ErrorEventHandler(socketKey)); + socketAdapter.setOpenErrorEventHandler(new OpenErrorEventHandler(callbackContext)); + socketAdapter.setOpenEventHandler(new OpenEventHandler(socketKey, socketAdapter, callbackContext)); - try { - socketAdapter.open(host, port); - this.socketAdapters.put(socketKey, socketAdapter); - callbackContext.success(); - } catch (Throwable t) { - callbackContext.error(t.toString()); - } + socketAdapter.open(host, port); } private void write(CordovaArgs args, CallbackContext callbackContext) throws JSONException { @@ -198,9 +194,9 @@ public class SocketPlugin extends CordovaPlugin { } } - private class ErrorHandler implements Consumer { + private class ErrorEventHandler implements Consumer { private String socketKey; - public ErrorHandler(String socketKey) { + public ErrorEventHandler(String socketKey) { this.socketKey = socketKey; } @Override @@ -217,4 +213,31 @@ public class SocketPlugin extends CordovaPlugin { } } } + + private class OpenErrorEventHandler implements Consumer { + private CallbackContext openCallbackContext; + public OpenErrorEventHandler(CallbackContext openCallbackContext) { + this.openCallbackContext = openCallbackContext; + } + @Override + public void accept(String errorMessage) { + this.openCallbackContext.error(errorMessage); + } + } + + private class OpenEventHandler implements Consumer { + private String socketKey; + private SocketAdapter socketAdapter; + private CallbackContext openCallbackContext; + public OpenEventHandler(String socketKey, SocketAdapter socketAdapter, CallbackContext openCallbackContext) { + this.socketKey = socketKey; + this.socketAdapter = socketAdapter; + this.openCallbackContext = openCallbackContext; + } + @Override + public void accept(Void voidObject) { + socketAdapters.put(socketKey, socketAdapter); + this.openCallbackContext.success(); + } + } }