// SimpleSlave.java
 

package dogma.examples.dynamic;
 

import djm.dynamic.*;
import java.rmi.RemoteException;
 

/**
 * This class implements a simple DynamicObjectGroup slave.
 */
public class SimpleSlave extends SlaveImpl {
    private boolean initialized;
    private boolean running;

    /**
     * A default constructor which throws RemoteException is
     * required.  It doesn't have to do anything though.
     */
    public SimpleSlave() throws RemoteException {
    }

    /**
     * Perform initilization and exit.  It is a good idea to call
     * master.registerSlave(), but it is not necessary.
     * Note that the field "master" was inherited from SlaveImpl.
     */
    public void init() {
        try {
            initialized = true;
            master.registerSlave(this);
        } catch (RemoteException re) {
            initialized = false;
            re.printStackTrace();
        }
    }

    /**
     * Perform the slave's work.
     */
    public void start() {
        running = initialized;
        try {
            while(running) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ie) {
                }
                out.println("Requesting work");
                /*
                 * Request work from the master.
                 */
                master.request(SimpleMaster.SAY_HELLO,
                    "Hello from "+ this);
            }
        } catch (RemoteException re) {
            System.out.println("RemoteException in RayTracerEngine.init()");
            re.printStackTrace();
        }
    }

    /**
     * Stop the slave.  It may be restarted later.
     */
    public void stop() {
        running = false;
    }

    /**
     * Destroy this slave.  This instance will not be restarted.
     * It is a good idea to call master.unregisterSlave(),
     * but it is not necessary.
     */
    public void destroy() {
        try {
            master.unregisterSlave(this);
        } catch (RemoteException re) {
            re.printStackTrace();
        }
    }
}