package therm;
 
import java.rmi.*;
import java.rmi.server.*;
import java.util.Observable;
import java.util.Vector;
 
//public class ThermometerImpl extends java.util.Observable
public class ThermometerImpl extends UnicastRemoteObject
                         implements Thermometer,
                                    java.io.Serializable
{
    String title;
    int minTemp, maxTemp, temp;

    public ThermometerImpl()		// Default constructor
        throws RemoteException
    {
        // Export the remote object by listening for incoming calls to the obj
        // on a port.
        //UnicastRemoteObject.exportObject( (Remote)this );
        super();

        title = "Farhenheit Thermometer";
        minTemp = 0; maxTemp = 100; temp = 50;
    }

    public ThermometerImpl(int temp)		// Constructor
        throws RemoteException
    {
        this();
        this.temp = temp;
    }

    public ThermometerImpl(int minTemp, int maxTemp, int temp)	// Constructor
        throws RemoteException
    {
        this(temp);
        this.minTemp = minTemp;
        this.maxTemp = maxTemp;
    }

    public ThermometerImpl(String title, int minTemp, int maxTemp, int temp)
        throws RemoteException
    {
        this(minTemp,maxTemp,temp);
        this.title = title;
    }

    public void setTitle(String t)
    {
        System.out.println( "ThermometerImpl: setTitle()" );
        title = t;
        changed();		// Notify observers of change
        System.out.println( "ThermometerImpl: setTitle() exiting" );
    }

    public String getTitle()
       {return title;}

    public void setTemp(int t)
    {
        if (0 <= t && t <= 100)
            temp = t;			// Set temperature
        else
            {
             System.out.println(
                 "New temp out of range.  Setting to default of 50F" );
             temp = 50;			// Set to default if t out of range
            }

        changed();			// Notify observers of change
    }

    public int getTemp()
       {return temp;}

    public int getMinTemp()
       {return minTemp;}

    public int getMaxTemp()
       {return maxTemp;}

    public void changed()
    {
        //setChanged();		// Signal that a noteworthy change has occurred.
        notifyObservers();
    }

    // The following code is modeled on that provided by
    // java.util.Observable

    private Vector observers = new Vector();

    void notifyObservers()
    {
        System.out.println("Enter ThermometerImpl: notifyObservers()");
        int size = observers.size();
        
        for ( int i = 0; i < size; i++ )
        {
            try{
            ((ThermObserver)(observers.elementAt(i))).update( this,null );
            }
            catch ( RemoteException e )
            {}
        }
    }

    public void addObserver( Object o ) throws java.rmi.RemoteException
    {
        System.out.println("Enter ThermometerImpl: addObserver(), o = " +
                            o );
        if ( !observers.contains( o ) )
            observers.addElement( o );
    }

    public void deleteObserver( Object o ) throws java.rmi.RemoteException
    {
        System.out.println("Enter ThermometerImpl: deleteObservers()");
        observers.removeElement( o );
    }
}
