Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
WalletImpl.java 6.17 KiB
package fucoin.wallet;

import akka.actor.ActorRef;
import akka.actor.ActorSelection;
import akka.actor.Props;
import fucoin.actions.ClientAction;
import fucoin.actions.join.ActionJoin;
import fucoin.actions.join.ActionJoinAnswer;
import fucoin.actions.join.ServerActionJoin;
import fucoin.actions.persist.ActionInvokeLeave;
import fucoin.actions.persist.ActionInvokeRevive;
import fucoin.actions.transaction.ActionGetAmount;
import fucoin.actions.transaction.ActionGetAmountAnswer;
import fucoin.actions.transaction.ActionInvokeSentMoney;
import fucoin.gui.WalletGuiControl;

import javax.swing.*;

public class WalletImpl extends AbstractWallet {

    private ActorRef preKnownNeighbour;
    private ActorRef remoteSuperVisorActor;
    private WalletGuiControl gui;
    private String preKnownNeighbourName;
    private boolean isActive;

    public WalletImpl(String name) {
        super(name);
    }

    public WalletImpl(ActorRef preKnownNeighbour, String preKnownNeighbourName,
                      String walletName, ActorRef remoteSuperVisorActor) {
        super(walletName);
        this.preKnownNeighbourName = preKnownNeighbourName;
        this.preKnownNeighbour = preKnownNeighbour;
        this.remoteSuperVisorActor = remoteSuperVisorActor;

        if(remoteSuperVisorActor == null){
            String path = JOptionPane.showInputDialog(null, "Enter a supervisor address: ");
            System.out.println(path);
            ActorSelection selection = getContext().actorSelection(path);
            //this.setRemoteSuperVisorActor(selection.anchor());
            selection.tell(new ActionJoin(), self());
            //selection.tell("Hallo!", self());
        }
    }

    public static Props props(ActorRef preKnownNeighbour, String preKnownNeighbourName,
                              String walletName, ActorRef remoteSuperVisorActor) {
        return Props.create(new WalletCreator(preKnownNeighbour, preKnownNeighbourName, walletName, remoteSuperVisorActor));
    }

    public void addAmount(int amount) {
        setAmount(this.getAmount() + amount);
        log(" My amount is now " + this.getAmount());
    }

    @Override
    public void leave() {
        getSelf().tell(new ActionInvokeLeave(), getSelf());
    }

    @Override
    public void onReceive(Object message) {

        log(getSender().path().name() + " invokes " + getSelf().path().name() + " to do " + message.getClass().getSimpleName());
        if (message instanceof ActionInvokeRevive) {
            ((ActionInvokeRevive) message).doAction(this);
        }
        if (!isActive && !(message instanceof ActionInvokeRevive)) return;

        if (message instanceof ClientAction) {
            ((ClientAction) message).doAction(this);
        }

        /*if (message instanceof ActionJoin){
            ((ActionJoin) message).doAction(this);
        }*/

    }

    @Override
    public void preStart() throws Exception {
        isActive = true;
        if (gui != null) {
            gui.setAddress(getAddress());
        }

        if (preKnownNeighbour != null) {
            addKnownNeighbor(preKnownNeighbourName, preKnownNeighbour);
            preKnownNeighbour.tell(new ActionJoin(), getSelf());
            ActionJoinAnswer aja = new ActionJoinAnswer(this.getRemoteSuperVisorActor());
            aja.someNeighbors.putAll(getKnownNeighbors());
            aja.someNeighbors.put(name, getSelf());
            preKnownNeighbour.tell(aja, getSelf());

        }
    }

    @Override
    public void postStop() throws Exception {
        leave();
        super.postStop();

    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof WalletImpl) {
            WalletImpl wobj = (WalletImpl) obj;
            return amount == wobj.getAmount() && name.equals(wobj.getName());
        }
        return false;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
        System.out.print("Setting amount and supervisor is: ");
        System.out.println(remoteSuperVisorActor);
        if (remoteSuperVisorActor != null) {
            remoteSuperVisorActor.tell(new ActionGetAmountAnswer(getAddress(), getName(), amount), getSelf());
        }
        if (gui != null) {
            gui.setAmount(this.amount);
        }
    }

    public ActorRef getPreKnownNeighbour() {
        return preKnownNeighbour;
    }

    public void setPreKnownNeighbour(ActorRef preKnownNeighbour) {
        this.preKnownNeighbour = preKnownNeighbour;
    }

    public ActorRef getRemoteSuperVisorActor() {
        return remoteSuperVisorActor;
    }

    public void setRemoteSuperVisorActor(ActorRef remoteSuperVisorActor) {
        this.remoteSuperVisorActor = remoteSuperVisorActor;
    }

    public WalletGuiControl getGui() {
        return gui;
    }

    public void setGui(WalletGuiControl gui) {
        this.gui = gui;
    }

    public String getPreKnownNeighbourName() {
        return preKnownNeighbourName;
    }

    public void setPreKnownNeighbourName(String preKnownNeighbourName) {
        this.preKnownNeighbourName = preKnownNeighbourName;
    }

    public boolean isActive() {
        return isActive;
    }

    public void setActive(boolean isActive) {
        this.isActive = isActive;
    }

    @Override
    public boolean addKnownNeighbor(String key, ActorRef value) {
        log(key + " is newNeighbor of " + name + "?" + !getKnownNeighbors().containsKey(key));
        if (getKnownNeighbors().containsKey(key) || key.equals(name)) {
            return false;
        }

        boolean newNeighbor = super.addKnownNeighbor(key, value);
        if (gui != null && newNeighbor) {
            gui.addKnownAddress(key);
        }
        return newNeighbor;
    }

    @Override
    public void log(String msg) {
        if (gui != null) {
            gui.addLogMsg(msg);
        } else {
            System.out.println(msg);
        }
    }

    public void logTransaction(String msg) {
        if (gui != null) {
            gui.addTransactionLogMessage(msg);
        } else {
            System.out.println(msg);
        }
    }

    @Override
    public void send(String address, int amount) {
        getSelf().tell(new ActionInvokeSentMoney(address, amount), getSelf());
    }

}