diff --git a/src/main/java/fucoin/Main.java b/src/main/java/fucoin/Main.java index be9fc41d7d9b74b361d0861465c0037b736384c2..c8b796127491d56e9ba97440f50c65fa0aa162b0 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 e7bd81419911b9291a7d4da154d227020f1828a8..15540eb32118d340e399204b81c41cb527c39e7b 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 e20bbcc5b94d11d920a0200d6166467328dff701..dc7c9a53b6fc0432de7b31e0d62a235e3db3e880 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 4c57b136f65958aa606bb75b7f404ca8e6edaf70..6b218b8adf024b6222cfd899c9184c1a7eb2e97a 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 2333a97f635ba869eae7263d7e89fb018f3c1620..dbfcfe87ba04a58da16f7e9637e59c84e58359b8 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 afcbcfa713ba847c372f63c91e8a5246aa399b11..67d150424c55c05528b19a774afe4ca2ac7321da 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 e2cdbefb30737491a3b23331777843ac55642892..3826726628d7478bd37fd7cd7119d7633c16be7a 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) {