view src/main/java/alice/datasegment/MulticastDataSegmentManager.java @ 508:b7d02ea79850 dispose

change multicast Data Segment API
author sugi
date Sun, 04 Jan 2015 13:51:01 +0900
parents c06070403ed4
children 53d7cff1fe10
line wrap: on
line source

package alice.datasegment;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.SocketAddress;
import java.net.StandardProtocolFamily;
import java.net.StandardSocketOptions;
import java.nio.channels.DatagramChannel;

import org.apache.log4j.Logger;

import alice.daemon.IncomingUdpConnection;
import alice.daemon.MulticastConnection;
import alice.daemon.OutboundTcpConnection;

public class MulticastDataSegmentManager extends RemoteDataSegmentManager {

    public enum SocketType{Sender, Receiver, Both};
    public MulticastDataSegmentManager(String connectionKey ,final String MCASTADDR, final int port, final String nis, SocketType type) {
        logger = Logger.getLogger(connectionKey);
        InetAddress mAddr;
        try {
            mAddr = InetAddress.getByName(MCASTADDR);

            DatagramChannel dcr =  createDatagramChannel(mAddr, port, nis);
            dcr.bind(new InetSocketAddress(port));
            SocketAddress sAddrr = new InetSocketAddress(mAddr,port);
            MulticastConnection receiver = new MulticastConnection(dcr, sAddrr);

            DatagramChannel dcs =  createDatagramChannel(mAddr, port, nis);
            SocketAddress sAddrs = new InetSocketAddress(mAddr,port);
            connection = new MulticastConnection(dcs, sAddrs); // sender

            if (type !=SocketType.Sender) {
                IncomingUdpConnection in = new IncomingUdpConnection((MulticastConnection) connection, receiver, this);
                in.setName("multicast-IncomingUdp");
                in.start();
            }
            if (type !=SocketType.Receiver) {
                OutboundTcpConnection out = new OutboundTcpConnection(connection); // OutboundUdpConnection sender
                out.setName(connectionKey+"OutboundUdp");
                out.start();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private DatagramChannel createDatagramChannel(InetAddress group, int port, String nis) {
        DatagramChannel dc = null;
        NetworkInterface ni;
        try {
            ni = NetworkInterface.getByName(nis);
            if (ni==null) {
                System.err.println("Can't open network interface "+nis);
                throw new IOException();
            }
            if (!ni.supportsMulticast()) {
                System.err.println("Network interface does not support multicast"+nis);
                throw new IOException();
            }

            dc = DatagramChannel.open(StandardProtocolFamily.INET);
            dc.setOption(StandardSocketOptions.SO_REUSEADDR, true);
            dc.setOption(StandardSocketOptions.IP_MULTICAST_IF, ni);
            dc.join(group, ni);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dc;
    }
}