Compare commits

..

No commits in common. "4978d4dccfbae559dc92d375fee0afb43c0a4282" and "6733ca87686faaced903d10119791f9ea618222b" have entirely different histories.

20 changed files with 24 additions and 30 deletions

Binary file not shown.

View File

@ -74,4 +74,4 @@ public class AddressBook extends AbstractBehavior<AddressBook.Message> {
msg.replyTo.tell(Collections.unmodifiableList(customers));
return this;
}
}
}

View File

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