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

Commit

Permalink
New Packet Registration System
Browse files Browse the repository at this point in the history
  • Loading branch information
BelgianDev committed Dec 17, 2023
1 parent 1814234 commit 923de9d
Show file tree
Hide file tree
Showing 24 changed files with 439 additions and 183 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package fr.atlasworld.network.api.networking;

import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import org.jetbrains.annotations.NotNull;
/**
* Represents information exchanged during the handshake.
*
* @param <T> the type of the handshake value
*/
public final class HandshakeInformation<T> {
private final @NotNull T value;
private final @NotNull Function<T, String> factory;

public HandshakeInformation(@NotNull T value, @NotNull Function<T, String> factory) {
Preconditions.checkArgument(value != null && factory != null, "Handshake information and string factory may not be null!");

this.value = value;
this.factory = factory;
}

public @NotNull T getValue() {
return value;
}

/**
* Returns a string representation of the current data.
* This will use the provided function to transform the object into string
* before sending it through the handshake packet.
*
* @return A string representation of the object.
*/
public String asString() {
return this.factory.apply(this.value);
}

@Override
public String toString() {
return "handshake_data[data:" + this.asString() + "]";
}

// Static Fields

/**
* Returns a new instance of HandshakeInformation with the provided information.
*
* @param information The information to be included in the handshake.
* @return A new instance of HandshakeInformation with the provided information and resource key.
*/
public static HandshakeInformation<String> text(@NotNull String information) {
return new HandshakeInformation<>(information, data -> data);
}

/**
* Returns a new instance of HandshakeInformation with the provided information.
*
* @param information The information to be included in the handshake.
* @return A new instance of HandshakeInformation with the provided information and resource key.
*/
public static HandshakeInformation<Integer> integer(int information) {
return new HandshakeInformation<>(information, String::valueOf);
}

/**
* Returns a new instance of HandshakeInformation with the provided information.
*
* @param information The information to be included in the handshake.
* @return A new instance of HandshakeInformation with the provided information and resource key.
*/
public static HandshakeInformation<Long> asLong(long information) {
return new HandshakeInformation<>(information, String::valueOf);
}


/**
* Returns a new instance of HandshakeInformation with the provided value.
* <p>
* String transformation will use the default {@link Object#toString()} method.
*
* @param value The value to be included in the handshake.
* @param <T> The type of the value.
* @return A new instance of HandshakeInformation with the provided value.
*/
public static <T> HandshakeInformation<T> of(@NotNull T value) {
return new HandshakeInformation<>(value, Object::toString);
}
}
33 changes: 0 additions & 33 deletions api/src/main/java/fr/atlasworld/network/api/networking/Socket.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package fr.atlasworld.network.api.networking;

import com.google.gson.JsonElement;
import fr.atlasworld.network.api.concurrent.action.FutureAction;
import fr.atlasworld.network.api.module.Module;
import fr.atlasworld.network.api.networking.packet.NetworkPacket;
import org.jetbrains.annotations.NotNull;

import java.util.function.Supplier;

/**
* Abstract API Socket. Handles networking for AtlasNetwork
Expand All @@ -27,31 +21,4 @@ public interface Socket {
* @return true if running
*/
boolean running();

/**
* Register information that will be sent to the client when the handshake process starts.
* Client will receive a json object containing all the system information.
* Your data can be found as {@code "your_module_id": <your data here>}.
* <p>
* Don't send any private or user information! At this state encryption is not yet established with the client.
*
* @param module the id of the module will be used as key when sending data to the client.
* @param data data to attach to the request
*/
void registerHandshakeInfo(Module module, JsonElement data);

/**
* Register a packet in the active state registry.
*
* @param module module that registers the packet.
* @param builder builder of the packet.
*/
void registerPacket(@NotNull Module module, @NotNull Supplier<? extends NetworkPacket> builder);

/**
* Unregister packet from the active state registry.
*
* @param key key of the packet.
*/
void unregisterPacket(@NotNull String key);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package fr.atlasworld.network.api.networking.event;

import fr.atlasworld.network.api.event.Event;
import fr.atlasworld.network.api.module.Module;
import fr.atlasworld.network.api.networking.HandshakeInformation;
import fr.atlasworld.network.api.registry.ResourceKey;
import org.jetbrains.annotations.NotNull;

/**
* Event called when modules are supposed to inject their data into the handshake packet.
* The packet is in plaintext nor is the client trusted at this state.
* Don't send any sensitive information through here.
*/
public interface HandshakeDataInjectionEvent extends Event {

/**
* Injects the provided information into the handshake using the specified key.
*
* @param key The key associated with the information.
* @param information The information to be injected into the handshake.
*/
void inject(@NotNull String key, @NotNull HandshakeInformation<?> information);

/**
* Injects the provided information into the handshake using the specified module.
*
* @param module The module associated with the information.
* @param information The information to be injected into the handshake.
*/
void inject(@NotNull Module module, @NotNull HandshakeInformation<?> information);

/**
* Injects the provided information into the handshake packet associated with the given resource key.
*
* @param key The resource key that identifies the handshake packet.
* @param information The information to be injected into the handshake packet.
*/
void inject(@NotNull ResourceKey key, @NotNull HandshakeInformation<?> information);

/**
* Clears the data of the handshake packet.
* <p>
* Important internal data such as Protocol version or System version are not stored in the handshake packet.
* So you can clear the data without worrying breaking AtlasNetwork's protocol.
* But don't forget you may not be the only one injecting data in the packet and this could lead to unexpected issues.
*/
void clear();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fr.atlasworld.network.api.networking.event;

import fr.atlasworld.network.api.networking.packet.NetworkPacket;
import fr.atlasworld.network.api.registry.event.RegistrationEvent;

public interface PacketRegistrationEvent extends RegistrationEvent<NetworkPacket> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
*/
public interface NetworkPacket {

/**
* Get the unique identifier of the packet.
*/
@NotNull String getKey();

/**
* Decodes and process received requests.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import fr.atlasworld.network.api.test.command.TestCommands;
import fr.atlasworld.network.api.test.configuration.TestConfiguration;
import fr.atlasworld.network.api.test.configuration.TestConfigurationSchema;
import fr.atlasworld.network.api.test.event.NetworkListener;
import fr.atlasworld.network.api.test.event.ServerListener;
import fr.atlasworld.network.api.test.services.database.LocalDatabaseService;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -39,6 +40,7 @@ protected void onActivate(@NotNull ModuleActivationContext ctx) {
FileManager fileManager = ctx.getServer().getFileManager();

ctx.getServer().getModuleManager().registerListener(this, new ServerListener());
ctx.getServer().getModuleManager().registerListener(this, new NetworkListener());
ctx.getServer().getModuleManager().registerListener(this, new TestCommands());

ConfigurationReader<TestConfiguration> testConfReader =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package fr.atlasworld.network.api.test.event;

import fr.atlasworld.network.api.event.EventHandler;
import fr.atlasworld.network.api.event.EventListener;
import fr.atlasworld.network.api.networking.HandshakeInformation;
import fr.atlasworld.network.api.networking.event.HandshakeDataInjectionEvent;
import fr.atlasworld.network.api.test.TestModule;

public class NetworkListener implements EventListener {

@EventHandler
private void onHandshake(HandshakeDataInjectionEvent event) {
event.inject("my_unique_id", HandshakeInformation.text("my_custom_info"));

TestModule.LOGGER.info("Successfully injected handshake data.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package fr.atlasworld.network.api.test.networking;

import fr.atlasworld.network.api.networking.packet.NetworkPacket;
import fr.atlasworld.network.api.registry.Register;
import fr.atlasworld.network.api.registry.RegistryObject;
import fr.atlasworld.network.api.test.TestModule;
import fr.atlasworld.network.api.test.networking.packets.MyCustomPacket;

public class TestPackets {
private static final Register<NetworkPacket> PACKETS = new Register<>(TestModule.MODULE);

public static final RegistryObject<NetworkPacket> MY_CUSTOM_PACKET = PACKETS.register(MyCustomPacket.KEY.getKey(),
MyCustomPacket::new);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package fr.atlasworld.network.api.test.networking.packets;

import fr.atlasworld.network.api.networking.ConnectionSource;
import fr.atlasworld.network.api.networking.exception.packet.PacketExecutionException;
import fr.atlasworld.network.api.networking.packet.NetworkPacket;
import fr.atlasworld.network.api.networking.packet.PacketByteBuf;
import fr.atlasworld.network.api.registry.ResourceKey;
import fr.atlasworld.network.api.test.TestModule;
import org.jetbrains.annotations.NotNull;

public class MyCustomPacket implements NetworkPacket {
public static final ResourceKey KEY = new ResourceKey(TestModule.MODULE, "myCustomPacket");

private int myInt;
private String myString;
private boolean myBoolean;

@Override
public @NotNull String getKey() {
return KEY.toString();
}

public MyCustomPacket() {

}

public MyCustomPacket(int myInt, String myString, boolean myBoolean) {
this.myInt = myInt;
this.myString = myString;
this.myBoolean = myBoolean;
}

@Override
public void decode(@NotNull ConnectionSource source, @NotNull PacketByteBuf buffer) throws PacketExecutionException {
this.myInt = buffer.readInt();
this.myString = buffer.readString();
this.myBoolean = buffer.readBoolean();
}

@Override
public void encode(@NotNull PacketByteBuf buffer) throws PacketExecutionException {
buffer.writeInt(this.myInt);
buffer.writeString(buffer.readString());
buffer.writeBoolean(buffer.readBoolean());
}

public int getMyInt() {
return myInt;
}

public String getMyString() {
return myString;
}

public boolean isMyBoolean() {
return myBoolean;
}
}
1 change: 1 addition & 0 deletions src/main/java/fr/atlasworld/network/core/AtlasNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public void start() {
// Events Listener
SystemModuleManager.INSTANCE.registerListener(this, new Commands());

this.socket.register(); // Register Packets and Handshake info
this.console.register(); // Register commands & stuff

LOGGER.info("Starting AtlasNetwork..");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public SystemConsole(CommandDispatcher<CommandSource> dispatcher) {
public void register() {
SystemModuleManager.INSTANCE.callEvent(new CommandRegistrationEventImpl());

NetworkRegistries.COMMAND.getAllEntries()
NetworkRegistries.COMMANDS.getAllEntries()
.forEach(command -> this.dispatcher.register(command.getCommand()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,13 @@
import fr.atlasworld.network.api.command.Command;
import fr.atlasworld.network.api.command.CommandSource;
import fr.atlasworld.network.api.command.event.CommandRegistrationEvent;
import fr.atlasworld.network.api.registry.Register;
import fr.atlasworld.network.api.registry.Registry;
import fr.atlasworld.network.core.registry.NetworkRegistries;
import fr.atlasworld.network.core.registry.event.SimpleRegistrationEvent;

public class CommandRegistrationEventImpl implements CommandRegistrationEvent {
@Override
public void register(Register<Command<CommandSource>> register) {
register.getEntries().forEach((key, value) -> {
NetworkRegistries.COMMAND.register(key, value.get());
});
public class CommandRegistrationEventImpl extends SimpleRegistrationEvent<Command<CommandSource>>
implements CommandRegistrationEvent {

register.getObjects().values().forEach(regObj ->
regObj.updateReference(NetworkRegistries.COMMAND));
}

@Override
public Registry<Command<CommandSource>> getRegistry() {
return NetworkRegistries.COMMAND;
public CommandRegistrationEventImpl() {
super(NetworkRegistries.COMMANDS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class LogUtils {
private static final StackWalker STACK_WALKER = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
private static final Logger LOGGER = LogUtils.getLogger();

public static final Logger CODE_LINTING_LOGGER = LoggerFactory.getLogger("Code-Linting");

public static Logger getLogger() {
return LoggerFactory.getLogger(STACK_WALKER.getCallerClass().getSimpleName());
}
Expand Down
Loading

0 comments on commit 923de9d

Please sign in to comment.