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

Commit

Permalink
Server API
Browse files Browse the repository at this point in the history
  • Loading branch information
BelgianDev committed Dec 23, 2023
1 parent a330e9b commit bad6e80
Show file tree
Hide file tree
Showing 23 changed files with 299 additions and 25 deletions.
2 changes: 0 additions & 2 deletions api/src/main/java/fr/atlasworld/network/api/AtlasNetwork.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package fr.atlasworld.network.api;

import fr.atlasworld.network.api.server.AtlasNetworkServer;

/**
* The AtlasNetwork class is the main class that represents the AtlasNetwork framework.
* It provides functionality to set and get the server instance.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.atlasworld.network.api.server;
package fr.atlasworld.network.api;

import com.google.errorprone.annotations.CanIgnoreReturnValue;
import fr.atlasworld.network.api.concurrent.action.FutureAction;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package fr.atlasworld.network.api.command;

import fr.atlasworld.network.api.server.AtlasNetworkServer;
import fr.atlasworld.network.api.AtlasNetworkServer;

public interface CommandSource {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package fr.atlasworld.network.api.module.lifecycle;

import fr.atlasworld.network.api.server.AtlasNetworkServer;
import fr.atlasworld.network.api.AtlasNetworkServer;

/**
* Lifecycle context for when a module activates
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package fr.atlasworld.network.api.module.lifecycle;

import fr.atlasworld.network.api.server.AtlasNetworkServer;
import fr.atlasworld.network.api.AtlasNetworkServer;

/**
* Lifecycle context for when a module deactivates.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fr.atlasworld.network.api.module.lifecycle;

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

/**
* Lifecycle context for when a module activates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import fr.atlasworld.network.api.networking.packet.PacketByteBuf;
import fr.atlasworld.network.api.networking.packet.SentPacket;
import fr.atlasworld.network.api.registry.ResourceKey;
import fr.atlasworld.network.api.server.AtlasNetworkServer;
import fr.atlasworld.network.api.AtlasNetworkServer;
import org.jetbrains.annotations.NotNull;

import java.net.InetSocketAddress;
Expand Down
101 changes: 101 additions & 0 deletions api/src/main/java/fr/atlasworld/network/api/server/NetworkServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package fr.atlasworld.network.api.server;

import fr.atlasworld.network.api.concurrent.action.FutureAction;
import fr.atlasworld.network.api.server.lifecycle.ServerStatus;
import org.jetbrains.annotations.NotNull;

import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.util.UUID;

/**
* Represents a server running on AtlasWorld's Network.
* A server can be anything, from a Minecraft Server to the Discord bot.
*/
public interface NetworkServer {

/**
* Retrieve the unique identifier of the server.
* @return unique identifier
*/
@NotNull UUID identifier();

/**
* Retrieve the user readable name of the server.
* @return the user readable name of the server
*/
@NotNull String getUserReadableName();

/**
* Retrieve the local status of the server.
* <p>
* Warning: This data may not be entirely true.
* The server may be marked as running or active,
* but this does not mean that the server is accessible!
* <p>
* if you want to make sure that the server is accessible you should ping the server.
* With {@link #retrieveCachedPingRequest()} and {@link #ping()}
* @return the detected server status
*/
ServerStatus getStatus();

/**
* Retrieve the remote address of the server.
* @return remote server address
*/
InetSocketAddress remoteAddress();

/**
* Retrieve the last cached ping request.
* <p>
* Use {@link PingRequest#timestamp()} to check the time of the last request
* always check the timestamp of the request before trusting the data!
* It may be out-dated!
* @return null if no ping request was sent.
*/
PingRequest retrieveCachedPingRequest();

/**
* Send a ping request to the server.
* <p>
* Do not use this method extensively!
* If you need to fetch the ping request multiple times use {@link #retrieveCachedPingRequest()}
* Instead of pinging the server multiple times
* @return the future answer of the ping request.
*/
FutureAction<PingRequest> ping();

/**
* Checks if the server was created by the automated system or by the user.
* <p>
* If this returns false the server should not be managed by the system.
*
* @return false if the server was created or added by a user.
*/
boolean isDynamicallyCreated();

/**
* Checks whether the server should be listed in the server selection menu.
*
* @return false if it shouldn't be listed, true otherwise.
*/
boolean shouldBeListed();

/**
* Starts the server.
*
* @return the future action completed when the server is started.
* @throws UnsupportedOperationException if the server implementation does not support starting the server.
* @throws IllegalStateException if the server is not running.
*/
FutureAction<Void> start();

/**
* Stops the server.
*
* @return the future action completed when the server is stopped.
* @throws UnsupportedOperationException if the server implementation does not support stopping the server.
* @throws IllegalStateException if the server is not running.
*/
FutureAction<Void> stop();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package fr.atlasworld.network.api.server;

import com.google.gson.JsonObject;
import org.jetbrains.annotations.Nullable;

/**
* Abstract Ping request implementation that represents a server ping request.
*/
public interface PingRequest {

/**
* Get the timestamp of the time the request was received.
*/
long timestamp();

/**
* Checks if the request was successful.
* @return true if the request was successful, false otherwise
*/
boolean success();

/**
* Get the returned request code.
* @return returns the resulted HTTP code request. Returns -1 if the request failed.
*/
int requestCode();

/**
* Retrieve the retrieved response of the server.
* @return null if the server did not respond or did not send any data back.
*/
@Nullable
JsonObject getResponse();

/**
* Check if the server sent data back.
* @return false if the server did not respond or did not send any data back.
*/
boolean hasResponse();
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package fr.atlasworld.network.api.server.event;

import fr.atlasworld.network.api.event.Event;
import fr.atlasworld.network.api.server.AtlasNetworkServer;
import fr.atlasworld.network.api.AtlasNetworkServer;

/**
* The ServerStartedEvent interface represents an event indicating that a server has started.
*/
public class ServerStartedEvent implements Event {
public class SystemServerStartedEvent implements Event {
private final AtlasNetworkServer server;
private final long startTime;
private final long bootTime;

public ServerStartedEvent(AtlasNetworkServer server, long startTime, long bootTime) {
public SystemServerStartedEvent(AtlasNetworkServer server, long startTime, long bootTime) {
this.server = server;
this.startTime = startTime;
this.bootTime = bootTime;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package fr.atlasworld.network.api.server.lifecycle;

import fr.atlasworld.network.api.server.NetworkServer;

/**
* Server status, this list every status possible by the server.
*/
public enum ServerStatus {

/**
* Server is setting up / installing.
* In this state not much can be done.
*/
INSTALLING,

/**
* Server installation failed.
* This could mean that the server installation instructions may not be correct.
*/
CORRUPTED,

/**
* Server is stopped and in an inactive state.
*/
STOPPED,

/**
* Server is starting and is not yet able to handle requests.
*/
STARTING,

/**
* Server is started/running and is in an active state.
*/
STARTED,

/**
* Server is stopping.
*/
STOPPING,

/**
* Server is deleted and in this state the server should not be interacted with.
* The server actions are very limited,
* and interacting with it may cause unexpected crashes or issues.
*/
DELETED,

/**
* The server status could not be detected,
* or the {@link NetworkServer} implementation did not support server statuses.
* <p>
* Methods changing the server state will skip the state checks if the server is
* in this state.
*/
UNKNOWN;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package fr.atlasworld.network.api.server.servers;

import java.net.InetSocketAddress;
import java.util.List;
import java.util.UUID;

public abstract class MinecraftProxyServer extends MinecraftServer{
protected MinecraftProxyServer(UUID identifier, String name, boolean dynamicallyCreated, InetSocketAddress address) {
super(identifier, name, dynamicallyCreated, address);
}

public abstract List<MinecraftServer> listAvailableServers();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package fr.atlasworld.network.api.server.servers;

import fr.atlasworld.network.api.server.NetworkServer;
import fr.atlasworld.network.api.server.lifecycle.ServerStatus;
import org.jetbrains.annotations.NotNull;

import java.net.InetSocketAddress;
import java.util.UUID;

/**
* Abstract representation of a Minecraft Server running on AtlasWorld's Network.
*/
public abstract class MinecraftServer implements NetworkServer {
protected final UUID identifier;
protected final String name;
protected final boolean dynamicallyCreated;
protected final InetSocketAddress address;

protected ServerStatus status;

protected MinecraftServer(UUID identifier, String name, boolean dynamicallyCreated, InetSocketAddress address) {
this.identifier = identifier;
this.name = name;
this.dynamicallyCreated = dynamicallyCreated;
this.address = address;
}


@Override
public @NotNull UUID identifier() {
return this.identifier;
}

@Override
public @NotNull String getUserReadableName() {
return this.name;
}

@Override
public ServerStatus getStatus() {
return this.status;
}

@Override
public InetSocketAddress remoteAddress() {
return this.remoteAddress();
}

@Override
public boolean isDynamicallyCreated() {
return this.dynamicallyCreated;
}

/**
* Retrieve the server player count.
* @return player count.
*/
public abstract int getPlayerCount();

/**
* Retrieve the MOTD of the server.
* @return MOTD of the server.
*/
public abstract String getMOTD();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import fr.atlasworld.network.api.event.EventHandler;
import fr.atlasworld.network.api.event.EventListener;
import fr.atlasworld.network.api.server.event.ServerStartedEvent;
import fr.atlasworld.network.api.server.event.SystemServerStartedEvent;
import fr.atlasworld.network.api.test.TestModule;

public class ServerListener implements EventListener {

@EventHandler
private void onServerStarted(ServerStartedEvent event) {
private void onServerStarted(SystemServerStartedEvent event) {
TestModule.LOGGER.info("(Auto-Detected) Server took {}ms to boot up.", event.getTotalBootTime());
}
}
6 changes: 3 additions & 3 deletions src/main/java/fr/atlasworld/network/core/AtlasNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import fr.atlasworld.network.api.module.lifecycle.ModuleDeactivationContext;
import fr.atlasworld.network.api.module.lifecycle.ModuleLoadContext;
import fr.atlasworld.network.api.security.SecurityManager;
import fr.atlasworld.network.api.server.AtlasNetworkServer;
import fr.atlasworld.network.api.server.event.ServerStartedEvent;
import fr.atlasworld.network.api.AtlasNetworkServer;
import fr.atlasworld.network.api.server.event.SystemServerStartedEvent;
import fr.atlasworld.network.api.services.ServiceManager;
import fr.atlasworld.network.api.util.NetworkVersion;
import fr.atlasworld.network.api.util.archive.Archive;
Expand Down Expand Up @@ -125,7 +125,7 @@ public void start() {
private void notifyModulesServerStarted() {
Timing timing = Environment.START_TIMING;

ServerStartedEvent event = new ServerStartedEvent(this, timing.getStartTime(), timing.getRunningTime());
SystemServerStartedEvent event = new SystemServerStartedEvent(this, timing.getStartTime(), timing.getRunningTime());
SystemModuleManager.INSTANCE.callEvent(event);
}

Expand Down
Loading

0 comments on commit bad6e80

Please sign in to comment.