160 lines
6.9 KiB
Java
160 lines
6.9 KiB
Java
package org.openautonomousconnection.protocol;
|
|
|
|
import org.openautonomousconnection.protocol.listeners.ClientListener;
|
|
import org.openautonomousconnection.protocol.listeners.ServerListener;
|
|
import org.openautonomousconnection.protocol.packets.OACPacket;
|
|
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.AuthPacket;
|
|
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.GetDestinationPacket;
|
|
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.UnsupportedClassicPacket;
|
|
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.ValidateDomainPacket;
|
|
import org.openautonomousconnection.protocol.packets.v1_0_0.classic.Classic_DomainPacket;
|
|
import org.openautonomousconnection.protocol.side.client.ProtocolClient;
|
|
import org.openautonomousconnection.protocol.side.server.ProtocolServer;
|
|
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
|
|
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.ClassicHandlerClient;
|
|
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.ClassicHandlerServer;
|
|
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.Classic_ClientListener;
|
|
import lombok.Getter;
|
|
import lombok.Setter;
|
|
import dev.unlegitdqrk.unlegitlibrary.utils.Logger;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
public class ProtocolBridge {
|
|
|
|
@Getter
|
|
private static ProtocolBridge instance;
|
|
@Getter
|
|
private final ProtocolSettings protocolSettings;
|
|
@Getter
|
|
private final ProtocolVersion protocolVersion;
|
|
@Getter
|
|
private final Logger logger;
|
|
@Getter
|
|
private ProtocolServer protocolServer;
|
|
@Getter
|
|
private ProtocolClient protocolClient;
|
|
@Getter
|
|
@Setter
|
|
private ClassicHandlerServer classicHandlerServer;
|
|
@Getter
|
|
@Setter
|
|
private ClassicHandlerClient classicHandlerClient;
|
|
|
|
public ProtocolBridge(ProtocolServer protocolServer, ProtocolSettings protocolSettings, ProtocolVersion protocolVersion, File logFolder) throws InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {
|
|
this.protocolServer = protocolServer;
|
|
this.protocolSettings = protocolSettings;
|
|
this.protocolVersion = protocolVersion;
|
|
|
|
Logger tmpLogger = null;
|
|
try {
|
|
tmpLogger = new Logger(logFolder, false, true);
|
|
} catch (IOException | NoSuchFieldException | IllegalAccessException exception) {
|
|
exception.printStackTrace();
|
|
tmpLogger = null;
|
|
System.exit(1);
|
|
}
|
|
|
|
this.logger = tmpLogger;
|
|
protocolSettings.eventManager.registerListener(new ServerListener());
|
|
protocolSettings.eventManager.unregisterListener(new ClientListener());
|
|
|
|
if (!validateProtocolSide()) {
|
|
this.logger.error("Invalid protocol version '" + protocolVersion.toString() + "'!");
|
|
System.exit(1);
|
|
}
|
|
|
|
if (isClassicSupported()) protocolSettings.eventManager.unregisterListener(new Classic_ClientListener());
|
|
registerPackets();
|
|
|
|
instance = this;
|
|
}
|
|
|
|
public ProtocolBridge(ProtocolClient protocolClient, ProtocolSettings protocolSettings, ProtocolVersion protocolVersion, File logFolder) throws InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {
|
|
this.protocolClient = protocolClient;
|
|
this.protocolSettings = protocolSettings;
|
|
this.protocolVersion = protocolVersion;
|
|
|
|
Logger tmpLogger = null;
|
|
try {
|
|
tmpLogger = new Logger(logFolder, false, true);
|
|
} catch (IOException | NoSuchFieldException | IllegalAccessException exception) {
|
|
exception.printStackTrace();
|
|
tmpLogger = null;
|
|
System.exit(1);
|
|
}
|
|
|
|
this.logger = tmpLogger;
|
|
protocolSettings.eventManager.registerListener(new ClientListener());
|
|
protocolSettings.eventManager.unregisterListener(new ServerListener());
|
|
|
|
if (!validateProtocolSide()) {
|
|
this.logger.error("Invalid protocol version '" + protocolVersion.toString() + "'!");
|
|
System.exit(1);
|
|
}
|
|
|
|
if (isClassicSupported()) protocolSettings.eventManager.registerListener(new Classic_ClientListener());
|
|
registerPackets();
|
|
|
|
instance = this;
|
|
}
|
|
|
|
private void registerPackets() {
|
|
// Classic packets
|
|
Classic_DomainPacket cDomainPacket = new Classic_DomainPacket();
|
|
Classic_DomainPacket cMessagePacket = new Classic_DomainPacket();
|
|
Classic_DomainPacket cPingPacket = new Classic_DomainPacket();
|
|
|
|
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();
|
|
ValidateDomainPacket v100bValidateDomainPacket = new ValidateDomainPacket();
|
|
GetDestinationPacket v100bGetDestinationPacket = new GetDestinationPacket();
|
|
|
|
if (isPacketSupported(v100bAuthPath)) protocolSettings.packetHandler.registerPacket(v100bAuthPath);
|
|
if (isPacketSupported(v100bUnsupportedClassicPacket))
|
|
protocolSettings.packetHandler.registerPacket(v100bUnsupportedClassicPacket);
|
|
if (isPacketSupported(v100bValidateDomainPacket))
|
|
protocolSettings.packetHandler.registerPacket(v100bValidateDomainPacket);
|
|
if (isPacketSupported(v100bGetDestinationPacket))
|
|
protocolSettings.packetHandler.registerPacket(v100bGetDestinationPacket);
|
|
}
|
|
|
|
public boolean isPacketSupported(OACPacket packet) {
|
|
return isVersionSupported(packet.getProtocolVersion());
|
|
}
|
|
|
|
public boolean isClassicSupported() {
|
|
boolean yes = false;
|
|
for (ProtocolVersion compatibleVersion : protocolVersion.getCompatibleVersions()) {
|
|
yes = compatibleVersion.getProtocolType() == ProtocolVersion.ProtocolType.CLASSIC;
|
|
if (yes) break;
|
|
}
|
|
|
|
return protocolVersion.getProtocolType() == ProtocolVersion.ProtocolType.CLASSIC || yes;
|
|
}
|
|
|
|
public boolean isRunningAsServer() {
|
|
return protocolServer != null;
|
|
}
|
|
|
|
public boolean isRunningAsClient() {
|
|
return protocolClient != null;
|
|
}
|
|
|
|
private boolean validateProtocolSide() {
|
|
return (isRunningAsServer() && protocolVersion.getProtocolSide() != ProtocolVersion.ProtocolSide.CLIENT) ||
|
|
(isRunningAsClient() && protocolVersion.getProtocolSide() != ProtocolVersion.ProtocolSide.SERVER);
|
|
}
|
|
|
|
public boolean isVersionSupported(ProtocolVersion targetVersion) {
|
|
return protocolVersion == targetVersion || protocolVersion.getCompatibleVersions().contains(targetVersion);
|
|
}
|
|
}
|