working on route reservation

This commit is contained in:
Stephan Richter
2020-09-19 00:38:18 +02:00
parent cf4df1882e
commit 917ac5dd07
8 changed files with 131 additions and 40 deletions

View File

@@ -52,8 +52,8 @@ public class Application {
plan = new Plan();
}
Locomotive BR110 = new Locomotive("BR110");
Block block = new Vector<>(plan.blocks()).firstElement();
if (block != null) block.setTrain(new Train(BR110));
Block block = new Vector<>(plan.blocks()).lastElement();
if (block != null) block.train(new Train(BR110));
Desktop.getDesktop().browse(URI.create("http://localhost:"+config.getInt(PORT)+"/plan"));
}

View File

@@ -100,6 +100,7 @@ public class Plan {
private static final String ROUTE = "route";
private static final HashMap<OutputStreamWriter,Integer> clients = new HashMap<OutputStreamWriter, Integer>();
private static final String ACTION_SHOW_TRAIN = "showTrain";
private static final String ACTION_START_TRAIN = "startTrain";
private HashMap<Integer,HashMap<Integer,Tile>> tiles = new HashMap<Integer,HashMap<Integer,Tile>>();
private HashSet<Block> blocks = new HashSet<Block>();
@@ -378,7 +379,9 @@ public class Plan {
case ACTION_SAVE:
return saveTo(params.get(FILE));
case ACTION_SHOW_TRAIN:
return showTrain(get(params.get(X),params.get(Y),true));
return show(train(get(params.get(X),params.get(Y),true)));
case ACTION_START_TRAIN:
return start(train(get(params.get(X),params.get(Y),true)));
case ACTION_UPDATE:
return update(params);
default:
@@ -392,6 +395,14 @@ public class Plan {
}
}
private Train train(Tile tile) {
if (tile instanceof Block) {
Block block = (Block) tile;
return block.train();
}
return null;
}
private Object routeProperties(String routeId) {
Route route = routes.get(routeId);
if (route == null) return t("Could not find route \"{}\"",routeId);
@@ -463,19 +474,20 @@ public class Plan {
column = new HashMap<Integer, Tile>();
tiles.put(x,column);
}
column.put(y,tile.position(x, y));
tile.position(x, y).plan(this);
column.put(y,tile);
}
private Object showTrain(Tile tile) {
if (tile instanceof Block) {
Block block = (Block) tile;
Train train = block.train();
if (train != null) return train.props();
}
return null;
private Tag show(Train train) {
return (train == null) ? null : train.props();
}
private synchronized void stream(String data) {
private String start(Train train) {
if (train == null) return null;
return train.start();
}
public synchronized void stream(String data) {
data = data.replaceAll("\n", "").replaceAll("\r", "");
LOG.debug("streaming: {}",data);
Vector<OutputStreamWriter> badClients = null;

View File

@@ -14,6 +14,7 @@ import org.slf4j.LoggerFactory;
import de.keawe.tools.translations.Translation;
import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.Plan.Direction;
import de.srsoftware.web4rail.moving.Train;
import de.srsoftware.web4rail.tags.Form;
import de.srsoftware.web4rail.tiles.Block;
import de.srsoftware.web4rail.tiles.Contact;
@@ -82,6 +83,11 @@ public class Route {
}
return id;
}
public boolean inUse() {
return false;
}
public String json() {
JSONObject props = new JSONObject();
@@ -105,6 +111,10 @@ public class Route {
return props.toString();
}
public Route lock(Train train) {
for (Tile tile : path) tile.lock(train);
return this;
}
public List<Route> multiply(int size) {
Vector<Route> routes = new Vector<Route>();
@@ -203,6 +213,10 @@ public class Route {
return Translation.get(Application.class, txt, fills);
}
public void unlock() {
for (Tile tile : path) tile.unlock();
}
public void update(HashMap<String, String> params) {
LOG.debug("update({})",params);
if (params.containsKey(NAME)) name(params.get(NAME));

View File

@@ -1,16 +1,23 @@
package de.srsoftware.web4rail.moving;
import java.util.HashSet;
import java.util.Random;
import java.util.Vector;
import de.keawe.tools.translations.Translation;
import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.Application;
import de.srsoftware.web4rail.Route;
import de.srsoftware.web4rail.Window;
import de.srsoftware.web4rail.tiles.Block;
import de.srsoftware.web4rail.tiles.Tile;
public class Train {
private Vector<Locomotive> locos = new Vector<Locomotive>();
private Vector<Car> cars = new Vector<Car>();
private String name = null;
private Block block = null;
private Route route;
public Train(Locomotive loco) {
add(loco);
@@ -43,4 +50,25 @@ public class Train {
private String t(String message, Object...fills) {
return Translation.get(Application.class, message, fills);
}
public void block(Block block) {
this.block = block;
}
public String start() {
if (block == null) return t("{] not in a block",this);
HashSet<Route> routes = block.routes();
Vector<Route> availableRoutes = new Vector<Route>();
for (Route route : routes) {
Vector<Tile> path = route.path();
if (path.firstElement() != block) continue;
if (route.inUse()) continue;
availableRoutes.add(route);
}
Random rand = new Random();
if (route != null) route.unlock();
int sel = rand.nextInt(availableRoutes.size());
route = availableRoutes.get(sel).lock(this);
return t("started {}",this);
}
}

View File

@@ -73,8 +73,9 @@ public abstract class Block extends StretchableTile{
return this;
}
public void setTrain(Train train) {
public void train(Train train) {
this.train = train;
train.block(this);
}
public Train train() {

View File

@@ -17,10 +17,12 @@ import de.keawe.tools.translations.Translation;
import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.Application;
import de.srsoftware.web4rail.Connector;
import de.srsoftware.web4rail.Plan;
import de.srsoftware.web4rail.Plan.Direction;
import de.srsoftware.web4rail.tags.Form;
import de.srsoftware.web4rail.Route;
import de.srsoftware.web4rail.Window;
import de.srsoftware.web4rail.moving.Train;
public abstract class Tile {
@@ -28,6 +30,8 @@ public abstract class Tile {
protected HashSet<String> classes = new HashSet<>();
protected HashSet<Shadow> shadows = new HashSet<>();
private HashSet<Route> routes = new HashSet<>();
private Plan plan;
private Train lockedBy;
protected static Logger LOG = LoggerFactory.getLogger(Tile.class);
@@ -36,6 +40,10 @@ public abstract class Tile {
classes.add(getClass().getSimpleName());
}
public void add(Route route) {
this.routes.add(route);
}
public void addShadow(Shadow shadow) {
shadows.add(shadow);
}
@@ -59,6 +67,15 @@ public abstract class Tile {
return 1;
}
public void lock(Train train) {
lockedBy = train;
plan.stream("addclass tile-"+x+"-"+y+" locked");
}
public void plan(Plan plan) {
this.plan = plan;
}
public Tile position(int x, int y) {
this.x = x;
this.y = y;
@@ -95,6 +112,29 @@ public abstract class Tile {
return window;
}
private static String replace(String line, Entry<String, Object> replacement) {
String key = replacement.getKey();
Object val = replacement.getValue();
int start = line.indexOf(key);
int len = key.length();
while (start>0) {
int end = line.indexOf("\"",start);
int end2 = line.indexOf("<",start);
if (end2>0 && (end<0 || end2<end)) end=end2;
String tag = line.substring(start, end);
if (tag.length()>len) {
val = Integer.parseInt(tag.substring(len)) + (int) val;
}
line = line.replace(tag, ""+val);
start = line.indexOf(key);
}
return line;
}
public HashSet<Route> routes() {
return routes;
}
public Tag tag(Map<String,Object> replacements) throws IOException {
int width = 100*len();
int height = 100*height();
@@ -138,24 +178,6 @@ public abstract class Tile {
return svg;
}
private static String replace(String line, Entry<String, Object> replacement) {
String key = replacement.getKey();
Object val = replacement.getValue();
int start = line.indexOf(key);
int len = key.length();
while (start>0) {
int end = line.indexOf("\"",start);
int end2 = line.indexOf("<",start);
if (end2>0 && (end<0 || end2<end)) end=end2;
String tag = line.substring(start, end);
if (tag.length()>len) {
val = Integer.parseInt(tag.substring(len)) + (int) val;
}
line = line.replace(tag, ""+val);
start = line.indexOf(key);
}
return line;
}
protected static String t(String txt, Object...fills) {
return Translation.get(Application.class, txt, fills);
@@ -166,16 +188,13 @@ public abstract class Tile {
return t("{}({},{})",getClass().getSimpleName(),x,y) ;
}
public void unlock() {
lockedBy = null;
plan.stream("dropclass tile-"+x+"-"+y+" locked");
}
public Tile update(HashMap<String, String> params) {
LOG.debug("{}.update({})",getClass().getSimpleName(),params);
return this;
}
public HashSet<Route> routes() {
return routes;
}
public void add(Route route) {
this.routes.add(route);
}
}