diff --git a/transport/src/main/java/io/netty/channel/socket/nio/DefaultNioDatagramChannelConfig.java b/transport/src/main/java/io/netty/channel/socket/nio/DefaultNioDatagramChannelConfig.java index 8a329b546f..30c98d000c 100644 --- a/transport/src/main/java/io/netty/channel/socket/nio/DefaultNioDatagramChannelConfig.java +++ b/transport/src/main/java/io/netty/channel/socket/nio/DefaultNioDatagramChannelConfig.java @@ -23,10 +23,15 @@ import io.netty.util.internal.ConversionUtil; import io.netty.util.internal.DetectionUtil; import java.io.IOException; +import java.net.InetAddress; +import java.net.MulticastSocket; import java.net.NetworkInterface; +import java.net.SocketException; import java.net.StandardSocketOptions; import java.nio.channels.DatagramChannel; +import java.util.Enumeration; import java.util.Map; +import java.util.Set; /** * The default {@link NioSocketChannelConfig} implementation. @@ -173,4 +178,80 @@ class DefaultNioDatagramChannelConfig extends DefaultDatagramChannelConfig } } + @Override + public int getTimeToLive() { + if (DetectionUtil.javaVersion() < 7) { + throw new UnsupportedOperationException(); + } else { + try { + return (int) channel.getOption(StandardSocketOptions.IP_MULTICAST_TTL); + } catch (IOException e) { + throw new ChannelException(e); + } + } + } + + @Override + public void setTimeToLive(int ttl) { + if (DetectionUtil.javaVersion() < 7) { + throw new UnsupportedOperationException(); + } else { + try { + channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl); + } catch (IOException e) { + throw new ChannelException(e); + } + } + + } + + @Override + public InetAddress getInterface() { + NetworkInterface inf = getNetworkInterface(); + if (inf == null) { + return null; + } else { + Enumeration addresses = inf.getInetAddresses(); + if (addresses.hasMoreElements()) { + return addresses.nextElement(); + } + return null; + } + } + + @Override + public void setInterface(InetAddress interfaceAddress) { + try { + setNetworkInterface(NetworkInterface.getByInetAddress(interfaceAddress)); + } catch (SocketException e) { + throw new ChannelException(e); + } + } + + @Override + public boolean isLoopbackModeDisabled() { + if (DetectionUtil.javaVersion() < 7) { + throw new UnsupportedOperationException(); + } else { + try { + return (Boolean) channel.getOption(StandardSocketOptions.IP_MULTICAST_LOOP); + } catch (IOException e) { + throw new ChannelException(e); + } + } + } + + @Override + public void setLoopbackModeDisabled(boolean loopbackModeDisabled) { + if (DetectionUtil.javaVersion() < 7) { + throw new UnsupportedOperationException(); + } else { + try { + channel.setOption(StandardSocketOptions.IP_MULTICAST_LOOP, loopbackModeDisabled); + } catch (IOException e) { + throw new ChannelException(e); + } + } + } + } diff --git a/transport/src/test/java/io/netty/channel/socket/nio/DefaultNioDatagramChannelConfigTest.java b/transport/src/test/java/io/netty/channel/socket/nio/DefaultNioDatagramChannelConfigTest.java new file mode 100644 index 0000000000..5545cb61f9 --- /dev/null +++ b/transport/src/test/java/io/netty/channel/socket/nio/DefaultNioDatagramChannelConfigTest.java @@ -0,0 +1,53 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.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://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.channel.socket.nio; + +import io.netty.util.internal.DetectionUtil; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.StandardProtocolFamily; +import java.nio.channels.DatagramChannel; + +import junit.framework.Assert; + +import org.junit.Test; + +public class DefaultNioDatagramChannelConfigTest { + + @Test + public void testMulticastOptions() throws IOException { + if (DetectionUtil.javaVersion() < 7) { + return; + } + DefaultNioDatagramChannelConfig config = new DefaultNioDatagramChannelConfig(DatagramChannel.open(StandardProtocolFamily.INET)); + NetworkInterface inf = NetworkInterface.getNetworkInterfaces().nextElement(); + config.setNetworkInterface(inf); + Assert.assertEquals(inf, config.getNetworkInterface()); + + InetAddress localhost = inf.getInetAddresses().nextElement(); + config.setInterface(localhost); + Assert.assertEquals(localhost, config.getInterface()); + + config.setTimeToLive(100); + Assert.assertEquals(100, config.getTimeToLive()); + + config.setLoopbackModeDisabled(false); + Assert.assertEquals(false, config.isLoopbackModeDisabled()); + + } +}