Skip to content

Commit

Permalink
ActiveConnection: Make the socket instance upgradable
Browse files Browse the repository at this point in the history
  • Loading branch information
velitasali committed Jan 5, 2021
1 parent 55c420c commit 47793e5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
public class ActiveConnection implements Closeable
{
private final Socket socket;
private Socket socket;

private OutputStream privOutputStream;

Expand All @@ -49,13 +49,7 @@ public class ActiveConnection implements Closeable
*/
public ActiveConnection(Socket socket)
{
if (socket == null)
throw new NullPointerException("Socket cannot be null.");

if (!socket.isConnected())
throw new IllegalStateException("Socket should have a valid connection.");

this.socket = socket;
setSocket(socket);
}

/**
Expand Down Expand Up @@ -734,6 +728,27 @@ public void setInternalCacheLimit(int internalCacheLimit)
this.internalCacheLimit = internalCacheLimit;
}

/**
* Update the socket instance with the given socket.
* <p>
* This method is in place so that you can upgrade to a secure connection (usually wrapped around the same socket
* instance).
*
* In any case, the remote should also be ready for the change.
*
* @param socket The socket instance.
*/
public void setSocket(Socket socket)
{
if (socket == null)
throw new NullPointerException("Socket cannot be null.");

if (!socket.isConnected())
throw new IllegalStateException("Socket should have a valid connection.");

this.socket = socket;
}

/**
* Verify that the given description is open and can read/write data.
*
Expand Down Expand Up @@ -883,8 +898,8 @@ public synchronized Description writeBegin(long flags, long totalLength) throws
* Finalize the write operation that was started with {@link #writeBegin(long, long)}.
*
* @param description The description object representing the operation.
* @throws IOException If an IO error occurs, or {@link CancelledException} if the operation is
* cancelled.
* @throws IOException If an IO error occurs, or {@link CancelledException} if the operation is
* cancelled.
* @throws SizeUnderflowException If the operation is not chunked, and there are bytes left.
*/
public synchronized void writeEnd(Description description) throws IOException
Expand Down
1 change: 1 addition & 0 deletions src/test/java/org/monora/coolsocket/core/ConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ public void randomPortTest() throws IOException, InterruptedException
coolSocket.start();
Assert.assertNotEquals("The random port should be the assigned port when started",
coolSocket.getLocalPort(), 0);
coolSocket.stop();
}
}

0 comments on commit 47793e5

Please sign in to comment.