Commit fa38bb4c authored by chiaming2000's avatar chiaming2000
Browse files

Provide a method to set/override the network interface used for

sending/receiving heartbeat messages. 
parent e30cfd1f
Loading
Loading
Loading
Loading
+4 −26
Original line number Diff line number Diff line
@@ -23,11 +23,11 @@ import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.seagate.kinetic.common.lib.NetUtil;

/**
 *
 * Kinetic applications may start an instance of this class and set a message
@@ -79,7 +79,7 @@ public class HeartbeatListener implements Runnable {
    private void init() throws IOException {

        // find network interface
        NetworkInterface ni = this.findNetworkInterface();
        NetworkInterface ni = NetUtil.findMulticastNetworkInterface();

        // my multicast listening address
        mcastAddress = InetAddress.getByName(mcastDestination);
@@ -89,7 +89,7 @@ public class HeartbeatListener implements Runnable {

        // only set it if we are allowed to search
        if (ni != null) {
            mcastSocket.setNetworkInterface(ni);
            // mcastSocket.setNetworkInterface(ni);
        }

        // join the m group
@@ -101,28 +101,6 @@ public class HeartbeatListener implements Runnable {
        thread.start();
    }

    private NetworkInterface findNetworkInterface() {
        NetworkInterface ni = null;

        try {
            Enumeration<NetworkInterface> nis = NetworkInterface
                    .getNetworkInterfaces();

            while (nis.hasMoreElements()) {
                ni = nis.nextElement();
                if (ni.supportsMulticast() && ni.isUp()) {
                    logger.info("found interface that supports multicast: "
                            + ni.getDisplayName());
                    break;
                }
            }
        } catch (SocketException e) {
            logger.log(Level.WARNING, e.getMessage(), e);
        }

        return ni;
    }

    public void close() {
        this.isClosed = true;
        this.mcastSocket.close();
+78 −0
Original line number Diff line number Diff line
/**
 * Copyright (C) 2014 Seagate Technology.
 *
 * 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.1 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package com.seagate.kinetic.common.lib;

import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;

public class NetUtil {

    private final static Logger logger = Logger.getLogger(NetUtil.class
            .getName());

    /**
     * set interface name to use for multicast heartbeat.
     */
    public final static String NET_INTERFACE_PROP_NAME = "kinetic.net.interface";

    /**
     * Find a valid network interface that supports multicast.
     * 
     * @return a valid network interface that supports multicast. Or null if no
     *         network interface is found or no permission to do the search.
     */
    public static NetworkInterface findMulticastNetworkInterface() {

        NetworkInterface ni = null;

        try {

            String niName = System.getProperty(NET_INTERFACE_PROP_NAME);

            if (niName != null) {
                ni = NetworkInterface.getByName(niName);

                logger.info("user defined multicast interface is used., name="
                        + ni.getDisplayName());
            } else {

                Enumeration<NetworkInterface> nis = NetworkInterface
                        .getNetworkInterfaces();

                while (nis.hasMoreElements()) {
                    ni = nis.nextElement();
                    if (ni.supportsMulticast() && ni.isUp()
                            && ni.isVirtual() == false) {
                        logger.info("found interface that supports multicast: "
                                + ni.getDisplayName());
                        break;
                    }
                }
            }

        } catch (SocketException e) {
            logger.log(Level.WARNING, e.getMessage(), e);
        }

        return ni;
    }

}
+3 −31
Original line number Diff line number Diff line
@@ -24,7 +24,6 @@ import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -32,6 +31,7 @@ import java.util.logging.Logger;
import kinetic.simulator.SimulatorConfiguration;

import com.google.gson.Gson;
import com.seagate.kinetic.common.lib.NetUtil;
import com.seagate.kinetic.heartbeat.HeartbeatMessage;
import com.seagate.kinetic.heartbeat.KineticNetworkInterface;
import com.seagate.kinetic.simulator.heartbeat.HeartbeatProvider;
@@ -129,7 +129,8 @@ public class MulticastHeartbeatProvider implements HeartbeatProvider {
            // multicast socket
            mcastSocket = new MulticastSocket();

            NetworkInterface mni = findMulticastNetworkInterface();
            NetworkInterface mni = NetUtil.findMulticastNetworkInterface();

            if (mni != null) {
                mcastSocket.setNetworkInterface(mni);
            }
@@ -248,33 +249,4 @@ public class MulticastHeartbeatProvider implements HeartbeatProvider {
        return sb.toString();
    }

    /**
     * Find a valid network interface that supports multicast.
     * 
     * @return a valid network interface that supports multicast. Or null if no
     *         network interface is found or no permission to do the search.
     */
    public static NetworkInterface findMulticastNetworkInterface() {

        NetworkInterface ni = null;

        try {
            Enumeration<NetworkInterface> nis = NetworkInterface
                    .getNetworkInterfaces();

            while (nis.hasMoreElements()) {
                ni = nis.nextElement();
                if (ni.supportsMulticast() && ni.isUp()) {
                    logger.info("found interface that supports multicast: "
                            + ni.getDisplayName());
                    break;
                }
            }
        } catch (SocketException e) {
            logger.log(Level.WARNING, e.getMessage(), e);
        }

        return ni;
    }

}