Newer
Older
package fucoin.actions.join;
import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;
import fucoin.actions.ClientAction;
import fucoin.actions.persist.ActionSearchMyWallet;
Simon Könnecke
committed
import fucoin.wallet.AbstractWallet;
import java.util.HashMap;
import java.util.Map.Entry;
/**
* This action is the response from a wallet which is already in the network
* to a wallet which wants to join the network.
*
* The node in the network sends all its known neighbours which then become
* neighbours of the new node. This action also contains a reference to the
* supervisor. If this is the first time the new node learned about the systems
* supervisor, it proceeds to register at the supervisor.
*/
Simon Könnecke
committed
public class ActionJoinAnswer extends ClientAction {
public final HashMap<String, ActorRef> someNeighbors = new HashMap<>();
public final ActorRef supervisor;
public ActionJoinAnswer(ActorRef supervisor) {
this.supervisor = supervisor;
}
Simon Könnecke
committed
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, AbstractWallet wallet) {
wallet.log("Addressed to " + self.path().name() + " from " + sender.path().name() + ": someNeighbors:" + someNeighbors);
Simon Könnecke
committed
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);
}
// register at the supervisor if the wallet just learned about it
if (wallet.getRemoteSuperVisorActor() == null) {
wallet.setRemoteSuperVisorActor(supervisor);
}
Simon Könnecke
committed
}