/*
 * $Id: TcpSocketKey.java 10489 2008-01-23 17:53:38Z dfeist $
 * --------------------------------------------------------------------------------------
 * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 *
 * The software in this package is published under the terms of the CPAL v1.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE.txt file.
 */

package org.mule.transport.tcp;

import org.mule.api.endpoint.ImmutableEndpoint;

import java.net.InetAddress;
import java.net.InetSocketAddress;

/**
 * This is used to adapt an endpoint so that it can be used as a key for sockets.  It must
 * meet two requirements: (1) implement hash and equals in a way that reflects socket identity
 * (ie using address and port); (2) allow access to the endpoint for use in the socket factory.
 * For simplicity we also expose the connector, address and port directly.
 */
public class TcpSocketKey
{

    private InetSocketAddress address;
    private ImmutableEndpoint endpoint;

    public TcpSocketKey(ImmutableEndpoint endpoint)
    {
        if (!(endpoint.getConnector() instanceof TcpConnector))
        {
            throw new IllegalArgumentException("Sockets must be keyed via a TCP endpoint");
        }
        this.endpoint = endpoint;
        address = new InetSocketAddress(
                endpoint.getEndpointURI().getHost(),
                endpoint.getEndpointURI().getPort());
    }

    public boolean equals(Object obj)
    {
        return obj instanceof TcpSocketKey && address.equals(((TcpSocketKey) obj).address);
    }

    public int hashCode()
    {
        return address.hashCode();
    }

    public ImmutableEndpoint getEndpoint()
    {
        return endpoint;
    }

    public TcpConnector getConnector()
    {
        return (TcpConnector) endpoint.getConnector();
    }

    public InetAddress getInetAddress()
    {
        return address.getAddress();
    }

    public int getPort()
    {
        return address.getPort();
    }

    public String toString()
    {
        return getInetAddress() + ":" + getPort();
    }

}
