Skip to content
Snippets Groups Projects
ActionJoinAnswer.java 1.79 KiB
Newer Older
Michael Kmoch's avatar
Michael Kmoch committed
package fucoin.actions.join;

import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;
import fucoin.actions.ClientAction;
import fucoin.actions.persist.ActionSearchMyWallet;
import fucoin.wallet.AbstractWallet;
import java.util.HashMap;
import java.util.Map.Entry;
Michael Kmoch's avatar
Michael Kmoch committed

/**
 * 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.
 */
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) {
        wallet.log("Addressed to " + self.path().name() + " from " + sender.path().name() + ": someNeighbors:" + someNeighbors);

        // your neighbours? my neighbours!
        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);
        }