Skip to content
Snippets Groups Projects
Unverified Commit e7eba789 authored by David Bohn's avatar David Bohn
Browse files

Implemented join of remote node into network

parent 8cbbf76e
No related branches found
No related tags found
1 merge request!3Preliminary result of the software
......@@ -3,6 +3,7 @@ package fucoin;
import akka.actor.ActorRef;
import akka.actor.Address;
import akka.actor.UntypedActor;
import fucoin.actions.transaction.ActionGetAmount;
import fucoin.wallet.AbstractWallet;
import java.io.Serializable;
......@@ -58,6 +59,7 @@ public abstract class AbstractNode extends UntypedActor implements Serializable
public boolean addKnownNeighbor(String key, ActorRef value) {
if (!knownNeighbors.containsKey(key)) {
knownNeighbors.put(key, value);
value.tell(new ActionGetAmount(), getSelf());
return true;
}
return false;
......
......@@ -16,7 +16,7 @@ import java.util.List;
public class Main {
private static int numberOfWallets = 1;
private static int numberOfWallets = 2;
private static ActorSystem cSystem;
......@@ -38,6 +38,8 @@ public class Main {
//Init System Actor System
cSystem = ActorSystem.create("Core", config);
cSuperVisorActor = cSystem.actorOf(SuperVisorImpl.props(), "SuperVisorImpl");
System.out.print("Supervisor address: ");
System.out.println(cSuperVisorActor.path().toStringWithAddress(cSystem.provider().getDefaultAddress()));
}
public static void main(String[] args) throws InterruptedException {
......
......@@ -34,7 +34,7 @@ public class MainRemote {
//System.out.println(system.actorSelection(ActorPath.fromString(path)));
//System.out.println(ActorPath.isValidPathElement(""+address+"/user/Main"));
ActorRef a1 = system.actorOf(WalletImpl.props(null,"","Main2",remoteSuperVisorActor),"Main2");
ActorRef a1 = system.actorOf(WalletImpl.props(null,"","Remote1",remoteSuperVisorActor),"Remote1");
}
}
......@@ -3,17 +3,18 @@ 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)
public class ActionJoin extends GeneralAction {
public class ActionJoin extends ClientAction {
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, AbstractNode node) {
ActionJoinAnswer aja = new ActionJoinAnswer();
protected void onAction(ActorRef sender, ActorRef self, UntypedActorContext context, AbstractWallet node) {
ActionJoinAnswer aja = new ActionJoinAnswer(node.getRemoteSuperVisorActor());
aja.someNeighbors.putAll(node.getKnownNeighbors());
System.out.println("Answer to "+sender.path().name());
System.out.println("Answer to " + sender.path().name());
sender.tell(aja, self);
//node.addKnownNeighbor(sender.path().name(), sender);
}
}
......@@ -4,6 +4,7 @@ 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;
......@@ -13,6 +14,11 @@ import java.util.Map.Entry;
// and/or local neighbors
public class ActionJoinAnswer extends ClientAction {
public final HashMap<String, ActorRef> someNeighbors = new HashMap<>();
public final ActorRef supervisor;
public ActionJoinAnswer(ActorRef supervisor) {
this.supervisor = supervisor;
}
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, AbstractWallet wallet) {
......@@ -23,6 +29,11 @@ public class ActionJoinAnswer extends ClientAction {
for (Entry<String, ActorRef> neighbor : someNeighbors.entrySet()) {
neighbor.getValue().tell(new ActionSearchMyWallet(wallet.getName()), self);
}
if (wallet.getRemoteSuperVisorActor() == null) {
wallet.setRemoteSuperVisorActor(supervisor);
supervisor.tell(new ServerActionJoin(wallet.getName()), self);
}
}
}
......@@ -16,7 +16,7 @@ public class ServerActionJoin extends SuperVisorAction {
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, SuperVisorImpl node) {
ActionJoinAnswer aja = new ActionJoinAnswer();
ActionJoinAnswer aja = new ActionJoinAnswer(node.getSelf());
aja.someNeighbors.putAll(node.getKnownNeighbors());
sender.tell(aja, self);
node.addKnownNeighbor(name, sender);
......
......@@ -20,7 +20,7 @@ public class ActionGetAmountAnswer extends Transaction {
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, AbstractWallet wallet) {
wallet.amounts.put(sender, amount);
}
}
......@@ -32,6 +32,12 @@ public class ActionPrepareDistributedCommittedTransfer extends Transaction {
//sender have enough money
&& wallet.amounts.getOrDefault(source, 0) >= amount));
if (granted) {
wallet.log("I do grant transaction.");
} else {
wallet.log("I do not grant.");
}
sender.tell(new ActionPrepareDistributedCommittedTransferAnswer(source, target, amount, timestamp, granted, id), self);
}
......
......@@ -65,6 +65,8 @@ public abstract class AbstractWallet extends AbstractNode {
public abstract ActorRef getRemoteSuperVisorActor();
public abstract void setRemoteSuperVisorActor(ActorRef remoteSuperVisorActor);
public abstract void logTransaction(String msg);
public abstract void send(String address, int amount);
......
......@@ -6,8 +6,10 @@ 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;
......@@ -34,9 +36,10 @@ public class WalletImpl extends AbstractWallet {
this.remoteSuperVisorActor = remoteSuperVisorActor;
if(remoteSuperVisorActor == null){
String path = JOptionPane.showInputDialog(null, "Enter a neighbour address: ");
String path = JOptionPane.showInputDialog(null, "Enter a supervisor address: ");
System.out.println(path);
ActorSelection selection = getContext().actorSelection(path);
//this.setRemoteSuperVisorActor(selection.anchor());
selection.tell(new ActionJoin(), self());
//selection.tell("Hallo!", self());
}
......@@ -70,9 +73,9 @@ public class WalletImpl extends AbstractWallet {
((ClientAction) message).doAction(this);
}
if (message instanceof ActionJoin){
/*if (message instanceof ActionJoin){
((ActionJoin) message).doAction(this);
}
}*/
}
......@@ -86,7 +89,7 @@ public class WalletImpl extends AbstractWallet {
if (preKnownNeighbour != null) {
addKnownNeighbor(preKnownNeighbourName, preKnownNeighbour);
preKnownNeighbour.tell(new ActionJoin(), getSelf());
ActionJoinAnswer aja = new ActionJoinAnswer();
ActionJoinAnswer aja = new ActionJoinAnswer(this.getRemoteSuperVisorActor());
aja.someNeighbors.putAll(getKnownNeighbors());
aja.someNeighbors.put(name, getSelf());
preKnownNeighbour.tell(aja, getSelf());
......@@ -116,6 +119,8 @@ public class WalletImpl extends AbstractWallet {
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());
}
......
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