/* 
 * BoundedBufferPObs.java (Implements Producer Using Event Notification).
 *
 * Written by : Nitin  Motgi (nmotgi@cs.ucf.edu)
 *
 * This file basically implements Producer using Event Notification. 
 * 
 * Portions copyright(c) 2000 to School of Electrical Engineering and 
 * Computer Science, UCF, Orlando.                   
 *
 * Use and distribution of this source code are strictly governed by 
 * terms and conditions set by the authors.
 * 
 * $Id : BoundedBufferPObs.java, v1.0.0 02/07/2001. $
 *
 * Revision History:
 *
 * 1. Created by.               Nitin,        v1.0.0  02/07/2001.
 * 2. Added Documenttaion,      Nitin,        v1.0.1  02/09/2001.    
*/

import java.util.*;
import java.lang.*;

/*****************************************************************************
 * This class implements Observer and extends Thread for a producer. This 
 * is notified whenever the observable i.e. Buffer gets changed. Upon change
 * the Producer makes an attempt to create a new item and try to put in buffer
 * if buffer is full, he will wait till it gets empty mean has atleast one 
 * slot free to create it.
 ****************************************************************************/
public class BoundedBufferPObs extends Thread implements Observer{

     /* Each producer is given an ID so that the higher level
     routine can interact with a specific processor.*/
     int nID;

     /* End condition indicator set to push the Producer from
     endlessly producing an item and put in the queue.*/
     boolean bEndCondition;

     /* This is state indicator of the observable class, whenever 
     observable changes he makes an attempt to update this state
     to true so that Producer can make and attempt to create a 
     new item and put in the buffer.*/
     boolean bModified;

     /* This is pointer to higher level data structure. This object handle
     is used to make calls to functions written in higher class.*/
     BoundedBufferObs BBObs;

     /* This function is constructor for this class.*/
     public BoundedBufferPObs(int nID,BoundedBufferObs BBObs){
       this.nID   = nID;                  /* Store ID.*/
       bEndCondition = true;              /* Produce one item and put in
                                             shared data structure.*/
       bModified = true;                  /* Initially this is set to
                                             true mean that they can 
                                             start producing items and
                                             put them in the buffer.*/
       this.BBObs = BBObs;                /* Store pointer to higher level
                                             class.*/
     }/* End of BoundedBufferPObs.*/

     /* This is function which is updated when Observable notifies
        this class.*/
     public void update(Observable ob, Object arg){
      ObsState State = (ObsState)arg;           /* Retrieve state of    
                                                   Observable Object.*/
      int nState = State.Get();                 /* Get state count.*/

      switch(nState){
       case 1:
               /* When the Buffer is full. This will stop Producer
                  from requesting to produce new items.*/
               bModified = false;
               break;
       case 2:
               /* This signal is received when buffer becomes empty.*/
               bModified = true;
               break;

       case 3:
               /* This signal is received when they all events for 
                  producer has completed.*/
               bEndCondition = false;
               break;
      }/* End of switch.*/

     }/* End of Update.*/

     /* This program is heart of this thread. */
     public void run(){
         
         /* Till the indicator turns false.*/
         while(bEndCondition){
           if(bModified == true){
             BBObs.Insert();
           }/* End if.*/
         }/* End of while.*/
        }/* End of run.*/

}/* End of BoundedBufferPObs.*/

