Skip to content
Snippets Groups Projects
ActionInvokeSentMoney.java 1.48 KiB
Newer Older
Michael Kmoch's avatar
Michael Kmoch committed
package fucoin.actions.transaction;


import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;
import fucoin.actions.search.ActionSearchWalletReference;
import fucoin.wallet.AbstractWallet;

public class ActionInvokeSentMoney extends Transaction {
    public final String name;
    public final int amount;
Michael Kmoch's avatar
Michael Kmoch committed

    public ActionInvokeSentMoney(String name, int amount) {
Michael Kmoch's avatar
Michael Kmoch committed
        this.amount = amount;
    }

    @Override
    protected void onAction(ActorRef sender, ActorRef self,
                            UntypedActorContext context, AbstractWallet wallet) {
        wallet.log(wallet.getKnownNeighbors() + "");
        if (wallet.getKnownNeighbors().containsKey(name)) {
            wallet.getRemoteSuperVisorActor().tell(
                    new ActionInvokeDistributedCommittedTransfer(self, wallet.getKnownNeighbors().get(name), amount), sender);
        } else {
            ActionSearchWalletReference aswr = new ActionSearchWalletReference(name);
            for (ActorRef neighbor : wallet.getKnownNeighbors().values()) {
                neighbor.tell(aswr, self);
            }
            sleep(self, context, 200);
            self.tell(this, self);
        }
    }

    private void sleep(ActorRef self, UntypedActorContext context, int timeOfIdle) {
        try {
            context.unwatch(self);
            Thread.sleep(timeOfIdle);
            context.watch(self);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
Michael Kmoch's avatar
Michael Kmoch committed
}