Implementierung des neuen Routen-Algorithmus weitgehend abgeschlossen.
Was noch fehlt ist der Brems-Prozessor; außerdem muss der neue Code getestet werden.
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package de.srsoftware.web4rail.threads;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import de.srsoftware.web4rail.Application;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.moving.Train;
|
||||
|
||||
public class BrakeProcess extends BaseClass implements Runnable{
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(BrakeProcess.class);
|
||||
|
||||
public static int defaultTimeStep = 500;
|
||||
private Train train;
|
||||
private int targetSpeed = Train.defaultEndSpeed;
|
||||
boolean ended = false;
|
||||
long distance = 0;
|
||||
private int startSpeed;
|
||||
private int lastSpeed;
|
||||
private long lastTime;
|
||||
|
||||
public BrakeProcess(Train train) {
|
||||
this.train = train;
|
||||
new Thread(this, Application.threadName(this)).start();
|
||||
}
|
||||
|
||||
public BrakeProcess end() {
|
||||
LOG.debug("{}.end()",this);
|
||||
ended = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
Integer delay = train.route().brakeTime(train.brakeId());
|
||||
startSpeed = train.speed;
|
||||
lastTime = timestamp();
|
||||
while (!train.hasNextPreparedRoute()) {
|
||||
sleep(delay);
|
||||
lastSpeed = train.speed;
|
||||
updateDistance();
|
||||
if (lastSpeed > targetSpeed) lastSpeed -= 10;
|
||||
if (lastSpeed < targetSpeed && (ended = true)) lastSpeed = targetSpeed;
|
||||
if (ended) break;
|
||||
if (lastSpeed != train.speed) train.setSpeed(lastSpeed);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateDistance() {
|
||||
long newTime = timestamp();
|
||||
distance += (newTime-lastTime)*lastSpeed;
|
||||
lastTime = newTime;
|
||||
LOG.debug("measured distance: {} units",distance);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getSimpleName()+"("+train+")";
|
||||
}
|
||||
|
||||
public void updateTime() {
|
||||
updateDistance();
|
||||
LOG.debug("updateTime(): start speed was {} {}.",startSpeed,BaseClass.speedUnit);
|
||||
// TODO
|
||||
}
|
||||
}
|
||||
@@ -300,7 +300,7 @@ public class ControlUnit extends Thread implements Constants{
|
||||
private void startInfoThread() {
|
||||
infoSocket = commandSocket; // handshake läuft immer über commandSocket und commandScanner
|
||||
infoScanner = commandScanner;
|
||||
Thread infoThread = new Thread() {
|
||||
new Thread(Application.threadName("CU.InfoThread")) {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
@@ -318,14 +318,12 @@ public class ControlUnit extends Thread implements Constants{
|
||||
case FEEDBACK:
|
||||
int addr = Integer.parseInt(parts[5]);
|
||||
boolean active = !parts[6].equals("0");
|
||||
Thread thread = new Thread() {
|
||||
new Thread(Application.threadName("CU.FeedBack("+addr+")")) {
|
||||
@Override
|
||||
public void run() {
|
||||
plan.sensor(addr,active);
|
||||
}
|
||||
};
|
||||
thread.setName(Application.threadName("CU.FeedBack("+addr+")"));
|
||||
thread.start();
|
||||
}.start();
|
||||
case ACESSORY:
|
||||
break;
|
||||
default:
|
||||
@@ -351,9 +349,7 @@ public class ControlUnit extends Thread implements Constants{
|
||||
public String toString() {
|
||||
return "CU.InfoThread";
|
||||
}
|
||||
};
|
||||
infoThread.setName(Application.threadName("CU.InfoThread"));
|
||||
infoThread.start();
|
||||
}.start();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -10,9 +10,8 @@ public abstract class DelayedExecution extends Thread {
|
||||
}
|
||||
|
||||
public DelayedExecution(int delay,Object cause) {
|
||||
super(Application.threadName(cause));
|
||||
this.delay = delay;
|
||||
|
||||
setName(Application.threadName(cause));
|
||||
start();
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import java.util.Vector;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import de.srsoftware.web4rail.Application;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Plan.Direction;
|
||||
import de.srsoftware.web4rail.Route;
|
||||
@@ -17,6 +18,10 @@ import de.srsoftware.web4rail.tiles.Block;
|
||||
import de.srsoftware.web4rail.tiles.Tile;
|
||||
|
||||
public class RouteManager extends BaseClass {
|
||||
|
||||
private enum State{
|
||||
IDLE,SEARCHING,PREPARING
|
||||
}
|
||||
|
||||
public static abstract class Callback {
|
||||
public abstract void routePrepared(Route route);
|
||||
@@ -24,11 +29,11 @@ public class RouteManager extends BaseClass {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(RouteManager.class);
|
||||
private Context ctx;
|
||||
private State state;
|
||||
private Callback callback;
|
||||
private boolean searching;
|
||||
|
||||
|
||||
public RouteManager() {
|
||||
searching = false;
|
||||
state =State.IDLE;
|
||||
}
|
||||
|
||||
private static TreeMap<Integer, List<Route>> availableRoutes(Context context, HashSet<Route> visitedRoutes) {
|
||||
@@ -152,6 +157,38 @@ public class RouteManager extends BaseClass {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean isActive() {
|
||||
return state != State.IDLE;
|
||||
}
|
||||
|
||||
public boolean isSearching() {
|
||||
return state == State.SEARCHING;
|
||||
}
|
||||
|
||||
public boolean prepareRoute() {
|
||||
try {
|
||||
if (isNull(ctx) || ctx.invalidated()) return false;
|
||||
state = State.SEARCHING;
|
||||
Route route = chooseRoute(ctx);
|
||||
if (isNull(route)) return false;
|
||||
ctx.route(route);
|
||||
if (!route.reserveFor(ctx)) {
|
||||
route.reset();
|
||||
return false;
|
||||
}
|
||||
state = State.PREPARING;
|
||||
if (!route.prepareAndLock()) {
|
||||
route.reset();
|
||||
return false;
|
||||
}
|
||||
if (isNull(route) || isNull(callback)) return false;
|
||||
callback.routePrepared(route);
|
||||
return true;
|
||||
} finally {
|
||||
state = State.IDLE;
|
||||
}
|
||||
}
|
||||
|
||||
public void quit() {
|
||||
LOG.debug("{}.quit", this);
|
||||
@@ -159,49 +196,25 @@ public class RouteManager extends BaseClass {
|
||||
if (isSet(ctx)) ctx.invalidate();
|
||||
}
|
||||
|
||||
public Route prepareRoute(Context context) {
|
||||
if (isNull(context) || context.invalidated()) return null;
|
||||
Route route = chooseRoute(context);
|
||||
if (isNull(route)) return null;
|
||||
context.route(route);
|
||||
if (!route.reserveFor(context)) {
|
||||
route.reset();
|
||||
return null;
|
||||
}
|
||||
if (!route.prepareAndLock()) {
|
||||
route.reset();
|
||||
return null;
|
||||
}
|
||||
return route;
|
||||
}
|
||||
|
||||
public void setContext(Context context) {
|
||||
ctx = context;
|
||||
}
|
||||
|
||||
public boolean setCallback(Callback callback) {
|
||||
if (ctx.invalidated()) return false;
|
||||
this.callback = callback;
|
||||
if (searching) return false; // search already running, do not start again!
|
||||
searching = true;
|
||||
Route route = prepareRoute(ctx);
|
||||
searching = false;
|
||||
if (isNull(route) || isNull(callback)) return false;
|
||||
callback.routePrepared(route);
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean isSearching() {
|
||||
return searching;
|
||||
public void setCallback(Callback callback) {
|
||||
if (!ctx.invalidated()) this.callback = callback;
|
||||
}
|
||||
|
||||
public void start(Callback callback) {
|
||||
Thread thread = new Thread() {
|
||||
setCallback(callback);
|
||||
new Thread(Application.threadName(this)) {
|
||||
public void run() {
|
||||
setCallback(callback);
|
||||
prepareRoute();
|
||||
};
|
||||
};
|
||||
thread.setName(getClass().getSimpleName() + "(" + ctx.train() + ")");
|
||||
thread.start();
|
||||
}.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getSimpleName() + "(" + ctx.train() + ")";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user