package tom.java.agent;
//import java.rmi.*;
//import java.rmi.server.*;
//import java.rmi.registry.*;

import java.rmi.server.UnicastRemoteObject;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;

//public class AgentFactoryImpl extends UnicastRemoteObject implements AgentFactory
class AgentFactoryImpl extends UnicastRemoteObject implements AgentFactory
{
        int data = 0;

        //no-op constructor; you've got to implement it, though, or
        //the compile fails with a really strange message
        public AgentFactoryImpl() throws RemoteException
        {
                super();
        }
        //main: a nifty trick here is that the program creates its own
        //RMI registry
        public static void main(String[] argv) 
        {
                try
                {
                LocateRegistry.createRegistry(1099);
                System.out.println("Registry started");
                LocateRegistry.getRegistry().bind("AgentFactory",new AgentFactoryImpl());
                System.out.println("Agent factory bound");
                } catch (Exception e)
                {
                        System.out.println("Exception caught: " + e);
                        e.printStackTrace();
                }
        }

        public Agent getAgent() throws RemoteException
        {
                Agent a = new Agent();
                a.setData(this.data); 
                return a;
        }

        public void setAgent(Agent a) throws RemoteException
        {
                this.data += a.getData();          
        }
}

