/*
 * 
 * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * The contents of this file are subject to the Sun Community Source
 * License v3.0/Jini Technology Specific Attachment v 1.0 (the
 * "License"). You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 * http://wwws.sun.com/software/jini/licensing/SCSL3_JiniTSA1.html.
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Reference Code is Jini Technology Core Platform code, v2.0. The
 * Developer of the Reference Code is Sun Microsystems, Inc.
 * 
 * Contributor(s): Sun Microsystems, Inc.
 * 
 * The contents of this file comply with the Jini Technology Core
 * Platform Compatibility Kit, v2.0A.
 * 
 * Tester(s): Sun Microsystems, Inc.
 * 
 * Test Platform(s):
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Solaris(TM)SPARC/x86
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Linux (Intel x86)
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Windows (Intel Platform)
 * 
 */

package net.jini.jeri.tcp;

import com.sun.jini.action.GetBooleanAction;
import com.sun.jini.jeri.internal.runtime.Util;
import com.sun.jini.logging.Levels;
import java.io.IOException;
import java.io.InputStream;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.nio.channels.SocketChannel;
import java.security.AccessController;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.WeakHashMap;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import javax.net.SocketFactory;
import net.jini.core.constraint.InvocationConstraints;
import net.jini.io.UnsupportedConstraintException;
import net.jini.jeri.Endpoint;
import net.jini.jeri.OutboundRequest;
import net.jini.jeri.OutboundRequestIterator;
import net.jini.jeri.connection.Connection;
import net.jini.jeri.connection.ConnectionEndpoint;
import net.jini.jeri.connection.ConnectionManager;
import net.jini.jeri.connection.OutboundRequestHandle;
import net.jini.security.proxytrust.TrustEquivalence;

/**
 * An implementation of the {@link Endpoint} abstraction that uses TCP
 * sockets (instances of {@link Socket}) for the underlying
 * communication mechanism.
 *
 * <p><code>TcpEndpoint</code> instances contain a host name and a TCP
 * port number, as well as an optional {@link SocketFactory} for
 * customizing the type of <code>Socket</code> to use.  The host name
 * and port number are used as the remote address to connect to when
 * making socket connections.
 *
 * <p><code>TcpEndpoint</code> uses the <a
 * href="../connection/doc-files/mux.html">Jini(TM) extensible remote
 * invocation (Jini ERI) multiplexing protocol</a> to map outgoing
 * requests to socket connections.
 *
 * <p>A <code>SocketFactory</code> used with a
 * <code>TcpEndpoint</code> should be serializable and must implement
 * {@link Object#equals Object.equals} to obey the guidelines that are
 * specified for <code>equals</code> methods of {@link Endpoint}
 * instances.
 *
 * @author Sun Microsystems, Inc.
 * @see TcpServerEndpoint
 * @since 2.0
 **/
public final class TcpEndpoint
    implements Endpoint, TrustEquivalence, Serializable
{
    private static final long serialVersionUID = -2840731722681368933L;

    /**
     * weak set of canonical instances; in order to use WeakHashMap,
     * maps canonical instances to weak references to themselves
     **/
    private static final Map internTable = new WeakHashMap();

    /** client transport logger */
    private static final Logger logger =
	Logger.getLogger("net.jini.jeri.tcp.client");

    /** whether or not to use NIO-based sockets if possible */
    private static final boolean useNIO =		// default false
	((Boolean) AccessController.doPrivileged(new GetBooleanAction(
	    "com.sun.jini.jeri.tcp.useNIO"))).booleanValue();

    /**
     * The host that this <code>TcpEndpoint</code> connects to.
     *
     * @serial
     **/
    private final String host;

    /**
     * The TCP port that this <code>TcpEndpoint</code> connects to.
     *
     * @serial
     **/
    private final int port;

    /**
     * The socket factory that this <code>TcpEndpoint</code> uses to
     * create <code>Socket</code> objects.
     *
     * @serial
     **/
    private final SocketFactory sf;

    private transient ConnectionManager connectionManager;

    /**
     * Returns a <code>TcpEndpoint</code> instance for the given
     * host name and TCP port number.
     *
     * <p>The <code>SocketFactory</code> contained in the returned
     * <code>TcpEndpoint</code> will be <code>null</code>, indicating
     * that this endpoint will create <code>Socket</code> objects
     * directly.
     *
     * @param host the host for the endpoint to connect to
     *
     * @param port the TCP port on the given host for the endpoint to
     * connect to
     *
     * @return a <code>TcpEndpoint</code> instance
     *
     * @throws IllegalArgumentException if the port number is out of
     * the range <code>1</code> to <code>65535</code> (inclusive)
     *
     * @throws NullPointerException if <code>host</code> is
     * <code>null</code>
     **/
    public static TcpEndpoint getInstance(String host, int port) {
	return intern(new TcpEndpoint(host, port, null));
    }

    /**
     * Returns a <code>TcpEndpoint</code> instance for the given host
     * name and TCP port number that contains the given
     * <code>SocketFactory</code>.
     *
     * <p>If the socket factory argument is <code>null</code>, then
     * this endpoint will create <code>Socket</code> objects directly.
     *
     * @param host the host for the endpoint to connect to
     *
     * @param port the TCP port on the given host for the endpoint to
     * connect to
     *
     * @param sf the <code>SocketFactory</code> to use for this
     * <code>TcpEndpoint</code>, or <code>null</code>
     *
     * @return a <code>TcpEndpoint</code> instance
     *
     * @throws IllegalArgumentException if the port number is out of
     * the range <code>1</code> to <code>65535</code> (inclusive)
     *
     * @throws NullPointerException if <code>host</code> is
     * <code>null</code>
     **/
    public static TcpEndpoint getInstance(String host, int port,
					  SocketFactory sf)
    {
	return intern(new TcpEndpoint(host, port, sf));
    }

    /**
     * Returns canonical instance equivalent to given instance.
     **/
    private static TcpEndpoint intern(TcpEndpoint endpoint) {
	synchronized (internTable) {
	    Reference ref = (WeakReference) internTable.get(endpoint);
	    if (ref != null) {
		TcpEndpoint canonical = (TcpEndpoint) ref.get();
		if (canonical != null) {
		    return canonical;
		}
	    }
	    endpoint.connectionManager =
		new ConnectionManager(endpoint.new ConnectionEndpointImpl());
	    internTable.put(endpoint, new WeakReference(endpoint));
	    return endpoint;
	}
    }

    /**
     * Constructs a new instance.
     **/
    private TcpEndpoint(String host, int port, SocketFactory sf) {
	if (host == null) {
	    throw new NullPointerException();
	}
	if (port < 1 || port > 0xFFFF) {
	    throw new IllegalArgumentException(
	        "port number out of range: " + port);
	}

	this.host = host;
	this.port = port;
	this.sf = sf;
    }

    /*
     * [This is not a doc comment to prevent its appearance in
     * TcpEndpoint's serialized form specification.]
     *
     * Resolves deserialized instance to equivalent canonical instance.
     */
    private Object readResolve() {
	return intern(this);
    }

    /**
     * Returns the host that this <code>TcpEndpoint</code> connects to.
     *
     * @return the host that this endpoint connects to
     **/
    public String getHost() {
	return host;
    }

    /**
     * Returns the TCP port that this <code>TcpEndpoint</code> connects to.
     *
     * @return the TCP port that this endpoint connects to
     **/
    public int getPort() {
	return port;
    }

    /**
     * Returns the <code>SocketFactory</code> that this endpoint uses
     * to create <code>Socket</code> objects.
     *
     * @return the socket factory that this endpoint uses to create
     * sockets, or <code>null</code> if no factory is used
     **/
    public SocketFactory getSocketFactory() {
	return sf;
    }

    /**
     * {@inheritDoc}
     *
     * <p>The returned <code>OutboundRequestIterator</code>'s {@link
     * OutboundRequestIterator#next next} method behaves as follows:
     *
     * <blockquote>
     *
     * Initiates an attempt to communicate the request to this remote
     * endpoint.
     *
     * <p>When the implementation of this method needs to create a new
     * <code>Socket</code>, it will do so by invoking one of the
     * <code>createSocket</code> methods on the
     * <code>SocketFactory</code> of the <code>TcpEndpoint</code> that
     * produced this iterator if non-<code>null</code>, or it will
     * create a <code>Socket</code> directly otherwise.
     *
     * <p>If there is a security manager, its {@link
     * SecurityManager#checkConnect(String,int) checkConnect} method
     * will be invoked with the remote IP address and port of the
     * <code>Socket</code> to be used for the communication; this
     * could result in a <code>SecurityException</code>.
     *
     * <p>Throws {@link NoSuchElementException} if this iterator does
     * not support making another attempt to communicate the request
     * (that is, if <code>hasNext</code> would return
     * <code>false</code>).
     *
     * <p>Throws {@link ConnectException} if a failure occurs while
     * connecting a socket to the remote host and port, such as if the
     * connection was refused (no entity is listening on that port or
     * the host is too busy).
     *
     * <p>Throws {@link IOException} if an I/O exception occurs while
     * performing this operation.
     *
     * <p>Throws {@link SecurityException} if there is a security
     * manager and the invocation of its <code>checkConnect</code>
     * method fails.
     *
     * </blockquote>
     *
     * @throws NullPointerException {@inheritDoc}
     **/
    public OutboundRequestIterator
	newRequest(final InvocationConstraints constraints)
    {
	if (constraints == null) {
	    throw new NullPointerException();
	}

	try {
	    Constraints.Distilled distilled =
		Constraints.distill(constraints, false);
	    return connectionManager.newRequest(new Handle(distilled));

	} catch (final UnsupportedConstraintException e) {
	    return new OutboundRequestIterator() {
		private boolean nextCalled = false;
		public boolean hasNext() { return !nextCalled; }
		public OutboundRequest next() throws IOException {
		    if (!hasNext()) { throw new NoSuchElementException(); }
		    nextCalled = true;
		    e.fillInStackTrace(); // REMIND: is this cool?
		    throw e;
		}
	    };
	}
    }

    /**
     * Returns the hash code value for this <code>TcpEndpoint</code>.
     *
     * @return the hash code value for this <code>TcpEndpoint</code>
     **/
    public int hashCode() {
	return host.hashCode() ^ port ^
	    (sf != null ? sf.hashCode() : 0);
    }

    /**
     * Compares the specified object with this
     * <code>TcpEndpoint</code> for equality.
     *
     * <p>This method returns <code>true</code> if and only if
     *
     * <ul>
     *
     * <li>the specified object is also a <code>TcpEndpoint</code>,
     *
     * <li>the host and port in the specified object are equal to the
     * host and port in this object, and
     *
     * <li>either this object and the specified object both have no
     * <code>SocketFactory</code> or the <code>SocketFactory</code> in
     * the specified object has the same class and is equal to the one
     * in this object.
     *
     * </ul>
     *
     * @param obj the object to compare with
     *
     * @return <code>true</code> if <code>obj</code> is equivalent to
     * this object; <code>false</code> otherwise
     **/ 
    public boolean equals(Object obj) {
	if (obj == this) {
	    return true;
	} else if (!(obj instanceof TcpEndpoint)) {
	    return false;
	}
	TcpEndpoint other = (TcpEndpoint) obj;
	return
	    host.equals(other.host) &&
	    port == other.port &&
	    Util.sameClassAndEquals(sf, other.sf);
    }

    /**
     * Returns <code>true</code> if the specified object (which is not
     * yet known to be trusted) is equivalent in trust, content, and
     * function to this known trusted object, and <code>false</code>
     * otherwise.
     *
     * <p>This method returns <code>true</code> if and only if
     *
     * <ul>
     *
     * <li>the specified object is also a <code>TcpEndpoint</code>,
     *
     * <li>the host and port in the specified object are equal to the
     * host and port in this object, and
     *
     * <li>either this object and the specified object both have no
     * <code>SocketFactory</code> or the <code>SocketFactory</code> in
     * the specified object has the same class and is equal to the one
     * in this object.
     *
     * </ul>
     **/
    public boolean checkTrustEquivalence(Object obj) {
	if (obj == this) {
	    return true;
	} else if (!(obj instanceof TcpEndpoint)) {
	    return false;
	}
	TcpEndpoint other = (TcpEndpoint) obj;
	return
	    host.equals(other.host) &&
	    port == other.port &&
	    Util.sameClassAndEquals(sf, other.sf);
    }

    /**
     * Returns a string representation of this
     * <code>TcpEndpoint</code>.
     *
     * @return a string representation of this
     * <code>TcpEndpoint</code>
     **/
    public String toString() {
	return "TcpEndpoint[" + host + ":" + port +
	    (sf != null ? "," + sf : "") + "]";
    }

    /**
     * @throws InvalidObjectException if the host name is
     * <code>null</code> or if the port number is out of the range
     * <code>1</code> to <code>65535</code> (inclusive)
     **/
    private void readObject(ObjectInputStream in)
	throws IOException, ClassNotFoundException
    {
	in.defaultReadObject();
	if (host == null) {
	    throw new InvalidObjectException("null host");
	}
	if (port < 1 || port > 0xFFFF) {
	    throw new InvalidObjectException(
	        "port number out of range: " + port);
	}
    }

    /**
     * OutboundRequestHandle implementation.
     **/
    private class Handle implements OutboundRequestHandle {

	private final Constraints.Distilled distilled;

	Handle(Constraints.Distilled distilled) {
	    this.distilled = distilled;
	}

	TcpEndpoint getTcpEndpoint() {
	    return TcpEndpoint.this;
	}

	Constraints.Distilled getDistilledConstraints() {
	    return distilled;
	}

	InvocationConstraints getUnfulfilledConstraints() {
	    return distilled.getUnfulfilledConstraints();
	}
    }

    /**
     * ConnectionEndpoint implementation.
     *
     * Instances of this class should never get exposed to anything
     * other than our ConnectionManager, which we trust to operate
     * correctly, so we do not bother to validate request handles and
     * connections passed in.
     **/
    private class ConnectionEndpointImpl implements ConnectionEndpoint {

	ConnectionEndpointImpl() { }

	public Connection connect(OutboundRequestHandle handle)
	    throws IOException
	{
	    Handle h = (Handle) handle;
	    Constraints.Distilled distilled = h.getDistilledConstraints();

	    Socket socket;
	    if (sf != null) {
		socket = sf.createSocket();
	    } else {
		if (useNIO) {
		    socket = SocketChannel.open().socket();
		} else {
		    socket = new Socket();
		}
	    }

	    int timeout;
	    if (distilled.hasConnectDeadline()) {
		long now = System.currentTimeMillis();
		long deadline = distilled.getConnectDeadline();
		if (deadline <= now) {
		    throw new SocketTimeoutException(
			"deadline past before connect attempt");
		}
		assert now > 0; // if not, we could overflow
		long delta = deadline - now;
		// assume that no socket connect will last over 24 days
		timeout = (delta > Integer.MAX_VALUE ? 0 : (int) delta);
	    } else {
		timeout = 0;
	    }

	    socket.connect(new InetSocketAddress(host, port), timeout);

	    if (logger.isLoggable(Level.FINE)) {
		logger.log(Level.FINE,
			   "created socket {0}" +
			   (sf != null ? " using factory {1}" : ""),
			   new Object[]{ socket, sf });
	    }

	    /*
	     * Set socket options.
	     */
	    try {
		socket.setTcpNoDelay(true);
	    } catch (SocketException e) {
		// ignore possible failures and proceed anyway
		if (logger.isLoggable(Levels.HANDLED)) {
		    LogRecord lr = new LogRecord(Levels.HANDLED,
			"exception setting TCP_NODELAY on socket {0}");
		    lr.setLoggerName(logger.getName());
		    lr.setSourceClassName(this.getClass().getName());
		    lr.setSourceMethodName("connect");
		    lr.setParameters(new Object[]{ socket });
		    lr.setThrown(e);
		    logger.log(lr);
		}
	    }
	    try {
		socket.setKeepAlive(true);
	    } catch (SocketException e) {
		// ignore possible failures and proceed anyway
		if (logger.isLoggable(Levels.HANDLED)) {
		    LogRecord lr = new LogRecord(Levels.HANDLED,
			"exception setting SO_KEEPALIVE on socket {0}");
		    lr.setLoggerName(logger.getName());
		    lr.setSourceClassName(this.getClass().getName());
		    lr.setSourceMethodName("connect");
		    lr.setParameters(new Object[]{ socket });
		    lr.setThrown(e);
		    logger.log(lr);
		}
	    }

	    return new ConnectionImpl(socket);
	}

	public Connection connect(OutboundRequestHandle handle,
				  Collection active,
				  Collection idle)
	{
	    if (active == null || idle == null) {
		throw new NullPointerException();
	    }

	    /*
	     * The transport level aspects of all constraints
	     * supported by this transport provider are always
	     * satisfied by all open connections, so we don't need to
	     * consider constraints here.
	     */

	    ConnectionImpl connection = null;
	    for (Iterator i = active.iterator(); i.hasNext();) {
		connection = (ConnectionImpl) i.next();
		break;
	    }
	    if (connection == null) {
		for (Iterator i = idle.iterator(); i.hasNext();) {
		    connection = (ConnectionImpl) i.next();
		    break;
		}
	    }
	    if (connection == null) {
		return null;
	    }

	    SecurityManager sm = System.getSecurityManager();
	    if (sm != null) {
		Socket socket = connection.getSocket();
		sm.checkConnect(socket.getInetAddress().getHostAddress(),
				socket.getPort());
	    }
	    return connection;
	}
    }

    /**
     * Connection implementation.
     *
     * Instances of this class should never get exposed to anything
     * other than our ConnectionManager, which we trust to operate
     * correctly, so we do not bother to validate request handles
     * passed in.
     **/
    private static class ConnectionImpl implements Connection {

	private final Socket socket;

	ConnectionImpl(Socket socket) {
	    this.socket = socket;
	}

	Socket getSocket() {
	    return socket;
	}

	public InputStream getInputStream() throws IOException {
	    return socket.getInputStream();
	}

	public OutputStream getOutputStream() throws IOException {
	    return socket.getOutputStream();
	}

	public SocketChannel getChannel() {
	    return socket.getChannel();
	}

	public void populateContext(OutboundRequestHandle handle,
				    Collection context)
	{
	    if (context == null) {
		throw new NullPointerException();
	    }
	}

	public InvocationConstraints
	    getUnfulfilledConstraints(OutboundRequestHandle handle)
	{
	    Handle h = (Handle) handle;
	    return h.getUnfulfilledConstraints();
	}

	public void writeRequestData(OutboundRequestHandle handle,
				     OutputStream out)
	{
	    if (out == null) {
		throw new NullPointerException();
	    }
	}

	public IOException readResponseData(OutboundRequestHandle handle,
					    InputStream in)
	{
	    if (in == null) {
		throw new NullPointerException();
	    }
	    return null;
	}

	public void close() {
	    try {
		socket.close();
	    } catch (Exception e) {
	    }

	    if (logger.isLoggable(Level.FINE)) {
		logger.log(Level.FINE, "closed socket {0}", 
			   new Object[]{ socket });
	    }
	}
    }
}
