Skip to content

Commit 217f64e

Browse files
committed
command entry now supports location based client sessions
1 parent cabdac4 commit 217f64e

3 files changed

Lines changed: 38 additions & 23 deletions

File tree

velocity/src/main/java/dev/objz/commandbridge/velocity/dispatch/CommandEntry.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import dev.objz.commandbridge.logging.Summary;
1515
import dev.objz.commandbridge.scripting.model.Script;
1616
import dev.objz.commandbridge.scripting.model.enums.ArgType;
17+
import dev.objz.commandbridge.scripting.model.enums.Location;
1718
import dev.objz.commandbridge.scripting.model.enums.RunAs;
1819
import dev.objz.commandbridge.scripting.model.records.mapping.ArgMapping;
1920
import dev.objz.commandbridge.scripting.model.records.mapping.CmdMapping;
@@ -163,7 +164,7 @@ private void dispatchCommand(ExecutionContext ctx, CmdMapping cmd) {
163164
var targets = Optional.ofNullable(cmd.execute()).orElse(List.of());
164165

165166
if (targets.isEmpty()) {
166-
Log.warn("Command '{}' has no execution targets", cmd.command());
167+
Log.warn("Command '{}' has no execution targets defined", cmd.command());
167168
notifyExecutionError(ctx.source(), cmd.command(), "No execution targets configured");
168169
return;
169170
}
@@ -172,25 +173,22 @@ private void dispatchCommand(ExecutionContext ctx, CmdMapping cmd) {
172173
}
173174

174175
private void dispatchToTarget(ExecutionContext ctx, CmdMapping cmd, IdMapping target) {
175-
var dispatcher = switch (target.location()) {
176-
case VELOCITY -> velocityDispatcher(ctx, cmd);
177-
case BACKEND -> remoteDispatcher(ctx, cmd);
178-
};
179-
dispatcher.accept(target.id());
180-
}
176+
String targetId = target.id();
177+
Location targetLoc = target.location();
181178

182-
private Consumer<String> velocityDispatcher(ExecutionContext ctx, CmdMapping cmd) {
183-
return targetId -> {
179+
if (targetLoc == Location.VELOCITY) {
184180
if (velocityExecutor.isLocal(targetId)) {
185181
executeLocally(ctx, cmd);
186182
} else {
187-
dispatchToRemoteSession(ctx, cmd, targetId);
183+
dispatchToRemoteSession(ctx, cmd, targetId, Location.VELOCITY);
188184
}
189-
};
190-
}
191-
192-
private Consumer<String> remoteDispatcher(ExecutionContext ctx, CmdMapping cmd) {
193-
return targetId -> dispatchToRemoteSession(ctx, cmd, targetId);
185+
} else if (targetLoc == Location.BACKEND) {
186+
dispatchToRemoteSession(ctx, cmd, targetId, Location.BACKEND);
187+
} else {
188+
Log.warn("Unknown location type '{}' for target '{}'", targetLoc, targetId);
189+
notifyExecutionError(ctx.source(), cmd.command(),
190+
"Unknown location type: " + targetLoc);
191+
}
194192
}
195193

196194
private void executeLocally(ExecutionContext ctx, CmdMapping cmd) {
@@ -204,7 +202,6 @@ private void executeLocally(ExecutionContext ctx, CmdMapping cmd) {
204202
notifyExecutionError(ctx.source(), cmd.command(),
205203
"Command execution failed");
206204

207-
// Log using feedback system
208205
Feedback feedback = new Feedback(1, 0, 1, List.of(),
209206
List.of("Local command execution failed: "
210207
+ cmd.command()));
@@ -223,19 +220,23 @@ private void executeLocally(ExecutionContext ctx, CmdMapping cmd) {
223220
});
224221
}
225222

226-
private void dispatchToRemoteSession(ExecutionContext ctx, CmdMapping cmd, String targetId) {
227-
findSession(targetId)
223+
private void dispatchToRemoteSession(ExecutionContext ctx, CmdMapping cmd, String targetId,
224+
Location requiredLocation) {
225+
findSession(targetId, requiredLocation)
228226
.filter(this::isSessionConnected)
229227
.ifPresentOrElse(
230228
session -> sendExecuteCommand(session, ctx, cmd, targetId),
231229
() -> {
232-
Log.warn("Target '{}' not found or not connected", targetId);
230+
Log.warn("Target '{}' ({}) not found or not connected",
231+
targetId,
232+
requiredLocation);
233233
notifyExecutionError(ctx.source(), cmd.command(),
234-
"Backend server '" + targetId
234+
requiredLocation + " server '" + targetId
235235
+ "' is not connected");
236236

237237
Feedback feedback = new Feedback(1, 0, 1, List.of(),
238-
List.of("Backend not connected: " + targetId));
238+
List.of(requiredLocation + " not connected: "
239+
+ targetId));
239240
Summary.feedbackSummary("Execution Failed", feedback, targetId);
240241
});
241242
}
@@ -366,9 +367,9 @@ private Optional<CommandSource> resolveSource(SenderContext sender) {
366367
};
367368
}
368369

369-
private Optional<ClientSession> findSession(String id) {
370+
private Optional<ClientSession> findSession(String id, Location requiredLocation) {
370371
return StreamSupport.stream(sessions.spliterator(), false)
371-
.filter(s -> id.equals(s.id()))
372+
.filter(s -> id.equals(s.id()) && s.location() == requiredLocation)
372373
.findFirst();
373374
}
374375

velocity/src/main/java/dev/objz/commandbridge/velocity/net/in/AuthHandler.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import dev.objz.commandbridge.net.payloads.util.AuthResponsePayload;
1010
import dev.objz.commandbridge.net.proto.Envelope;
1111
import dev.objz.commandbridge.net.proto.MessageType;
12+
import dev.objz.commandbridge.scripting.model.enums.Location;
1213
import dev.objz.commandbridge.velocity.net.WsServer;
1314
import dev.objz.commandbridge.velocity.net.session.ClientSession;
1415
import dev.objz.commandbridge.velocity.net.session.SessionHub;
@@ -92,6 +93,8 @@ public void accept(WebSocketChannel ch, Envelope env) {
9293
ClientSession s = sessions.add(ch, env.from());
9394
s.status(AuthStatus.AUTH_FAIL);
9495

96+
s.location(ap.location() != null ? ap.location() : Location.BACKEND);
97+
9598
reply(ch, env, MessageType.AUTH_OK, new AuthResponsePayload(serverNonce, serverMac))
9699
.dispatch()
97100
.exceptionally(ex -> {

velocity/src/main/java/dev/objz/commandbridge/velocity/net/session/ClientSession.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package dev.objz.commandbridge.velocity.net.session;
22

3+
import dev.objz.commandbridge.scripting.model.enums.Location;
34
import dev.objz.commandbridge.security.AuthStatus;
45
import io.undertow.websockets.core.WebSocketChannel;
56

67
public final class ClientSession {
78
private final WebSocketChannel ch;
89
private volatile String id = "unknown";
910
private volatile AuthStatus status = AuthStatus.AUTH_OK;
11+
private volatile Location location = Location.BACKEND;
1012

1113
public ClientSession(WebSocketChannel ch, String clientId) {
1214
this.ch = ch;
@@ -28,4 +30,13 @@ public AuthStatus status() {
2830
public void status(AuthStatus status) {
2931
this.status = status;
3032
}
33+
34+
public Location location() {
35+
return location;
36+
}
37+
38+
public void location(Location location) {
39+
this.location = location;
40+
41+
}
3142
}

0 commit comments

Comments
 (0)