diff --git a/src/main/java/fucoin/Main.java b/src/main/java/fucoin/Main.java index be9fc41d7d9b74b361d0861465c0037b736384c2..f992c440a959554f54202ec4ef9c4a260ea4f599 100644 --- a/src/main/java/fucoin/Main.java +++ b/src/main/java/fucoin/Main.java @@ -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); diff --git a/src/main/java/fucoin/actions/join/ActionJoin.java b/src/main/java/fucoin/actions/join/ActionJoin.java index e7bd81419911b9291a7d4da154d227020f1828a8..205d09a5e5c36c25f3588606cfbccad9b4fe24b7 100644 --- a/src/main/java/fucoin/actions/join/ActionJoin.java +++ b/src/main/java/fucoin/actions/join/ActionJoin.java @@ -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); + } } } diff --git a/src/main/java/fucoin/actions/join/ActionJoinAnswer.java b/src/main/java/fucoin/actions/join/ActionJoinAnswer.java index e20bbcc5b94d11d920a0200d6166467328dff701..677d1ff4d097bce815c72c3891a8f91651e8d971 100644 --- a/src/main/java/fucoin/actions/join/ActionJoinAnswer.java +++ b/src/main/java/fucoin/actions/join/ActionJoinAnswer.java @@ -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); - } } } diff --git a/src/main/java/fucoin/actions/join/ActionTellSupervisor.java b/src/main/java/fucoin/actions/join/ActionTellSupervisor.java new file mode 100644 index 0000000000000000000000000000000000000000..3c7ef7ae9079976d4212a4c193d5202d45ffc2ac --- /dev/null +++ b/src/main/java/fucoin/actions/join/ActionTellSupervisor.java @@ -0,0 +1,23 @@ +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); + } +} diff --git a/src/main/java/fucoin/actions/join/ServerActionJoin.java b/src/main/java/fucoin/actions/join/ServerActionJoin.java index 4c57b136f65958aa606bb75b7f404ca8e6edaf70..abc8ea18ee83293250ff44178f8a3caeb5f06b91 100644 --- a/src/main/java/fucoin/actions/join/ServerActionJoin.java +++ b/src/main/java/fucoin/actions/join/ServerActionJoin.java @@ -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); diff --git a/src/main/java/fucoin/gui/SuperVisorGuiControlImpl.java b/src/main/java/fucoin/gui/SuperVisorGuiControlImpl.java index cb4aaf23670a958642e67e1d53ebffe200f032a8..43ccec385ce96b2f88561bccabef530d394fa88e 100644 --- a/src/main/java/fucoin/gui/SuperVisorGuiControlImpl.java +++ b/src/main/java/fucoin/gui/SuperVisorGuiControlImpl.java @@ -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()); diff --git a/src/main/java/fucoin/wallet/AbstractWallet.java b/src/main/java/fucoin/wallet/AbstractWallet.java index afcbcfa713ba847c372f63c91e8a5246aa399b11..1069cf8a8f5c0786ecd943a5ccb30591429b7734 100644 --- a/src/main/java/fucoin/wallet/AbstractWallet.java +++ b/src/main/java/fucoin/wallet/AbstractWallet.java @@ -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 * diff --git a/src/main/java/fucoin/wallet/WalletImpl.java b/src/main/java/fucoin/wallet/WalletImpl.java index e2cdbefb30737491a3b23331777843ac55642892..dd52e86c3db51b2f12693453102eca1b5d6b36b1 100644 --- a/src/main/java/fucoin/wallet/WalletImpl.java +++ b/src/main/java/fucoin/wallet/WalletImpl.java @@ -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()); + } } }