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    *   http://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 org.jboss.netty.channel.socket.oio;
17  
18  import org.jboss.netty.channel.ChannelException;
19  import org.jboss.netty.channel.ChannelFactory;
20  import org.jboss.netty.channel.ChannelFuture;
21  import org.jboss.netty.channel.ChannelPipeline;
22  import org.jboss.netty.channel.ChannelSink;
23  import org.jboss.netty.channel.socket.DatagramChannel;
24  import org.jboss.netty.channel.socket.DatagramChannelConfig;
25  import org.jboss.netty.channel.socket.DefaultDatagramChannelConfig;
26  
27  import java.io.IOException;
28  import java.net.InetAddress;
29  import java.net.InetSocketAddress;
30  import java.net.MulticastSocket;
31  import java.net.NetworkInterface;
32  import java.net.SocketException;
33  
34  import static org.jboss.netty.channel.Channels.*;
35  
36  final class OioDatagramChannel extends AbstractOioChannel
37                                  implements DatagramChannel {
38  
39      final MulticastSocket socket;
40      private final DatagramChannelConfig config;
41  
42      OioDatagramChannel(
43              ChannelFactory factory,
44              ChannelPipeline pipeline,
45              ChannelSink sink) {
46  
47          super(null, factory, pipeline, sink);
48  
49          try {
50              socket = new MulticastSocket(null);
51          } catch (IOException e) {
52              throw new ChannelException("Failed to open a datagram socket.", e);
53          }
54  
55          try {
56              socket.setSoTimeout(10);
57              socket.setBroadcast(false);
58          } catch (SocketException e) {
59              throw new ChannelException(
60                      "Failed to configure the datagram socket timeout.", e);
61          }
62          config = new DefaultDatagramChannelConfig(socket);
63  
64          fireChannelOpen(this);
65  
66      }
67  
68      public DatagramChannelConfig getConfig() {
69          return config;
70      }
71  
72      public ChannelFuture joinGroup(InetAddress multicastAddress) {
73          ensureBound();
74          try {
75              socket.joinGroup(multicastAddress);
76              return succeededFuture(this);
77          } catch (IOException e) {
78              return failedFuture(this, e);
79          }
80      }
81  
82      public ChannelFuture joinGroup(
83              InetSocketAddress multicastAddress, NetworkInterface networkInterface) {
84          ensureBound();
85          try {
86              socket.joinGroup(multicastAddress, networkInterface);
87              return succeededFuture(this);
88          } catch (IOException e) {
89              return failedFuture(this, e);
90          }
91      }
92  
93      private void ensureBound() {
94          if (!isBound()) {
95              throw new IllegalStateException(
96                      DatagramChannel.class.getName() +
97                      " must be bound to join a group.");
98          }
99      }
100 
101     public ChannelFuture leaveGroup(InetAddress multicastAddress) {
102         try {
103             socket.leaveGroup(multicastAddress);
104             return succeededFuture(this);
105         } catch (IOException e) {
106             return failedFuture(this, e);
107         }
108     }
109 
110     public ChannelFuture leaveGroup(
111             InetSocketAddress multicastAddress, NetworkInterface networkInterface) {
112         try {
113             socket.leaveGroup(multicastAddress, networkInterface);
114             return succeededFuture(this);
115         } catch (IOException e) {
116             return failedFuture(this, e);
117         }
118     }
119 
120     @Override
121     boolean isSocketBound() {
122         return socket.isBound();
123     }
124 
125     @Override
126     boolean isSocketConnected() {
127         return socket.isConnected();
128     }
129 
130     @Override
131     InetSocketAddress getLocalSocketAddress() throws Exception {
132         return (InetSocketAddress) socket.getLocalSocketAddress();
133     }
134 
135     @Override
136     InetSocketAddress getRemoteSocketAddress() throws Exception {
137         return (InetSocketAddress) socket.getRemoteSocketAddress();
138     }
139 
140     @Override
141     void closeSocket() {
142         socket.close();
143     }
144 
145     @Override
146     boolean isSocketClosed() {
147         return socket.isClosed();
148     }
149 
150 
151 }