Skip to content
Snippets Groups Projects
Wallet.java 4.75 KiB
Newer Older
Michael Kmoch's avatar
Michael Kmoch committed
package fucoin;

import akka.actor.ActorRef;
import akka.actor.Props;
Michael Kmoch's avatar
Michael Kmoch committed
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.ActionGetAmountAnswer;
import fucoin.actions.transaction.ActionInvokeSentMoney;
Michael Kmoch's avatar
Michael Kmoch committed
import fucoin.gui.IWalletControle;
import fucoin.gui.IWalletGuiControle;
Michael Kmoch's avatar
Michael Kmoch committed
public class Wallet extends AbstractWallet implements IWalletControle{
Michael Kmoch's avatar
Michael Kmoch committed
	private ActorRef preknownNeighbour;
	private ActorRef remoteSuperVisorActor;
	private IWalletGuiControle gui;
	private String preknownNeighbourName;
	private boolean isActive;
Michael Kmoch's avatar
Michael Kmoch committed
	public Wallet(ActorRef preknownNeighbour, String preknownNeighbourName, String walletName, ActorRef remoteSuperVisorActor) {
		super(walletName);
		this.preknownNeighbourName=preknownNeighbourName;
		this.preknownNeighbour=preknownNeighbour;
		this.remoteSuperVisorActor=remoteSuperVisorActor;
Michael Kmoch's avatar
Michael Kmoch committed
	public void addAmount(int amount) {
Michael Kmoch's avatar
Michael Kmoch committed
		setAmount(this.amount+amount);
		log(" My amount is now "+this.amount);
Michael Kmoch's avatar
Michael Kmoch committed
	}
Michael Kmoch's avatar
Michael Kmoch committed
	@Override
Michael Kmoch's avatar
Michael Kmoch committed
	public void leave() {
Michael Kmoch's avatar
Michael Kmoch committed
		getSelf().tell(new ActionInvokeLeave(), getSelf());
Michael Kmoch's avatar
Michael Kmoch committed
	}
Michael Kmoch's avatar
Michael Kmoch committed
	@Override
Michael Kmoch's avatar
Michael Kmoch committed
	public void onReceive(Object message) {
Michael Kmoch's avatar
Michael Kmoch committed
		log(message.getClass().getSimpleName());
		
		//log(getSender().path().name()+" invokes "+getSelf().path().name()+" to do "+message.getClass().getSimpleName());
Michael Kmoch's avatar
Michael Kmoch committed
		if(message instanceof ActionInvokeRevive){
Michael Kmoch's avatar
Michael Kmoch committed
			((ActionInvokeRevive) message).doAction(this);
Michael Kmoch's avatar
Michael Kmoch committed
		}
Michael Kmoch's avatar
Michael Kmoch committed
		if(!isActive&&!(message instanceof ActionInvokeRevive))return;
Michael Kmoch's avatar
Michael Kmoch committed
		//System.out.println(message);
Michael Kmoch's avatar
Michael Kmoch committed
		if(message instanceof ClientAction){
			((ClientAction) message).doAction(this);
Michael Kmoch's avatar
Michael Kmoch committed
		}
Michael Kmoch's avatar
Michael Kmoch committed
	@Override
	public void preStart() throws Exception {
Michael Kmoch's avatar
Michael Kmoch committed
		isActive=true;
		if(gui!=null){
			gui.setAddress(getAddress());
		}
		String path = "akka.tcp://Core@127.0.0.1:1234/user/Main";
Michael Kmoch's avatar
Michael Kmoch committed
		//System.out.println(getContext().provider().getExternalAddressFor(getSelf().path().address()));
Michael Kmoch's avatar
Michael Kmoch committed
		//log("my address should be "+getAddress());
		//log(""+preknownNeighbour);
		//knownNeighbors.put(getName(),getSelf());
		
Michael Kmoch's avatar
Michael Kmoch committed
		//System.out.println(knownNeighbors);
Michael Kmoch's avatar
Michael Kmoch committed
		if(preknownNeighbour!=null){
Michael Kmoch's avatar
Michael Kmoch committed
			addKnownNeighbor(preknownNeighbourName,preknownNeighbour);
Michael Kmoch's avatar
Michael Kmoch committed
			preknownNeighbour.tell(new ActionJoin(), getSelf());
			ActionJoinAnswer aja = new ActionJoinAnswer();
Michael Kmoch's avatar
Michael Kmoch committed
			aja.someNeighbors.putAll(getKnownNeighbors());
			aja.someNeighbors.put(name, getSelf());
Michael Kmoch's avatar
Michael Kmoch committed
			preknownNeighbour.tell(aja, getSelf());
			
Michael Kmoch's avatar
Michael Kmoch committed
		}
Michael Kmoch's avatar
Michael Kmoch committed
		//setAmount(100);
		//remoteSuperVisorActor.tell(new ServerActionJoin(name), getSelf());
Michael Kmoch's avatar
Michael Kmoch committed
	@Override
	public void postStop() throws Exception {
		leave();
		super.postStop();
Michael Kmoch's avatar
Michael Kmoch committed
	public static Props props(ActorRef preknownNeighbour, String preknownNeighbourName, String walletName, ActorRef remoteSuperVisorActor) {
		return Props.create(Wallet.class,new WalletCreator(preknownNeighbour,preknownNeighbourName,walletName,remoteSuperVisorActor));
Michael Kmoch's avatar
Michael Kmoch committed
	}
	
	@Override
Michael Kmoch's avatar
Michael Kmoch committed
	public boolean equals(Object obj) {
		if(obj instanceof Wallet){
			Wallet wobj = (Wallet) obj;
			return amount==wobj.amount&&name.equals(wobj.name);
		}
		return false;
Michael Kmoch's avatar
Michael Kmoch committed
	}

	public void setGui(IWalletGuiControle gui) {
		this.gui=gui;
	}
Michael Kmoch's avatar
Michael Kmoch committed
	
	public void setAmount(int amount){
		this.amount = amount;
		if(remoteSuperVisorActor != null){
			remoteSuperVisorActor.tell(new ActionGetAmountAnswer(getAddress(), getName(), amount), getSelf());
		}
		if(gui!=null){
			gui.setAmount(this.amount);
		}
Michael Kmoch's avatar
Michael Kmoch committed
	}

	public ActorRef getPreknownNeighbour() {
		return preknownNeighbour;
	}

	public ActorRef getRemoteSuperVisorActor() {
		return remoteSuperVisorActor;
	}

	public IWalletGuiControle getGui() {
		return gui;
	}

	public String getPreknownNeighbourName() {
		return preknownNeighbourName;
	}

	public boolean isActive() {
		return isActive;
	}
	
	@Override
	public boolean addKnownNeighbor(String key, ActorRef value) {
		log(key+" is newNeighbor?"+!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;
	}

	public void setPreknownNeighbour(ActorRef preknownNeighbour) {
		this.preknownNeighbour = preknownNeighbour;
	}

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

	public void setPreknownNeighbourName(String preknownNeighbourName) {
		this.preknownNeighbourName = preknownNeighbourName;
	}

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

	@Override
	public void send(String address, int amount) {
		getSelf().tell(new ActionInvokeSentMoney(address, amount), getSelf());
Michael Kmoch's avatar
Michael Kmoch committed
	}