From 2a66494890382cfdaa1b29a9058b5fe559b06a19 Mon Sep 17 00:00:00 2001 From: Luca Keidel <info@lucakeidel.de> Date: Tue, 14 Jun 2016 19:42:47 +0200 Subject: [PATCH] Added documentation to changed parts --- src/main/java/fucoin/MainRemote.java | 47 +++++++++---------- .../java/fucoin/actions/join/ActionJoin.java | 8 ++-- .../fucoin/actions/join/ActionJoinAnswer.java | 17 +++++-- .../fucoin/actions/join/GeneralAction.java | 8 ---- src/main/java/fucoin/actions/join/Join.java | 6 --- .../fucoin/actions/join/ServerActionJoin.java | 5 ++ .../java/fucoin/wallet/AbstractWallet.java | 35 ++++++++++++++ src/main/java/fucoin/wallet/WalletImpl.java | 36 ++++++++++++-- 8 files changed, 110 insertions(+), 52 deletions(-) delete mode 100644 src/main/java/fucoin/actions/join/GeneralAction.java delete mode 100644 src/main/java/fucoin/actions/join/Join.java diff --git a/src/main/java/fucoin/MainRemote.java b/src/main/java/fucoin/MainRemote.java index 29b574c..337c577 100644 --- a/src/main/java/fucoin/MainRemote.java +++ b/src/main/java/fucoin/MainRemote.java @@ -11,30 +11,25 @@ import com.typesafe.config.ConfigFactory; import fucoin.wallet.WalletImpl; public class MainRemote { - public static ActorRef remoteSuperVisorActor; - - public static void main(String[] args) throws InterruptedException { - - //Load configuration from current directory or from resources directory of jar - File file = new File("application.conf"); - Config config = ConfigFactory.parseFile(file); - if (!file.exists()) { - System.out.println("Load default application.conf"); - config = ConfigFactory.parseResources("application.conf"); - } else { - System.out.println("Load local application.conf"); - } - - //Init System Actor System - ActorSystem system = ActorSystem.create("Test", config); - - Address address = new Address("akka.tcp", "Core", "127.0.0.1", 1234); - System.out.println(address); - String path = "akka.tcp://Core@127.0.0.1:1234/user/Main"; - //System.out.println(system.actorSelection(ActorPath.fromString(path))); - - //System.out.println(ActorPath.isValidPathElement(""+address+"/user/Main")); - ActorRef a1 = system.actorOf(WalletImpl.props(null,"","Remote1",remoteSuperVisorActor),"Remote1"); - - } + public static ActorRef remoteSuperVisorActor; + + public static void main(String[] args) throws InterruptedException { + + //Load configuration from current directory or from resources directory of jar + File file = new File("application.conf"); + Config config = ConfigFactory.parseFile(file); + if (!file.exists()) { + System.out.println("Load default application.conf"); + config = ConfigFactory.parseResources("application.conf"); + } else { + System.out.println("Load local application.conf"); + } + + //Init System Actor System + ActorSystem system = ActorSystem.create("Test", config); + + // spawn wallet + system.actorOf(WalletImpl.props(null, "", "Remote1", remoteSuperVisorActor), "Remote1"); + + } } diff --git a/src/main/java/fucoin/actions/join/ActionJoin.java b/src/main/java/fucoin/actions/join/ActionJoin.java index d3d44bc..e7bd814 100644 --- a/src/main/java/fucoin/actions/join/ActionJoin.java +++ b/src/main/java/fucoin/actions/join/ActionJoin.java @@ -2,18 +2,20 @@ package fucoin.actions.join; import akka.actor.ActorRef; import akka.actor.UntypedActorContext; -import fucoin.AbstractNode; import fucoin.actions.ClientAction; import fucoin.wallet.AbstractWallet; -//Used to join the network (a pre known participant/WalletImpl must be known) +/** + * This action is used by nodes wanting to join the network. + */ public class ActionJoin extends ClientAction { @Override 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()); aja.someNeighbors.putAll(node.getKnownNeighbors()); - System.out.println("Answer to " + sender.path().name()); 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 7fe4457..ffb86a6 100644 --- a/src/main/java/fucoin/actions/join/ActionJoinAnswer.java +++ b/src/main/java/fucoin/actions/join/ActionJoinAnswer.java @@ -4,14 +4,19 @@ import akka.actor.ActorRef; import akka.actor.UntypedActorContext; import fucoin.actions.ClientAction; import fucoin.actions.persist.ActionSearchMyWallet; -import fucoin.actions.transaction.ActionGetAmount; import fucoin.wallet.AbstractWallet; - import java.util.HashMap; import java.util.Map.Entry; -// Returns some neighbors that might be used as known -// and/or local neighbors +/** + * This action is the response from a wallet which is already in the network + * to a wallet which wants to join the network. + * + * The node in the network sends all its known neighbours which then become + * neighbours of the new node. This action also contains a reference to the + * supervisor. If this is the first time the new node learned about the systems + * supervisor, it proceeds to register at the supervisor. + */ public class ActionJoinAnswer extends ClientAction { public final HashMap<String, ActorRef> someNeighbors = new HashMap<>(); public final ActorRef supervisor; @@ -22,7 +27,10 @@ public class ActionJoinAnswer extends ClientAction { protected void onAction(ActorRef sender, ActorRef self, UntypedActorContext context, AbstractWallet wallet) { + wallet.log("Addressed to " + self.path().name() + " from " + sender.path().name() + ": someNeighbors:" + someNeighbors); + + // your neighbours? my neighbours! for (Entry<String, ActorRef> neighbor : someNeighbors.entrySet()) { wallet.addKnownNeighbor(neighbor.getKey(), neighbor.getValue()); } @@ -30,6 +38,7 @@ public class ActionJoinAnswer extends ClientAction { neighbor.getValue().tell(new ActionSearchMyWallet(wallet.getName()), self); } + // 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/actions/join/GeneralAction.java b/src/main/java/fucoin/actions/join/GeneralAction.java deleted file mode 100644 index 8dd3c59..0000000 --- a/src/main/java/fucoin/actions/join/GeneralAction.java +++ /dev/null @@ -1,8 +0,0 @@ -package fucoin.actions.join; - -import fucoin.actions.Action; -import fucoin.AbstractNode; - -public abstract class GeneralAction extends Action<AbstractNode> { - -} diff --git a/src/main/java/fucoin/actions/join/Join.java b/src/main/java/fucoin/actions/join/Join.java deleted file mode 100644 index 616dbf9..0000000 --- a/src/main/java/fucoin/actions/join/Join.java +++ /dev/null @@ -1,6 +0,0 @@ -package fucoin.actions.join; - -import fucoin.actions.ClientAction; - -public abstract class Join extends ClientAction { -} diff --git a/src/main/java/fucoin/actions/join/ServerActionJoin.java b/src/main/java/fucoin/actions/join/ServerActionJoin.java index 7a60692..4c57b13 100644 --- a/src/main/java/fucoin/actions/join/ServerActionJoin.java +++ b/src/main/java/fucoin/actions/join/ServerActionJoin.java @@ -6,6 +6,11 @@ import fucoin.actions.transaction.ActionInvokeDistributedCommittedTransfer; import fucoin.actions.transaction.SuperVisorAction; import fucoin.supervisor.SuperVisorImpl; +/** + * Used by nodes to register at the supervisor. In return, the supervisor + * sends all its neighbours to the node and initiates the transfer of a fixed amount + * of FUCs to get started. + */ public class ServerActionJoin extends SuperVisorAction { private String name; diff --git a/src/main/java/fucoin/wallet/AbstractWallet.java b/src/main/java/fucoin/wallet/AbstractWallet.java index 186444d..13faa08 100644 --- a/src/main/java/fucoin/wallet/AbstractWallet.java +++ b/src/main/java/fucoin/wallet/AbstractWallet.java @@ -3,7 +3,11 @@ package fucoin.wallet; import akka.actor.ActorRef; import fucoin.AbstractNode; +/** + * + */ public abstract class AbstractWallet extends AbstractNode { + /** * Currently amount of this wallet */ @@ -59,15 +63,46 @@ public abstract class AbstractWallet extends AbstractNode { */ public abstract void addAmount(int amount); + /** + * Sets the wallet into the active state. + * TODO: Is this actually used/necessary/wanted? + * + * @param isActive + */ public abstract void setActive(boolean isActive); + /** + * Returns the + * + * @return + */ public abstract ActorRef getPreKnownNeighbour(); + /** + * Returns the supervisor of this wallet + * + * @return + */ public abstract ActorRef getRemoteSuperVisorActor(); + /** + * + * @param remoteSuperVisorActor + */ public abstract void setRemoteSuperVisorActor(ActorRef remoteSuperVisorActor); + /** + * Appends a transaction related message to the log + * + * @param msg + */ public abstract void logTransaction(String msg); + /** + * Sends amount FUCs to the wallet with the address adress + * + * @param address Recipients address + * @param amount Amount to send + */ public abstract void send(String address, int amount); } diff --git a/src/main/java/fucoin/wallet/WalletImpl.java b/src/main/java/fucoin/wallet/WalletImpl.java index 1dfaf2c..abc223e 100644 --- a/src/main/java/fucoin/wallet/WalletImpl.java +++ b/src/main/java/fucoin/wallet/WalletImpl.java @@ -6,10 +6,8 @@ import akka.actor.Props; 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.ActionGetAmount; import fucoin.actions.transaction.ActionGetAmountAnswer; import fucoin.actions.transaction.ActionInvokeSentMoney; import fucoin.gui.WalletGuiControl; @@ -35,9 +33,16 @@ public class WalletImpl extends AbstractWallet { 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: "); - System.out.println(path); + + // 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()); } @@ -48,6 +53,11 @@ public class WalletImpl extends AbstractWallet { return Props.create(new WalletCreator(preKnownNeighbour, preKnownNeighbourName, walletName, remoteSuperVisorActor)); } + /** + * Adds amount to the current amount of FUCs in the wallet. + * + * @param amount value to add to current account. + */ public void addAmount(int amount) { setAmount(this.getAmount() + amount); log(" My amount is now " + this.getAmount()); @@ -75,7 +85,9 @@ public class WalletImpl extends AbstractWallet { @Override public void preStart() throws Exception { + isActive = true; + if (gui != null) { gui.setAddress(getAddress()); } @@ -107,14 +119,23 @@ public class WalletImpl extends AbstractWallet { return false; } + /** + * Returns the amount of FUCs currently in the wallet. + * + * @return + */ public int getAmount() { return amount; } + /** + * Sets the amount of FUCs in the wallet to amount. + * + * @param amount New amount of the wallet + */ public void setAmount(int amount) { this.amount = amount; - System.out.print("Setting amount and supervisor is: "); - System.out.println(remoteSuperVisorActor); + if (remoteSuperVisorActor != null) { remoteSuperVisorActor.tell(new ActionGetAmountAnswer(getAddress(), getName(), amount), getSelf()); } @@ -123,6 +144,7 @@ public class WalletImpl extends AbstractWallet { } } + @Override public ActorRef getPreKnownNeighbour() { return preKnownNeighbour; } @@ -131,10 +153,12 @@ public class WalletImpl extends AbstractWallet { this.preKnownNeighbour = preKnownNeighbour; } + @Override public ActorRef getRemoteSuperVisorActor() { return remoteSuperVisorActor; } + @Override public void setRemoteSuperVisorActor(ActorRef remoteSuperVisorActor) { this.remoteSuperVisorActor = remoteSuperVisorActor; } @@ -186,6 +210,8 @@ public class WalletImpl extends AbstractWallet { } } + + @Override public void logTransaction(String msg) { if (gui != null) { gui.addTransactionLogMessage(msg); -- GitLab