From 98ea52c60c1c18a308b326dab63fed7259ce6029 Mon Sep 17 00:00:00 2001 From: Veli Date: Sat, 15 Sep 2018 18:46:24 +0300 Subject: [PATCH] Update version to 1.0.2 --- README.md | 42 +----- pom.xml | 2 +- .../com/genonbeta/CoolSocket/CoolSocket.java | 42 ++++-- .../genonbeta/CoolSocket/CoolTransfer.java | 129 +++++++++--------- 4 files changed, 96 insertions(+), 119 deletions(-) diff --git a/README.md b/README.md index 66ea8b5..db696d7 100644 --- a/README.md +++ b/README.md @@ -83,56 +83,20 @@ public class CommunicationServer extends CoolSocket } ``` -

Implement CoolSocket

-

Maven

- -```xml -... - - - - - - false - - bintray-genonbeta-coolsocket - bintray - https://dl.bintray.com/genonbeta/coolsocket - - - - - - false - - bintray-genonbeta-coolsocket - bintray-plugins - https://dl.bintray.com/genonbeta/coolsocket - - - bintray - - - - bintray - -Implementing CoolSocket

Gradle

```xml ... repositories { ... - maven { url "https://dl.bintray.com/genonbeta/coolsocket" } + jcenter() ... } ... dependencies { ... - implementation 'com.genonbeta.coolsocket:main:1.0' + implementation 'com.genonbeta.coolsocket:main:1.0.2' ... } ... diff --git a/pom.xml b/pom.xml index 546f67c..ca5892f 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.genonbeta.coolsocket main - 1.0.1.5 + 1.0.2 CoolSocket Socket implementation https://github.com/genonbeta/CoolSocket diff --git a/src/main/java/com/genonbeta/CoolSocket/CoolSocket.java b/src/main/java/com/genonbeta/CoolSocket/CoolSocket.java index f3be537..ba0543d 100755 --- a/src/main/java/com/genonbeta/CoolSocket/CoolSocket.java +++ b/src/main/java/com/genonbeta/CoolSocket/CoolSocket.java @@ -253,7 +253,8 @@ public boolean startDelayed(int timeout) } // ensures the server is started otherwise returns false - public boolean startEnsured(int timeout) { + public boolean startEnsured(int timeout) + { long startTime = System.currentTimeMillis(); if (!this.startDelayed(timeout)) @@ -302,9 +303,19 @@ public static class ActiveConnection private int mTimeout = NO_TIMEOUT; private int mId = getClass().hashCode(); + public ActiveConnection() + { + this(new Socket()); + } + + public ActiveConnection(int timeout) + { + this(new Socket(), timeout); + } + public ActiveConnection(Socket socket) { - this.mSocket = socket; + mSocket = socket; } public ActiveConnection(Socket socket, int timeout) @@ -313,6 +324,17 @@ public ActiveConnection(Socket socket, int timeout) setTimeout(timeout); } + public ActiveConnection connect(SocketAddress socketAddress) throws IOException + { + if (getTimeout() != NO_TIMEOUT) + getSocket().setSoTimeout(getTimeout()); + + getSocket().bind(null); + getSocket().connect(socketAddress); + + return this; + } + @Override protected void finalize() throws Throwable { @@ -344,7 +366,7 @@ public Socket getSocket() return this.mSocket; } - public long getTimeout() + public int getTimeout() { return mTimeout; } @@ -488,15 +510,13 @@ public static class Client public ActiveConnection connect(SocketAddress socketAddress, int operationTimeout) throws IOException { - Socket socket = new Socket(); - - if (operationTimeout != NO_TIMEOUT) - socket.setSoTimeout(operationTimeout); - - socket.bind(null); - socket.connect(socketAddress); + return new ActiveConnection(operationTimeout) + .connect(socketAddress); + } - return new ActiveConnection(socket, operationTimeout); + public void connect(ActiveConnection connection, SocketAddress socketAddress) throws IOException + { + connection.connect(socketAddress); } public Object getReturn() diff --git a/src/main/java/com/genonbeta/CoolSocket/CoolTransfer.java b/src/main/java/com/genonbeta/CoolSocket/CoolTransfer.java index ce13abc..206f141 100755 --- a/src/main/java/com/genonbeta/CoolSocket/CoolTransfer.java +++ b/src/main/java/com/genonbeta/CoolSocket/CoolTransfer.java @@ -27,29 +27,21 @@ abstract public class CoolTransfer public abstract void onNotify(TransferHandler handler, int percentage); - public abstract void onTransferCompleted(TransferHandler handler); + public abstract Flag onPrepare(TransferHandler handler); - public abstract void onInterrupted(TransferHandler handler); + public abstract Flag onTaskPrepareSocket(TransferHandler handler); - public abstract Flag onSocketReady(TransferHandler handler); + public abstract void onTaskEnd(TransferHandler handler); - public abstract Flag onStart(TransferHandler handler); - - public Flag onCloseStreams(TransferHandler handler) - { - return Flag.CONTINUE; - } + public abstract void onDestroy(TransferHandler handler); public void onPrepareNext(TransferHandler handler) { } - public void onStop(TransferHandler handler) - { - } - - public void onOrientatingStreams(TransferHandler handler, InputStream inputStream, OutputStream outputStream) + public Flag onTaskOrientateStreams(TransferHandler handler, InputStream inputStream, OutputStream outputStream) { + return Flag.CONTINUE; } public void onProcessListChanged(ArrayList> processList, TransferHandler handler, boolean isAdded) @@ -146,11 +138,17 @@ public TransferHandler(int port, long fileSize, byte[] bufferSize, T extra) protected abstract void onRun(); - public void interrupt() + public void interrupt(boolean onlyThis) { + setFlag(onlyThis ? Flag.CANCEL_CURRENT : Flag.CANCEL_ALL); getTransferProgress().interrupt(); } + public void interrupt() + { + interrupt(false); + } + public boolean isInterrupted() { return getTransferProgress().isInterrupted(); @@ -255,7 +253,7 @@ public void run() public static abstract class Receive extends CoolTransfer { - public abstract Flag onSocketReady(TransferHandler handler, ServerSocket serverSocket); + public abstract Flag onTaskPrepareSocket(TransferHandler handler, ServerSocket serverSocket); public Handler receive(int port, File file, long fileSize, int bufferSize, int timeOut, T extra, boolean currentThread) throws FileNotFoundException { @@ -293,7 +291,7 @@ protected void onRun() { addProcess(this); - setFlag(onStart(this)); + setFlag(onPrepare(this)); try { if (Flag.CONTINUE.equals(getFlag())) { @@ -302,7 +300,7 @@ protected void onRun() if (getTimeout() != CoolSocket.NO_TIMEOUT) getServerSocket().setSoTimeout(getTimeout()); - setFlag(onSocketReady(this, getServerSocket())); + setFlag(onTaskPrepareSocket(this, getServerSocket())); if (Flag.CONTINUE.equals(getFlag())) { setSocket(getServerSocket().accept()); @@ -310,54 +308,49 @@ protected void onRun() if (getTimeout() != CoolSocket.NO_TIMEOUT) getSocket().setSoTimeout(getTimeout()); - setFlag(onSocketReady(this)); + setFlag(onTaskPrepareSocket(this)); if (Flag.CONTINUE.equals(getFlag())) { InputStream inputStream = getSocket().getInputStream(); int len = 0; long lastRead = System.currentTimeMillis(); - onOrientatingStreams(this, inputStream, getOutputStream()); + setFlag(onTaskOrientateStreams(this, inputStream, getOutputStream())); - while (len != -1) { - synchronized (getBlockingObject()) { - if ((len = inputStream.read(getBuffer())) > 0) { - getOutputStream().write(getBuffer(), 0, len); - getOutputStream().flush(); + if (Flag.CONTINUE.equals(getFlag())) { + while (len != -1) { + synchronized (getBlockingObject()) { + if ((len = inputStream.read(getBuffer())) > 0) { + getOutputStream().write(getBuffer(), 0, len); + getOutputStream().flush(); - lastRead = System.currentTimeMillis(); + lastRead = System.currentTimeMillis(); - getTransferProgress().incrementTransferredByte(len); + getTransferProgress().incrementTransferredByte(len); + } } - } - getTransferProgress().doNotify(Receive.this, this); + getTransferProgress().doNotify(Receive.this, this); - if ((mTimeout > 0 && (System.currentTimeMillis() - lastRead) > mTimeout) || isInterrupted()) { - System.out.println("CoolTransfer: Timed out... Exiting."); - break; + if ((mTimeout > 0 && (System.currentTimeMillis() - lastRead) > mTimeout) || isInterrupted()) { + System.out.println("CoolTransfer: Timed out... Exiting."); + break; + } + } + + if (Flag.CONTINUE.equals(getFlag())) { + getTransferProgress().incrementTransferredFileCount(); + onTaskEnd(this); } } getOutputStream().close(); inputStream.close(); - - setFlag(onCloseStreams(this)); } } - - if (!Flag.CANCEL_CURRENT.equals(getFlag())) - if (isInterrupted()) { - setFlag(Flag.CANCEL_ALL); - onInterrupted(this); - } else { - getTransferProgress().incrementTransferredFileCount(); - onTransferCompleted(this); - } } } catch (Exception e) { setFlag(onError(this, e)); - } finally { try { if (getSocket() != null && !getSocket().isClosed()) @@ -373,7 +366,7 @@ protected void onRun() e.printStackTrace(); } - onStop(this); + onDestroy(this); if (!Flag.CANCEL_ALL.equals(getFlag())) onPrepareNext(this); @@ -446,7 +439,7 @@ public Handler(String serverIp, int port, InputStream stream, long fileSize, byt protected void onRun() { addProcess(this); - setFlag(onStart(this)); + setFlag(onPrepare(this)); try { if (Flag.CONTINUE.equals(getFlag())) { @@ -455,41 +448,40 @@ protected void onRun() getSocket().bind(null); getSocket().connect(new InetSocketAddress(getServerIp(), getPort())); - setFlag(onSocketReady(this)); + setFlag(onTaskPrepareSocket(this)); if (Flag.CONTINUE.equals(getFlag())) { OutputStream outputStream = getSocket().getOutputStream(); int len = 0; - onOrientatingStreams(this, getInputStream(), outputStream); + setFlag(onTaskOrientateStreams(this, getInputStream(), outputStream)); - while (len != -1) { - synchronized (getBlockingObject()) { - if ((len = getInputStream().read(getBuffer())) > 0) { - outputStream.write(getBuffer(), 0, len); - outputStream.flush(); + if (Flag.CONTINUE.equals(getFlag())) { + while (len != -1) { + synchronized (getBlockingObject()) { + if ((len = getInputStream().read(getBuffer())) > 0) { + outputStream.write(getBuffer(), 0, len); + outputStream.flush(); - getTransferProgress().incrementTransferredByte(len); + getTransferProgress().incrementTransferredByte(len); + } } - } - getTransferProgress().doNotify(Send.this, this); + getTransferProgress().doNotify(Send.this, this); + + if (isInterrupted()) + break; + } - if (isInterrupted()) - break; + if (Flag.CONTINUE.equals(getFlag())) { + getTransferProgress().incrementTransferredFileCount(); + onTaskEnd(this); + } } outputStream.close(); getInputStream().close(); } - - if (isInterrupted()) { - setFlag(Flag.CANCEL_ALL); - onInterrupted(this); - } else { - getTransferProgress().incrementTransferredFileCount(); - onTransferCompleted(this); - } } } catch (Exception e) { setFlag(onError(this, e)); @@ -501,7 +493,7 @@ protected void onRun() e.printStackTrace(); } - onStop(this); + onDestroy(this); if (!Flag.CANCEL_ALL.equals(getFlag())) onPrepareNext(this); @@ -649,7 +641,8 @@ public boolean isInterrupted() return mInterrupted; } - public void resetCurrentTransferredByte() { + public void resetCurrentTransferredByte() + { mCurrentTransferredByte = 0; }