Skip to content

Commit 66e19e9

Browse files
committed
refactor(channel): Simply how packets are handled
fix(packet): Fix misreporting of packet sizes in one of the write functions
1 parent 0869d28 commit 66e19e9

27 files changed

+1034
-1660
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ public class Main
2323
CoolSocket coolSocket = new CoolSocket(port)
2424
{
2525
@Override
26-
public void onConnected(ActiveConnection activeConnection)
26+
public void onConnected(ActiveConnection channel)
2727
{
2828
try {
29-
activeConnection.reply("Hello!");
30-
System.out.println(activeConnection.receive().getAsString()); // Merhaba!
29+
channel.reply("Hello!");
30+
System.out.println(channel.receive().getAsString()); // Merhaba!
3131
} catch (IOException e) {
3232
e.printStackTrace();
3333
}
@@ -36,9 +36,9 @@ public class Main
3636

3737
coolSocket.start();
3838

39-
try (ActiveConnection activeConnection = ActiveConnection.connect(new InetSocketAddress(port), 0)) {
40-
System.out.println(activeConnection.receive().getAsString()); // "Hello!"
41-
activeConnection.reply("Merhaba!");
39+
try (ActiveConnection channel = ActiveConnection.connect(new InetSocketAddress(port), 0)) {
40+
System.out.println(channel.receive().getAsString()); // "Hello!"
41+
channel.reply("Merhaba!");
4242
} finally {
4343
coolSocket.stop();
4444
}

pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,6 @@
157157
</profile>
158158
</profiles>
159159
<dependencies>
160-
<dependency>
161-
<groupId>org.json</groupId>
162-
<artifactId>json</artifactId>
163-
<version>20230618</version>
164-
</dependency>
165160
<dependency>
166161
<groupId>junit</groupId>
167162
<artifactId>junit</artifactId>

src/main/java/org/monora/coolsocket/core/client/ClientHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.monora.coolsocket.core.client;
22

33
import org.jetbrains.annotations.NotNull;
4-
import org.monora.coolsocket.core.session.ActiveConnection;
4+
import org.monora.coolsocket.core.session.Channel;
55

66
/**
77
* This class handles the last sequence of a client connection which is to answer to it.
@@ -13,9 +13,9 @@ public interface ClientHandler
1313
/**
1414
* When a client is connected, this method will be called.
1515
*
16-
* @param activeConnection The connection object that represents the client.
16+
* @param channel The connection object that represents the client.
1717
*/
18-
default void onConnected(@NotNull ActiveConnection activeConnection)
18+
default void onConnected(@NotNull Channel channel)
1919
{
2020

2121
}

src/main/java/org/monora/coolsocket/core/config/Config.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ public final class Config
1515
*/
1616
public static final int DEFAULT_BUFFER_SIZE = 8192;
1717

18-
/**
19-
* The default internal cache size spared for polling requests in memory.
20-
*/
21-
public static final int DEFAULT_INTERNAL_CACHE_SIZE = 0x100000;
22-
2318
/**
2419
* The default inverse exchange point number.
2520
* <p>

src/main/java/org/monora/coolsocket/core/config/ConfigFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.monora.coolsocket.core.config;
22

33
import org.jetbrains.annotations.NotNull;
4-
import org.monora.coolsocket.core.session.ActiveConnection;
4+
import org.monora.coolsocket.core.session.Channel;
55

66
import java.io.IOException;
77
import java.io.InputStream;
@@ -32,14 +32,14 @@ public interface ConfigFactory
3232
ServerSocket createServer() throws IOException;
3333

3434
/**
35-
* Configure the socket connection to a client before its actual usage and produce an {@link ActiveConnection}
35+
* Configure the socket connection to a client before its actual usage and produce an {@link Channel}
3636
* instance. The configuration may be different from that of {@link ServerSocket#accept()} assigns.
3737
*
3838
* @param client To configure.
39-
* @return The configured socket encapsulated in a {@link ActiveConnection}.
39+
* @return The configured socket encapsulated in a {@link Channel}.
4040
* @throws SocketException When an unrecoverable error occurs due to misconfiguration.
4141
*/
42-
ActiveConnection configureClient(@NotNull Socket client) throws SocketException;
42+
Channel configureClient(@NotNull Socket client) throws IOException;
4343

4444
/**
4545
* The address that the upcoming products will be assigned to. This does not necessarily reflect the address

src/main/java/org/monora/coolsocket/core/config/DefaultConfigFactory.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.monora.coolsocket.core.config;
22

33
import org.jetbrains.annotations.NotNull;
4-
import org.monora.coolsocket.core.session.ActiveConnection;
4+
import org.monora.coolsocket.core.session.Channel;
55

66
import java.io.IOException;
77
import java.net.ServerSocket;
@@ -30,9 +30,10 @@ public void configureServer(@NotNull ServerSocket serverSocket) throws IOExcepti
3030
}
3131

3232
@Override
33-
public @NotNull ActiveConnection configureClient(@NotNull Socket socket) throws SocketException
33+
public @NotNull Channel configureClient(@NotNull Socket socket) throws IOException
3434
{
35-
return new ActiveConnection(socket, readTimeout);
35+
socket.setSoTimeout(readTimeout);
36+
return Channel.wrap(socket);
3637
}
3738

3839
@Override

src/main/java/org/monora/coolsocket/core/response/Flags.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
public class Flags
99
{
1010
/**
11-
* Bit order: 0
11+
* The bit order: 0
1212
* <p>
1313
* Tells that the read is inconclusive and another read is needed. If the data is not chunked, then, the data will
1414
* be read as many as its length.
Lines changed: 19 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package org.monora.coolsocket.core.response;
22

33
import org.jetbrains.annotations.NotNull;
4-
import org.jetbrains.annotations.Nullable;
5-
import org.json.JSONObject;
6-
import org.monora.coolsocket.core.session.ActiveConnection;
4+
import org.monora.coolsocket.core.session.Channel;
75

86
import java.io.ByteArrayOutputStream;
97
import java.io.OutputStream;
@@ -13,75 +11,45 @@
1311
/**
1412
* This class represents the response received from the remote client that the CoolSocket is connected to.
1513
*
16-
* @see ActiveConnection#receive(OutputStream)
14+
* @see Channel#receive(OutputStream)
1715
*/
18-
public class Response
19-
{
16+
public class Response {
2017
/**
2118
* The remote that sent the response.
2219
*/
2320
public final @NotNull SocketAddress remote;
2421

2522
/**
26-
* The feature flags set for this part.
23+
* The flags set for the response.
2724
*/
2825
public final @NotNull Flags flags;
2926

3027
/**
31-
* The length of the data received for this part. This will also be the total length of a chunked data.
28+
* The total length of the data.
3229
*/
3330
public final long length;
3431

3532
/**
36-
* If the received data is small in size, and is written to a byte buffer (e.g., {@link ByteArrayOutputStream}),
37-
* it will be included in this field.
33+
* The data.
3834
*/
39-
public final @Nullable ByteArrayOutputStream data;
35+
public final @NotNull ByteArrayOutputStream data;
4036

4137
/**
4238
* Creates a new Response instance.
4339
*
4440
* @param remote Where the remote is located at.
4541
* @param flags The feature flags for this response.
4642
* @param length The total length of the data.
47-
* @param data The byte data stored in the heap.
43+
* @param data The data.
4844
*/
4945
public Response(@NotNull SocketAddress remote, @NotNull Flags flags, long length,
50-
@Nullable ByteArrayOutputStream data)
51-
{
46+
@NotNull ByteArrayOutputStream data) {
5247
this.remote = remote;
5348
this.flags = flags;
5449
this.length = length;
5550
this.data = data;
5651
}
5752

58-
/**
59-
* Check whether the data contains actual data.
60-
*
61-
* @return True if the data is actually an object.
62-
*/
63-
public boolean containsData()
64-
{
65-
return data != null;
66-
}
67-
68-
public @NotNull JSONObject getAsJson()
69-
{
70-
return new JSONObject(getAsString());
71-
}
72-
73-
/**
74-
* Get the data as a JSON object.
75-
*
76-
* @param charsetName The name of the charset to use to decode the data.
77-
* @return The encapsulated JSON data.
78-
* @throws UnsupportedEncodingException If the supplied charset name is not available.
79-
*/
80-
public @NotNull JSONObject getAsJson(@NotNull String charsetName) throws UnsupportedEncodingException
81-
{
82-
return new JSONObject(getAsString(charsetName));
83-
}
84-
8553
/**
8654
* Return the data as a string.
8755
*
@@ -90,11 +58,7 @@ public boolean containsData()
9058
* @throws UnsupportedEncodingException If the supplied charset name is not available.
9159
* @see #getAsString()
9260
*/
93-
public @NotNull String getAsString(@NotNull String charsetName) throws UnsupportedEncodingException
94-
{
95-
if (data == null) {
96-
throw new IllegalStateException("Trying to read data from a response whose data point is separate.");
97-
}
61+
public @NotNull String getAsString(@NotNull String charsetName) throws UnsupportedEncodingException {
9862
return data.toString(charsetName);
9963
}
10064

@@ -104,11 +68,15 @@ public boolean containsData()
10468
* @return The string representation of the {@link #data}.
10569
* @see #getAsString(String)
10670
*/
107-
public @NotNull String getAsString()
108-
{
109-
if (data == null) {
110-
throw new IllegalStateException("Trying to read data from a response whose data point is separate.");
111-
}
71+
public @NotNull String getAsString() {
11272
return data.toString();
11373
}
74+
75+
/**
76+
* Get the raw data.
77+
* @return The raw data.
78+
*/
79+
public @NotNull byte[] getBytes() {
80+
return data.toByteArray();
81+
}
11482
}

src/main/java/org/monora/coolsocket/core/server/ConnectionManager.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import org.jetbrains.annotations.NotNull;
44
import org.monora.coolsocket.core.CoolSocket;
5-
import org.monora.coolsocket.core.session.ActiveConnection;
5+
import org.monora.coolsocket.core.session.Channel;
66

77
import java.net.InetAddress;
88
import java.util.List;
@@ -18,12 +18,12 @@ public interface ConnectionManager
1818
int CLOSING_CONTRACT_CLOSE_IMMEDIATELY = 1;
1919

2020
/**
21-
* Close using {@link ActiveConnection#closeSafely()} which will inform the remote and close the connection.
21+
* Close using {@link Channel#closeMutually()} which will inform the remote and close the connection.
2222
*/
2323
int CLOSING_CONTRACT_CLOSE_SAFELY = 2;
2424

2525
/**
26-
* Cancel the ongoing operations informing the remote about it using {@link ActiveConnection#cancel()}. This doesn't
26+
* Cancel the ongoing operations informing the remote about it using {@link Channel#cancel()}. This doesn't
2727
* guarantee that the connections will be closed. If both sides of a connection want, the existing connection can
2828
* still be maintained after the cancel request is handled.
2929
*/
@@ -45,17 +45,17 @@ public interface ConnectionManager
4545
* Handle a client connection assigning it to a thread that will execute it asynchronously.
4646
*
4747
* @param coolSocket The calling CoolSocket instance.
48-
* @param activeConnection To handle.
48+
* @param channel To handle.
4949
*/
50-
void handleClient(@NotNull CoolSocket coolSocket, @NotNull ActiveConnection activeConnection);
50+
void handleClient(@NotNull CoolSocket coolSocket, @NotNull Channel channel);
5151

5252
/**
5353
* Returns the list of active connections that are still ongoing. This list does not hold the connections that are
5454
* closed.
5555
*
5656
* @return A copy list of active connections that are still alive.
5757
*/
58-
List<@NotNull ActiveConnection> getActiveConnectionList();
58+
List<@NotNull Channel> getActiveConnectionList();
5959

6060
/**
6161
* Counts the total connection of a client to the CoolSocket server.
@@ -67,8 +67,8 @@ default int getConnectionCountByAddress(@NotNull InetAddress address)
6767
{
6868
int returnObject = 0;
6969

70-
for (ActiveConnection activeConnection : getActiveConnectionList())
71-
if (activeConnection.getAddress().equals(address))
70+
for (Channel channel : getActiveConnectionList())
71+
if (channel.getSocket().getInetAddress().equals(address))
7272
returnObject++;
7373

7474
return returnObject;

src/main/java/org/monora/coolsocket/core/server/DefaultConnectionManager.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import org.jetbrains.annotations.NotNull;
44
import org.monora.coolsocket.core.CoolSocket;
5-
import org.monora.coolsocket.core.session.ActiveConnection;
5+
import org.monora.coolsocket.core.session.Channel;
66

77
import java.io.IOException;
88
import java.util.ArrayList;
@@ -14,7 +14,7 @@
1414

1515
public class DefaultConnectionManager implements ConnectionManager
1616
{
17-
private final @NotNull List<@NotNull ActiveConnection> connectionList = new ArrayList<>();
17+
private final @NotNull List<@NotNull Channel> connectionList = new ArrayList<>();
1818

1919
private final @NotNull ExecutorService executorService = Executors.newFixedThreadPool(10);
2020

@@ -33,14 +33,14 @@ public void closeAll()
3333

3434
if (closingContract != CLOSING_CONTRACT_DO_NOTHING) {
3535
synchronized (connectionList) {
36-
for (ActiveConnection connection : connectionList) {
36+
for (Channel connection : connectionList) {
3737
try {
3838
switch (contract) {
3939
case CLOSING_CONTRACT_CANCEL:
4040
connection.cancel();
4141
break;
4242
case CLOSING_CONTRACT_CLOSE_SAFELY:
43-
connection.closeSafely();
43+
connection.closeMutually();
4444
break;
4545
case CLOSING_CONTRACT_CLOSE_IMMEDIATELY:
4646
default:
@@ -64,34 +64,34 @@ public void closeAll()
6464
}
6565

6666
@Override
67-
public void handleClient(@NotNull CoolSocket coolSocket, final @NotNull ActiveConnection activeConnection)
67+
public void handleClient(@NotNull CoolSocket coolSocket, final @NotNull Channel channel)
6868
{
6969
synchronized (connectionList) {
70-
connectionList.add(activeConnection);
70+
connectionList.add(channel);
7171
}
7272

7373
executorService.submit(() -> {
7474
try {
75-
coolSocket.getClientHandler().onConnected(activeConnection);
75+
coolSocket.getClientHandler().onConnected(channel);
7676
} catch (Exception e) {
7777
coolSocket.getLogger().log(Level.SEVERE, "An error occurred during handling of a client", e);
7878
} finally {
79-
if (!activeConnection.isRoaming()) {
79+
if (!channel.isRoaming()) {
8080
try {
81-
activeConnection.close();
81+
channel.close();
8282
} catch (IOException ignored) {
8383
}
8484
}
8585

8686
synchronized (connectionList) {
87-
connectionList.remove(activeConnection);
87+
connectionList.remove(channel);
8888
}
8989
}
9090
});
9191
}
9292

9393
@Override
94-
public @NotNull List<@NotNull ActiveConnection> getActiveConnectionList()
94+
public @NotNull List<@NotNull Channel> getActiveConnectionList()
9595
{
9696
return new ArrayList<>(connectionList);
9797
}

0 commit comments

Comments
 (0)