
import java.rmi.server.UnicastRemoteObject;
import java.rmi.*;
import java.net.*;

public class FlipServerImpl extends UnicastRemoteObject
                            implements FlipServer {

    private boolean state;

    public FlipServerImpl() throws RemoteException {
	state = false;
    }

    public synchronized void flip() {
	state = !state;
    }

    public boolean getState() {
	return state;
    }

    public static void main(String[] args) {
	try {
	    String host = args.length>=1 ? args[0] : "localhost";
	    String uri = "rmi://" +host+ "/" + FlipServer.NAME;

	    FlipServerImpl server = new FlipServerImpl();
	    Naming.rebind(uri,server);
	} catch (MalformedURLException e) {
	    e.printStackTrace();
	} catch (RemoteException e) {
	    e.printStackTrace();
	}
    }
}
