diff --git a/wash/src/actor/ActorThread.java b/wash/src/actor/ActorThread.java index 9a93b3b..b7f992f 100644 --- a/wash/src/actor/ActorThread.java +++ b/wash/src/actor/ActorThread.java @@ -2,6 +2,7 @@ package actor; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; +import java.util.Optional; public class ActorThread extends Thread { @@ -23,4 +24,8 @@ public class ActorThread extends Thread { protected M receiveWithTimeout(long timeout) throws InterruptedException { return q.poll(timeout, TimeUnit.MILLISECONDS); } + + protected Optional poll(long timeOut) throws InterruptedException { + return Optional.ofNullable(q.poll(timeOut, TimeUnit.MILLISECONDS)); + } } \ No newline at end of file diff --git a/wash/src/wash/control/Wash.java b/wash/src/wash/control/Wash.java index d39d0f4..086ed44 100644 --- a/wash/src/wash/control/Wash.java +++ b/wash/src/wash/control/Wash.java @@ -19,13 +19,34 @@ public class Wash { water.start(); spin.start(); - while (true) { + ActorThread t = null; + + outer: while (true) { int n = io.awaitButton(); System.out.println("user selected program " + n); - // TODO: // if the user presses buttons 1-3, start a washing program // if the user presses button 0, and a program has been started, stop it + + switch (n) { + case 0 -> t = new WashingProgram0(io, temp, water, spin); + case 1 -> t = new WashingProgram1(io, temp, water, spin); + case 2 -> t = new WashingProgram1(io, temp, water, spin); + case 3 -> t = new WashingProgram3(io, temp, water, spin); + default -> { + System.out.println("Invalid program number"); + break outer; + } + } + + t.start(); } + + temp.interrupt(); + water.interrupt(); + spin.interrupt(); + + if (t != null) + t.interrupt(); } }; diff --git a/wash/src/wash/control/WashingMessage.java b/wash/src/wash/control/WashingMessage.java index 0148779..2fe4dd1 100644 --- a/wash/src/wash/control/WashingMessage.java +++ b/wash/src/wash/control/WashingMessage.java @@ -8,11 +8,8 @@ import actor.ActorThread; * - from washing programs to temperature controller (TEMP_xxx) * - from washing programs to water controller (WATER_xxx) * - from controllers to washing programs (ACKNOWLEDGMENT) - * - * @param sender the thread that sent the message - * @param order an order, such as SPIN_FAST or WATER_DRAIN */ -public record WashingMessage(ActorThread sender, Order order) { +public final class WashingMessage { // possible values for the 'order' attribute public enum Order { @@ -27,4 +24,42 @@ public record WashingMessage(ActorThread sender, Order order) { WATER_DRAIN, ACKNOWLEDGMENT } + + // Who sent the message + private final ActorThread sender; + + // What the message is about + private final Order order; + + /** + * @param sender the thread that sent the message + * @param order an order, such as SPIN_FAST or WATER_DRAIN + */ + public WashingMessage(ActorThread sender, Order order) { + this.sender = sender; + this.order = order; + } + + // Extract the sender from the message + public ActorThread sender() { + return sender; + } + + // Extract the order from the message + public Order order() { + return order; + } + + // Return a string representation of the message + public String toString() { + return new StringBuilder(order.toString()) + .append(" from ") + .append(sender == null ? "(null)" : sender.getClass().getSimpleName()) + .toString(); + } + + // Send acknowledgment to the sender + public void sendAck(ActorThread sender) { + this.sender().send(new WashingMessage(sender, Order.ACKNOWLEDGMENT)); + } }