package com.example; import akka.actor.typed.ActorRef; import akka.actor.typed.Behavior; import akka.actor.typed.javadsl.*; import java.util.ArrayList; import java.util.List; public class RelayBot extends AbstractBehavior { public interface Message {}; public record Hello(List> reihenfolge, int count) implements Message { } public record Start(List> reihenfolge) implements Message { } public static Behavior create(String name) { return Behaviors.setup(context -> new RelayBot(context, name)); } private final String name; private RelayBot(ActorContext context, String name) { super(context); this.name = name; } @Override public Receive createReceive() { return newReceiveBuilder() .onMessage(Hello.class, this::onHello) .onMessage(Start.class, this::onStart) .build(); } private Behavior onStart(Start msg) { var next_hop = msg.reihenfolge.remove(0); var new_reihenfolge = new ArrayList<>(msg.reihenfolge); new_reihenfolge.add(next_hop); next_hop.tell(new Hello(new_reihenfolge, 1)); return this; } private Behavior onHello(Hello msg) { if (msg.count > 15) { return Behaviors.stopped(); } getContext().getLog().info("I, {}, got Hello ({})", this.name, msg.count); var next_hop = msg.reihenfolge.remove(0); var new_reihenfolge = new ArrayList<>(msg.reihenfolge); new_reihenfolge.add(next_hop); next_hop.tell(new Hello(new_reihenfolge, msg.count + 1)); return this; } }