//
// CounterServiceImpl.java: implement the remote counter
//

import java.rmi.*;
import java.rmi.server.*;

public class CounterServiceImpl extends UnicastRemoteObject
   implements CounterService
{
   //--------------------------------------------------
   // TODO: consider modifying the following to be unique
   public final static String ServiceName = "counter-service";

   // TODO: change following port to a value between 10000 and 30000
   // The conventional RMI port is 1099, but this will not work on most hosts
   public final static int ServicePort = 10000;

   // TODO: update following to hostname of server
   public final static String ServiceHost = "localhost";
   //--------------------------------------------------

   protected int _value;        // the value that will be modified
                                // In your application, this would probably
                                //   also be written to a file and reloaded
                                //   on demand.

   public CounterServiceImpl(int start) throws RemoteException
   {
      super();
      _value = start;
   }

   // implementations for methods in CounterService
   public int  value()     throws RemoteException { return _value; }
   public void increment() throws RemoteException { _value++; }
   public void decrement() throws RemoteException { _value--; }

   public static void main(String args[])
   {
      System.out.println("Initializing CounterService...");
      try
      {
         CounterService cserv = new CounterServiceImpl(0);
         String serverObjectName = "rmi://localhost:" + ServicePort
            + "/" + ServiceName;
         Naming.rebind(serverObjectName, cserv);
         System.out.println("CounterService running.");
      }
      catch (Exception e)
      {
         System.out.println("Exception: " + e.getMessage());
         e.printStackTrace();
      }
   }
}
