From ccce411d9816acb71c95e9ce724851a6f088266d Mon Sep 17 00:00:00 2001 From: Captain ALM Date: Sat, 20 May 2023 15:14:45 +0100 Subject: [PATCH] Finish NetMarshalServer. --- .../lib/calmnet/marshal/NetMarshalServer.java | 125 +++++++++++++++++- 1 file changed, 124 insertions(+), 1 deletion(-) diff --git a/src/com/captainalm/lib/calmnet/marshal/NetMarshalServer.java b/src/com/captainalm/lib/calmnet/marshal/NetMarshalServer.java index df5d29b..1a79bc4 100644 --- a/src/com/captainalm/lib/calmnet/marshal/NetMarshalServer.java +++ b/src/com/captainalm/lib/calmnet/marshal/NetMarshalServer.java @@ -38,7 +38,6 @@ public class NetMarshalServer implements Closeable { protected Consumer closedConsumer; protected BiConsumer acceptanceBiConsumer; protected BiConsumer socketSetupBiConsumer; - protected BiConsumer dSocketSetupBiConsumer; protected final Thread acceptThread; @@ -224,6 +223,128 @@ public class NetMarshalServer implements Closeable { disconnectAllInternal(); } + /** + * Gets the {@link BiConsumer} receiver consumer. + * + * @return The receiver consumer or null. + */ + public BiConsumer getReceiveBiConsumer() { + return receiveBiConsumer; + } + + /** + * Sets the {@link BiConsumer} receiver consumer. + * + * @param consumer The new receiver consumer. + * @throws NullPointerException consumer is null. + */ + public void setReceiveBiConsumer(BiConsumer consumer) { + if (consumer == null) throw new NullPointerException("consumer is null"); + receiveBiConsumer = consumer; + } + + /** + * Gets the {@link BiConsumer} receive exception consumer. + * + * @return The exception consumer or null. + */ + public BiConsumer getReceiveExceptionBiConsumer() { + return receiveExceptionBiConsumer; + } + + /** + * Sets the {@link BiConsumer} receive exception consumer. + * + * @param consumer The new exception consumer. + * @throws NullPointerException consumer is null. + */ + public void setReceiveExceptionBiConsumer(BiConsumer consumer) { + if (consumer == null) throw new NullPointerException("consumer is null"); + receiveExceptionBiConsumer = consumer; + } + + /** + * Gets the {@link BiConsumer} accept exception consumer. + * + * @return The exception consumer or null. + */ + public BiConsumer getAcceptExceptionBiConsumer() { + return acceptExceptionBiConsumer; + } + + /** + * Sets the {@link BiConsumer} accept exception consumer. + * + * @param consumer The new exception consumer. + * @throws NullPointerException consumer is null. + */ + public void setAcceptExceptionBiConsumer(BiConsumer consumer) { + if (consumer == null) throw new NullPointerException("consumer is null"); + acceptExceptionBiConsumer = consumer; + } + + /** + * Gets the {@link Consumer} closed consumer. + * + * @return The closed or null. + */ + public Consumer getClosedConsumer() { + return closedConsumer; + } + + /** + * Sets the {@link Consumer} closed consumer. + * + * @param consumer The new closed consumer. + * @throws NullPointerException consumer is null. + */ + public void setClosedConsumer(Consumer consumer) { + if (consumer == null) throw new NullPointerException("consumer is null"); + closedConsumer = consumer; + } + + /** + * Gets the {@link BiConsumer} client acceptance consumer. + * Use {@link CandidateClient#accept} to declare whether the candidate should be accepted. + * + * @return The acceptance consumer or null. + */ + public BiConsumer getClientAcceptanceBiConsumer() { + return acceptanceBiConsumer; + } + + /** + * Sets the {@link BiConsumer} client acceptance consumer. + * Use {@link CandidateClient#accept} to declare whether the candidate should be accepted. + * + * @param consumer The new acceptance consumer. + * @throws NullPointerException consumer is null. + */ + public void setClientAcceptanceBiConsumer(BiConsumer consumer) { + if (consumer == null) throw new NullPointerException("consumer is null"); + acceptanceBiConsumer = consumer; + } + + /** + * Gets the {@link BiConsumer} socket setup consumer. + * + * @return The setup consumer or null. + */ + public BiConsumer getSocketSetupBiConsumer() { + return socketSetupBiConsumer; + } + + /** + * Sets the {@link BiConsumer} socket setup consumer. + * + * @param consumer The new setup consumer. + * @throws NullPointerException consumer is null. + */ + public void setSocketSetupBiConsumer(BiConsumer consumer) { + if (consumer == null) throw new NullPointerException("consumer is null"); + socketSetupBiConsumer = consumer; + } + private void disconnectAllInternal() throws IOException { synchronized (slocksock) { for (NetMarshalClient c : clients) @@ -279,6 +400,7 @@ public class NetMarshalServer implements Closeable { } else { Socket clientSocket = new Socket(); clientSocket.connect(new InetSocketAddress(remoteAddress, remotePort), timeout); + if (socketSetupBiConsumer != null) socketSetupBiConsumer.accept(clientSocket, this); found = generateClientSocket(clientSocket); } try { @@ -352,6 +474,7 @@ public class NetMarshalServer implements Closeable { protected void acceptThreadExecutedSocket() { try { Socket clientSocket = socket.accept(); + if (socketSetupBiConsumer != null) socketSetupBiConsumer.accept(clientSocket, this); CandidateClient candidateClient = new CandidateClient(clientSocket.getInetAddress(), clientSocket.getPort()); try { if (acceptanceBiConsumer != null) acceptanceBiConsumer.accept(candidateClient, this);