rolled back usage of CompletableFuture in favour of callbacks

This commit is contained in:
Stephan Richter
2020-10-27 16:39:11 +01:00
parent f5dbb0916a
commit ad25dd8166
11 changed files with 316 additions and 200 deletions

View File

@@ -7,6 +7,7 @@ import java.util.Vector;
import org.json.JSONObject;
import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.Command;
import de.srsoftware.web4rail.Constants;
import de.srsoftware.web4rail.Device;
import de.srsoftware.web4rail.Plan;
@@ -146,7 +147,20 @@ public class Locomotive extends Car implements Constants,Device{
case ZIMO:
proto = "Z"; break;
}
plan.queue("INIT {} GL "+address+" "+proto);
plan.queue(new Command("INIT {} GL "+address+" "+proto) {
@Override
public void onSuccess() {
super.onSuccess();
plan.stream(t("{} initialized.",this));
}
@Override
public void onFailure(Reply r) {
super.onFailure(r);
plan.stream(t("Was not able to initialize {}!",this));
}
});
init = true;
}
@@ -226,7 +240,14 @@ public class Locomotive extends Car implements Constants,Device{
}
private void queue() {
plan.queue("SET {} GL "+address+" "+(reverse?1:0)+" "+speed+" "+VMAX+" "+(f1?1:0)+" "+(f2?1:0)+" "+(f3?1:0)+" "+(f4?1:0));
plan.queue(new Command("SET {} GL "+address+" "+(reverse?1:0)+" "+speed+" "+VMAX+" "+(f1?1:0)+" "+(f2?1:0)+" "+(f3?1:0)+" "+(f4?1:0)) {
@Override
public void onFailure(Reply reply) {
super.onFailure(reply);
plan.stream(t("Failed to send command to {}: {}",this,reply.message()));
}
});
}
public String setSpeed(int newSpeed) {

View File

@@ -23,6 +23,7 @@ import de.srsoftware.web4rail.Application;
import de.srsoftware.web4rail.Constants;
import de.srsoftware.web4rail.Plan;
import de.srsoftware.web4rail.Plan.Direction;
import de.srsoftware.web4rail.actions.SetSignalsToStop;
import de.srsoftware.web4rail.Route;
import de.srsoftware.web4rail.Window;
import de.srsoftware.web4rail.tags.Button;
@@ -366,22 +367,18 @@ public class Train implements Constants {
if (availableRoutes.isEmpty()) return t("No free routes from {}",block);
route = availableRoutes.get(rand.nextInt(availableRoutes.size()));
route.lock(this).thenApply(reply -> {
try {
route.setSignals(null);
if (direction != route.startDirection) turn();
setSpeed(100);
return t("started {}",this);
} catch (Exception e) {
throw new RuntimeException(e);
}
}).thenAccept(message -> plan.stream(message))
.exceptionally(ex -> {
plan.stream(ex.getMessage());
throw new RuntimeException(ex);
});
return t("Trying to start {}",this);
if (!route.lock()) return t("Was not able to lock {}",route);
String error = null;
if (!route.setTurnouts()) error = t("Was not able to set all turnouts!");
if (error == null && !route.setSignals(null)) error = t("Was not able to set all signals!");
if (error == null && !route.train(this)) error = t("Was not able to assign {} to {}!",this,route);
if (error == null) {
setSpeed(128);
return t("Started {}",this);
}
route.unlock();
this.block.train(this); // re-set train on previous block
return error;
}
private Object stop() {