package nxm.audio.net;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.rmi.server.RMISocketFactory;

/**
 *  CustomRMISocketFactory extends the java.rmi.server.RMISocketFactory class in order
 *  to customize the settings of the client and server sockets that are obtained for
 *  RMI calls. Use of this factory rather than the default RMISocketFactory is
 *  established with a call to the static method
 *  RMISocketFactory.setSocketFactory(RMISocketFactory rsf).
 *
 *  @author Lisa M. Tursky
 */
public class CustomRMISocketFactory extends RMISocketFactory{

  /** equals is based on Class, so Hash fixed for this class */
  private static final int CLASS_HASH = 67827773; // Note: ASCII for CRMI is 067082077073

  /** default constructor */
  public CustomRMISocketFactory () {}

  public Socket createSocket(String host, int port) throws IOException {
    Socket sock = new Socket(host,port);
    sock.setSoTimeout(5000);
    sock.setReuseAddress(true);
    //boolean keepAlive = sock.getKeepAlive();
    //int linger = sock.getSoLinger();
    //sock.setKeepAlive(true);
    //JOptionPane.showMessageDialog(null, "Created socket with keepAlive " + keepAlive + " and SO_LINGER " + linger);
    //keepAlive is false by default
    //SO_LINGER is -1 by default
    return sock;
  }

  public ServerSocket createServerSocket(int port) throws IOException {
    ServerSocket sock = new ServerSocket(port);
    sock.setSoTimeout(5000);
    sock.setReuseAddress(true);
    return sock;
  }

  public boolean equals(Object obj) {
    return obj != null && this.getClass().equals(obj.getClass());
  }

  public int hashCode() {
    return CLASS_HASH;
  }
}
