/** FILE: bufferfi.java **/

package BUFFER;


// Referenced classes of package BUFFER:
//            buffer, accex

/*
 * gestione FIFO del buffer:
 *  
 * ad ogni thread che arriva assegna un numero che stà a significare il suo turno.
 * quando termina un thread si passa al turno successivo.
 */


public class bufferfi extends buffer
{

    public synchronized void put(String s)
        throws InterruptedException
    {
        accex accex1 = accput;   // nuovo oggetto assegnato al thread per memorizzare gli accessi
        accex1.next = new accex();
        accex1.acc1 = accessi++;    // QUI incremento accessi == gli EVENTI
        accput = accput.next;
	  // è il mio turno?? se si c'è spazio nel buffer??
        for(int i = putinfila++; i != putcorrente || liberi < s.length(); wait());  
        putcorrente++;  // incrementa il turno
        accex1.acc2 = accessi;
        notifyAll();
        super.put(s);  // inserimento --> vedi buffer.java
    }

    public synchronized String get()
        throws InterruptedException
    {
        accex accex1 = accget;   // nuovo oggetto assegnato al thread per memorizzare gli accessi
        accex1.next = new accex();
        accex1.acc1 = accessi++;    // QUI incremento accessi == gli EVENTI
        accget = accget.next;
        // è il mio turno?? se si c'è qualcosa nel buffer??
        for(int i = getinfila++; i != getcorrente || liberi == 200; wait());   
        getcorrente++;  // incrementa il turno
        accex1.acc2 = accessi;
        notifyAll();
        return super.get(); // cancellazione --> buffer.java
    }

    public bufferfi()
    {
        putcorrente = 1;
        getcorrente = 1;
        putinfila = 1;
        getinfila = 1;
    }

    int putcorrente;
    int getcorrente;
    int putinfila;
    int getinfila;
}