From fb3086c28e59e61ea059e9ff0296dd696becfc0a Mon Sep 17 00:00:00 2001 From: Finn Date: Thu, 11 Dec 2025 11:50:47 +0100 Subject: [PATCH] Constructor fixes --- pom.xml | 2 +- .../protocol/ProtocolBridge.java | 6 ++++ .../protocol/side/client/ProtocolClient.java | 30 +++++++++++----- .../protocol/side/ins/ProtocolINSServer.java | 36 ++++++++++++++----- .../protocol/side/web/ProtocolWebServer.java | 25 ++++++++++--- 5 files changed, 76 insertions(+), 23 deletions(-) diff --git a/pom.xml b/pom.xml index 540128e..eb1475c 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.openautonomousconnection protocol - 1.0.0-BETA.13 + 1.0.0-BETA.14 Open Autonomous Connection https://open-autonomous-connection.org/ diff --git a/src/main/java/org/openautonomousconnection/protocol/ProtocolBridge.java b/src/main/java/org/openautonomousconnection/protocol/ProtocolBridge.java index c2c980b..1186e84 100644 --- a/src/main/java/org/openautonomousconnection/protocol/ProtocolBridge.java +++ b/src/main/java/org/openautonomousconnection/protocol/ProtocolBridge.java @@ -108,6 +108,8 @@ public final class ProtocolBridge { */ @ProtocolInfo(protocolSide = ProtocolVersion.ProtocolSide.INS) public ProtocolBridge(ProtocolINSServer protocolINSServer, ProtocolSettings protocolSettings, ProtocolVersion protocolVersion, File logFolder) throws Exception { + protocolINSServer.attachBridge(this); + // Assign the parameters to the class fields this.protocolINSServer = protocolINSServer; this.protocolSettings = protocolSettings; @@ -133,6 +135,8 @@ public final class ProtocolBridge { */ @ProtocolInfo(protocolSide = ProtocolVersion.ProtocolSide.WEB) public ProtocolBridge(ProtocolWebServer protocolWebServer, ProtocolSettings protocolSettings, ProtocolVersion protocolVersion, File logFolder) throws Exception { + protocolWebServer.attachBridge(this); + // Assign the parameters to the class fields this.protocolWebServer = protocolWebServer; this.protocolSettings = protocolSettings; @@ -158,6 +162,8 @@ public final class ProtocolBridge { */ @ProtocolInfo(protocolSide = ProtocolVersion.ProtocolSide.CLIENT) public ProtocolBridge(ProtocolClient protocolClient, ProtocolSettings protocolSettings, ProtocolVersion protocolVersion, File logFolder) throws Exception { + protocolClient.attachBridge(this); + // Assign the parameters to the class fields this.protocolClient = protocolClient; this.protocolSettings = protocolSettings; diff --git a/src/main/java/org/openautonomousconnection/protocol/side/client/ProtocolClient.java b/src/main/java/org/openautonomousconnection/protocol/side/client/ProtocolClient.java index 17a059f..532f31b 100644 --- a/src/main/java/org/openautonomousconnection/protocol/side/client/ProtocolClient.java +++ b/src/main/java/org/openautonomousconnection/protocol/side/client/ProtocolClient.java @@ -28,7 +28,7 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider { /** * Handles everything with INS-Connection. */ - private final NetworkClient clientToINS; + private NetworkClient clientToINS; /** * Manages the folder structure for client certificates. @@ -39,7 +39,7 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider { * The reference to the ProtocolBridge Object */ @Getter - private final ProtocolBridge protocolBridge; + private ProtocolBridge protocolBridge; /** * Stores the protocol version of the connected server. */ @@ -47,17 +47,16 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider { /** * Initializes the ProtocolClient, setting up certificate folders and the INS client connection. - * - * @throws CertificateException if there are issues with the certificates. - * @throws IOException if there are I/O issues during initialization. */ - public ProtocolClient(ProtocolBridge protocolBridge) throws CertificateException, IOException { - this.protocolBridge = protocolBridge; - + public ProtocolClient() { // Initialize and verify certificate folders and files folderStructure = new ClientCertificateFolderStructure(); + } - // Initialize connection to INS server + /** + * Initialize connection to INS server + */ + private void createNetworkClient() { clientToINS = new NetworkClient.ClientBuilder().setLogger(protocolBridge.getLogger()).setProxy(protocolBridge.getProxy()). setHost(protocolBridge.getProtocolSettings().host).setPort(protocolBridge.getProtocolSettings().port). setPacketHandler(protocolBridge.getProtocolSettings().packetHandler).setEventManager(protocolBridge.getProtocolSettings().eventManager). @@ -65,6 +64,19 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider { build(); } + /** + * Injects the ProtocolBridge. + * @param bridge the Bridge instance. + * @throws IOException when NetworkServer failed to create. + */ + public final void attachBridge(ProtocolBridge bridge) throws IOException { + if (this.protocolBridge != null) + throw new IllegalStateException("ProtocolBridge already attached!"); + + this.protocolBridge = bridge; + createNetworkClient(); + } + /** * Gets the INS connection client. * diff --git a/src/main/java/org/openautonomousconnection/protocol/side/ins/ProtocolINSServer.java b/src/main/java/org/openautonomousconnection/protocol/side/ins/ProtocolINSServer.java index 297d33f..7e8518f 100644 --- a/src/main/java/org/openautonomousconnection/protocol/side/ins/ProtocolINSServer.java +++ b/src/main/java/org/openautonomousconnection/protocol/side/ins/ProtocolINSServer.java @@ -27,7 +27,7 @@ public abstract class ProtocolINSServer extends DefaultMethodsOverrider { * The network server instance. */ @Getter - private final NetworkServer networkServer; + private NetworkServer networkServer; /** * The configuration manager for handling server configurations. @@ -37,7 +37,7 @@ public abstract class ProtocolINSServer extends DefaultMethodsOverrider { * The reference to the ProtocolBridge Object */ @Getter - private final ProtocolBridge protocolBridge; + private ProtocolBridge protocolBridge; /** * List of connected protocol clients. */ @@ -56,8 +56,7 @@ public abstract class ProtocolINSServer extends DefaultMethodsOverrider { * @throws IOException If an I/O error occurs. * @throws CertificateException If a certificate error occurs. */ - public ProtocolINSServer(File configFile, ProtocolBridge protocolBridge) throws IOException, CertificateException { - this.protocolBridge = protocolBridge; + public ProtocolINSServer(File configFile) throws IOException, CertificateException { // Ensure the configuration file exists if (!configFile.exists()) configFile.createNewFile(); @@ -87,14 +86,35 @@ public abstract class ProtocolINSServer extends DefaultMethodsOverrider { checkFileExists(folderStructure.publicServerFolder, folderStructure.certPrefix, ".crt"); checkFileExists(folderStructure.privateServerFolder, folderStructure.certPrefix, ".key"); - // Define the certificate and key files based on the public IP address - File certFile = new File(folderStructure.publicServerFolder, folderStructure.certPrefix + NetworkUtils.getPublicIPAddress() + ".crt"); - File keyFile = new File(folderStructure.privateServerFolder, folderStructure.certPrefix + NetworkUtils.getPublicIPAddress() + ".key"); + // Initialize the protocol bridge and clients list this.clients = new ArrayList<>(); - // Build the network server with the specified settings + + } + + /** + * Injects the ProtocolBridge. + * @param bridge the Bridge instance. + * @throws IOException when NetworkServer failed to create. + */ + public final void attachBridge(ProtocolBridge bridge) throws IOException { + if (this.protocolBridge != null) + throw new IllegalStateException("ProtocolBridge already attached!"); + + this.protocolBridge = bridge; + createNetworkServer(); + } + + /** + * Build the network server with the specified settings + */ + private void createNetworkServer() throws IOException { + // Define the certificate and key files based on the public IP address + File certFile = new File(folderStructure.publicServerFolder, folderStructure.certPrefix + NetworkUtils.getPublicIPAddress() + ".crt"); + File keyFile = new File(folderStructure.privateServerFolder, folderStructure.certPrefix + NetworkUtils.getPublicIPAddress() + ".key"); + this.networkServer = new NetworkServer.ServerBuilder(). setLogger(protocolBridge.getLogger()). setEventManager(protocolBridge.getProtocolSettings().eventManager). diff --git a/src/main/java/org/openautonomousconnection/protocol/side/web/ProtocolWebServer.java b/src/main/java/org/openautonomousconnection/protocol/side/web/ProtocolWebServer.java index ff55a3f..b02df46 100644 --- a/src/main/java/org/openautonomousconnection/protocol/side/web/ProtocolWebServer.java +++ b/src/main/java/org/openautonomousconnection/protocol/side/web/ProtocolWebServer.java @@ -66,7 +66,7 @@ public final class ProtocolWebServer { * The reference to the ProtocolBridge Object */ @Getter - private final ProtocolBridge protocolBridge; + private ProtocolBridge protocolBridge; /** * The network server handling pipeline connections. */ @@ -96,9 +96,7 @@ public final class ProtocolWebServer { * @param rulesFile The rules file. * @throws Exception If an error occurs during initialization. */ - public ProtocolWebServer(File configFile, File authFile, File rulesFile, ProtocolBridge protocolBridge) throws Exception { - this.protocolBridge = protocolBridge; - + public ProtocolWebServer(File configFile, File authFile, File rulesFile) throws Exception { // Initialize the list of connected clients this.clients = new ArrayList<>(); @@ -162,8 +160,25 @@ public final class ProtocolWebServer { AuthManager.loadAuthFile(authFile); RuleManager.loadRules(rulesFile); + } - // Initialize the pipeline server + /** + * Injects the ProtocolBridge. + * @param bridge the Bridge instance. + * @throws IOException when NetworkServer failed to create. + */ + public final void attachBridge(ProtocolBridge bridge) throws IOException { + if (this.protocolBridge != null) + throw new IllegalStateException("ProtocolBridge already attached!"); + + this.protocolBridge = bridge; + createNetworkServer(); + } + + /** + * Initialize the pipeline server + */ + private void createNetworkServer() { pipelineServer = new NetworkServer.ServerBuilder(). setPort(configurationManager.getInt("port.pipeline")).setTimeout(0). setPacketHandler(protocolBridge.getProtocolSettings().packetHandler).setEventManager(protocolBridge.getProtocolSettings().eventManager).