From 7374bee97fdde76641f16a70947164457d2d694d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20K=C3=B6nnecke?= <simonkoennecke@gmail.com> Date: Thu, 7 Jul 2016 15:45:51 +0200 Subject: [PATCH] tell a new node only 2 random neighbours --- .../java/fucoin/actions/join/ActionJoin.java | 27 ++++++++++++++++--- .../fucoin/actions/join/ActionJoinAnswer.java | 9 +++++-- .../fucoin/actions/join/ServerActionJoin.java | 3 +-- src/main/java/fucoin/wallet/WalletImpl.java | 10 ++++--- 4 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/main/java/fucoin/actions/join/ActionJoin.java b/src/main/java/fucoin/actions/join/ActionJoin.java index 205d09a..4c3e083 100644 --- a/src/main/java/fucoin/actions/join/ActionJoin.java +++ b/src/main/java/fucoin/actions/join/ActionJoin.java @@ -5,17 +5,38 @@ import akka.actor.UntypedActorContext; import fucoin.actions.ClientAction; import fucoin.wallet.AbstractWallet; +import java.util.HashMap; +import java.util.concurrent.ThreadLocalRandom; + /** * This action is used by nodes wanting to join the network. */ public class ActionJoin extends ClientAction { + private static final Integer sShareNNeighbour = 2; @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(); - aja.someNeighbors.putAll(node.getKnownNeighbors()); + + //Choose sShareNNeighbour to send the new wallet. + HashMap<String, ActorRef> sendNeighbours = new HashMap<>(); + if (node.getKnownNeighbors().size() > 0) { + String[] rndNeighbours = (String[]) node.getKnownNeighbors().keySet().toArray(); + int sizeKnownNeighbours = rndNeighbours.length; + for (int i = 0; i < sShareNNeighbour; i++) { + String walletName = rndNeighbours[ThreadLocalRandom.current().nextInt(sizeKnownNeighbours)]; + if (sendNeighbours.containsKey(walletName)) { + continue; + } else if (i < sizeKnownNeighbours) { + break; + } else { + sendNeighbours.put(walletName, node.getKnownNeighbors().get(walletName)); + } + } + } + + //Tell the wallet the chosen wallets + ActionJoinAnswer aja = new ActionJoinAnswer(sendNeighbours); sender.tell(aja, self); if (node.getRemoteSuperVisorActor() == null) { diff --git a/src/main/java/fucoin/actions/join/ActionJoinAnswer.java b/src/main/java/fucoin/actions/join/ActionJoinAnswer.java index 89c4aad..4a6dc15 100644 --- a/src/main/java/fucoin/actions/join/ActionJoinAnswer.java +++ b/src/main/java/fucoin/actions/join/ActionJoinAnswer.java @@ -23,7 +23,11 @@ import java.util.Map.Entry; * supervisor, it proceeds to register at the supervisor. */ public class ActionJoinAnswer extends ClientAction { - public final HashMap<String, ActorRef> someNeighbors = new HashMap<>(); + private final HashMap<String, ActorRef> someNeighbors; + + public ActionJoinAnswer(HashMap<String, ActorRef> someNeighbors) { + this.someNeighbors = someNeighbors; + } protected void onAction(ActorRef sender, ActorRef self, UntypedActorContext context, AbstractWallet wallet) { @@ -34,9 +38,10 @@ public class ActionJoinAnswer extends ClientAction { for (Entry<String, ActorRef> neighbor : someNeighbors.entrySet()) { wallet.addKnownNeighbor(neighbor.getKey(), neighbor.getValue()); } + /* for (Entry<String, ActorRef> neighbor : someNeighbors.entrySet()) { neighbor.getValue().tell(new ActionSearchMyWallet(wallet.getName()), self); - } + }*/ } } diff --git a/src/main/java/fucoin/actions/join/ServerActionJoin.java b/src/main/java/fucoin/actions/join/ServerActionJoin.java index 18a1cf6..e385619 100644 --- a/src/main/java/fucoin/actions/join/ServerActionJoin.java +++ b/src/main/java/fucoin/actions/join/ServerActionJoin.java @@ -21,8 +21,7 @@ public class ServerActionJoin extends SuperVisorAction { @Override protected void onAction(ActorRef sender, ActorRef self, UntypedActorContext context, SuperVisorImpl node) { - ActionJoinAnswer aja = new ActionJoinAnswer(); // TODO: Might need added TellSupervisor - aja.someNeighbors.putAll(node.getKnownNeighbors()); + ActionJoinAnswer aja = new ActionJoinAnswer(node.getKnownNeighbors()); // TODO: Might need added TellSupervisor sender.tell(aja, self); node.addKnownNeighbor(name, sender); self.tell( diff --git a/src/main/java/fucoin/wallet/WalletImpl.java b/src/main/java/fucoin/wallet/WalletImpl.java index 951a151..8b7c59d 100644 --- a/src/main/java/fucoin/wallet/WalletImpl.java +++ b/src/main/java/fucoin/wallet/WalletImpl.java @@ -16,6 +16,7 @@ import scala.concurrent.Future; import static akka.dispatch.Futures.future; +import java.util.HashMap; import java.util.concurrent.ConcurrentLinkedQueue; public class WalletImpl extends AbstractWallet { @@ -89,11 +90,14 @@ public class WalletImpl extends AbstractWallet { if (preKnownNeighbour != null) { addKnownNeighbor(preKnownNeighbourName, preKnownNeighbour); preKnownNeighbour.tell(new ActionJoin(), getSelf()); - ActionJoinAnswer aja = new ActionJoinAnswer(); - aja.someNeighbors.putAll(getKnownNeighbors()); - aja.someNeighbors.put(name, getSelf()); + + HashMap neighborsList = new HashMap(); + neighborsList.putAll(getKnownNeighbors()); + neighborsList.put(name, getSelf()); + ActionJoinAnswer aja = new ActionJoinAnswer(neighborsList); preKnownNeighbour.tell(aja, getSelf()); + if (this.getRemoteSuperVisorActor() != null) { preKnownNeighbour.tell(new ActionTellSupervisor(this.getRemoteSuperVisorActor()), self()); } -- GitLab