Erster Ansatz für das Projekt

This commit is contained in:
Egor Rutkowski 2024-05-15 01:29:12 +02:00
parent 3672e114ed
commit fc6f611458
5 changed files with 155 additions and 4 deletions

View File

@ -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<AddressBook.Message> {
ArrayList<ActorRef<Customer.Message>> customers;
public interface Message {}
public record CustomerMessage(ActorRef<Customer.Message> fragender, String nameFragender) implements Message{}
public record Response(ActorRef<Customer.Message> addresse) implements Message{}
@Override
public Receive<AddressBook.Message> createReceive() {
return newReceiveBuilder()
.onMessage(CustomerMessage.class, this::onCustomerMessage)
.build();
}
public static Behavior<AddressBook.Message> create(ArrayList<ActorRef<Customer.Message>> custo){
return Behaviors.setup(context -> new AddressBook(context, custo));
}
private AddressBook(ActorContext<AddressBook.Message> context, ArrayList<ActorRef<Customer.Message>> actors){
super(context);
customers = actors;
}
//Methode, um einen zufälligen Empfänger auszugeben (wahrscheinlich das Senden am Ende noch nicht ganz richtig)
public Behavior<Message> 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;
}
}

View File

@ -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<AkkaMainSystem.Create> {
public static class Create {
@ -24,11 +27,13 @@ public class AkkaMainSystem extends AbstractBehavior<AkkaMainSystem.Create> {
private Behavior<Create> onCreate(Create command) {
//#create-actors
ActorRef<ExampleActor.Message> a = this.getContext().spawn(ExampleActor.create("Alice"), "alice");
ActorRef<ExampleTimerActor.Message> b = this.getContext().spawn(ExampleTimerActor.create(), "timeractor");
ActorRef<Customer.Message> a = this.getContext().spawn(Customer.create("Alice"), "alice");
ActorRef<Customer.Message> b = this.getContext().spawn(Customer.create("Bob"), "bob");
ActorRef<Customer.Message> c = this.getContext().spawn(Customer.create("Charlie"), "charlie");
ActorRef<ExampleTimerActor.Message> timerActor = this.getContext().spawn(ExampleTimerActor.create(), "timeractor");
//#create-actors
a.tell(new ExampleActor.ExampleMessage(this.getContext().getSelf(),"Test123"));
ArrayList<ActorRef<Customer.Message>> actors = new ArrayList<>(Arrays.asList(a,b,c));
ActorRef<AddressBook.Message> adressBook = this.getContext().spawn(AddressBook.create(actors), "addressbook");
return this;
}
}

View File

@ -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<Customer.Message> {
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<AkkaMainSystem.Create> truckReference, String someString) implements Message {}
private Customer(ActorContext<Message> context, String name){
super(context);
this.name = name;
}
public static Behavior<Customer.Message> create(String name) {
return Behaviors.setup(context -> new Customer(context, name));
}
//Verarbeiten von den DeliveryMessage und PickUpMessage Nachrichten
@Override
public Receive<Message> createReceive() {
return newReceiveBuilder()
.onMessage(DeliveryMessage.class, this::onDeliveryMessage)
.onMessage(PickUpMessage.class, this::onPickUpMessage)
.build();
}
//Verhalten, wenn Pickup Nachricht eingegangen ist
private Behavior<Message> 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<Message> onDeliveryMessage(DeliveryMessage pkt){
getContext().getLog().info("Ich habe ein Paket von {} erhalten mit dem Inhalt: {}",pkt.paket.inhalt, pkt.paket.absender);
return this;
}
}

View File

@ -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<DeliveryCar.Message> {
ArrayList<Paket> pakete;
ArrayList<ActorRef<Customer.Message>> route;
public record LoadMessage(ArrayList<Paket> pakete) implements Message{}
public interface Message {}
@Override
public Receive<Message> createReceive() {
return newReceiveBuilder().build();
}
private DeliveryCar(ActorContext<DeliveryCar.Message> context,ArrayList<ActorRef<Customer.Message>> route){
super(context);
this.route = route;
}
}

View File

@ -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;
}
}