View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.channel.socket.nio;
17  
18  import io.netty.channel.ChannelException;
19  import io.netty.channel.ChannelOption;
20  import io.netty.channel.socket.DatagramChannelConfig;
21  import io.netty.channel.socket.DefaultDatagramChannelConfig;
22  import io.netty.util.internal.SocketUtils;
23  
24  import java.io.IOException;
25  import java.net.InetAddress;
26  import java.net.NetworkInterface;
27  import java.net.SocketException;
28  import java.net.SocketOption;
29  import java.net.StandardSocketOptions;
30  import java.nio.channels.DatagramChannel;
31  import java.util.Enumeration;
32  import java.util.Map;
33  
34  /**
35   * The default {@link NioDatagramChannelConfig} implementation.
36   */
37  class NioDatagramChannelConfig extends DefaultDatagramChannelConfig {
38      private final DatagramChannel javaChannel;
39  
40      NioDatagramChannelConfig(NioDatagramChannel channel, DatagramChannel javaChannel) {
41          super(channel, javaChannel.socket());
42          this.javaChannel = javaChannel;
43      }
44  
45      @Override
46      public int getTimeToLive() {
47          return getOption0(StandardSocketOptions.IP_MULTICAST_TTL);
48      }
49  
50      @Override
51      public DatagramChannelConfig setTimeToLive(int ttl) {
52          setOption0(StandardSocketOptions.IP_MULTICAST_TTL, ttl);
53          return this;
54      }
55  
56      @Override
57      public InetAddress getInterface() {
58          NetworkInterface inf = getNetworkInterface();
59          if (inf != null) {
60              Enumeration<InetAddress> addresses = SocketUtils.addressesFromNetworkInterface(inf);
61              if (addresses.hasMoreElements()) {
62                  return addresses.nextElement();
63              }
64          }
65          return null;
66      }
67  
68      @Override
69      public DatagramChannelConfig setInterface(InetAddress interfaceAddress) {
70          try {
71              setNetworkInterface(NetworkInterface.getByInetAddress(interfaceAddress));
72          } catch (SocketException e) {
73              throw new ChannelException(e);
74          }
75          return this;
76      }
77  
78      @Override
79      public NetworkInterface getNetworkInterface() {
80          return getOption0(StandardSocketOptions.IP_MULTICAST_IF);
81      }
82  
83      @Override
84      public DatagramChannelConfig setNetworkInterface(NetworkInterface networkInterface) {
85          setOption0(StandardSocketOptions.IP_MULTICAST_IF, networkInterface);
86          return this;
87      }
88  
89      @Override
90      public boolean isLoopbackModeDisabled() {
91          return getOption0(StandardSocketOptions.IP_MULTICAST_LOOP);
92      }
93  
94      @Override
95      public DatagramChannelConfig setLoopbackModeDisabled(boolean loopbackModeDisabled) {
96          setOption0(StandardSocketOptions.IP_MULTICAST_LOOP, loopbackModeDisabled);
97          return this;
98      }
99  
100     @Override
101     public DatagramChannelConfig setAutoRead(boolean autoRead) {
102         super.setAutoRead(autoRead);
103         return this;
104     }
105 
106     @Override
107     protected void autoReadCleared() {
108         ((NioDatagramChannel) channel).clearReadPending0();
109     }
110 
111     private <T> T getOption0(SocketOption<T> option) {
112         try {
113             return javaChannel.getOption(option);
114         } catch (IOException e) {
115             throw new ChannelException(e);
116         }
117     }
118 
119     private <T> void setOption0(SocketOption<T> option, T value) {
120         try {
121             javaChannel.setOption(option, value);
122         } catch (IOException e) {
123             throw new ChannelException(e);
124         }
125     }
126 
127     @Override
128     public <T> boolean setOption(ChannelOption<T> option, T value) {
129         if (option instanceof NioChannelOption) {
130             return NioChannelOption.setOption(javaChannel, (NioChannelOption<T>) option, value);
131         }
132         return super.setOption(option, value);
133     }
134 
135     @Override
136     public <T> T getOption(ChannelOption<T> option) {
137         if (option instanceof NioChannelOption) {
138             return NioChannelOption.getOption(javaChannel, (NioChannelOption<T>) option);
139         }
140         return super.getOption(option);
141     }
142 
143     @Override
144     public Map<ChannelOption<?>, Object> getOptions() {
145         return getOptions(super.getOptions(), NioChannelOption.getOptions(javaChannel));
146     }
147 }