/*

THIS SOFTWARE IS DISTRIBUTED UNDER GPL LICENSE

Copyright (C) 2006  Lukasz Grzegorz Maciak
http://terminally-incoherent.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA or visit http://www.gnu.org/licenses/gpl.txt.

*/



import java.util.Random;

/*
 * Created on Mar 25, 2006
 */

/**
 * Reuesting Trhead for the Bankers' Algorithm implementation.
 *
 * @author Lukasz Grzegorz Maciak
 *
 */
public class Requester extends Thread
{
	private Banker banker; 	// The Banker object associated with this thread
	private int id; 		// The id of this thread
	private boolean[] done;	// The array indicating which requests are completed

	private int[][] requests;// Array of requests that will be made by this thread
	private int[] max;		// Array of maimum claims on resources

	private Random rgen;	// a random number generator

	/**
	 * The default constructor for the Requester
	 *
	 * @param banker a Banker object
	 * @param id and id of this thread
	 * @param max array of maximum claims on resources this thread may need
	 * @param req an array of requests that will be made by this thread
	 */
	public Requester(Banker banker, int id, int[] max, int req)
	{
		this.id = id;

		this.banker = banker;
		this.max = max;

		banker.setMax(id, max);

		rgen = new Random();

		initiate(req);
	}

	/**
	 * Initiate the request array
	 *
	 * @param req number of requests to generate
	 */
	private void initiate(int req)
	{
		requests = new int[req][];


		for(int i=0; i<requests.length; i++)
		{
			requests[i] = new int[max.length];

			for(int j=0; j<max.length; j++)
			{
				requests[i][j] = rgen.nextInt(100)%max[j];
			}
		}

		done = new boolean[requests.length];

		for(int i=0; i<done.length; i++)
			done[i]= false;

		System.out.println("( -0- ) " + id + " created. Max: " + banker.showArray(max) + "\n\t\tReq:\t" + banker.showArray(requests));

	}

	public void run()
	{
		for(int i=0; i<requests.length; i++)
		{
			// request resources from the banker
			if(banker.request(id, requests[i]))
			{
				// pretend to do some work
				try{this.sleep(1000);}catch (InterruptedException e){e.printStackTrace();}

				// release the resources
				banker.release(id, requests[i]);
				done[i] = true;
			}
		}

		// all the work is done - release the claim and ternminate
		banker.releaseFromPool(id);
		System.out.println("( -0- ) " + id + " terminated and revoked it's maximum claim");
	}


}
