Skip to content
Snippets Groups Projects
Commit 7374bee9 authored by Simon Könnecke's avatar Simon Könnecke
Browse files

tell a new node only 2 random neighbours

parent e9ea7f29
Branches
No related tags found
No related merge requests found
......@@ -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) {
......
......@@ -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);
}
}*/
}
}
......@@ -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(
......
......@@ -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());
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment