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

import java.awt.Label;
import java.util.ArrayList;
import java.util.HashMap;
Michael Kmoch's avatar
Michael Kmoch committed
import java.util.LinkedList;
Michael Kmoch's avatar
Michael Kmoch committed
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Michael Kmoch's avatar
Michael Kmoch committed
import java.util.Queue;
Michael Kmoch's avatar
Michael Kmoch committed
import java.util.concurrent.Semaphore;

import akka.actor.ActorRef;
import akka.actor.Props;
Michael Kmoch's avatar
Michael Kmoch committed
import fucoin.AbstractNode;
import fucoin.actions.Action;
import fucoin.actions.ClientAction;
Michael Kmoch's avatar
Michael Kmoch committed
import fucoin.actions.persist.ActionInvokeUpdate;
import fucoin.actions.transaction.ActionGetAmountAnswer;
import fucoin.actions.transaction.SuperVisorAction;
Michael Kmoch's avatar
Michael Kmoch committed

Michael Kmoch's avatar
Michael Kmoch committed
public class SuperVisor extends AbstractNode {
Michael Kmoch's avatar
Michael Kmoch committed


    private AmountTableModel amountTableModel;
    private Label averageamountLbl;

    public SuperVisor(AmountTableModel amountTableModel, Label averageamountLbl) {
        this.amountTableModel = amountTableModel;
        this.averageamountLbl = averageamountLbl;
    }

    @Override
    public void onReceive(Object msg) {

        // dirty but necessary since ActionGetAmountAnswer is a
        // ClientAction for some reason
        if (msg instanceof ActionGetAmountAnswer) {
            ActionGetAmountAnswer answer = (ActionGetAmountAnswer) msg;
            amountTableModel.updateTable(answer.address, answer.name, answer.amount);
        } else if (msg instanceof SuperVisorAction) {
            ((Action) msg).doAction(this);
        }
    }

    Semaphore mutex = new Semaphore(1);
    private Map<Long, DistributedCommitedTransferRequest> requestQueue;

    public static Props props() {
        return Props.create(SuperVisor.class, new SuperVisorCreator());
    }

    public void updateValues() {
        getSelf().tell(new ActionInvokeUpdate(), getSelf());
    }

    public void exit() {
        getContext().stop(getSelf());
    }

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

    public void addDistributedCommitedTransferRequest(
            DistributedCommitedTransferRequest request) {
        System.out.println("Füge Request in queue ein: " + request.getId());
        requestQueue.put(request.getId(), request);
    }

    @Override
    public void preStart() throws Exception {
        super.preStart();
        requestQueue = new HashMap<>();
        self().tell(new ActionUpdateQueue(), self());
    }

    /**
     * filters the request for outdated and removes them
     *
     * @return deleted outdated request
     */
    public List<DistributedCommitedTransferRequest> updateList() {
        List<Long> deletesIds = new ArrayList<Long>();
        List<DistributedCommitedTransferRequest> deletes = new ArrayList<DistributedCommitedTransferRequest>();
        for (Entry<Long, DistributedCommitedTransferRequest> outdatedRequest : requestQueue.entrySet()) {
            if (outdatedRequest.getValue().getTimeout() < System.currentTimeMillis()) {
                deletesIds.add(outdatedRequest.getKey());
                deletes.add(outdatedRequest.getValue());
            }
        }
        for (Long delete : deletesIds) {
            requestQueue.remove(delete);
        }

        return deletes;
    }

    public DistributedCommitedTransferRequest getRequest(Long id) {
        DistributedCommitedTransferRequest searchedrequest = requestQueue.get(id);
        return searchedrequest;
    }

    public void deleteRequest(DistributedCommitedTransferRequest request) {
        requestQueue.remove(request.getId());
    }
Michael Kmoch's avatar
Michael Kmoch committed
}