/**
 * @author Dr. Haiping Xu
 * Created at the CIS Department, UMass Dartmouth
 */

import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.net.*;

public class RMIServer extends java.rmi.server.UnicastRemoteObject
                                        implements ReceiveMessageInterface {
    int serverPort;
    String serverAddress;
    Registry registry;    // rmi registry for lookup the remote objects.

    // This method is called from the remote RMI client.
    // This is the implementation of the receiveMessageInterface.
    public void receiveMessage(String x) throws RemoteException {
        System.out.println(x);
    }

    public RMIServer() throws RemoteException {

        try{
            // get the address of this host.
            serverAddress= (InetAddress.getLocalHost()).toString();

        } catch(Exception e){
            throw new RemoteException("can't get inet address.");
        }

        serverPort=8686;  // server port (registry's port)
        System.out.println("Server address: " + serverAddress+", port:" + serverPort);

        try{
            // create the registry and bind the name and object.
            registry = LocateRegistry.createRegistry(serverPort);
            registry.rebind("RMIServer", this);
        } catch(RemoteException e){
            throw e;
        }
    }

    public static void main(String args[]) {

        try{
            RMIServer s=new RMIServer();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
}