Api refactoring - refactored js part and Android platform
This commit is contained in:
parent
a781e7cd6b
commit
6824f18c8e
72
socket.js
72
socket.js
@ -10,9 +10,54 @@ function Socket() {
|
|||||||
this.socketKey = guid();
|
this.socketKey = guid();
|
||||||
}
|
}
|
||||||
|
|
||||||
Socket.create = function(callback) {
|
//Socket.create = function(/*callback*/) {
|
||||||
|
//
|
||||||
|
// var socket = new Socket();
|
||||||
|
//
|
||||||
|
// function socketEventHandler(event) {
|
||||||
|
//
|
||||||
|
// var payload = event.payload;
|
||||||
|
//
|
||||||
|
// if (payload.socketKey !== socket.socketKey) {
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// switch(payload.type) {
|
||||||
|
// case "Close":
|
||||||
|
// console.debug("SocketsForCordova: Close event, socket key: " + payload.socketKey);
|
||||||
|
// window.document.removeEventListener(SOCKET_EVENT, socketEventHandler);
|
||||||
|
// socket.onClose();
|
||||||
|
// break;
|
||||||
|
// case "DataReceived":
|
||||||
|
// console.debug("SocketsForCordova: DataReceived event, socket key: " + payload.socketKey);
|
||||||
|
// socket.onData(new Int8Array(payload.data));
|
||||||
|
// break;
|
||||||
|
// case "Error":
|
||||||
|
// console.debug("SocketsForCordova: Error event, socket key: " + payload.socketKey);
|
||||||
|
// socket.onError(payload.errorMessage);
|
||||||
|
// break;
|
||||||
|
// default:
|
||||||
|
// console.error("SocketsForCordova: Unknown event type " + payload.type + ", socket key: " + payload.socketKey);
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// window.document.addEventListener(SOCKET_EVENT, socketEventHandler);
|
||||||
|
//
|
||||||
|
// exec(
|
||||||
|
// function() {
|
||||||
|
// console.debug("SocketsForCordova: Socket object successfully constructed.");
|
||||||
|
// callback(socket);
|
||||||
|
// },
|
||||||
|
// function(error) {
|
||||||
|
// console.error("SocketsForCordova: Unexpected error during constructing Socket object. Error: " + error);
|
||||||
|
// },
|
||||||
|
// CORDOVA_SERVICE_NAME,
|
||||||
|
// "create",
|
||||||
|
// [ socket.socketKey ]);
|
||||||
|
//};
|
||||||
|
|
||||||
var socket = new Socket();
|
Socket.prototype.open = function (host, port, success, error) {
|
||||||
|
|
||||||
function socketEventHandler(event) {
|
function socketEventHandler(event) {
|
||||||
|
|
||||||
@ -42,35 +87,20 @@ Socket.create = function(callback) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
window.document.addEventListener(SOCKET_EVENT, socketEventHandler);
|
|
||||||
|
|
||||||
exec(
|
exec(
|
||||||
function() {
|
function() {
|
||||||
console.debug("SocketsForCordova: Socket object successfully constructed.");
|
console.debug("SocketsForCordova: Socket successfully opened.");
|
||||||
callback(socket);
|
window.document.addEventListener(SOCKET_EVENT, socketEventHandler);
|
||||||
},
|
|
||||||
function(error) {
|
|
||||||
console.error("SocketsForCordova: Unexpected error during constructing Socket object. Error: " + error);
|
|
||||||
},
|
|
||||||
CORDOVA_SERVICE_NAME,
|
|
||||||
"create",
|
|
||||||
[ socket.socketKey ]);
|
|
||||||
};
|
|
||||||
|
|
||||||
Socket.prototype.connect = function (host, port, success, error) {
|
|
||||||
exec(
|
|
||||||
function() {
|
|
||||||
console.debug("SocketsForCordova: Socket successfully connected.");
|
|
||||||
if (success)
|
if (success)
|
||||||
success();
|
success();
|
||||||
},
|
},
|
||||||
function(errorMessage) {
|
function(errorMessage) {
|
||||||
console.error("SocketsForCordova: Error during socket connecting. Error: " + errorMessage);
|
console.error("SocketsForCordova: Error during opening socket. Error: " + errorMessage);
|
||||||
if (error)
|
if (error)
|
||||||
error(errorMessage);
|
error(errorMessage);
|
||||||
},
|
},
|
||||||
CORDOVA_SERVICE_NAME,
|
CORDOVA_SERVICE_NAME,
|
||||||
"connect",
|
"open",
|
||||||
[ this.socketKey, host, port ]);
|
[ this.socketKey, host, port ]);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -5,11 +5,12 @@ import java.net.SocketException;
|
|||||||
|
|
||||||
|
|
||||||
public interface SocketAdapter {
|
public interface SocketAdapter {
|
||||||
public void connect(String host, int port) throws Throwable;
|
public void open(String host, int port) throws Throwable;
|
||||||
public void write(byte[] data) throws IOException;
|
public void write(byte[] data) throws IOException;
|
||||||
|
public void shutdownWrite() throws IOException;
|
||||||
public void close() throws IOException;
|
public void close() throws IOException;
|
||||||
public void setOptions(SocketAdapterOptions options) throws SocketException;
|
public void setOptions(SocketAdapterOptions options) throws SocketException;
|
||||||
public void setDataConsumer(Consumer<byte[]> dataConsumer);
|
public void setDataConsumer(Consumer<byte[]> dataConsumer);
|
||||||
public void setCloseEventHandler(Consumer<Boolean> closeEventHandler);
|
public void setCloseEventHandler(Consumer<Boolean> closeEventHandler);
|
||||||
public void setErrorHandler(Consumer<Throwable> errorHandler);
|
public void setErrorHandler(Consumer<String> errorHandler);
|
||||||
}
|
}
|
@ -1,7 +1,6 @@
|
|||||||
package cz.blocshop.socketsforcordova;
|
package cz.blocshop.socketsforcordova;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InvalidObjectException;
|
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
@ -19,7 +18,7 @@ public class SocketAdapterImpl implements SocketAdapter {
|
|||||||
|
|
||||||
private Consumer<byte[]> dataConsumer;
|
private Consumer<byte[]> dataConsumer;
|
||||||
private Consumer<Boolean> closeEventHandler;
|
private Consumer<Boolean> closeEventHandler;
|
||||||
private Consumer<Throwable> exceptionHandler;
|
private Consumer<String> exceptionHandler;
|
||||||
|
|
||||||
private ExecutorService executor;
|
private ExecutorService executor;
|
||||||
|
|
||||||
@ -29,8 +28,8 @@ public class SocketAdapterImpl implements SocketAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void connect(String host, int port) throws Throwable {
|
public void open(String host, int port) throws Throwable {
|
||||||
this.connectSocket(host, port);
|
this.openWithBackgroundThread(host, port);
|
||||||
this.submitReadTask();
|
this.submitReadTask();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,11 +38,14 @@ public class SocketAdapterImpl implements SocketAdapter {
|
|||||||
this.socket.getOutputStream().write(data);
|
this.socket.getOutputStream().write(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void shutdownWrite() throws IOException {
|
||||||
|
this.socket.shutdownOutput();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
if (!this.socket.isClosed()) {
|
this.socket.close();
|
||||||
this.socket.shutdownOutput();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -82,24 +84,25 @@ public class SocketAdapterImpl implements SocketAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setErrorHandler(Consumer<Throwable> exceptionHandler) {
|
public void setErrorHandler(Consumer<String> exceptionHandler) {
|
||||||
this.exceptionHandler = exceptionHandler;
|
this.exceptionHandler = exceptionHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void connectSocket(final String host, final int port) throws Throwable {
|
private void openWithBackgroundThread(final String host, final int port) throws Throwable {
|
||||||
Future future = this.executor.submit(new Runnable() {
|
Future<?> future = this.executor.submit(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
socket.connect(new InetSocketAddress(host, port));
|
socket.connect(new InetSocketAddress(host, port));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Logging.Error(SocketAdapterImpl.class.getName(), "Error during connecting of socket", e);
|
Logging.Error(SocketAdapterImpl.class.getName(), "Error during connecting of socket", e.getCause());
|
||||||
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Object result = future.get();
|
future.get();
|
||||||
}
|
}
|
||||||
catch (ExecutionException e) {
|
catch (ExecutionException e) {
|
||||||
throw e.getCause();
|
throw e.getCause();
|
||||||
@ -122,7 +125,7 @@ public class SocketAdapterImpl implements SocketAdapter {
|
|||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
Logging.Error(SocketAdapterImpl.class.getName(), "Error during reading of socket input stream", e);
|
Logging.Error(SocketAdapterImpl.class.getName(), "Error during reading of socket input stream", e);
|
||||||
hasError = true;
|
hasError = true;
|
||||||
invokeExceptionHandler(e);
|
invokeExceptionHandler(e.getMessage());
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
socket.close();
|
socket.close();
|
||||||
@ -159,9 +162,9 @@ public class SocketAdapterImpl implements SocketAdapter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void invokeExceptionHandler(Throwable exception) {
|
private void invokeExceptionHandler(String errorMessage) {
|
||||||
if (this.exceptionHandler != null) {
|
if (this.exceptionHandler != null) {
|
||||||
this.exceptionHandler.accept(exception);
|
this.exceptionHandler.accept(errorMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,11 @@
|
|||||||
package cz.blocshop.socketsforcordova;
|
package cz.blocshop.socketsforcordova;
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.apache.cordova.CallbackContext;
|
import org.apache.cordova.CallbackContext;
|
||||||
import org.apache.cordova.CordovaArgs;
|
import org.apache.cordova.CordovaArgs;
|
||||||
import org.apache.cordova.CordovaPlugin;
|
import org.apache.cordova.CordovaPlugin;
|
||||||
@ -15,6 +13,7 @@ import org.json.JSONArray;
|
|||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint;
|
||||||
|
|
||||||
public class SocketPlugin extends CordovaPlugin {
|
public class SocketPlugin extends CordovaPlugin {
|
||||||
|
|
||||||
@ -23,12 +22,12 @@ public class SocketPlugin extends CordovaPlugin {
|
|||||||
@Override
|
@Override
|
||||||
public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException {
|
public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException {
|
||||||
|
|
||||||
if(action.equals("create")) {
|
if (action.equals("open")) {
|
||||||
this.create(args, callbackContext);
|
this.open(args, callbackContext);
|
||||||
} else if (action.equals("connect")) {
|
|
||||||
this.connect(args, callbackContext);
|
|
||||||
} else if (action.equals("write")) {
|
} else if (action.equals("write")) {
|
||||||
this.write(args, callbackContext);
|
this.write(args, callbackContext);
|
||||||
|
} else if (action.equals("shutdownWrite")) {
|
||||||
|
this.shutdownWrite(args, callbackContext);
|
||||||
} else if (action.equals("close")) {
|
} else if (action.equals("close")) {
|
||||||
this.close(args, callbackContext);
|
this.close(args, callbackContext);
|
||||||
} else if (action.equals("setOptions")) {
|
} else if (action.equals("setOptions")) {
|
||||||
@ -40,29 +39,19 @@ public class SocketPlugin extends CordovaPlugin {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void create(CordovaArgs args, CallbackContext callbackContext) throws JSONException {
|
private void open(CordovaArgs args, CallbackContext callbackContext) throws JSONException {
|
||||||
|
String socketKey = args.getString(0);
|
||||||
final String socketKey = args.getString(0);
|
String host = args.getString(1);
|
||||||
|
int port = args.getInt(2);
|
||||||
|
|
||||||
SocketAdapter socketAdapter = new SocketAdapterImpl();
|
SocketAdapter socketAdapter = new SocketAdapterImpl();
|
||||||
socketAdapter.setCloseEventHandler(new CloseEventHandler(socketKey));
|
socketAdapter.setCloseEventHandler(new CloseEventHandler(socketKey));
|
||||||
socketAdapter.setDataConsumer(new DataConsumer(socketKey));
|
socketAdapter.setDataConsumer(new DataConsumer(socketKey));
|
||||||
socketAdapter.setErrorHandler(new ErrorHandler(socketKey));
|
socketAdapter.setErrorHandler(new ErrorHandler(socketKey));
|
||||||
|
|
||||||
this.socketAdapters.put(socketKey, socketAdapter);
|
|
||||||
|
|
||||||
callbackContext.success(socketKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void connect(CordovaArgs args, CallbackContext callbackContext) throws JSONException {
|
|
||||||
String socketKey = args.getString(0);
|
|
||||||
String host = args.getString(1);
|
|
||||||
int port = args.getInt(2);
|
|
||||||
|
|
||||||
SocketAdapter socket = this.getSocketAdapter(socketKey);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
socket.connect(host, port);
|
socketAdapter.open(host, port);
|
||||||
|
this.socketAdapters.put(socketKey, socketAdapter);
|
||||||
callbackContext.success();
|
callbackContext.success();
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
callbackContext.error(t.toString());
|
callbackContext.error(t.toString());
|
||||||
@ -88,6 +77,19 @@ public class SocketPlugin extends CordovaPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void shutdownWrite(CordovaArgs args, CallbackContext callbackContext) throws JSONException {
|
||||||
|
String socketKey = args.getString(0);
|
||||||
|
|
||||||
|
SocketAdapter socket = this.getSocketAdapter(socketKey);
|
||||||
|
|
||||||
|
try {
|
||||||
|
socket.shutdownWrite();
|
||||||
|
callbackContext.success();
|
||||||
|
} catch (IOException e) {
|
||||||
|
callbackContext.error(e.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void close(CordovaArgs args, CallbackContext callbackContext) throws JSONException {
|
private void close(CordovaArgs args, CallbackContext callbackContext) throws JSONException {
|
||||||
String socketKey = args.getString(0);
|
String socketKey = args.getString(0);
|
||||||
|
|
||||||
@ -135,8 +137,7 @@ public class SocketPlugin extends CordovaPlugin {
|
|||||||
|
|
||||||
private SocketAdapter getSocketAdapter(String socketKey) {
|
private SocketAdapter getSocketAdapter(String socketKey) {
|
||||||
if (!this.socketAdapters.containsKey(socketKey)) {
|
if (!this.socketAdapters.containsKey(socketKey)) {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalStateException("Socket isn't connected.");
|
||||||
String.format("Cannot find socketKey: %s. Connection is probably closed.", socketKey));
|
|
||||||
}
|
}
|
||||||
return this.socketAdapters.get(socketKey);
|
return this.socketAdapters.get(socketKey);
|
||||||
}
|
}
|
||||||
@ -197,17 +198,17 @@ public class SocketPlugin extends CordovaPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class ErrorHandler implements Consumer<Throwable> {
|
private class ErrorHandler implements Consumer<String> {
|
||||||
private String socketKey;
|
private String socketKey;
|
||||||
public ErrorHandler(String socketKey) {
|
public ErrorHandler(String socketKey) {
|
||||||
this.socketKey = socketKey;
|
this.socketKey = socketKey;
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public void accept(Throwable exception) {
|
public void accept(String errorMessage) {
|
||||||
try {
|
try {
|
||||||
JSONObject event = new JSONObject();
|
JSONObject event = new JSONObject();
|
||||||
event.put("type", "Error");
|
event.put("type", "Error");
|
||||||
event.put("errorMessage", exception.toString());
|
event.put("errorMessage", errorMessage);
|
||||||
event.put("socketKey", socketKey);
|
event.put("socketKey", socketKey);
|
||||||
|
|
||||||
dispatchEvent(event);
|
dispatchEvent(event);
|
||||||
|
Loading…
Reference in New Issue
Block a user