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

Added tell supervisor action to make system init work

parent a51bbee0
No related branches found
No related tags found
2 merge requests!5Configuration system,!4Added tell supervisor action to make system init work
......@@ -5,7 +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.ActionTellSupervisor;
import fucoin.setup.NetworkInterfaceReader;
import fucoin.supervisor.SuperVisorImpl;
import fucoin.wallet.WalletImpl;
......@@ -67,7 +67,8 @@ 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(cSuperVisorActor), cSuperVisorActor);
actorRef.tell(new ActionTellSupervisor(cSuperVisorActor), cSuperVisorActor);
}
cActiveActors.add(actorRef);
......
......@@ -14,8 +14,14 @@ 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();
aja.someNeighbors.putAll(node.getKnownNeighbors());
sender.tell(aja, self);
if (node.getRemoteSuperVisorActor() == null) {
node.deferSendOfSuperVisorActor(sender);
} else {
sender.tell(new ActionTellSupervisor(node.getRemoteSuperVisorActor()), self);
}
}
}
......@@ -20,11 +20,6 @@ import java.util.Map.Entry;
*/
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) {
......@@ -38,11 +33,6 @@ public class ActionJoinAnswer extends ClientAction {
for (Entry<String, ActorRef> neighbor : someNeighbors.entrySet()) {
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);
}
}
}
package fucoin.actions.join;
import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;
import fucoin.actions.ClientAction;
import fucoin.wallet.AbstractWallet;
/**
* Tell the joining node the supervisor
*/
public class ActionTellSupervisor extends ClientAction {
public final ActorRef supervisor;
public ActionTellSupervisor(ActorRef supervisor) {
this.supervisor = supervisor;
}
@Override
protected void onAction(ActorRef sender, ActorRef self, UntypedActorContext context, AbstractWallet abstractNode) {
abstractNode.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(); // TODO: Might need added TellSupervisor
aja.someNeighbors.putAll(node.getKnownNeighbors());
sender.tell(aja, self);
node.addKnownNeighbor(name, sender);
......
......@@ -3,6 +3,8 @@ package fucoin.gui;
import fucoin.supervisor.SuperVisorImpl;
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.WindowAdapter;
......@@ -31,6 +33,7 @@ public class SuperVisorGuiControlImpl implements SuperVisorGuiControl {
//Init Amount Table and SuperVisorImpl
JTable amountListView = new JTable(superVisor.getAmountTableModel());
superVisor.getAmountTableModel().addTableModelListener(e -> SwingUtilities.invokeLater(() -> frame.setTitle("Server (" + superVisor.getAmountTableModel().getRowCount() + " Wallets)")));
contentPanel.add(new JScrollPane(amountListView));
JPanel logPanel = new JPanel(new BorderLayout());
......
......@@ -95,6 +95,8 @@ public abstract class AbstractWallet extends AbstractNode implements Serializabl
*/
public abstract void setRemoteSuperVisorActor(ActorRef remoteSuperVisorActor);
public abstract void deferSendOfSuperVisorActor(ActorRef destinationWallet);
/**
* Sends amount FUCs to the wallet with the address adress
*
......
......@@ -5,6 +5,7 @@ import akka.actor.Props;
import fucoin.actions.ClientAction;
import fucoin.actions.join.ActionJoin;
import fucoin.actions.join.ActionJoinAnswer;
import fucoin.actions.join.ActionTellSupervisor;
import fucoin.actions.join.ServerActionJoin;
import fucoin.actions.persist.ActionInvokeLeave;
import fucoin.actions.persist.ActionInvokeRevive;
......@@ -12,6 +13,8 @@ import fucoin.actions.transaction.ActionGetAmountAnswer;
import fucoin.actions.transaction.ActionInvokeSentMoney;
import fucoin.gui.WalletGuiControl;
import java.util.concurrent.ConcurrentLinkedQueue;
public class WalletImpl extends AbstractWallet {
private ActorRef preKnownNeighbour;
......@@ -19,6 +22,7 @@ public class WalletImpl extends AbstractWallet {
private transient WalletGuiControl gui;
private String preKnownNeighbourName;
private boolean isActive;
private ConcurrentLinkedQueue<ActorRef> deferedSupervisorReceivers = new ConcurrentLinkedQueue<>();
public WalletImpl(String name) {
super(name);
......@@ -82,11 +86,15 @@ 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();
aja.someNeighbors.putAll(getKnownNeighbors());
aja.someNeighbors.put(name, getSelf());
preKnownNeighbour.tell(aja, getSelf());
if (this.getRemoteSuperVisorActor() != null) {
preKnownNeighbour.tell(new ActionTellSupervisor(this.getRemoteSuperVisorActor()), self());
}
}
}
......@@ -150,6 +158,21 @@ public class WalletImpl extends AbstractWallet {
if (this.remoteSuperVisorActor == null) {
this.remoteSuperVisorActor = remoteSuperVisorActor;
this.remoteSuperVisorActor.tell(new ServerActionJoin(getName()), getSelf());
this.tellDeferedSuperVisorReceivers(remoteSuperVisorActor);
}
}
@Override
public void deferSendOfSuperVisorActor(ActorRef destinationWallet) {
deferedSupervisorReceivers.add(destinationWallet);
}
protected void tellDeferedSuperVisorReceivers(ActorRef supervisor) {
while (deferedSupervisorReceivers.size() > 0) {
ActorRef receiver = deferedSupervisorReceivers.poll();
if (receiver != null) {
receiver.tell(new ActionTellSupervisor(supervisor), self());
}
}
}
......
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