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

Implemented futures for receiving the supervisor

parent a51bbee0
No related branches found
No related tags found
1 merge request!5Configuration system
......@@ -14,11 +14,13 @@ import java.io.File;
import java.util.ArrayList;
import java.util.List;
import static akka.dispatch.Futures.future;
public class Main {
private static int numberOfWallets = 2;
private static boolean createGUI = false;
private static boolean createGUI = true;
private static ActorSystem cSystem;
......@@ -67,7 +69,7 @@ public class Main {
// 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);
actorRef.tell(new ActionJoinAnswer(future(() -> cSuperVisorActor, cSystem.dispatcher())), cSuperVisorActor);
}
cActiveActors.add(actorRef);
......
......@@ -14,7 +14,7 @@ public class ActionJoin extends ClientAction {
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());
ActionJoinAnswer aja = new ActionJoinAnswer(node.resolveSuperVisorActor());
aja.someNeighbors.putAll(node.getKnownNeighbors());
sender.tell(aja, self);
}
......
......@@ -2,9 +2,13 @@ package fucoin.actions.join;
import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;
import akka.dispatch.OnSuccess;
import fucoin.actions.ClientAction;
import fucoin.actions.persist.ActionSearchMyWallet;
import fucoin.wallet.AbstractWallet;
import scala.Function1;
import scala.concurrent.Future;
import scala.runtime.BoxedUnit;
import java.util.HashMap;
import java.util.Map.Entry;
......@@ -20,9 +24,9 @@ import java.util.Map.Entry;
*/
public class ActionJoinAnswer extends ClientAction {
public final HashMap<String, ActorRef> someNeighbors = new HashMap<>();
public final ActorRef supervisor;
public final Future<ActorRef> supervisor;
public ActionJoinAnswer(ActorRef supervisor) {
public ActionJoinAnswer(Future<ActorRef> supervisor) {
this.supervisor = supervisor;
}
......@@ -41,7 +45,14 @@ public class ActionJoinAnswer extends ClientAction {
// register at the supervisor if the wallet just learned about it
if (wallet.getRemoteSuperVisorActor() == null) {
wallet.setRemoteSuperVisorActor(supervisor);
supervisor.onSuccess(new OnSuccess<ActorRef>() {
@Override
public void onSuccess(ActorRef result) throws Throwable {
wallet.setRemoteSuperVisorActor(result);
}
}, context.system().dispatcher());
//wallet.setRemoteSuperVisorActor(supervisor);
}
}
......
......@@ -21,7 +21,7 @@ public class ServerActionJoin extends SuperVisorAction {
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, SuperVisorImpl node) {
ActionJoinAnswer aja = new ActionJoinAnswer(node.getSelf());
ActionJoinAnswer aja = new ActionJoinAnswer(node.resolveSelf());
aja.someNeighbors.putAll(node.getKnownNeighbors());
sender.tell(aja, self);
node.addKnownNeighbor(name, sender);
......
package fucoin.supervisor;
import akka.actor.ActorRef;
import akka.actor.Props;
import fucoin.actions.Action;
import fucoin.actions.persist.ActionInvokeUpdate;
......@@ -8,6 +9,7 @@ import fucoin.actions.transaction.SuperVisorAction;
import fucoin.gui.SuperVisorGuiControl;
import fucoin.AbstractNode;
import fucoin.gui.TransactionLogger;
import scala.concurrent.Future;
import java.util.ArrayList;
import java.util.HashMap;
......@@ -15,6 +17,8 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import static akka.dispatch.Futures.future;
public class SuperVisorImpl extends AbstractNode implements TransactionLogger{
private AmountTableModel amountTableModel;
......@@ -80,6 +84,10 @@ public class SuperVisorImpl extends AbstractNode implements TransactionLogger{
self().tell(new ActionUpdateQueue(), self());
}
public Future<ActorRef> resolveSelf() {
return future(() -> self(), context().system().dispatcher());
}
/**
* filters the request for outdated and removes them
*
......
......@@ -3,6 +3,7 @@ package fucoin.wallet;
import akka.actor.ActorRef;
import fucoin.AbstractNode;
import fucoin.gui.TransactionLogger;
import scala.concurrent.Future;
import java.io.Serializable;
......@@ -95,6 +96,8 @@ public abstract class AbstractWallet extends AbstractNode implements Serializabl
*/
public abstract void setRemoteSuperVisorActor(ActorRef remoteSuperVisorActor);
public abstract Future<ActorRef> resolveSuperVisorActor();
/**
* Sends amount FUCs to the wallet with the address adress
*
......
......@@ -11,6 +11,9 @@ import fucoin.actions.persist.ActionInvokeRevive;
import fucoin.actions.transaction.ActionGetAmountAnswer;
import fucoin.actions.transaction.ActionInvokeSentMoney;
import fucoin.gui.WalletGuiControl;
import scala.concurrent.Future;
import static akka.dispatch.Futures.future;
public class WalletImpl extends AbstractWallet {
......@@ -82,7 +85,7 @@ public class WalletImpl extends AbstractWallet {
if (preKnownNeighbour != null) {
addKnownNeighbor(preKnownNeighbourName, preKnownNeighbour);
preKnownNeighbour.tell(new ActionJoin(), getSelf());
ActionJoinAnswer aja = new ActionJoinAnswer(this.getRemoteSuperVisorActor());
ActionJoinAnswer aja = new ActionJoinAnswer(this.resolveSuperVisorActor());
aja.someNeighbors.putAll(getKnownNeighbors());
aja.someNeighbors.put(name, getSelf());
preKnownNeighbour.tell(aja, getSelf());
......@@ -145,6 +148,11 @@ public class WalletImpl extends AbstractWallet {
return remoteSuperVisorActor;
}
public Future<ActorRef> resolveSuperVisorActor() {
// TODO: this should return only, if getRemoteSuperVisorActor() != null
return future(() -> getRemoteSuperVisorActor(), context().system().dispatcher());
}
@Override
public void setRemoteSuperVisorActor(ActorRef remoteSuperVisorActor) {
if (this.remoteSuperVisorActor == null) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment