Compare commits
1 Commits
Author | SHA1 | Date |
---|---|---|
Christoph Stahl | 6613fc461c |
|
@ -24,11 +24,11 @@ public class AkkaMainSystem extends AbstractBehavior<AkkaMainSystem.Create> {
|
|||
|
||||
private Behavior<Create> onCreate(Create command) {
|
||||
//#create-actors
|
||||
ActorRef<FibCont.Message> c = this.getContext().spawn(C.create(), "c");
|
||||
ActorRef<Fib.Message> fib = this.getContext().spawn(Fib.create(), "fib");
|
||||
ActorRef<C.Message> c = this.getContext().spawn(C.create(), "c");
|
||||
ActorRef<GCD.Message> gcd = this.getContext().spawn(GCD.create(), "gcd");
|
||||
//#create-actors
|
||||
|
||||
fib.tell(new Fib.FibMessage(7, c));
|
||||
gcd.tell(new GCD.GCDMessage(127, 74, c));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,25 +6,29 @@ import akka.actor.typed.javadsl.ActorContext;
|
|||
import akka.actor.typed.javadsl.Behaviors;
|
||||
import akka.actor.typed.javadsl.Receive;
|
||||
|
||||
public class C extends AbstractBehavior<FibCont.Message> {
|
||||
public static Behavior<FibCont.Message> create() {
|
||||
public class C extends AbstractBehavior<C.Message> {
|
||||
|
||||
public interface Message {};
|
||||
|
||||
public record CMessage(int i) implements Message { }
|
||||
|
||||
public static Behavior<Message> create() {
|
||||
return Behaviors.setup(C::new);
|
||||
}
|
||||
|
||||
|
||||
private C(ActorContext<FibCont.Message> context) {
|
||||
private C(ActorContext<Message> context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive<FibCont.Message> createReceive() {
|
||||
public Receive<Message> createReceive() {
|
||||
return newReceiveBuilder()
|
||||
.onMessage(FibCont.FibContMessage.class, this::onFibContMessage)
|
||||
.onMessage(CMessage.class, this::onCMessage)
|
||||
.build();
|
||||
}
|
||||
|
||||
private Behavior<FibCont.Message> onFibContMessage(FibCont.FibContMessage msg) {
|
||||
getContext().getLog().info("{}", msg.n());
|
||||
private Behavior<Message> onCMessage(CMessage msg) {
|
||||
getContext().getLog().info("{}", msg.i);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,41 +0,0 @@
|
|||
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 Fib extends AbstractBehavior<Fib.Message> {
|
||||
|
||||
public interface Message {};
|
||||
|
||||
public record FibMessage(int n, ActorRef<FibCont.Message> cust) implements Message { }
|
||||
|
||||
public static Behavior<Message> create() {
|
||||
return Behaviors.setup(Fib::new);
|
||||
}
|
||||
|
||||
private Fib(ActorContext<Message> context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive<Message> createReceive() {
|
||||
return newReceiveBuilder()
|
||||
.onMessage(FibMessage.class, this::onFibMessage)
|
||||
.build();
|
||||
}
|
||||
|
||||
private Behavior<Message> onFibMessage(FibMessage msg) {
|
||||
if(msg.n <= 1) {
|
||||
msg.cust.tell(new FibCont.FibContMessage(msg.n));
|
||||
} else {
|
||||
ActorRef<FibCont.Message> cont = this.getContext().spawnAnonymous(FibCont.create(msg.cust));
|
||||
this.getContext().getSelf().tell(new FibMessage(msg.n - 1, cont));
|
||||
this.getContext().getSelf().tell(new FibMessage(msg.n - 2, cont));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -1,48 +0,0 @@
|
|||
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 FibCont extends AbstractBehavior<FibCont.Message> {
|
||||
|
||||
private boolean flag;
|
||||
private int store;
|
||||
|
||||
public interface Message {};
|
||||
|
||||
public record FibContMessage(int n) implements Message { }
|
||||
|
||||
public static Behavior<Message> create(ActorRef<Message> cust) {
|
||||
return Behaviors.setup(context -> new FibCont(context, cust));
|
||||
}
|
||||
|
||||
private final ActorRef<Message> cust;
|
||||
|
||||
private FibCont(ActorContext<Message> context, ActorRef<Message> cust) {
|
||||
super(context);
|
||||
this.cust = cust;
|
||||
this.flag = false;
|
||||
this.store = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive<Message> createReceive() {
|
||||
return newReceiveBuilder()
|
||||
.onMessage(FibContMessage.class, this::onFibContMessage)
|
||||
.build();
|
||||
}
|
||||
|
||||
private Behavior<Message> onFibContMessage(FibContMessage msg) {
|
||||
if (this.flag) {
|
||||
this.cust.tell(new FibContMessage(this.store + msg.n));
|
||||
} else {
|
||||
this.flag = true;
|
||||
this.store = msg.n;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -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<GCD.Message> {
|
||||
|
||||
public interface Message {};
|
||||
|
||||
public record GCDMessage(int a, int b, ActorRef<C.Message> cust) implements Message { }
|
||||
|
||||
public static Behavior<Message> create() {
|
||||
return Behaviors.setup(GCD::new);
|
||||
}
|
||||
|
||||
private GCD(ActorContext<Message> context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive<Message> createReceive() {
|
||||
return newReceiveBuilder()
|
||||
.onMessage(GCDMessage.class, this::onGCDMessage)
|
||||
.build();
|
||||
}
|
||||
|
||||
private Behavior<Message> 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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue