// SimpleMaster.java
 

package dogma.examples.dynamic;
 

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

/**
 * This class implements a simple DynamicObjectGroup master.
 */
public class SimpleMaster extends MasterImpl implements Startable {
    static final int   REQUEST_WORK = 0;
    static final int   SAY_HELLO = 1;
    private DynamicObjectGroup dog;
 

    public SimpleMaster() throws RemoteException {
    }

    /**
     * This method allows this program to be started from the DJM
     * prompt.  Arguments given to the run command are passed in here.
     */

    public void start(String[] args) {
        try {
            out.print("Creating group...");
            /*
             * Create a DynamicObjectGroup.  Nodes added to the system
             * after this call will be added to the group and have an
             * instance of the slave (dogma.examples.SimpleSlave)
             * started on them.
             */
            dog = new DynamicObjectGroup("dogma.examples.dynamic.SimpleSlave", this);
            out.println("Done.");
        } catch (Exception e) {
               e.printStackTrace();
        }
    }

    /**
     * This method could be used for keeping track of active slaves.
     * You may omit this method.
     */
    public Object registerSlave(Slave slave) {
        out.println("registering a slave");
        return null;
    }

    /**
     * This method could be used for keeping track of active slaves.
     * You may omit this method.
     */
    public Object unregisterSlave(Slave slave) {
        out.println("unregistering a slave");
        return null;
    }

    /**
     * This method is the main method of the master object.  Workers
     * call request in order to request work from the master.
     * The master then returns work to them.
    */
    public Object request(int type, Object req) {
        switch (type) {
        case REQUEST_WORK:
            out.println("received work request");
            return new Integer(0); // return some work
       case SAY_HELLO:
            out.println("MSG: "+ req);
            break;
        default:
            out.println("received a bad request");
        }
        return null;
    }
}