//Created automatically by CentiJ
package graphics.raytracer;

import rmi.rmiSynth.Host;
import rmi.rmiSynth.HostManager;
import rmi.rmiSynth.HostManagerInterface;

import java.awt.*;
import java.rmi.AlreadyBoundException;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

/**
 *  Launch <code>DoImageServer</code> <em>after</em>
 * a launch of the <code>DoImageClientManager</code>.
 * The DoImageServer must contact the HostManager to register itself
 * as available for use.
 */
public class DoImageServer
    extends java.rmi.server.UnicastRemoteObject
    implements DoImageInterface {

  private graphics.raytracer.DoImage v = null;


  public DoImageServer(Dimension d)
      throws java.rmi.RemoteException {
    super();
    v = new graphics.raytracer.DoImage(d);
    registerWithTheHostMaster();
  }

  private void registerWithTheHostMaster() throws RemoteException {
    System.out.println("getting proxy...");
    HostManagerInterface hm =
        HostManager.getProxy();
    System.out.println("contacting HostManager...");
    Host h = new Host();
    System.out.println("Adding new host:" + h);
    hm.add(h);
  }

  public int[][] int2SubInt(int i[][])
      throws java.rmi.RemoteException {
    return v.int2SubInt(i);
  }

  public int[][] getSubPixels()
      throws java.rmi.RemoteException {
    return v.getSubPixels();
  }


  public void doTheWork()
      throws java.rmi.RemoteException {
    v.doTheWork();
  }

  public void setBand(java.awt.Dimension p0) throws java.rmi.RemoteException {
    v.setBand(p0);
  }

  public int[][] getPixels() throws java.rmi.RemoteException {
    return v.getPixels();
  }

  public java.awt.Dimension getSize() throws java.rmi.RemoteException {
    return v.getSize();
  }

  public void setSize(Dimension d) throws java.rmi.RemoteException {
    v.setSize(d);
  }

  public static void main(String args[]) {
    try {
      createRegistryIfNeeded();

      bindToRegistry();
    } catch (Exception e) {
      e.printStackTrace();
    }

  }

  private static void createRegistryIfNeeded() {
    try {
      // Create and install a security manager
      System.setSecurityManager(new RMISecurityManager());

      //Create the registry and bind the Server class to the registry
      LocateRegistry.createRegistry(1099);
    } catch (RemoteException e) {
      System.out.println("DoImageServer: port 1099 Registry detected");
    }
  }

  private static void bindToRegistry() throws RemoteException, AlreadyBoundException {
    DoImageServer rs = new DoImageServer(new Dimension(200, 200));
    Registry r = LocateRegistry.getRegistry();
    r.bind("DoImageServer", rs);
    System.out.println("DoImageServer is bound and running");
  }

}