/* 
 * ReaderWriterWObs.java (Implements Readers/Writers Writer.)
 *
 * Written by : Nitin  Motgi (nmotgi@cs.ucf.edu)
 *
 * 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 : ReaderWriterWObs.java, v1.0.0 02/07/2001. $
 *
 * Revision History Revisited:
*/

import java.util.*;
import java.lang.*;

/* Start of ReaderWriterWObs.*/
public class ReaderWriterWObs extends Thread implements Observer{
  /* Each consumer is given an ID so that the higher level
     routine can interact with a specific Reader.*/
     int nID;

  /* This is up level pointer points to the parent class
     which provides mechanism for synchronization.*/
     ReaderWriterObs RWObs;

  /* End condition indicator set to push the Producer from
     endlessly producing an item and put in the queue.*/
     boolean bEndCondition;

  /* This is used to indicate the state of Writer.*/
     boolean bModified;


     /* 
      * This function is constructor for this class.
      */
     public ReaderWriterWObs(int nID,ReaderWriterObs RWObs){
       this.nID   = nID;                  /* Store ID.*/
       this.RWObs = RWObs;                /* Pointer to Higher class.*/
       bEndCondition = true;              /* Produce one item and put in
                                             shared data structure.*/
       bModified = true;                  /* Intialized to can write.*/      
     }/* End of ReaderWriterWObs.*/

     public void update(Observable Ob, Object arg){
      ObsState State = (ObsState)arg;
      int nState = State.Get();

      switch(nState){
        case 4:         /* All readers read the value.*/
                bModified = true;
                break;
        case 2:
                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){
           RWObs.Write();
           bModified = false;
          }/* End if.*/
         }/* End of while.*/
      }/* End of run.*/

}/* End of ReaderWriterWObs.*/

