Skip to content

Commit

Permalink
Made the QueueObject key into a string again
Browse files Browse the repository at this point in the history
  • Loading branch information
jonafanho committed Oct 18, 2024
1 parent 68d3cff commit 5d5abee
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/mtr/core/data/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.mtr.core.operation.VehicleUpdate;
import org.mtr.core.serializer.ReaderBase;
import org.mtr.core.serializer.SerializedDataBase;
import org.mtr.core.servlet.Operation;
import org.mtr.core.servlet.OperationProcessor;
import org.mtr.core.simulation.Simulator;
import org.mtr.libraries.it.unimi.dsi.fastutil.longs.Long2ObjectAVLTreeMap;
import org.mtr.libraries.it.unimi.dsi.fastutil.longs.LongAVLTreeSet;
Expand Down Expand Up @@ -71,7 +71,7 @@ public void sendUpdates(Simulator simulator) {
});

if (hasUpdate1 || hasUpdate2 || hasUpdate3) {
simulator.sendMessageS2C(Operation.VEHICLES_LIFTS, vehicleLiftResponse, playerPresentResponse -> playerPresentResponse.verify(simulator, clientId), PlayerPresentResponse.class);
simulator.sendMessageS2C(OperationProcessor.VEHICLES_LIFTS, vehicleLiftResponse, playerPresentResponse -> playerPresentResponse.verify(simulator, clientId), PlayerPresentResponse.class);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/mtr/core/data/Depot.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.mtr.core.path.SidingPathFinder;
import org.mtr.core.serializer.ReaderBase;
import org.mtr.core.serializer.WriterBase;
import org.mtr.core.servlet.Operation;
import org.mtr.core.servlet.OperationProcessor;
import org.mtr.core.simulation.Simulator;
import org.mtr.core.tool.Angle;
import org.mtr.core.tool.Utilities;
Expand Down Expand Up @@ -388,7 +388,7 @@ public static void generateDepots(Simulator simulator, ObjectArrayList<Depot> de
idsToGenerate.remove(depot.getId());
updateDataResponse.addDepot(depot);
if (forceComplete || idsToGenerate.isEmpty()) {
simulator.sendMessageS2C(Operation.GENERATION_STATUS_UPDATE, updateDataResponse, null, null);
simulator.sendMessageS2C(OperationProcessor.GENERATION_STATUS_UPDATE, updateDataResponse, null, null);
}
});
});
Expand Down
25 changes: 0 additions & 25 deletions src/main/java/org/mtr/core/servlet/Operation.java

This file was deleted.

26 changes: 24 additions & 2 deletions src/main/java/org/mtr/core/servlet/OperationProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,34 @@

public final class OperationProcessor {

// Client to server
public static final String GET_DATA = "get_data";
public static final String UPDATE_DATA = "update_data";
public static final String DELETE_DATA = "delete_data";
public static final String LIST_DATA = "list_data";
public static final String ARRIVALS = "arrivals";
public static final String SET_TIME = "set_time";
public static final String UPDATE_RIDING_ENTITIES = "update_riding_entities";
public static final String PRESS_LIFT = "press_lift";
public static final String NEARBY_STATIONS = "nearby_stations";
public static final String NEARBY_DEPOTS = "nearby_depots";
public static final String RAILS = "rails";
public static final String GENERATE_BY_DEPOT_IDS = "generate_by_depot_ids";
public static final String GENERATE_BY_DEPOT_NAME = "generate_by_depot_name";
public static final String GENERATE_BY_LIFT = "generate_by_lift";
public static final String CLEAR_BY_DEPOT_IDS = "clear_by_depot_ids";
public static final String CLEAR_BY_DEPOT_NAME = "clear_by_depot_name";

// Server to client
public static final String VEHICLES_LIFTS = "vehicles_lifts";
public static final String GENERATION_STATUS_UPDATE = "generation_status_update";

@Nullable
public static SerializedDataBase process(Operation operation, SerializedDataBase data, long currentMillis, Simulator simulator) {
public static SerializedDataBase process(String key, SerializedDataBase data, long currentMillis, Simulator simulator) {
// TODO is there a better way to create these objects than to cast to a JSON and back?
final JsonReader jsonReader = new JsonReader(Utilities.getJsonObjectFromData(data));

switch (operation) {
switch (key) {
case GET_DATA:
return new DataRequest(jsonReader).getData(simulator);
case UPDATE_DATA:
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/mtr/core/servlet/QueueObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

public final class QueueObject {

public final Operation operation;
public final String key;
public final SerializedDataBase data;
@Nullable
private final Consumer<SerializedDataBase> callback;

public <T extends SerializedDataBase> QueueObject(Operation operation, SerializedDataBase data, @Nullable Consumer<T> callback, @Nullable Class<T> reaponseDataClass) {
this.operation = operation;
public <T extends SerializedDataBase> QueueObject(String key, SerializedDataBase data, @Nullable Consumer<T> callback, @Nullable Class<T> reaponseDataClass) {
this.key = key;
this.data = data;
this.callback = callback == null || reaponseDataClass == null ? null : serializedDataBase -> {
if (reaponseDataClass.isInstance(serializedDataBase)) {
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/org/mtr/core/simulation/Simulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.mtr.core.serializer.SerializedDataBase;
import org.mtr.core.serializer.SerializedDataBaseWithId;
import org.mtr.core.servlet.MessageQueue;
import org.mtr.core.servlet.Operation;
import org.mtr.core.servlet.OperationProcessor;
import org.mtr.core.servlet.QueueObject;
import org.mtr.core.tool.Utilities;
Expand Down Expand Up @@ -120,7 +119,7 @@ public void tick() {
queuedRuns.process(Runnable::run);

// Process messages
messageQueueC2S.process(queueObject -> queueObject.runCallback(OperationProcessor.process(queueObject.operation, queueObject.data, currentMillis, this)));
messageQueueC2S.process(queueObject -> queueObject.runCallback(OperationProcessor.process(queueObject.key, queueObject.data, currentMillis, this)));
} catch (Throwable e) {
Main.LOGGER.fatal("", e);
}
Expand Down Expand Up @@ -193,8 +192,8 @@ public void sendMessageC2S(QueueObject queueObject) {
messageQueueC2S.put(queueObject);
}

public <T extends SerializedDataBase> void sendMessageS2C(Operation operation, SerializedDataBase data, @Nullable Consumer<T> consumer, @Nullable Class<T> responseDataClass) {
messageQueueS2C.put(new QueueObject(operation, data, consumer == null ? null : responseData -> run(() -> consumer.accept(responseData)), responseDataClass));
public <T extends SerializedDataBase> void sendMessageS2C(String key, SerializedDataBase data, @Nullable Consumer<T> consumer, @Nullable Class<T> responseDataClass) {
messageQueueS2C.put(new QueueObject(key, data, consumer == null ? null : responseData -> run(() -> consumer.accept(responseData)), responseDataClass));
}

public void processMessagesS2C(Consumer<QueueObject> callback) {
Expand Down

0 comments on commit 5d5abee

Please sign in to comment.