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

import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;
import fucoin.Wallet;
import fucoin.actions.ClientAction;
import fucoin.supervisor.DistributedCommitedTransferRequest;

public class ActionCommitDistributedCommitedTransfer extends ClientAction{

	private ActorRef source;
	private ActorRef target;
	private int amount;
	private boolean granted;
	private long timestamp;
	private long id;

	
	public ActionCommitDistributedCommitedTransfer(ActorRef source,
			ActorRef target, int amount, boolean granted, long timestamp, long id) {
		this.source=source;
		this.target=target;
		this.amount=amount;
		this.granted=granted;
		this.timestamp=timestamp;
		this.id=id;
	}

	public ActionCommitDistributedCommitedTransfer(
			DistributedCommitedTransferRequest outdatedRequest) {
		this.source=outdatedRequest.getSource();
		this.target=outdatedRequest.getTarget();
		this.amount=0;
		this.granted=false;
		this.timestamp=outdatedRequest.getTimeout();
		this.id=outdatedRequest.getId();
	}

	@Override
	protected void onAction(ActorRef sender, ActorRef self,
			UntypedActorContext context, Wallet wallet) {
Michael Kmoch's avatar
Michael Kmoch committed
		log("ActionCommitDistributedCommitedTransfer is granted?"+granted);
Michael Kmoch's avatar
Michael Kmoch committed
		if(granted){
			Integer sourceAmount = wallet.amounts.getOrDefault(source,0);
			Integer targetAmount = wallet.amounts.getOrDefault(target,0);
			wallet.amounts.put(source,sourceAmount-amount);
			wallet.amounts.put(target,targetAmount+amount);
			if(source.compareTo(self)==0)wallet.amount-=amount;
			else if(target.compareTo(self)==0)wallet.amount+=amount;
			wallet.log("have now "+wallet.amounts.get(self)+" Fucoins");
		}else{
			log("abort transaction with id"+id);
		}
Michael Kmoch's avatar
Michael Kmoch committed
		log("wallet.amounts:"+wallet.amounts);