Skip to content
Snippets Groups Projects
Commit 4313ee7c authored by Luca Keidel's avatar Luca Keidel
Browse files

Refactored and cleaned up wallet creation

parent 2a664948
No related branches found
No related tags found
1 merge request!3Preliminary result of the software
......@@ -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);
}
}
......
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");
}
}
......@@ -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);
}
}
......
......@@ -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);
......
......@@ -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() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment