All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.objectweb.dream.protocol.IPExportIdentifier Maven / Gradle / Ivy

/**
 * Dream
 * Copyright (C) 2003-2004 INRIA Rhone-Alpes
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: [email protected]
 *
 * Initial developer(s): Matthieu Leclercq
 * Contributor(s): 
 */

package org.objectweb.dream.protocol;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * Export identifier created by TCP/IP protocol
 */
public final class IPExportIdentifier implements ExportIdentifier, Externalizable {

    private static final long serialVersionUID = 485348787L;

    private String hostName;

    private int port;

    private transient InetAddress address = null;

    /** Default constructor */
    public IPExportIdentifier() {
    }

    /**
     * @param hostName
     *            the host name of the exported interface.
     * @param port
     *            the port number of the exported interface.
     */
    public IPExportIdentifier(String hostName, int port) {
        this.hostName = hostName;
        this.port = port;
    }

    /**
     * @return Returns the hostName.
     */
    public String getHostName() {
        return hostName;
    }

    /**
     * @return Returns the port.
     */
    public int getPort() {
        return port;
    }

    private InetAddress getAddress() throws UnknownHostException {
        if (address == null) {
            address = InetAddress.getByName(hostName);
        }
        return address;
    } // ---------------------------------------------------------------------------

    // Implementation of the ExportIdentifier interface
    // ---------------------------------------------------------------------------

    /**
     * @see ExportIdentifier#getNextExportIds()
     */
    public ExportIdentifier[] getNextExportIds() {
        return EMPTY_EXPORT_IDENTIFIER_ARRAY;
    }

    // ---------------------------------------------------------------------------
    // Implementation of the Externalizable interface
    // ---------------------------------------------------------------------------

    /**
     * @see Externalizable#readExternal(ObjectInput)
     */
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        hostName = in.readUTF();
        port = in.readInt();
    }

    /**
     * @see Externalizable#writeExternal(ObjectOutput)
     */
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeUTF(hostName);
        out.writeInt(getPort());
    }

    // ---------------------------------------------------------------------------
    // Overridden methods.
    // ---------------------------------------------------------------------------

    /**
     * @see Object#equals(Object)
     */
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }

        if (obj == null || !(obj instanceof IPExportIdentifier)) {
            return false;
        }
        IPExportIdentifier tcpExpId = (IPExportIdentifier) obj;
        try {
            InetAddress add = getAddress();
            InetAddress add1 = tcpExpId.getAddress();
            return tcpExpId.port == port
                    && (add.equals(add1) || add.getCanonicalHostName().equals(
                            add1.getCanonicalHostName()));
        } catch (UnknownHostException e) {
            return tcpExpId.port == port && hostName.equals(tcpExpId.hostName);
        }
    }

    /**
     * @see Object#hashCode()
     */
    public int hashCode() {
        return hostName.hashCode() + port;
    }

    /**
     * @see Object#toString()
     */
    public String toString() {
        return hostName + ":" + port;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy