Add serverside PING-command #228

Merged
lars.winzer merged 6 commits from feat/38-add-serverside-ping-command into main 2026-04-02 12:45:56 +02:00
4 changed files with 93 additions and 0 deletions
@@ -1,5 +1,8 @@
package ch.unibas.dmi.dbis.cs108.casono.server;
import ch.unibas.dmi.dbis.cs108.casono.server.app.commands.ping.PingHandler;
import ch.unibas.dmi.dbis.cs108.casono.server.app.commands.ping.PingParser;
import ch.unibas.dmi.dbis.cs108.casono.server.app.commands.ping.PingRequest;
import ch.unibas.dmi.dbis.cs108.casono.server.domain.user.UserCleanupJob;
import ch.unibas.dmi.dbis.cs108.casono.server.domain.user.UserRegistry;
import ch.unibas.dmi.dbis.cs108.casono.server.network.NetworkManager;
@@ -7,6 +10,7 @@ import ch.unibas.dmi.dbis.cs108.casono.server.network.command.execution.CommandR
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.parsing.CommandParserDispatcher;
import ch.unibas.dmi.dbis.cs108.casono.server.network.events.DisconnectEvent;
import ch.unibas.dmi.dbis.cs108.casono.server.network.events.EventBus;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.dispatcher.ResponseDispatcher;
import ch.unibas.dmi.dbis.cs108.casono.server.network.sessions.SessionDisconnectJob;
import ch.unibas.dmi.dbis.cs108.casono.server.network.sessions.SessionManager;
import java.time.Duration;
@@ -58,6 +62,25 @@ public class ServerApp {
SESSION_DISCONNECT_JOB_PERIOD,
TimeUnit.SECONDS);
ResponseDispatcher responseDispatcher = new ResponseDispatcher(sessionManager);
registerCommands(dispatcher, router, responseDispatcher);
networkManager.start();
}
/**
* Registers command parsers and handlers.
*
* @param parserDispatcher the dispatcher responsible for parsing incoming commands
* @param commandRouter the router that dispatches parsed commands to appropriate handlers
* @param responseDispatcher the dispatcher responsible for sending responses back to clients
*/
private static void registerCommands(
CommandParserDispatcher parserDispatcher,
CommandRouter commandRouter,
ResponseDispatcher responseDispatcher) {
parserDispatcher.register("PING", new PingParser());
commandRouter.register(PingRequest.class, new PingHandler(responseDispatcher));
}
}
@@ -0,0 +1,29 @@
package ch.unibas.dmi.dbis.cs108.casono.server.app.commands.ping;
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.execution.CommandHandler;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.OkResponse;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.dispatcher.ResponseDispatcher;
/** Handler for {@link PingRequest}. */
public class PingHandler implements CommandHandler<PingRequest> {
private final ResponseDispatcher responseDispatcher;
/**
* Create a new PingHandler to execute {@link PingRequest}s
*
* @param responseDispatcher dispatcher used to send responses back to clients
*/
public PingHandler(ResponseDispatcher responseDispatcher) {
this.responseDispatcher = responseDispatcher;
}
/**
* Execute the ping request.
*
* @param request the ping request to handle
*/
@Override
public void execute(PingRequest request) {
responseDispatcher.dispatch(new OkResponse(request.getContext()));
}
}
@@ -0,0 +1,22 @@
package ch.unibas.dmi.dbis.cs108.casono.server.app.commands.ping;
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.parsing.CommandParser;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.PrimitiveRequest;
/**
* Parser for the Ping command.
*
* <p>Converts a low-level {@link PrimitiveRequest} into a {@link PingRequest}.
*/
public class PingParser implements CommandParser<PingRequest> {
/**
* Parse the given primitive request into a {@link PingRequest}.
*
* @param primitiveRequest the raw request to parse
* @return {@link PingRequest}
*/
@Override
public PingRequest parse(PrimitiveRequest primitiveRequest) {
return new PingRequest(primitiveRequest.context());
}
}
@@ -0,0 +1,19 @@
package ch.unibas.dmi.dbis.cs108.casono.server.app.commands.ping;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.Request;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.RequestContext;
/**
* Represents a "PING" request sent by a client to check server availability and keep the connection
* alive.
*/
public class PingRequest extends Request {
/**
* Constructs a new PingRequest with the given context.
*
* @param context the request context associated with this request
*/
public PingRequest(RequestContext context) {
super(context);
}
}