Files
Protocol/src/main/java/org/openautonomousconnection/protocol/ProtocolBridge.java

385 lines
16 KiB
Java
Raw Normal View History

2025-09-24 22:02:10 +02:00
package org.openautonomousconnection.protocol;
2025-09-19 21:27:35 +02:00
2025-09-25 23:41:17 +02:00
import dev.unlegitdqrk.unlegitlibrary.utils.Logger;
import lombok.Getter;
import lombok.Setter;
2025-09-29 13:18:15 +02:00
import org.openautonomousconnection.protocol.annotations.ProtocolInfo;
2025-09-24 22:02:10 +02:00
import org.openautonomousconnection.protocol.listeners.ClientListener;
2025-12-08 09:59:58 +01:00
import org.openautonomousconnection.protocol.listeners.INSServerListener;
2025-09-29 13:18:15 +02:00
import org.openautonomousconnection.protocol.listeners.WebServerListener;
2025-09-24 22:02:10 +02:00
import org.openautonomousconnection.protocol.packets.OACPacket;
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.AuthPacket;
2025-12-11 01:22:58 +01:00
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.INSQueryPacket;
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.INSResponsePacket;
2025-09-24 22:02:10 +02:00
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.UnsupportedClassicPacket;
import org.openautonomousconnection.protocol.packets.v1_0_0.classic.Classic_DomainPacket;
2025-12-08 10:33:46 +01:00
import org.openautonomousconnection.protocol.packets.v1_0_0.classic.Classic_MessagePacket;
import org.openautonomousconnection.protocol.packets.v1_0_0.classic.Classic_PingPacket;
2025-09-24 22:02:10 +02:00
import org.openautonomousconnection.protocol.side.client.ProtocolClient;
2025-12-08 09:59:58 +01:00
import org.openautonomousconnection.protocol.side.ins.ProtocolINSServer;
2025-09-25 23:40:24 +02:00
import org.openautonomousconnection.protocol.side.web.ProtocolWebServer;
2025-09-24 22:02:10 +02:00
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
2025-09-29 17:46:30 +02:00
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers.ClassicHandlerClient;
2025-12-08 09:59:58 +01:00
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers.ClassicHandlerINSServer;
2025-09-29 17:46:30 +02:00
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers.ClassicHandlerWebServer;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.utils.Classic_ClientListener;
2025-09-19 21:27:35 +02:00
import java.io.File;
import java.io.IOException;
2025-10-04 00:04:33 +02:00
import java.net.Proxy;
2025-09-19 21:27:35 +02:00
2025-09-29 17:46:30 +02:00
/**
* The main bridge class for the protocol connection.
* It manages the protocol settings, version, and side instances.
*/
2025-09-29 17:56:51 +02:00
public final class ProtocolBridge {
2025-09-29 17:46:30 +02:00
/**
* The protocol settings for the current connection
*/
2025-09-19 21:27:35 +02:00
@Getter
private final ProtocolSettings protocolSettings;
2025-09-29 17:46:30 +02:00
/**
* The protocol version for the current connection
*/
2025-09-19 21:27:35 +02:00
@Getter
private final ProtocolVersion protocolVersion;
2025-09-29 17:46:30 +02:00
/**
* The logger instance for logging events and errors
*/
2025-09-20 20:42:58 +02:00
@Getter
2025-09-29 17:46:30 +02:00
private Logger logger;
/**
* The protocol side instances
*/
2025-09-19 21:27:35 +02:00
@Getter
2025-12-08 09:59:58 +01:00
private ProtocolINSServer protocolINSServer;
2025-09-29 17:46:30 +02:00
/**
* The protocol side instances
*/
2025-09-19 21:27:35 +02:00
@Getter
private ProtocolClient protocolClient;
2025-09-29 17:46:30 +02:00
/**
* The protocol side instances
*/
2025-09-19 21:27:35 +02:00
@Getter
2025-09-25 23:40:24 +02:00
private ProtocolWebServer protocolWebServer;
2025-09-29 17:46:30 +02:00
/**
2025-12-08 09:59:58 +01:00
* The classic protocol handlers for INS server side
2025-09-29 17:46:30 +02:00
*/
2025-09-25 23:40:24 +02:00
@Getter
2025-09-20 20:42:58 +02:00
@Setter
2025-12-08 09:59:58 +01:00
private ClassicHandlerINSServer classicHandlerINSServer;
2025-09-29 17:46:30 +02:00
/**
* The classic protocol handlers for web server side
*/
2025-09-29 13:18:15 +02:00
@Getter
@Setter
private ClassicHandlerWebServer classicHandlerWebServer;
2025-09-29 17:46:30 +02:00
/**
* The classic protocol handlers for client side
*/
2025-09-19 20:46:57 +02:00
@Getter
2025-09-20 20:42:58 +02:00
@Setter
private ClassicHandlerClient classicHandlerClient;
2025-09-19 20:46:57 +02:00
2025-10-04 00:04:33 +02:00
/**
* The proxy for client side
*/
@Getter
@Setter
private Proxy proxy;
2025-09-29 17:46:30 +02:00
/**
2025-12-08 09:59:58 +01:00
* Initialize the ProtocolBridge instance for the INS server side
2025-09-29 18:46:31 +02:00
*
2025-12-08 09:59:58 +01:00
* @param protocolINSServer The ProtocolINSServer instance
2025-09-29 18:46:31 +02:00
* @param protocolSettings The ProtocolSettings instance
* @param protocolVersion The ProtocolVersion instance
* @param logFolder The folder to store the log files
2025-09-29 17:46:30 +02:00
* @throws Exception if an error occurs while initializing the ProtocolBridge
*/
2025-12-08 09:59:58 +01:00
@ProtocolInfo(protocolSide = ProtocolVersion.ProtocolSide.INS)
public ProtocolBridge(ProtocolINSServer protocolINSServer, ProtocolSettings protocolSettings, ProtocolVersion protocolVersion, File logFolder) throws Exception {
2025-09-29 17:46:30 +02:00
// Assign the parameters to the class fields
2025-12-08 09:59:58 +01:00
this.protocolINSServer = protocolINSServer;
2025-09-19 21:27:35 +02:00
this.protocolSettings = protocolSettings;
this.protocolVersion = protocolVersion;
2025-09-29 17:46:30 +02:00
// Initialize the logger and protocol version
initializeLogger(logFolder);
initializeProtocolVersion();
2025-09-29 13:18:15 +02:00
2025-09-29 17:46:30 +02:00
// Register the appropriate listeners and packets
registerListeners();
2025-09-29 13:18:15 +02:00
registerPackets();
}
2025-09-29 17:46:30 +02:00
/**
* Initialize the ProtocolBridge instance for the web server side
2025-09-29 18:46:31 +02:00
*
2025-09-29 17:46:30 +02:00
* @param protocolWebServer The ProtocolWebServer instance
2025-09-29 18:46:31 +02:00
* @param protocolSettings The ProtocolSettings instance
* @param protocolVersion The ProtocolVersion instance
* @param logFolder The folder to store the log files
2025-09-29 17:46:30 +02:00
* @throws Exception if an error occurs while initializing the ProtocolBridge
*/
2025-09-29 13:18:15 +02:00
@ProtocolInfo(protocolSide = ProtocolVersion.ProtocolSide.WEB)
2025-09-29 17:46:30 +02:00
public ProtocolBridge(ProtocolWebServer protocolWebServer, ProtocolSettings protocolSettings, ProtocolVersion protocolVersion, File logFolder) throws Exception {
// Assign the parameters to the class fields
2025-09-29 13:18:15 +02:00
this.protocolWebServer = protocolWebServer;
this.protocolSettings = protocolSettings;
this.protocolVersion = protocolVersion;
2025-09-29 17:46:30 +02:00
// Initialize the logger and protocol version
initializeLogger(logFolder);
initializeProtocolVersion();
2025-09-19 21:27:35 +02:00
2025-09-29 17:46:30 +02:00
// Register the appropriate listeners and packets
registerListeners();
2025-09-20 20:41:08 +02:00
registerPackets();
2025-09-19 21:27:35 +02:00
}
2025-09-29 17:46:30 +02:00
/**
* Initialize the ProtocolBridge instance for the client side
2025-09-29 18:46:31 +02:00
*
* @param protocolClient The ProtocolClient instance
2025-09-29 17:46:30 +02:00
* @param protocolSettings The ProtocolSettings instance
2025-09-29 18:46:31 +02:00
* @param protocolVersion The ProtocolVersion instance
* @param logFolder The folder to store the log files
2025-09-29 17:46:30 +02:00
* @throws Exception if an error occurs while initializing the ProtocolBridge
*/
2025-09-29 13:18:15 +02:00
@ProtocolInfo(protocolSide = ProtocolVersion.ProtocolSide.CLIENT)
2025-09-29 17:46:30 +02:00
public ProtocolBridge(ProtocolClient protocolClient, ProtocolSettings protocolSettings, ProtocolVersion protocolVersion, File logFolder) throws Exception {
// Assign the parameters to the class fields
2025-09-19 21:27:35 +02:00
this.protocolClient = protocolClient;
this.protocolSettings = protocolSettings;
this.protocolVersion = protocolVersion;
2025-09-29 17:46:30 +02:00
// Initialize the logger and protocol version
initializeLogger(logFolder);
initializeProtocolVersion();
2025-09-19 20:46:57 +02:00
2025-09-29 17:46:30 +02:00
// Register the appropriate listeners and packets
registerListeners();
2025-09-20 20:41:08 +02:00
registerPackets();
2025-09-19 21:27:35 +02:00
}
2025-09-29 17:46:30 +02:00
/**
* Register the appropriate packets based on the current protocol version
*/
2025-09-20 20:41:08 +02:00
private void registerPackets() {
// Classic packets
Classic_DomainPacket cDomainPacket = new Classic_DomainPacket();
2025-12-08 10:33:46 +01:00
Classic_MessagePacket cMessagePacket = new Classic_MessagePacket();
Classic_PingPacket cPingPacket = new Classic_PingPacket();
2025-09-20 20:41:08 +02:00
if (isPacketSupported(cDomainPacket)) protocolSettings.packetHandler.registerPacket(cDomainPacket);
if (isPacketSupported(cMessagePacket)) protocolSettings.packetHandler.registerPacket(cMessagePacket);
if (isPacketSupported(cPingPacket)) protocolSettings.packetHandler.registerPacket(cPingPacket);
// 1.0.0-BETA packets
AuthPacket v100bAuthPath = new AuthPacket();
UnsupportedClassicPacket v100bUnsupportedClassicPacket = new UnsupportedClassicPacket();
2025-12-11 01:22:58 +01:00
INSQueryPacket v100BINSQueryPacket = new INSQueryPacket();
INSResponsePacket v100BINSResponsePacket = new INSResponsePacket(this);
2025-09-20 20:41:08 +02:00
if (isPacketSupported(v100bAuthPath)) protocolSettings.packetHandler.registerPacket(v100bAuthPath);
2025-09-29 18:46:31 +02:00
if (isPacketSupported(v100bUnsupportedClassicPacket))
protocolSettings.packetHandler.registerPacket(v100bUnsupportedClassicPacket);
2025-12-11 01:22:58 +01:00
if (isPacketSupported(v100BINSQueryPacket))
protocolSettings.packetHandler.registerPacket(v100BINSQueryPacket);
if (isPacketSupported(v100BINSResponsePacket))
protocolSettings.packetHandler.registerPacket(v100BINSResponsePacket);
2025-09-20 20:41:08 +02:00
}
2025-09-29 17:46:30 +02:00
/**
* Register the appropriate listeners based on the current side
2025-09-29 18:46:31 +02:00
*
2025-09-29 17:46:30 +02:00
* @throws Exception if an error occurs while registering the listeners
*/
private void registerListeners() throws Exception {
// Classic listeners
2025-12-08 10:33:46 +01:00
if (isClassicSupported()) {
2025-12-08 10:44:15 +01:00
Classic_ClientListener classicListener = new Classic_ClientListener();
2025-12-08 10:33:46 +01:00
classicListener.setProtocolBridge(this);
protocolSettings.eventManager.registerListener(classicListener.getClass());
2025-12-08 10:44:15 +01:00
} else protocolSettings.eventManager.unregisterListener(Classic_ClientListener.class);
2025-09-29 17:46:30 +02:00
2025-12-08 09:59:58 +01:00
// INS Listeners
if (isRunningAsINSServer()) {
2025-12-08 10:33:46 +01:00
INSServerListener serverListener = new INSServerListener();
serverListener.setINSServer(protocolINSServer);
protocolSettings.eventManager.registerListener(serverListener.getClass());
2025-09-29 17:46:30 +02:00
protocolSettings.eventManager.unregisterListener(WebServerListener.class);
protocolSettings.eventManager.unregisterListener(ClientListener.class);
}
// Web Listeners
if (isRunningAsWebServer()) {
2025-12-08 10:33:46 +01:00
WebServerListener serverListener = new WebServerListener();
serverListener.setWebServer(protocolWebServer);
protocolSettings.eventManager.registerListener(serverListener.getClass());
2025-12-08 09:59:58 +01:00
protocolSettings.eventManager.unregisterListener(INSServerListener.class);
2025-09-29 17:46:30 +02:00
protocolSettings.eventManager.unregisterListener(ClientListener.class);
}
// Client Listeners
if (isRunningAsClient()) {
2025-12-08 10:33:46 +01:00
ClientListener clientListener = new ClientListener();
clientListener.setClient(protocolClient);
protocolSettings.eventManager.registerListener(clientListener.getClass());
2025-12-08 09:59:58 +01:00
protocolSettings.eventManager.unregisterListener(INSServerListener.class);
2025-09-29 17:46:30 +02:00
protocolSettings.eventManager.unregisterListener(WebServerListener.class);
}
}
/**
* Initialize the logger instance
2025-09-29 18:46:31 +02:00
*
2025-09-29 17:46:30 +02:00
* @param logFolder The folder to store the log files
*/
private void initializeLogger(File logFolder) {
// Create a temporary logger instance to avoid final field issues
Logger tmpLogger = null;
try {
// Initialize temporary logger
tmpLogger = new Logger(logFolder, false, true);
} catch (IOException | NoSuchFieldException | IllegalAccessException exception) {
exception.printStackTrace();
System.exit(1);
}
// Assign the temporary logger to the final field
this.logger = tmpLogger;
}
/**
* Initialize the protocol version
* Validate if the protocol version is valid for the current side
* If not, log an error and exit the application
*/
private void initializeProtocolVersion() {
// Check if the protocol version is valid for the current side
// If not, log an error and exit the application
if (!validateProtocolSide()) {
this.logger.error("Invalid protocol version '" + protocolVersion.toString() + "'!");
System.exit(1);
}
2025-09-20 15:17:36 +02:00
}
2025-09-29 17:46:30 +02:00
/**
* Check if the classic protocol is supported by the current protocol version
2025-09-29 18:46:31 +02:00
*
2025-09-29 17:46:30 +02:00
* @return true if the classic protocol is supported, false otherwise
*/
2025-09-29 18:46:31 +02:00
public boolean isClassicSupported() {
boolean yes = false;
for (ProtocolVersion compatibleVersion : protocolVersion.getCompatibleVersions()) {
2025-09-29 17:46:30 +02:00
// Check if the compatible version is classic
yes = compatibleVersion.getProtocolType() == ProtocolVersion.ProtocolType.CLASSIC;
if (yes) break;
}
2025-09-29 17:46:30 +02:00
// Check if the current protocol version is classic or if it is supported by any of the compatible versions
return protocolVersion.getProtocolType() == ProtocolVersion.ProtocolType.CLASSIC || yes;
}
2025-09-29 17:46:30 +02:00
/**
* Check if the target protocol is supported by the current protocol version
2025-09-29 18:46:31 +02:00
*
2025-09-29 17:46:30 +02:00
* @param protocol The target protocol to check
* @return true If the target protocol is supported, false otherwise
*/
2025-09-29 18:46:31 +02:00
public boolean isProtocolSupported(ProtocolVersion.Protocol protocol) {
2025-09-25 23:40:24 +02:00
boolean yes = false;
for (ProtocolVersion compatibleVersion : protocolVersion.getCompatibleVersions()) {
2025-09-29 17:46:30 +02:00
// Check if the compatible version supports the target protocol
2025-09-25 23:40:24 +02:00
yes = compatibleVersion.getSupportedProtocols().contains(protocol);
if (yes) break;
}
2025-09-29 17:46:30 +02:00
// Check if the current protocol version supports the target protocol or if it is supported by any of the compatible versions
2025-09-25 23:40:24 +02:00
return protocolVersion.getSupportedProtocols().contains(protocol) || yes;
}
2025-09-29 17:46:30 +02:00
/**
* Check if the target packet is supported by the current protocol version
2025-09-29 18:46:31 +02:00
*
2025-09-29 17:46:30 +02:00
* @param packet The target packet to check
* @return true if the target packet is supported, false otherwise
*/
2025-09-29 18:46:31 +02:00
public boolean isPacketSupported(OACPacket packet) {
2025-09-29 17:46:30 +02:00
return isVersionSupported(packet.getProtocolVersion());
2025-09-19 21:27:35 +02:00
}
2025-09-29 17:46:30 +02:00
/**
* Check if the target protocol version is supported by the current protocol version
2025-09-29 18:46:31 +02:00
*
2025-09-29 17:46:30 +02:00
* @param targetVersion The target protocol version to check
* @return true if the target protocol version is supported, false otherwise
*/
2025-09-29 18:46:31 +02:00
public boolean isVersionSupported(ProtocolVersion targetVersion) {
2025-09-29 17:46:30 +02:00
// Check if the target protocol version is the same as the current protocol version or if it is in the list of compatible versions
return protocolVersion == targetVersion || protocolVersion.getCompatibleVersions().contains(targetVersion);
2025-09-25 23:40:24 +02:00
}
2025-09-29 17:46:30 +02:00
/**
* Validate if the protocol version is valid for the current side
2025-09-29 18:46:31 +02:00
*
2025-09-29 17:46:30 +02:00
* @return true if the protocol version is valid for the current side, false otherwise
*/
2025-09-20 15:17:36 +02:00
private boolean validateProtocolSide() {
2025-09-25 23:40:24 +02:00
return
(isRunningAsClient() && protocolVersion.getProtocolSide() == ProtocolVersion.ProtocolSide.CLIENT) ||
2025-09-25 23:41:17 +02:00
(isRunningAsClient() && protocolVersion.getProtocolSide() == ProtocolVersion.ProtocolSide.CLIENT_WEB) ||
2025-12-08 09:59:58 +01:00
(isRunningAsClient() && protocolVersion.getProtocolSide() == ProtocolVersion.ProtocolSide.CLIENT_INS) ||
2025-09-25 23:41:17 +02:00
(isRunningAsClient() && protocolVersion.getProtocolSide() == ProtocolVersion.ProtocolSide.ALL) ||
(isRunningAsWebServer() && protocolVersion.getProtocolSide() == ProtocolVersion.ProtocolSide.WEB) ||
(isRunningAsWebServer() && protocolVersion.getProtocolSide() == ProtocolVersion.ProtocolSide.CLIENT_WEB) ||
2025-12-08 09:59:58 +01:00
(isRunningAsWebServer() && protocolVersion.getProtocolSide() == ProtocolVersion.ProtocolSide.WEB_INS) ||
2025-09-25 23:41:17 +02:00
(isRunningAsWebServer() && protocolVersion.getProtocolSide() == ProtocolVersion.ProtocolSide.ALL) ||
2025-12-08 09:59:58 +01:00
(isRunningAsINSServer() && protocolVersion.getProtocolSide() == ProtocolVersion.ProtocolSide.INS) ||
(isRunningAsINSServer() && protocolVersion.getProtocolSide() == ProtocolVersion.ProtocolSide.WEB_INS) ||
(isRunningAsINSServer() && protocolVersion.getProtocolSide() == ProtocolVersion.ProtocolSide.CLIENT_INS) ||
(isRunningAsINSServer() && protocolVersion.getProtocolSide() == ProtocolVersion.ProtocolSide.ALL);
2025-09-19 21:27:35 +02:00
}
2025-09-29 17:46:30 +02:00
/**
2025-12-08 09:59:58 +01:00
* Check if the current instance is running as a INS server
2025-09-29 18:46:31 +02:00
*
2025-12-08 09:59:58 +01:00
* @return true if the current instance is running as a INS server, false otherwise
2025-09-29 17:46:30 +02:00
*/
2025-12-08 09:59:58 +01:00
public boolean isRunningAsINSServer() {
return protocolINSServer != null;
2025-09-29 17:46:30 +02:00
}
/**
* Check if the current instance is running as a client
2025-09-29 18:46:31 +02:00
*
2025-09-29 17:46:30 +02:00
* @return true if the current instance is running as a client, false otherwise
*/
2025-09-29 18:46:31 +02:00
public boolean isRunningAsClient() {
2025-09-29 17:46:30 +02:00
return protocolClient != null;
}
/**
* Check if the current instance is running as a web server
2025-09-29 18:46:31 +02:00
*
2025-09-29 17:46:30 +02:00
* @return true if the current instance is running as a web server, false otherwise
*/
2025-09-29 18:46:31 +02:00
public boolean isRunningAsWebServer() {
2025-09-29 17:46:30 +02:00
return protocolWebServer != null;
2025-09-19 21:27:35 +02:00
}
}