Wash, WashingMessage updates and ActorThread poll method

This commit is contained in:
Imbus 2024-10-17 01:50:25 +02:00
parent 149b952fe5
commit f3a839e4d0
3 changed files with 67 additions and 6 deletions

View file

@ -2,6 +2,7 @@ package actor;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.Optional;
public class ActorThread<M> extends Thread { public class ActorThread<M> extends Thread {
@ -23,4 +24,8 @@ public class ActorThread<M> extends Thread {
protected M receiveWithTimeout(long timeout) throws InterruptedException { protected M receiveWithTimeout(long timeout) throws InterruptedException {
return q.poll(timeout, TimeUnit.MILLISECONDS); return q.poll(timeout, TimeUnit.MILLISECONDS);
} }
protected Optional<M> poll(long timeOut) throws InterruptedException {
return Optional.ofNullable(q.poll(timeOut, TimeUnit.MILLISECONDS));
}
} }

View file

@ -19,13 +19,34 @@ public class Wash {
water.start(); water.start();
spin.start(); spin.start();
while (true) { ActorThread<WashingMessage> t = null;
outer: while (true) {
int n = io.awaitButton(); int n = io.awaitButton();
System.out.println("user selected program " + n); System.out.println("user selected program " + n);
// TODO:
// if the user presses buttons 1-3, start a washing program // 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 // 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();
} }
}; };

View file

@ -8,11 +8,8 @@ import actor.ActorThread;
* - from washing programs to temperature controller (TEMP_xxx) * - from washing programs to temperature controller (TEMP_xxx)
* - from washing programs to water controller (WATER_xxx) * - from washing programs to water controller (WATER_xxx)
* - from controllers to washing programs (ACKNOWLEDGMENT) * - 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<WashingMessage> sender, Order order) { public final class WashingMessage {
// possible values for the 'order' attribute // possible values for the 'order' attribute
public enum Order { public enum Order {
@ -27,4 +24,42 @@ public record WashingMessage(ActorThread<WashingMessage> sender, Order order) {
WATER_DRAIN, WATER_DRAIN,
ACKNOWLEDGMENT ACKNOWLEDGMENT
} }
// Who sent the message
private final ActorThread<WashingMessage> 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<WashingMessage> sender, Order order) {
this.sender = sender;
this.order = order;
}
// Extract the sender from the message
public ActorThread<WashingMessage> 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<WashingMessage> sender) {
this.sender().send(new WashingMessage(sender, Order.ACKNOWLEDGMENT));
}
} }