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
No related branches found
No related tags found
No related merge requests found
...@@ -5,17 +5,38 @@ import akka.actor.UntypedActorContext; ...@@ -5,17 +5,38 @@ import akka.actor.UntypedActorContext;
import fucoin.actions.ClientAction; import fucoin.actions.ClientAction;
import fucoin.wallet.AbstractWallet; import fucoin.wallet.AbstractWallet;
import java.util.HashMap;
import java.util.concurrent.ThreadLocalRandom;
/** /**
* This action is used by nodes wanting to join the network. * This action is used by nodes wanting to join the network.
*/ */
public class ActionJoin extends ClientAction { public class ActionJoin extends ClientAction {
private static final Integer sShareNNeighbour = 2;
@Override @Override
protected void onAction(ActorRef sender, ActorRef self, UntypedActorContext context, AbstractWallet node) { 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(); //Choose sShareNNeighbour to send the new wallet.
aja.someNeighbors.putAll(node.getKnownNeighbors()); 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); sender.tell(aja, self);
if (node.getRemoteSuperVisorActor() == null) { if (node.getRemoteSuperVisorActor() == null) {
......
...@@ -23,7 +23,11 @@ import java.util.Map.Entry; ...@@ -23,7 +23,11 @@ import java.util.Map.Entry;
* supervisor, it proceeds to register at the supervisor. * supervisor, it proceeds to register at the supervisor.
*/ */
public class ActionJoinAnswer extends ClientAction { 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, protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, AbstractWallet wallet) { UntypedActorContext context, AbstractWallet wallet) {
...@@ -34,9 +38,10 @@ public class ActionJoinAnswer extends ClientAction { ...@@ -34,9 +38,10 @@ public class ActionJoinAnswer extends ClientAction {
for (Entry<String, ActorRef> neighbor : someNeighbors.entrySet()) { for (Entry<String, ActorRef> neighbor : someNeighbors.entrySet()) {
wallet.addKnownNeighbor(neighbor.getKey(), neighbor.getValue()); wallet.addKnownNeighbor(neighbor.getKey(), neighbor.getValue());
} }
/*
for (Entry<String, ActorRef> neighbor : someNeighbors.entrySet()) { for (Entry<String, ActorRef> neighbor : someNeighbors.entrySet()) {
neighbor.getValue().tell(new ActionSearchMyWallet(wallet.getName()), self); neighbor.getValue().tell(new ActionSearchMyWallet(wallet.getName()), self);
} }*/
} }
} }
...@@ -21,8 +21,7 @@ public class ServerActionJoin extends SuperVisorAction { ...@@ -21,8 +21,7 @@ public class ServerActionJoin extends SuperVisorAction {
@Override @Override
protected void onAction(ActorRef sender, ActorRef self, protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, SuperVisorImpl node) { UntypedActorContext context, SuperVisorImpl node) {
ActionJoinAnswer aja = new ActionJoinAnswer(); // TODO: Might need added TellSupervisor ActionJoinAnswer aja = new ActionJoinAnswer(node.getKnownNeighbors()); // TODO: Might need added TellSupervisor
aja.someNeighbors.putAll(node.getKnownNeighbors());
sender.tell(aja, self); sender.tell(aja, self);
node.addKnownNeighbor(name, sender); node.addKnownNeighbor(name, sender);
self.tell( self.tell(
......
...@@ -16,6 +16,7 @@ import scala.concurrent.Future; ...@@ -16,6 +16,7 @@ import scala.concurrent.Future;
import static akka.dispatch.Futures.future; import static akka.dispatch.Futures.future;
import java.util.HashMap;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentLinkedQueue;
public class WalletImpl extends AbstractWallet { public class WalletImpl extends AbstractWallet {
...@@ -89,11 +90,14 @@ public class WalletImpl extends AbstractWallet { ...@@ -89,11 +90,14 @@ public class WalletImpl extends AbstractWallet {
if (preKnownNeighbour != null) { if (preKnownNeighbour != null) {
addKnownNeighbor(preKnownNeighbourName, preKnownNeighbour); addKnownNeighbor(preKnownNeighbourName, preKnownNeighbour);
preKnownNeighbour.tell(new ActionJoin(), getSelf()); preKnownNeighbour.tell(new ActionJoin(), getSelf());
ActionJoinAnswer aja = new ActionJoinAnswer();
aja.someNeighbors.putAll(getKnownNeighbors()); HashMap neighborsList = new HashMap();
aja.someNeighbors.put(name, getSelf()); neighborsList.putAll(getKnownNeighbors());
neighborsList.put(name, getSelf());
ActionJoinAnswer aja = new ActionJoinAnswer(neighborsList);
preKnownNeighbour.tell(aja, getSelf()); preKnownNeighbour.tell(aja, getSelf());
if (this.getRemoteSuperVisorActor() != null) { if (this.getRemoteSuperVisorActor() != null) {
preKnownNeighbour.tell(new ActionTellSupervisor(this.getRemoteSuperVisorActor()), self()); 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