From 6613fc461c972118f74a4beac20ed27d28e2e4fd Mon Sep 17 00:00:00 2001 From: Christoph Stahl Date: Wed, 10 May 2023 16:21:50 +0200 Subject: [PATCH] GCD --- src/main/java/com/example/AkkaMainSystem.java | 5 ++- src/main/java/com/example/C.java | 34 ++++++++++++++++ src/main/java/com/example/ExampleActor.java | 35 ----------------- src/main/java/com/example/GCD.java | 39 +++++++++++++++++++ 4 files changed, 76 insertions(+), 37 deletions(-) create mode 100644 src/main/java/com/example/C.java delete mode 100644 src/main/java/com/example/ExampleActor.java create mode 100644 src/main/java/com/example/GCD.java diff --git a/src/main/java/com/example/AkkaMainSystem.java b/src/main/java/com/example/AkkaMainSystem.java index 7ddda70..4489d88 100644 --- a/src/main/java/com/example/AkkaMainSystem.java +++ b/src/main/java/com/example/AkkaMainSystem.java @@ -24,10 +24,11 @@ public class AkkaMainSystem extends AbstractBehavior { private Behavior onCreate(Create command) { //#create-actors - ActorRef a = this.getContext().spawn(ExampleActor.create("Alice"), "alice"); + ActorRef c = this.getContext().spawn(C.create(), "c"); + ActorRef gcd = this.getContext().spawn(GCD.create(), "gcd"); //#create-actors - a.tell(new ExampleActor.ExampleMessage(this.getContext().getSelf(),"Test123")); + gcd.tell(new GCD.GCDMessage(127, 74, c)); return this; } } diff --git a/src/main/java/com/example/C.java b/src/main/java/com/example/C.java new file mode 100644 index 0000000..347ca95 --- /dev/null +++ b/src/main/java/com/example/C.java @@ -0,0 +1,34 @@ +package com.example; + +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; + +public class C extends AbstractBehavior { + + public interface Message {}; + + public record CMessage(int i) implements Message { } + + public static Behavior create() { + return Behaviors.setup(C::new); + } + + private C(ActorContext context) { + super(context); + } + + @Override + public Receive createReceive() { + return newReceiveBuilder() + .onMessage(CMessage.class, this::onCMessage) + .build(); + } + + private Behavior onCMessage(CMessage msg) { + getContext().getLog().info("{}", msg.i); + return this; + } +} diff --git a/src/main/java/com/example/ExampleActor.java b/src/main/java/com/example/ExampleActor.java deleted file mode 100644 index cf56713..0000000 --- a/src/main/java/com/example/ExampleActor.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.example; - -import akka.actor.typed.ActorRef; -import akka.actor.typed.Behavior; -import akka.actor.typed.javadsl.*; - -public class ExampleActor extends AbstractBehavior { - - public interface Message {}; - - public record ExampleMessage(ActorRef someReference, String someString) implements Message { } - - public static Behavior create(String name) { - return Behaviors.setup(context -> new ExampleActor(context, name)); - } - - private final String name; - - private ExampleActor(ActorContext context, String name) { - super(context); - this.name = name; - } - - @Override - public Receive createReceive() { - return newReceiveBuilder() - .onMessage(ExampleMessage.class, this::onExampleMessage) - .build(); - } - - private Behavior onExampleMessage(ExampleMessage msg) { - getContext().getLog().info("I ({}) got a message: ExampleMessage({},{})", this.name, msg.someReference, msg.someString); - return this; - } -} diff --git a/src/main/java/com/example/GCD.java b/src/main/java/com/example/GCD.java new file mode 100644 index 0000000..94ae707 --- /dev/null +++ b/src/main/java/com/example/GCD.java @@ -0,0 +1,39 @@ +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; + +public class GCD extends AbstractBehavior { + + public interface Message {}; + + public record GCDMessage(int a, int b, ActorRef cust) implements Message { } + + public static Behavior create() { + return Behaviors.setup(GCD::new); + } + + private GCD(ActorContext context) { + super(context); + } + + @Override + public Receive createReceive() { + return newReceiveBuilder() + .onMessage(GCDMessage.class, this::onGCDMessage) + .build(); + } + + private Behavior onGCDMessage(GCDMessage msg) { + if(msg.b == 0) { + msg.cust.tell(new C.CMessage(msg.a)); + } else { + this.getContext().getSelf().tell(new GCDMessage(msg.b, msg.a % msg.b, msg.cust)); + } + return this; + } +}