diff options
-rw-r--r-- | Week14 Route 66/src/OO14route66/Model.java | 102 | ||||
-rw-r--r-- | Week14 Route 66/src/com/camilstaps/route66/Crossing.java | 12 |
2 files changed, 57 insertions, 57 deletions
diff --git a/Week14 Route 66/src/OO14route66/Model.java b/Week14 Route 66/src/OO14route66/Model.java index f4e725f..ee9da42 100644 --- a/Week14 Route 66/src/OO14route66/Model.java +++ b/Week14 Route 66/src/OO14route66/Model.java @@ -1,10 +1,7 @@ package OO14route66; -import com.camilstaps.route66.Crossing; import java.util.ArrayList; import java.util.Observable; -import java.util.logging.Level; -import java.util.logging.Logger; import javax.swing.JFrame; /** @@ -21,7 +18,12 @@ public class Model extends Observable NUMBEROFCARS = 5 * DIRECTIONS; // total number of cars in system private final ArrayList<JFrame> views; - private final Crossing crossing = new Crossing(this, Direction.North); + //private final Crossing crossing = new Crossing(this, Direction.North); + + private Direction allowed; + private boolean crossingWaiting = false; + private long lastCrossingChange = 0; + private static final int MIN_CROSS_TIME = 1000; /** * Constructor: create all cars @@ -75,62 +77,60 @@ public class Model extends Observable public synchronized boolean isSafeLocation(Car car, int requested_location) { Car that_car = cars[car.getNumber() < DIRECTIONS ? car.getNumber() + NUMBEROFCARS - DIRECTIONS : car.getNumber() - DIRECTIONS]; - boolean ok; - - System.err.println("Car " + car.getNumber() + " requests safety."); - - do { - ok = !(that_car.getLocation() > requested_location && - that_car.getLocation() < requested_location + Car.CARLENGTH + Car.MINCARSPACE); + boolean ok = !(that_car.getLocation() > requested_location && + that_car.getLocation() < requested_location + Car.CARLENGTH + Car.MINCARSPACE); -// if (!ok && that_car.getDriver().getWaitingToCross()) { -// crossing.doRequest(); -// } - - if (car.isInFrontOfCrossing() && !crossing.isAllowed(car.getDirection())) { - System.err.println("Requesting switch for car " + car.getNumber()); - ok = false; - //if (!car.getDriver().getWaitingToCross()) { - //car.getDriver().setWaitingToCross(true); - ok = crossing.doRequest(); - //car.getDriver().setWaitingToCross(!ok); - //} - } - - if (!ok) { - System.err.println("Car " + car.getNumber() + " waiting."); - try { - wait(); - } catch (InterruptedException ex) { - } - System.err.println("Car " + car.getNumber() + " acquired lock."); + if (car.isInFrontOfCrossing() && !isCrossingAllowed(car.getDirection())) { + ok = doCrossingRequest(); + } + + if (!ok) { + try { + wait(); + } catch (InterruptedException ex) { } - } while (!ok); - - System.err.println("Car " + car.getNumber() + " has safe location."); + } return ok; } - public synchronized boolean isNoCarsOnCrossing() { - boolean ok = false; - while (!ok) { - ok = true; - for (Car car : cars) { - if (car.isOnCrossing()) { - System.err.println("Car " + car.getNumber() + " is on crossing."); - ok = false; - } + public synchronized boolean isCarsOnCrossing() { + for (Car car : cars) { + if (car.isOnCrossing()) { + return true; } - if (!ok) { - System.err.println("Checking crossing waiting"); - try { - wait(); - } catch (InterruptedException ex) { - } - System.err.println("Checking crossing"); + } + return false; + } + + public synchronized void doSwitchCrossing() { + crossingWaiting = true; + while (isCarsOnCrossing()) { + try { + wait(); + } catch (InterruptedException ex) { } } + System.err.println("Switching"); + if (allowed == Direction.East || allowed == Direction.West) { + allowed = Direction.North; + } else { + allowed = Direction.East; + } + System.err.println("Switched to " + allowed); + crossingWaiting = false; + lastCrossingChange = System.currentTimeMillis(); + } + + public synchronized boolean isCrossingAllowed(Direction direction) { + return !crossingWaiting && (direction == allowed || Direction.opposite(direction) == allowed); + } + + public synchronized boolean doCrossingRequest() { + if (crossingWaiting || System.currentTimeMillis() - lastCrossingChange < MIN_CROSS_TIME) { + return false; + } + doSwitchCrossing(); return true; } } diff --git a/Week14 Route 66/src/com/camilstaps/route66/Crossing.java b/Week14 Route 66/src/com/camilstaps/route66/Crossing.java index eba5bfe..65e2527 100644 --- a/Week14 Route 66/src/com/camilstaps/route66/Crossing.java +++ b/Week14 Route 66/src/com/camilstaps/route66/Crossing.java @@ -16,7 +16,7 @@ public class Crossing { private final Model model; private Direction allowed; - private boolean waiting = false; + private boolean crossingWaiting = false; private int requests = 0; public Crossing(Model model, Direction allowed) { @@ -29,8 +29,8 @@ public class Crossing { } public synchronized void doSwitch() { - waiting = true; - while (!model.isNoCarsOnCrossing()); + crossingWaiting = true; + while (!model.isCarsOnCrossing()); System.err.println("Switching"); if (allowed == Direction.East || allowed == Direction.West) { allowed = Direction.North; @@ -39,17 +39,17 @@ public class Crossing { } requests = 0; System.err.println("Switched to " + allowed); - waiting = false; + crossingWaiting = false; //notifyAll(); } public boolean isAllowed(Direction direction) { - return !waiting && (direction == allowed || Direction.opposite(direction) == allowed); + return !crossingWaiting && (direction == allowed || Direction.opposite(direction) == allowed); } public boolean doRequest() { //if (++requests >= 2) { - if (waiting) { + if (crossingWaiting) { return false; } doSwitch(); |