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

import fucoin.supervisor.DistributedCommitedTransferRequest;
import fucoin.supervisor.SuperVisor;
import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;

public class ActionPrepareDistributedCommitedTransferAnswer extends CoordinatorTransaction {

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

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

	@Override
	protected void onAction(ActorRef sender, ActorRef self,
			UntypedActorContext context, SuperVisor superVisor) {
		log(""+superVisor.getKnownNeighbors());
		log("granted?"+granted);
		DistributedCommitedTransferRequest request = superVisor.getRequest(id);
		if(granted){
			if(request==null)//unknown DistributedCommitedTransferRequest ignore
				return;
			int newCount = request.addPositiveAnswer(sender);
			System.out.println(newCount+" have agreed on request"+id);
Michael Kmoch's avatar
Michael Kmoch committed
			if(newCount == superVisor.getKnownNeighbors().size()){
Michael Kmoch's avatar
Michael Kmoch committed
				ActionCommitDistributedCommitedTransfer acdct = new ActionCommitDistributedCommitedTransfer(source,target,amount,true,timestamp,id);
Michael Kmoch's avatar
Michael Kmoch committed
				for(ActorRef neighbor : request.getAnswers()){
Michael Kmoch's avatar
Michael Kmoch committed
					neighbor.tell(acdct, self);
Michael Kmoch's avatar
Michael Kmoch committed
				}
				superVisor.deleteRequest(request);
			}
		}else{
			//A client wants to rollback
Michael Kmoch's avatar
Michael Kmoch committed
			if(request!=null){
				ActionCommitDistributedCommitedTransfer acdct = new ActionCommitDistributedCommitedTransfer(source,target,amount,false,timestamp,id); 
				for(ActorRef neighbor : request.getAnswers()){
					neighbor.tell(acdct, self);
				}