package nxm.nettest.prim;

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

import nxm.nettest.inc.SampleInterface;
import nxm.sys.inc.InternalUseOnly;
import nxm.sys.lib.Table;

/** Sample RMI remote class (for server side) that implements SampleInterface Remote Interface
    @author Neon Ngo
    @since NeXtMidas 3.1.0
 */
@InternalUseOnly("Since 3.5.2")
public class SampleInterfaceImpl extends UnicastRemoteObject implements SampleInterface {
  /** NOTE: this should be changed if the structure/fields of this class changes. */
  private static final long serialVersionUID = 2011121400310L; // for Serializable interface

  /** SampleInterfaceImpl Default Constructor
   *  @throws RemoteException Remote method call Exception
   */
  public SampleInterfaceImpl () throws RemoteException {}

  ////////////////////////////////////////////////////////////////////////////
  // Start Implement SampleInterface
  ////////////////////////////////////////////////////////////////////////////
  /** computes {@link Math#log1p(double)} of x and returns it. */
  public double compute (double x) throws RemoteException {
    return Math.log1p(x);
  }

  public String echo (CharSequence str) throws RemoteException {
    return str.toString();
  }

  public String toString (Table tab) throws RemoteException {
    StringBuffer sb = tab.toStringBuffer();
    String insertVal = "_SERVER_=\"" + getServerId() + "\",";
    sb.insert(1, insertVal);
    return sb.toString();
  }

  public Table toTable (CharSequence str) throws RemoteException {
    Table tab = new Table(str.toString());
    tab.put("_SERVER_", getServerId()); // put our server info into table
    return tab;
  }
  ////////////////////////////////////////////////////////////////////////////
  // End Implement SampleInterface
  ////////////////////////////////////////////////////////////////////////////

  private final String getServerId () {
    return this.ref.remoteToString();
  }
}