Timer fixed and no locks

This commit is contained in:
olhag 2024-05-19 13:12:44 +02:00
parent 4978d4dccf
commit ef94068bc3
17 changed files with 37 additions and 65 deletions

Binary file not shown.

View File

@ -1,3 +1,4 @@
// DeliveryCar.java
package com.example;
import akka.actor.typed.ActorRef;
@ -50,74 +51,61 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
private Behavior<Message> onLoadMessage(LoadMessage msg) {
pakete.addAll(msg.pakete);
scheduleNextStopCheck();
getContext().getLog().info("Geladen {} Pakete in den LKW.", msg.pakete.size());
scheduleNextStopCheck(Duration.ofSeconds(3)); // Start checking the first stop after 3 seconds
return this;
}
private Behavior<Message> onPickupResponse(PickupResponse rsp) {
if (rsp.paket == null || rsp.paket.inhalt == null) {
scheduleNextStopCheck();
} else {
if (pakete.size() < maxCapacity) {
pakete.add(rsp.paket);
getContext().getLog().info("Current number of packages in the truck: {}", pakete.size());
}
scheduleNextStopCheck();
if (rsp.paket != null && rsp.paket.inhalt != null && pakete.size() < maxCapacity) {
pakete.add(rsp.paket);
getContext().getLog().info("Ein Paket abgeholt. Aktuelle Anzahl der Pakete im LKW: {}", pakete.size());
}
scheduleNextStopCheck(Duration.ofSeconds(1)); // Move to the next stop after 1 second
return this;
}
private Behavior<Message> onCheckStop(CheckStop stop) {
if (globalIndex >= route.size()) {
getContext().getLog().info("Car is returning to the distribution center.");
getContext().getLog().info("Der LKW kehrt zum Verteilzentrum zurück.");
DistributionCenter.ArriveMessage antwort = new DistributionCenter.ArriveMessage(pakete, getContext().getSelf());
getDistributionCenter().tell(antwort);
pakete.clear();
globalIndex = 0;
} else {
getContext().getLog().info("I am at {}", route.get(globalIndex).path().name());
if (pakete.size() >= maxCapacity) {
deliverPackages();
} else {
sendPickupMessage();
ActorRef<Customer.Message> currentStop = route.get(globalIndex);
getContext().getLog().info("Ich bin bei {}", currentStop.path().name());
deliverPackages(currentStop);
if (pakete.size() < maxCapacity) {
sendPickupMessage(currentStop);
}
globalIndex++;
scheduleNextStopCheck(pakete.size() >= maxCapacity ? Duration.ofSeconds(1) : Duration.ofSeconds(3));
}
scheduleNextStopCheck();
return this;
}
private Behavior<Message> onStartRoute(StartRoute msg) {
globalIndex = 0;
scheduleNextStopCheck();
scheduleNextStopCheck(Duration.ofSeconds(3)); // Start checking the first stop after 3 seconds
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"));
} else {
getContext().getLog().info("No more customers in the route.");
}
private void sendPickupMessage(ActorRef<Customer.Message> customer) {
customer.tell(new Customer.PickUpMessage(getContext().getSelf(), "LKW kann Paket abholen"));
}
private void deliverPackages() {
if (globalIndex < route.size()) {
ActorRef<Customer.Message> nextCustomer = route.get(globalIndex);
for (int i = 0; i < pakete.size(); i++) {
Paket paket = pakete.get(i);
if (paket.empfaenger == nextCustomer) {
nextCustomer.tell(new Customer.DeliveryMessage(paket));
pakete.remove(i);
i--; // Adjust index since an element is removed
}
private void deliverPackages(ActorRef<Customer.Message> customer) {
pakete.removeIf(paket -> {
if (paket.empfaenger.equals(customer)) {
customer.tell(new Customer.DeliveryMessage(paket));
return true;
}
}
return false;
});
}
private void scheduleNextStopCheck() {
timers.startSingleTimer(new CheckStop(), Duration.ofSeconds(3));
private void scheduleNextStopCheck(Duration duration) {
timers.startSingleTimer(new CheckStop(), duration);
}
}

View File

@ -1,3 +1,4 @@
// DistributionCenter.java
package com.example;
import akka.actor.typed.ActorRef;
@ -19,7 +20,6 @@ public class DistributionCenter extends AbstractBehavior<DistributionCenter.Mess
private final List<Paket> lagerraum = new ArrayList<>();
private final List<ActorRef<DeliveryCar.Message>> deliveryCars = new ArrayList<>();
private DistributionCenter(ActorContext<Message> context, ActorRef<AddressBook.Message> addressBook) {
super(context);
this.addressBook = addressBook;
@ -65,8 +65,6 @@ public class DistributionCenter extends AbstractBehavior<DistributionCenter.Mess
}
}
public static class GenerateRoutes implements Message {
private final List<ActorRef<Customer.Message>> customers;
@ -92,14 +90,7 @@ public class DistributionCenter extends AbstractBehavior<DistributionCenter.Mess
// Füge alle Pakete aus der Ankunftsnachricht dem Lagerraum hinzu
List<Paket> arrivedPackages = msg.getPakete();
lagerraum.addAll(arrivedPackages);
String arrivedPackagesString = "";
for (int i = 0; i < arrivedPackages.size(); i++) {
arrivedPackagesString += arrivedPackages.get(i).inhalt;
if (i < arrivedPackages.size() - 1) {
arrivedPackagesString += ", "; // Fügen Sie ein Trennzeichen hinzu, außer für das letzte Paket
}
}
getContext().getLog().info("Angekommene Pakete: {} Anz Packeten in Lager: {}", arrivedPackagesString, lagerraum.size());
getContext().getLog().info("Angekommene Pakete: {} Anz Packeten in Lager: {}", arrivedPackages.size(), lagerraum.size());
// Entferne zufällig 3 Pakete aus dem Lagerraum und sende sie dem angekommenen Wagen
int numPackagesToSend = Math.min(3, lagerraum.size());
@ -115,8 +106,6 @@ public class DistributionCenter extends AbstractBehavior<DistributionCenter.Mess
return this;
}
private Behavior<Message> onGenerateRoutes(GenerateRoutes msg) {
List<ActorRef<Customer.Message>> customers = msg.getCustomers();
generateRoutes(customers);
@ -124,32 +113,28 @@ public class DistributionCenter extends AbstractBehavior<DistributionCenter.Mess
}
private void generateRoutes(List<ActorRef<Customer.Message>> customers) {
// Überprüfen, ob die Kundenliste null oder leer ist
if (customers == null || customers.isEmpty()) {
getContext().getLog().info("Kundenliste ist leer oder null. Keine Routen generiert.");
return;
}
// Erstelle vier Lastwagen mit zufälligen Routen
for (int i = 0; i < 2; i++) {
/*List<ActorRef<Customer.Message>> route = generateRandomRoute(customers);
getContext().spawn(DeliveryCar.create(route), "deliveryCar" + i+1);
getContext().getLog().info("Lieferwagen {} erstellt mit Route: {}", i+1, route.toString());*/
for (int i = 0; i < 4; i++) { // Erstelle vier Lieferwagen
List<ActorRef<Customer.Message>> route = generateRandomRoute(customers);
ActorRef<DeliveryCar.Message> deliveryCar = getContext().spawn(DeliveryCar.create(route), "deliveryCar" + (i + 1));
deliveryCars.add(deliveryCar);
String RouteStr = "";
StringBuilder routeStr = new StringBuilder();
for (int j = 0; j < route.size(); j++) {
RouteStr += route.get(j).path().name();
routeStr.append(route.get(j).path().name());
if (j < route.size() - 1) {
RouteStr += ", "; // Fügen Sie ein Trennzeichen hinzu, außer für das letzte Paket
routeStr.append(", ");
}
}
getContext().getLog().info("Lieferwagen {} erstellt mit Route: {}", i + 1, RouteStr);
getContext().getLog().info("Lieferwagen {} erstellt mit Route: {}", i + 1, routeStr);
deliveryCar.tell(new DeliveryCar.StartRoute());
}
}
private Behavior<Message> onStartAllRoutes(StartAllRoutes msg) {
for (ActorRef<DeliveryCar.Message> deliveryCar : deliveryCars) {
deliveryCar.tell(new DeliveryCar.StartRoute());
@ -157,10 +142,9 @@ public class DistributionCenter extends AbstractBehavior<DistributionCenter.Mess
return this;
}
// Dummy-Methode zum Generieren einer zufälligen Route
private List<ActorRef<Customer.Message>> generateRandomRoute(List<ActorRef<Customer.Message>> customers) {
List<ActorRef<Customer.Message>> route = new ArrayList<>(customers);
Collections.shuffle(route);
return route;
}
}
}