Skip to content

Commit 47793e5

Browse files
committed
ActiveConnection: Make the socket instance upgradable
1 parent 55c420c commit 47793e5

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

src/main/java/org/monora/coolsocket/core/session/ActiveConnection.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*/
2525
public class ActiveConnection implements Closeable
2626
{
27-
private final Socket socket;
27+
private Socket socket;
2828

2929
private OutputStream privOutputStream;
3030

@@ -49,13 +49,7 @@ public class ActiveConnection implements Closeable
4949
*/
5050
public ActiveConnection(Socket socket)
5151
{
52-
if (socket == null)
53-
throw new NullPointerException("Socket cannot be null.");
54-
55-
if (!socket.isConnected())
56-
throw new IllegalStateException("Socket should have a valid connection.");
57-
58-
this.socket = socket;
52+
setSocket(socket);
5953
}
6054

6155
/**
@@ -734,6 +728,27 @@ public void setInternalCacheLimit(int internalCacheLimit)
734728
this.internalCacheLimit = internalCacheLimit;
735729
}
736730

731+
/**
732+
* Update the socket instance with the given socket.
733+
* <p>
734+
* This method is in place so that you can upgrade to a secure connection (usually wrapped around the same socket
735+
* instance).
736+
*
737+
* In any case, the remote should also be ready for the change.
738+
*
739+
* @param socket The socket instance.
740+
*/
741+
public void setSocket(Socket socket)
742+
{
743+
if (socket == null)
744+
throw new NullPointerException("Socket cannot be null.");
745+
746+
if (!socket.isConnected())
747+
throw new IllegalStateException("Socket should have a valid connection.");
748+
749+
this.socket = socket;
750+
}
751+
737752
/**
738753
* Verify that the given description is open and can read/write data.
739754
*
@@ -883,8 +898,8 @@ public synchronized Description writeBegin(long flags, long totalLength) throws
883898
* Finalize the write operation that was started with {@link #writeBegin(long, long)}.
884899
*
885900
* @param description The description object representing the operation.
886-
* @throws IOException If an IO error occurs, or {@link CancelledException} if the operation is
887-
* cancelled.
901+
* @throws IOException If an IO error occurs, or {@link CancelledException} if the operation is
902+
* cancelled.
888903
* @throws SizeUnderflowException If the operation is not chunked, and there are bytes left.
889904
*/
890905
public synchronized void writeEnd(Description description) throws IOException

src/test/java/org/monora/coolsocket/core/ConfigTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ public void randomPortTest() throws IOException, InterruptedException
1818
coolSocket.start();
1919
Assert.assertNotEquals("The random port should be the assigned port when started",
2020
coolSocket.getLocalPort(), 0);
21+
coolSocket.stop();
2122
}
2223
}

0 commit comments

Comments
 (0)