From fc6f611458c828298a3b9fc00dac75a5c0181648 Mon Sep 17 00:00:00 2001 From: Egor Rutkowski Date: Wed, 15 May 2024 01:29:12 +0200 Subject: [PATCH] =?UTF-8?q?Erster=20Ansatz=20f=C3=BCr=20das=20Projekt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/example/AddressBook.java | 44 ++++++++++++++ src/main/java/com/example/AkkaMainSystem.java | 13 ++-- src/main/java/com/example/Customer.java | 60 +++++++++++++++++++ src/main/java/com/example/DeliveryCar.java | 29 +++++++++ src/main/java/com/example/Paket.java | 13 ++++ 5 files changed, 155 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/example/AddressBook.java create mode 100644 src/main/java/com/example/Customer.java create mode 100644 src/main/java/com/example/DeliveryCar.java create mode 100644 src/main/java/com/example/Paket.java diff --git a/src/main/java/com/example/AddressBook.java b/src/main/java/com/example/AddressBook.java new file mode 100644 index 0000000..8f18efd --- /dev/null +++ b/src/main/java/com/example/AddressBook.java @@ -0,0 +1,44 @@ +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 java.util.ArrayList; +import java.util.Collections; + + +public class AddressBook extends AbstractBehavior { + ArrayList> customers; + public interface Message {} + public record CustomerMessage(ActorRef fragender, String nameFragender) implements Message{} + public record Response(ActorRef addresse) implements Message{} + + @Override + public Receive createReceive() { + return newReceiveBuilder() + .onMessage(CustomerMessage.class, this::onCustomerMessage) + .build(); + } + + public static Behavior create(ArrayList> custo){ + return Behaviors.setup(context -> new AddressBook(context, custo)); + } + + private AddressBook(ActorContext context, ArrayList> actors){ + super(context); + customers = actors; + } + + //Methode, um einen zufälligen Empfänger auszugeben (wahrscheinlich das Senden am Ende noch nicht ganz richtig) + public Behavior onCustomerMessage(CustomerMessage msg){ + Collections.shuffle(customers); + var adressOf = customers.get(0); + getContext().getLog().info("{} asked for an adress",msg.nameFragender); + msg.fragender().tell((Customer.Message) new Response(adressOf).addresse); + return this; + } +} diff --git a/src/main/java/com/example/AkkaMainSystem.java b/src/main/java/com/example/AkkaMainSystem.java index 868351b..e2e7289 100644 --- a/src/main/java/com/example/AkkaMainSystem.java +++ b/src/main/java/com/example/AkkaMainSystem.java @@ -4,6 +4,9 @@ import akka.actor.typed.ActorRef; import akka.actor.typed.Behavior; import akka.actor.typed.javadsl.*; +import java.util.ArrayList; +import java.util.Arrays; + public class AkkaMainSystem extends AbstractBehavior { public static class Create { @@ -24,11 +27,13 @@ public class AkkaMainSystem extends AbstractBehavior { private Behavior onCreate(Create command) { //#create-actors - ActorRef a = this.getContext().spawn(ExampleActor.create("Alice"), "alice"); - ActorRef b = this.getContext().spawn(ExampleTimerActor.create(), "timeractor"); + ActorRef a = this.getContext().spawn(Customer.create("Alice"), "alice"); + ActorRef b = this.getContext().spawn(Customer.create("Bob"), "bob"); + ActorRef c = this.getContext().spawn(Customer.create("Charlie"), "charlie"); + ActorRef timerActor = this.getContext().spawn(ExampleTimerActor.create(), "timeractor"); //#create-actors - - a.tell(new ExampleActor.ExampleMessage(this.getContext().getSelf(),"Test123")); + ArrayList> actors = new ArrayList<>(Arrays.asList(a,b,c)); + ActorRef adressBook = this.getContext().spawn(AddressBook.create(actors), "addressbook"); return this; } } diff --git a/src/main/java/com/example/Customer.java b/src/main/java/com/example/Customer.java new file mode 100644 index 0000000..25d7679 --- /dev/null +++ b/src/main/java/com/example/Customer.java @@ -0,0 +1,60 @@ +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 java.util.Random; + +public class Customer extends AbstractBehavior { + + String name; + String[] gegenstaende = {"Spülmaschine","Altes CD-Regal", "Goldbarren","20kg Hanteln", "Holzkohlegrill","Blumenerde"}; + + public interface Message {} + + //Delivery Nachricht Typ + public record DeliveryMessage(Paket paket) implements Message {} + //Pickup Nachricht Typ + public record PickUpMessage(ActorRef truckReference, String someString) implements Message {} + + private Customer(ActorContext context, String name){ + super(context); + this.name = name; + } + + public static Behavior create(String name) { + return Behaviors.setup(context -> new Customer(context, name)); + } + + //Verarbeiten von den DeliveryMessage und PickUpMessage Nachrichten + @Override + public Receive createReceive() { + return newReceiveBuilder() + .onMessage(DeliveryMessage.class, this::onDeliveryMessage) + .onMessage(PickUpMessage.class, this::onPickUpMessage) + .build(); + } + + //Verhalten, wenn Pickup Nachricht eingegangen ist + private Behavior onPickUpMessage(PickUpMessage msg){ + Random random = new Random(); + int number = random.nextInt(100); + if(number <= 80){ + //TODO: Nachricht an AddressBook schicken + + }else{ + //TODO: PickupResponse an Wagen schicken + } + return this; + } + + //Verhalten, wenn Paket eingegangen ist + private Behavior onDeliveryMessage(DeliveryMessage pkt){ + getContext().getLog().info("Ich habe ein Paket von {} erhalten mit dem Inhalt: {}",pkt.paket.inhalt, pkt.paket.absender); + return this; + } +} diff --git a/src/main/java/com/example/DeliveryCar.java b/src/main/java/com/example/DeliveryCar.java new file mode 100644 index 0000000..1bd1f11 --- /dev/null +++ b/src/main/java/com/example/DeliveryCar.java @@ -0,0 +1,29 @@ +package com.example; + +import akka.actor.typed.ActorRef; +import akka.actor.typed.javadsl.AbstractBehavior; +import akka.actor.typed.javadsl.ActorContext; +import akka.actor.typed.javadsl.Receive; + +import java.util.ArrayList; + +public class DeliveryCar extends AbstractBehavior { + + ArrayList pakete; + ArrayList> route; + + public record LoadMessage(ArrayList pakete) implements Message{} + public interface Message {} + + @Override + public Receive createReceive() { + return newReceiveBuilder().build(); + } + + private DeliveryCar(ActorContext context,ArrayList> route){ + super(context); + this.route = route; + + } + +} diff --git a/src/main/java/com/example/Paket.java b/src/main/java/com/example/Paket.java new file mode 100644 index 0000000..ea79a28 --- /dev/null +++ b/src/main/java/com/example/Paket.java @@ -0,0 +1,13 @@ +package com.example; + +public class Paket { + String inhalt; + String absender; + Customer empfaenger; + + Paket(String i, String a, Customer e){ + this.inhalt = i; + this.absender = a; + this.empfaenger = e; + } +}