Timer and changes in onCheckStop,onPickupResponse
This commit is contained in:
parent
6733ca8768
commit
2a60e47cc5
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -2,11 +2,9 @@ package com.example;
|
|||
|
||||
import akka.actor.typed.ActorRef;
|
||||
import akka.actor.typed.Behavior;
|
||||
import akka.actor.typed.javadsl.AbstractBehavior;
|
||||
import akka.actor.typed.javadsl.ActorContext;
|
||||
import akka.actor.typed.javadsl.Behaviors;
|
||||
import akka.actor.typed.javadsl.Receive;
|
||||
import akka.actor.typed.javadsl.*;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -17,6 +15,7 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
|
|||
private final ArrayList<Paket> pakete = new ArrayList<>();
|
||||
private final ArrayList<ActorRef<Customer.Message>> route;
|
||||
private final int maxCapacity = 3;
|
||||
private final TimerScheduler<Message> timers;
|
||||
private int globalIndex = 0;
|
||||
|
||||
public interface Message {}
|
||||
|
@ -27,13 +26,16 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
|
|||
|
||||
public record StartRoute() implements Message {}
|
||||
|
||||
private DeliveryCar(ActorContext<Message> context, ArrayList<ActorRef<Customer.Message>> route) {
|
||||
public record CheckStop() implements Message {}
|
||||
|
||||
private DeliveryCar(ActorContext<Message> context, TimerScheduler<Message> timers, ArrayList<ActorRef<Customer.Message>> route) {
|
||||
super(context);
|
||||
this.route = route;
|
||||
this.timers = timers;
|
||||
}
|
||||
|
||||
public static Behavior<Message> create(List<ActorRef<Customer.Message>> route) {
|
||||
return Behaviors.setup(context -> new DeliveryCar(context, new ArrayList<>(route)));
|
||||
return Behaviors.setup(context -> Behaviors.withTimers(timers -> new DeliveryCar(context, timers, new ArrayList<>(route))));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -42,62 +44,62 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
|
|||
.onMessage(PickupResponse.class, this::onPickupResponse)
|
||||
.onMessage(LoadMessage.class, this::onLoadMessage)
|
||||
.onMessage(StartRoute.class, this::onStartRoute)
|
||||
.onMessage(CheckStop.class, this::onCheckStop)
|
||||
.build();
|
||||
}
|
||||
|
||||
private Behavior<Message> onLoadMessage(LoadMessage msg) {
|
||||
pakete.addAll(msg.pakete);
|
||||
// Nach dem Laden der Pakete zum nächsten Kunden fahren
|
||||
sendPickupMessage();
|
||||
scheduleNextStopCheck();
|
||||
return this;
|
||||
}
|
||||
|
||||
private Behavior<Message> onPickupResponse(PickupResponse rsp) {
|
||||
if (rsp.paket == null || rsp.paket.inhalt == null) {
|
||||
globalIndex++;
|
||||
checkNextStop();
|
||||
scheduleNextStopCheck();
|
||||
} else {
|
||||
if (pakete.size() < maxCapacity) {
|
||||
pakete.add(rsp.paket);
|
||||
getContext().getLog().info("Aktuelle Anzahl der Pakete im Truck: {}", pakete.size());
|
||||
getContext().getLog().info("Current number of packages in the truck: {}", pakete.size());
|
||||
}
|
||||
globalIndex++;
|
||||
checkNextStop();
|
||||
scheduleNextStopCheck();
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
private void checkNextStop() {
|
||||
getContext().getLog().info("globalIndex {}", globalIndex);
|
||||
|
||||
private Behavior<Message> onCheckStop(CheckStop stop) {
|
||||
if (globalIndex >= route.size()) {
|
||||
getContext().getLog().info("Wagen kehrt zum Verteilzentrum zurück.");
|
||||
getContext().getLog().info("Car is returning to the distribution center.");
|
||||
DistributionCenter.ArriveMessage antwort = new DistributionCenter.ArriveMessage(pakete, getContext().getSelf());
|
||||
getDistributionCenter().tell(antwort);
|
||||
// Setze globalIndex auf 0, damit der Wagen weitermachen kann
|
||||
globalIndex = 0;
|
||||
return;
|
||||
} else {
|
||||
getContext().getLog().info("ich bin bei {}", route.get(globalIndex).path().name());
|
||||
getContext().getLog().info("I am at {}", route.get(globalIndex).path().name());
|
||||
if (pakete.size() >= maxCapacity) {
|
||||
deliverPackages();
|
||||
} else {
|
||||
sendPickupMessage();
|
||||
}
|
||||
globalIndex++;
|
||||
}
|
||||
scheduleNextStopCheck();
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
private Behavior<Message> onStartRoute(StartRoute msg) {
|
||||
globalIndex = 0;
|
||||
checkNextStop();
|
||||
scheduleNextStopCheck();
|
||||
return this;
|
||||
}
|
||||
|
||||
private void sendPickupMessage() {
|
||||
if (globalIndex < route.size()) {
|
||||
ActorRef<Customer.Message> nextCustomer = route.get(globalIndex);
|
||||
nextCustomer.tell(new Customer.PickUpMessage(getContext().getSelf(), "Wagen kann Paket aufnehmen"));
|
||||
nextCustomer.tell(new Customer.PickUpMessage(getContext().getSelf(), "Car can pick up package"));
|
||||
} else {
|
||||
getContext().getLog().info("Keine weiteren Kunden in der Route.");
|
||||
getContext().getLog().info("No more customers in the route.");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -109,9 +111,13 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
|
|||
if (paket.empfaenger == nextCustomer) {
|
||||
nextCustomer.tell(new Customer.DeliveryMessage(paket));
|
||||
pakete.remove(i);
|
||||
i--; // Index anpassen, da ein Element entfernt wurde
|
||||
i--; // Adjust index since an element is removed
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void scheduleNextStopCheck() {
|
||||
timers.startSingleTimer(new CheckStop(), Duration.ofSeconds(3));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue