From 52740f269712789e4eed3ce328ebcf915fbda003 Mon Sep 17 00:00:00 2001 From: David Bohn <davbohn@googlemail.com> Date: Tue, 21 Jun 2016 13:11:49 +0200 Subject: [PATCH] Implemented futures for receiving the supervisor --- src/main/java/fucoin/Main.java | 6 ++++-- .../java/fucoin/actions/join/ActionJoin.java | 2 +- .../fucoin/actions/join/ActionJoinAnswer.java | 17 ++++++++++++++--- .../fucoin/actions/join/ServerActionJoin.java | 2 +- .../java/fucoin/supervisor/SuperVisorImpl.java | 8 ++++++++ src/main/java/fucoin/wallet/AbstractWallet.java | 3 +++ src/main/java/fucoin/wallet/WalletImpl.java | 10 +++++++++- 7 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/main/java/fucoin/Main.java b/src/main/java/fucoin/Main.java index be9fc41..c8b7961 100644 --- a/src/main/java/fucoin/Main.java +++ b/src/main/java/fucoin/Main.java @@ -14,11 +14,13 @@ import java.io.File; import java.util.ArrayList; import java.util.List; +import static akka.dispatch.Futures.future; + public class Main { private static int numberOfWallets = 2; - private static boolean createGUI = false; + private static boolean createGUI = true; private static ActorSystem cSystem; @@ -67,7 +69,7 @@ public class Main { // first wallet does not have a neighbour, so it can't send a ActionJoin to anybody // instead we send directly an ActionJoinAnswer with the supervisor reference if (i == 0) { - actorRef.tell(new ActionJoinAnswer(cSuperVisorActor), cSuperVisorActor); + actorRef.tell(new ActionJoinAnswer(future(() -> cSuperVisorActor, cSystem.dispatcher())), cSuperVisorActor); } cActiveActors.add(actorRef); diff --git a/src/main/java/fucoin/actions/join/ActionJoin.java b/src/main/java/fucoin/actions/join/ActionJoin.java index e7bd814..15540eb 100644 --- a/src/main/java/fucoin/actions/join/ActionJoin.java +++ b/src/main/java/fucoin/actions/join/ActionJoin.java @@ -14,7 +14,7 @@ public class ActionJoin extends ClientAction { protected void onAction(ActorRef sender, ActorRef self, UntypedActorContext context, AbstractWallet node) { // send the joined node all known neighbours from node and a reference to the supervisor - ActionJoinAnswer aja = new ActionJoinAnswer(node.getRemoteSuperVisorActor()); + ActionJoinAnswer aja = new ActionJoinAnswer(node.resolveSuperVisorActor()); aja.someNeighbors.putAll(node.getKnownNeighbors()); sender.tell(aja, self); } diff --git a/src/main/java/fucoin/actions/join/ActionJoinAnswer.java b/src/main/java/fucoin/actions/join/ActionJoinAnswer.java index e20bbcc..dc7c9a5 100644 --- a/src/main/java/fucoin/actions/join/ActionJoinAnswer.java +++ b/src/main/java/fucoin/actions/join/ActionJoinAnswer.java @@ -2,9 +2,13 @@ package fucoin.actions.join; import akka.actor.ActorRef; import akka.actor.UntypedActorContext; +import akka.dispatch.OnSuccess; import fucoin.actions.ClientAction; import fucoin.actions.persist.ActionSearchMyWallet; import fucoin.wallet.AbstractWallet; +import scala.Function1; +import scala.concurrent.Future; +import scala.runtime.BoxedUnit; import java.util.HashMap; import java.util.Map.Entry; @@ -20,9 +24,9 @@ import java.util.Map.Entry; */ public class ActionJoinAnswer extends ClientAction { public final HashMap<String, ActorRef> someNeighbors = new HashMap<>(); - public final ActorRef supervisor; + public final Future<ActorRef> supervisor; - public ActionJoinAnswer(ActorRef supervisor) { + public ActionJoinAnswer(Future<ActorRef> supervisor) { this.supervisor = supervisor; } @@ -41,7 +45,14 @@ public class ActionJoinAnswer extends ClientAction { // register at the supervisor if the wallet just learned about it if (wallet.getRemoteSuperVisorActor() == null) { - wallet.setRemoteSuperVisorActor(supervisor); + supervisor.onSuccess(new OnSuccess<ActorRef>() { + + @Override + public void onSuccess(ActorRef result) throws Throwable { + wallet.setRemoteSuperVisorActor(result); + } + }, context.system().dispatcher()); + //wallet.setRemoteSuperVisorActor(supervisor); } } diff --git a/src/main/java/fucoin/actions/join/ServerActionJoin.java b/src/main/java/fucoin/actions/join/ServerActionJoin.java index 4c57b13..6b218b8 100644 --- a/src/main/java/fucoin/actions/join/ServerActionJoin.java +++ b/src/main/java/fucoin/actions/join/ServerActionJoin.java @@ -21,7 +21,7 @@ public class ServerActionJoin extends SuperVisorAction { @Override protected void onAction(ActorRef sender, ActorRef self, UntypedActorContext context, SuperVisorImpl node) { - ActionJoinAnswer aja = new ActionJoinAnswer(node.getSelf()); + ActionJoinAnswer aja = new ActionJoinAnswer(node.resolveSelf()); aja.someNeighbors.putAll(node.getKnownNeighbors()); sender.tell(aja, self); node.addKnownNeighbor(name, sender); diff --git a/src/main/java/fucoin/supervisor/SuperVisorImpl.java b/src/main/java/fucoin/supervisor/SuperVisorImpl.java index 2333a97..dbfcfe8 100644 --- a/src/main/java/fucoin/supervisor/SuperVisorImpl.java +++ b/src/main/java/fucoin/supervisor/SuperVisorImpl.java @@ -1,5 +1,6 @@ package fucoin.supervisor; +import akka.actor.ActorRef; import akka.actor.Props; import fucoin.actions.Action; import fucoin.actions.persist.ActionInvokeUpdate; @@ -8,6 +9,7 @@ import fucoin.actions.transaction.SuperVisorAction; import fucoin.gui.SuperVisorGuiControl; import fucoin.AbstractNode; import fucoin.gui.TransactionLogger; +import scala.concurrent.Future; import java.util.ArrayList; import java.util.HashMap; @@ -15,6 +17,8 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import static akka.dispatch.Futures.future; + public class SuperVisorImpl extends AbstractNode implements TransactionLogger{ private AmountTableModel amountTableModel; @@ -80,6 +84,10 @@ public class SuperVisorImpl extends AbstractNode implements TransactionLogger{ self().tell(new ActionUpdateQueue(), self()); } + public Future<ActorRef> resolveSelf() { + return future(() -> self(), context().system().dispatcher()); + } + /** * filters the request for outdated and removes them * diff --git a/src/main/java/fucoin/wallet/AbstractWallet.java b/src/main/java/fucoin/wallet/AbstractWallet.java index afcbcfa..67d1504 100644 --- a/src/main/java/fucoin/wallet/AbstractWallet.java +++ b/src/main/java/fucoin/wallet/AbstractWallet.java @@ -3,6 +3,7 @@ package fucoin.wallet; import akka.actor.ActorRef; import fucoin.AbstractNode; import fucoin.gui.TransactionLogger; +import scala.concurrent.Future; import java.io.Serializable; @@ -95,6 +96,8 @@ public abstract class AbstractWallet extends AbstractNode implements Serializabl */ public abstract void setRemoteSuperVisorActor(ActorRef remoteSuperVisorActor); + public abstract Future<ActorRef> resolveSuperVisorActor(); + /** * Sends amount FUCs to the wallet with the address adress * diff --git a/src/main/java/fucoin/wallet/WalletImpl.java b/src/main/java/fucoin/wallet/WalletImpl.java index e2cdbef..3826726 100644 --- a/src/main/java/fucoin/wallet/WalletImpl.java +++ b/src/main/java/fucoin/wallet/WalletImpl.java @@ -11,6 +11,9 @@ import fucoin.actions.persist.ActionInvokeRevive; import fucoin.actions.transaction.ActionGetAmountAnswer; import fucoin.actions.transaction.ActionInvokeSentMoney; import fucoin.gui.WalletGuiControl; +import scala.concurrent.Future; + +import static akka.dispatch.Futures.future; public class WalletImpl extends AbstractWallet { @@ -82,7 +85,7 @@ public class WalletImpl extends AbstractWallet { if (preKnownNeighbour != null) { addKnownNeighbor(preKnownNeighbourName, preKnownNeighbour); preKnownNeighbour.tell(new ActionJoin(), getSelf()); - ActionJoinAnswer aja = new ActionJoinAnswer(this.getRemoteSuperVisorActor()); + ActionJoinAnswer aja = new ActionJoinAnswer(this.resolveSuperVisorActor()); aja.someNeighbors.putAll(getKnownNeighbors()); aja.someNeighbors.put(name, getSelf()); preKnownNeighbour.tell(aja, getSelf()); @@ -145,6 +148,11 @@ public class WalletImpl extends AbstractWallet { return remoteSuperVisorActor; } + public Future<ActorRef> resolveSuperVisorActor() { + // TODO: this should return only, if getRemoteSuperVisorActor() != null + return future(() -> getRemoteSuperVisorActor(), context().system().dispatcher()); + } + @Override public void setRemoteSuperVisorActor(ActorRef remoteSuperVisorActor) { if (this.remoteSuperVisorActor == null) { -- GitLab