Session.java

48 lines | 1.31 kB Blame History Raw Download
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufrgs.inf.prosoft.requestssimulator;

import br.ufrgs.inf.prosoft.requestssimulator.requests.Request;
import br.ufrgs.inf.prosoft.requestssimulator.requests.RequestPlan;
import java.util.Collection;
import java.util.Random;

/**
 *
 * @author romulo
 */
public class Session {

    private final Collection<RequestPlan> roots;

    public Session(Collection<RequestPlan> roots) {
        this.roots = roots;
    }

    public void run() {
        Random random = new Random();
        Request request = null;
        int probability = 0;
        while (probability < 50) {
            request = pickNextRequest(request);
            request.fire();
            probability = random.nextInt(100);
        }
    }

    public Request pickNextRequest(Request currentRequest) {
        if (currentRequest == null) {
            if (this.roots == null || this.roots.isEmpty()) {
                throw new RuntimeException("root within cycle");
            }
            currentRequest = RequestPlan.get("root")
                    .addLinks(this.roots)
                    .build();
        }
        return currentRequest.pickNextRequest();
    }
}