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

Added documentation to changed parts

parent d681cea7
No related branches found
No related tags found
1 merge request!3Preliminary result of the software
......@@ -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");
}
}
......@@ -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);
}
}
......@@ -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);
......
package fucoin.actions.join;
import fucoin.actions.Action;
import fucoin.AbstractNode;
public abstract class GeneralAction extends Action<AbstractNode> {
}
package fucoin.actions.join;
import fucoin.actions.ClientAction;
public abstract class Join extends ClientAction {
}
......@@ -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;
......
......@@ -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);
}
......@@ -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);
......
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