diff --git a/src/main/java/fucoin/Main.java b/src/main/java/fucoin/Main.java index 756873a3d3d8ba9599b02dc42571d6fe670c9b48..0a37e3aedd7bf7d54f7a2382f9cc949059851421 100644 --- a/src/main/java/fucoin/Main.java +++ b/src/main/java/fucoin/Main.java @@ -5,6 +5,7 @@ import akka.actor.ActorSystem; import akka.actor.Props; import com.typesafe.config.Config; import com.typesafe.config.ConfigFactory; +import fucoin.actions.join.ActionJoinAnswer; import fucoin.actions.join.ServerActionJoin; import fucoin.supervisor.SuperVisorImpl; import fucoin.wallet.WalletImpl; @@ -52,12 +53,21 @@ public class Main { String nameOfTheWallet = "Wallet" + String.valueOf(i); //chain the wallets. wallet2 knows wallet1, wallet3 knows wallet2 and so on. String nameOfThePreviousWallet = ""; + Props props; if (i > 0) { - nameOfThePreviousWallet = "Wallet" + String.valueOf(i - 1); + props = WalletImpl.props(cActiveActors.get(i - 1), nameOfTheWallet); + } else { + props = WalletImpl.props(null, nameOfTheWallet); } - Props props = WalletImpl.props(null, nameOfThePreviousWallet, nameOfTheWallet, cSuperVisorActor); + ActorRef actorRef = cSystem.actorOf(props, nameOfTheWallet); - cSuperVisorActor.tell(new ServerActionJoin(nameOfTheWallet), actorRef); + + // 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); + } + cActiveActors.add(actorRef); } } diff --git a/src/main/java/fucoin/MainRemote.java b/src/main/java/fucoin/MainRemote.java index 337c577bce536094f3475d9602c316a8a5b59bc8..3d9006266376dfd81896c03d3dcc104c7bef31a8 100644 --- a/src/main/java/fucoin/MainRemote.java +++ b/src/main/java/fucoin/MainRemote.java @@ -1,14 +1,20 @@ package fucoin; import java.io.File; +import java.util.concurrent.TimeUnit; import akka.actor.ActorRef; +import akka.actor.ActorSelection; import akka.actor.ActorSystem; import akka.actor.Address; +import akka.util.Timeout; import com.typesafe.config.Config; import com.typesafe.config.ConfigFactory; import fucoin.wallet.WalletImpl; +import scala.concurrent.Await; + +import javax.swing.*; public class MainRemote { public static ActorRef remoteSuperVisorActor; @@ -26,10 +32,34 @@ public class MainRemote { } //Init System Actor System - ActorSystem system = ActorSystem.create("Test", config); + ActorSystem system = ActorSystem.create("Remote", config); + + + Timeout timeout = new Timeout(5, TimeUnit.SECONDS); + ActorRef preknownNeighbour = null; + + while (preknownNeighbour == null) { + + // get an address from a node which should be our preknown neighbour + String path = JOptionPane.showInputDialog(null, "Enter a neighbour node address: "); + + // terminate if user clicked abort + if (path == null) { + system.terminate(); + } + + // tell the node that we want to join the network + ActorSelection selection = system.actorSelection(path); + + try { + preknownNeighbour = Await.result(selection.resolveOne(timeout), timeout.duration()); + } catch (Exception e) { + System.err.println("Something went wrong while resolving: " + e.getMessage()); + } + } // spawn wallet - system.actorOf(WalletImpl.props(null, "", "Remote1", remoteSuperVisorActor), "Remote1"); + system.actorOf(WalletImpl.props(preknownNeighbour, "Remote1"), "Remote1"); } } diff --git a/src/main/java/fucoin/actions/join/ActionJoinAnswer.java b/src/main/java/fucoin/actions/join/ActionJoinAnswer.java index ffb86a6020b153a2fbcf8914affc9de7bb675f5d..09d48ac7bb6b59b592621ff627446987c0446e15 100644 --- a/src/main/java/fucoin/actions/join/ActionJoinAnswer.java +++ b/src/main/java/fucoin/actions/join/ActionJoinAnswer.java @@ -41,7 +41,6 @@ public class ActionJoinAnswer extends ClientAction { // register at the supervisor if the wallet just learned about it if (wallet.getRemoteSuperVisorActor() == null) { wallet.setRemoteSuperVisorActor(supervisor); - supervisor.tell(new ServerActionJoin(wallet.getName()), self); } } diff --git a/src/main/java/fucoin/wallet/WalletCreator.java b/src/main/java/fucoin/wallet/WalletCreator.java index 677991475fe5ad40c69b251bcd44f5529cad3e6d..2a512b9f797529e1a24a0508fe91194fb3431226 100644 --- a/src/main/java/fucoin/wallet/WalletCreator.java +++ b/src/main/java/fucoin/wallet/WalletCreator.java @@ -7,22 +7,15 @@ import fucoin.gui.WalletGuiControlImpl; public class WalletCreator implements Creator<AbstractWallet> { private ActorRef preKnownNeighbour; private String walletName; - private ActorRef remoteSuperVisorActor; - private String preKnownNeighbourName; - public WalletCreator(ActorRef preKnownNeighbour, String preKnownNeighbourName, - String walletName, ActorRef remoteSuperVisorActor) { + public WalletCreator(ActorRef preKnownNeighbour, String walletName) { this.preKnownNeighbour = preKnownNeighbour; - this.preKnownNeighbourName = preKnownNeighbourName; this.walletName = walletName; - this.remoteSuperVisorActor = remoteSuperVisorActor; - } @Override public WalletImpl create() throws Exception { - WalletImpl wallet = new WalletImpl(preKnownNeighbour, preKnownNeighbourName, - walletName, remoteSuperVisorActor); + WalletImpl wallet = new WalletImpl(preKnownNeighbour, walletName); WalletGuiControlImpl gui = new WalletGuiControlImpl(wallet); wallet.setGui(gui); diff --git a/src/main/java/fucoin/wallet/WalletImpl.java b/src/main/java/fucoin/wallet/WalletImpl.java index abc223e445a8cc96f6edcfed773bd056afe42149..a3eadeaf0cf10ac96c1d20c933bb17c703a77e13 100644 --- a/src/main/java/fucoin/wallet/WalletImpl.java +++ b/src/main/java/fucoin/wallet/WalletImpl.java @@ -3,16 +3,20 @@ package fucoin.wallet; import akka.actor.ActorRef; import akka.actor.ActorSelection; import akka.actor.Props; +import akka.util.Timeout; import fucoin.actions.ClientAction; import fucoin.actions.join.ActionJoin; import fucoin.actions.join.ActionJoinAnswer; +import fucoin.actions.join.ServerActionJoin; import fucoin.actions.persist.ActionInvokeLeave; import fucoin.actions.persist.ActionInvokeRevive; import fucoin.actions.transaction.ActionGetAmountAnswer; import fucoin.actions.transaction.ActionInvokeSentMoney; import fucoin.gui.WalletGuiControl; +import scala.concurrent.Await; import javax.swing.*; +import java.util.concurrent.TimeUnit; public class WalletImpl extends AbstractWallet { @@ -26,31 +30,16 @@ public class WalletImpl extends AbstractWallet { super(name); } - public WalletImpl(ActorRef preKnownNeighbour, String preKnownNeighbourName, - String walletName, ActorRef remoteSuperVisorActor) { + public WalletImpl(ActorRef preKnownNeighbour, String walletName) { super(walletName); - this.preKnownNeighbourName = preKnownNeighbourName; - this.preKnownNeighbour = preKnownNeighbour; - this.remoteSuperVisorActor = remoteSuperVisorActor; - - // if we don't have a reference to the supervisor from the start, we are a remote wallet - if(remoteSuperVisorActor == null){ - String path = JOptionPane.showInputDialog(null, "Enter a neighbour node address: "); - - // terminate if user clicked abort - if (path == null){ - getContext().system().terminate(); - } - - // tell the node that we want to join the network - ActorSelection selection = getContext().actorSelection(path); - selection.tell(new ActionJoin(), self()); + if(preKnownNeighbour != null) { + this.preKnownNeighbourName = preKnownNeighbour.path().name(); + this.preKnownNeighbour = preKnownNeighbour; } } - public static Props props(ActorRef preKnownNeighbour, String preKnownNeighbourName, - String walletName, ActorRef remoteSuperVisorActor) { - return Props.create(new WalletCreator(preKnownNeighbour, preKnownNeighbourName, walletName, remoteSuperVisorActor)); + public static Props props(ActorRef preKnownNeighbour, String walletName) { + return Props.create(new WalletCreator(preKnownNeighbour, walletName)); } /** @@ -160,7 +149,10 @@ public class WalletImpl extends AbstractWallet { @Override public void setRemoteSuperVisorActor(ActorRef remoteSuperVisorActor) { - this.remoteSuperVisorActor = remoteSuperVisorActor; + if(this.remoteSuperVisorActor == null) { + this.remoteSuperVisorActor = remoteSuperVisorActor; + this.remoteSuperVisorActor.tell(new ServerActionJoin(getName()), getSelf()); + } } public WalletGuiControl getGui() {