Skip to content
This repository has been archived by the owner on Mar 11, 2024. It is now read-only.

Commit

Permalink
Channel Init optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
BelgianDev committed Dec 9, 2023
1 parent 82c7398 commit aebc3b5
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ public final void initialize(@NotNull ModuleMeta meta, @NotNull File file, @NotN
this.logger = logger;

this.loaded = true;

this.deactivationFuture = new SimpleFutureAction<>();
}

@Override
Expand All @@ -110,7 +112,9 @@ public final void activate(@NotNull ModuleActivationContext ctx) {
if (this.status == ModuleStatus.ACTIVE)
throw new IllegalStateException("Module is already activated!");

this.deactivationFuture = new SimpleFutureAction<>();
if (this.deactivationFuture.isDone())
this.deactivationFuture = new SimpleFutureAction<>();

this.status = ModuleStatus.ACTIVE;
this.onActivate(ctx);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package fr.atlasworld.network.api.module.lifecycle;

import fr.atlasworld.network.api.AtlasNetworkServer;
import fr.atlasworld.network.api.loader.ModuleClassLoader;

/**
* Lifecycle context for when a module activates
*/
public interface ModuleLoadContext {

/**
* Retrieve the class loader used to load the module.
*/
Expand All @@ -15,4 +17,9 @@ public interface ModuleLoadContext {
* Retrieve the class loader used to load the module as a ModuleClassLoader.
*/
ModuleClassLoader getModuleClassLoader();

/**
* Get the system's server.
*/
AtlasNetworkServer getServer();
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ public void onLoad(@NotNull ModuleLoadContext ctx) {
ctx.getModuleClassLoader().useLibraryClassPath(true); // Makes sure the libraries can be used.
ctx.getModuleClassLoader().allowExternalClasspathUsage(false); // Test Module classes may only be used by itself.

MODULE = this;
LOGGER = this.getLogger();
MODULE = this;

LOGGER.info("Registering services..");
ctx.getServer().getServiceManager().registerService(this, DatabaseService.class, new LocalDatabaseService());
}

@Override
protected void onActivate(@NotNull ModuleActivationContext ctx) {
LOGGER.info("Module activated!");

LOGGER.info("Registering services..");
ctx.getServer().getServiceManager().registerService(this, DatabaseService.class, new LocalDatabaseService());

FileManager fileManager = ctx.getServer().getFileManager();

ConfigurationReader<TestConfiguration> testConfReader =
Expand Down
27 changes: 15 additions & 12 deletions src/main/java/fr/atlasworld/network/core/AtlasNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@ public AtlasNetwork(final LaunchArgs args) {
// LOAD STATE
LOGGER.info("Starting AtlasNetwork {}", this.version.getVersionMessage());
LOGGER.info("Loading AtlasNetwork..");
ModuleManager moduleManager = ModuleManager.init(args);
moduleManager.load();

this.fileManager = SystemFileManager.getInstance();
this.serviceManager = new SystemServiceManager();

ModuleManager moduleManager = ModuleManager.init(args);
moduleManager.load();

// Configuration files
SocketConfiguration socketConfiguration = this.fileManager
.registerConfiguration(new SocketConfiguration.SocketConfigurationSchema())
Expand All @@ -98,25 +99,27 @@ public AtlasNetwork(final LaunchArgs args) {
throw new RuntimeException(); // Makes compiler happy
}

this.socket = new SystemSocket(socketConfiguration);
this.console = new SystemConsole(new CommandDispatcher<>());

// INIT STATE
LOGGER.info("Initializing AtlasNetwork..");

//Module Manager -> Load Modules -> Done Later once AtlasNetwork is initialized
moduleManager.initialize();

// Check & validate registered services.
DatabaseService databaseService = this.serviceManager.getService(DatabaseService.class);
if (databaseService == null) {
LOGGER.error("No Database Modules installed! AtlasNetwork cannot proceed!");
Bootstrap.crash(new NullPointerException());
return;
throw new RuntimeException(); // Makes compiler happy
}

this.socket = new SystemSocket(socketConfiguration, databaseService, this.securityManager);
this.console = new SystemConsole(new CommandDispatcher<>());

// INIT STATE
LOGGER.info("Initializing AtlasNetwork..");

// Initialize modules
moduleManager.initialize();

// Start the database service
databaseService.connect();

// Set up the command thread
Thread commandThread = new Thread(this.console::start);
commandThread.setDaemon(false);
commandThread.setName("Command Thread");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void load() {
long startTime = System.currentTimeMillis();
this.registeredSourceHolders.forEach(source -> source.load(this.holder));

holder.loadModules(); // Load Modules and call onLoaded method.
this.holder.loadModules(); // Load Modules and call onLoaded method.

long calculatedLoadingTime = (System.currentTimeMillis() - startTime);
if (calculatedLoadingTime > 1000) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package fr.atlasworld.network.core.module.lifecycle;

import fr.atlasworld.network.api.AtlasNetworkServer;
import fr.atlasworld.network.api.loader.ModuleClassLoader;
import fr.atlasworld.network.api.module.lifecycle.ModuleLoadContext;
import fr.atlasworld.network.core.module.loader.SystemModuleClassLoader;

public class ModuleLoadContextImpl implements ModuleLoadContext {
private final SystemModuleClassLoader loader;
private final AtlasNetworkServer server;

public ModuleLoadContextImpl(SystemModuleClassLoader loader) {
public ModuleLoadContextImpl(SystemModuleClassLoader loader, AtlasNetworkServer server) {
this.loader = loader;
this.server = server;
}

@Override
Expand All @@ -20,4 +23,9 @@ public ClassLoader getClassloader() {
public ModuleClassLoader getModuleClassLoader() {
return this.loader;
}

@Override
public AtlasNetworkServer getServer() {
return this.server;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import fr.atlasworld.network.api.module.exception.InvalidModuleException;
import fr.atlasworld.network.api.module.exception.unchecked.UncheckedModuleException;
import fr.atlasworld.network.api.util.archive.JarArchive;
import fr.atlasworld.network.core.AtlasNetwork;
import fr.atlasworld.network.core.module.loader.ClassPathLoader;
import fr.atlasworld.network.core.module.loader.SystemGroupClassLoader;
import fr.atlasworld.network.core.module.ModuleMetaImpl;
Expand Down Expand Up @@ -70,7 +71,7 @@ public NetworkModule create() {
}

NetworkModule module = (NetworkModule) main.getDeclaredConstructor().newInstance();
module.onLoad(new ModuleLoadContextImpl(loader));
module.onLoad(new ModuleLoadContextImpl(loader, AtlasNetwork.getInstance()));

return module;
} catch (Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private Collection<NetworkModule> create() {

return module;
} catch (UncheckedModuleException e) {
LOGGER.error("Failed to load '{}'!", factory.getConfig().getId(), e.getCause());
LOGGER.error("Failed to load '{}'!", factory.getConfig().getId(), e.getCause().getCause());
return null;
}
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package fr.atlasworld.network.core.networking.handler;

import fr.atlasworld.network.api.security.SecurityManager;
import fr.atlasworld.network.api.services.database.Database;
import fr.atlasworld.network.api.services.database.DatabaseService;
import fr.atlasworld.network.core.networking.codec.ApiConverterCodec;
import fr.atlasworld.network.core.networking.codec.NetworkCodec;
import fr.atlasworld.network.core.networking.security.authentication.AuthenticationProfile;
import fr.atlasworld.network.core.networking.security.encryption.EncryptionManager;
import fr.atlasworld.network.core.networking.security.encryption.SystemEncryptionManager;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import org.jetbrains.annotations.NotNull;

public class SocketInitializer extends ChannelInitializer<SocketChannel> {
private final DatabaseService databaseService;
private final SecurityManager securityManager;

public SocketInitializer(DatabaseService databaseService, SecurityManager securityManager) {
this.databaseService = databaseService;
this.securityManager = securityManager;
}

@Override
protected void initChannel(@NotNull SocketChannel ch) throws Exception {
Database<AuthenticationProfile> database = this.databaseService.getDatabase("auth", new AuthenticationProfile.AuthenticationProfileFactory());
EncryptionManager encryptionManager = new SystemEncryptionManager(this.securityManager);

NetworkCodec codec = new ApiConverterCodec();

ch.pipeline()
//Out
.addLast(new DecodeHandler(database, encryptionManager, codec))
.addLast(new ResourceHandler())

//In
.addLast(new EncodeHandler(encryptionManager, codec));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import fr.atlasworld.network.api.concurrent.action.SimpleFutureAction;
import fr.atlasworld.network.api.networking.packet.NetworkPacket;
import fr.atlasworld.network.api.networking.packet.PacketRegistry;
import fr.atlasworld.network.api.security.SecurityManager;
import fr.atlasworld.network.api.services.database.Database;
import fr.atlasworld.network.api.services.database.DatabaseService;
import fr.atlasworld.network.core.AtlasNetwork;
Expand All @@ -18,6 +19,7 @@
import fr.atlasworld.network.core.networking.handler.DecodeHandler;
import fr.atlasworld.network.core.networking.handler.EncodeHandler;
import fr.atlasworld.network.core.networking.handler.ResourceHandler;
import fr.atlasworld.network.core.networking.handler.SocketInitializer;
import fr.atlasworld.network.core.networking.packet.registry.SystemPacketRegistry;
import fr.atlasworld.network.core.networking.security.authentication.AuthenticationProfile;
import fr.atlasworld.network.core.networking.security.encryption.EncryptionManager;
Expand All @@ -39,11 +41,17 @@ public class SystemSocket implements Socket {
private final ServerBootstrap bootstrap;
private final JsonObject handshakeInfo;

// Required Services
private final DatabaseService databaseService;
private final SecurityManager securityManager;

private boolean running = false;
private Channel channel;

public SystemSocket(SocketConfiguration socketConfiguration) {
public SystemSocket(SocketConfiguration socketConfiguration, DatabaseService databaseService, SecurityManager securityManager) {
this.socketConfiguration = socketConfiguration;
this.databaseService = databaseService;
this.securityManager = securityManager;

this.bossGroup = new NioEventLoopGroup();
this.workerGroup = new NioEventLoopGroup();
Expand All @@ -58,18 +66,7 @@ public SystemSocket(SocketConfiguration socketConfiguration) {
.childOption(ChannelOption.AUTO_CLOSE, false) // Do not close on failure
.group(this.bossGroup, this.workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(@NotNull SocketChannel ch) throws Exception {
DatabaseService databaseService = AtlasNetwork.getInstance().getServiceManager().getService(DatabaseService.class);
Database<AuthenticationProfile> database = databaseService.getDatabase("auth", new AuthenticationProfile.AuthenticationProfileFactory());
EncryptionManager encryptionManager = new SystemEncryptionManager(AtlasNetwork.getInstance().getSecurityManager());

ch.pipeline().addLast(new DecodeHandler(database, encryptionManager, new ApiConverterCodec()));
ch.pipeline().addLast(new ResourceHandler());
ch.pipeline().addLast(new EncodeHandler(encryptionManager, new ApiConverterCodec()));
}
});
.childHandler(new SocketInitializer(this.databaseService, this.securityManager));

this.handshakeInfo = new JsonObject();
}
Expand Down

0 comments on commit aebc3b5

Please sign in to comment.