View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.transport.socket.nio;
21  
22  import java.io.IOException;
23  import java.net.InetSocketAddress;
24  import java.net.SocketAddress;
25  import java.nio.channels.DatagramChannel;
26  import java.util.Collections;
27  import java.util.Iterator;
28  import java.util.concurrent.Executor;
29  
30  import org.apache.mina.core.polling.AbstractPollingIoConnector;
31  import org.apache.mina.core.service.IoConnector;
32  import org.apache.mina.core.service.IoProcessor;
33  import org.apache.mina.core.service.SimpleIoProcessorPool;
34  import org.apache.mina.core.service.TransportMetadata;
35  import org.apache.mina.transport.socket.DatagramConnector;
36  import org.apache.mina.transport.socket.DatagramSessionConfig;
37  import org.apache.mina.transport.socket.DefaultDatagramSessionConfig;
38  
39  /**
40   * {@link IoConnector} for datagram transport (UDP/IP).
41   *
42   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
43   */
44  public final class NioDatagramConnector extends AbstractPollingIoConnector<NioSession, DatagramChannel> implements
45  DatagramConnector {
46  
47      /**
48       * Creates a new instance.
49       */
50      public NioDatagramConnector() {
51          super(new DefaultDatagramSessionConfig(), NioProcessor.class);
52      }
53  
54      /**
55       * Creates a new instance.
56       */
57      public NioDatagramConnector(int processorCount) {
58          super(new DefaultDatagramSessionConfig(), NioProcessor.class, processorCount);
59      }
60  
61      /**
62       * Creates a new instance.
63       */
64      public NioDatagramConnector(IoProcessor<NioSession> processor) {
65          super(new DefaultDatagramSessionConfig(), processor);
66      }
67  
68      /**
69       * Constructor for {@link NioDatagramConnector} with default configuration which will use a built-in
70       * thread pool executor to manage the given number of processor instances. The processor class must have
71       * a constructor that accepts ExecutorService or Executor as its single argument, or, failing that, a
72       * no-arg constructor.
73       * 
74       * @param processorClass the processor class.
75       * @param processorCount the number of processors to instantiate.
76       * @see SimpleIoProcessorPool#SimpleIoProcessorPool(Class, Executor, int, java.nio.channels.spi.SelectorProvider)
77       * @since 2.0.0-M4
78       */
79      public NioDatagramConnector(Class<? extends IoProcessor<NioSession>> processorClass, int processorCount) {
80          super(new DefaultDatagramSessionConfig(), processorClass, processorCount);
81      }
82  
83      /**
84       * Constructor for {@link NioDatagramConnector} with default configuration with default configuration which will use a built-in
85       * thread pool executor to manage the default number of processor instances. The processor class must have
86       * a constructor that accepts ExecutorService or Executor as its single argument, or, failing that, a
87       * no-arg constructor. The default number of instances is equal to the number of processor cores
88       * in the system, plus one.
89       * 
90       * @param processorClass the processor class.
91       * @see SimpleIoProcessorPool#SimpleIoProcessorPool(Class, Executor, int, java.nio.channels.spi.SelectorProvider)
92       * @see org.apache.mina.core.service.SimpleIoProcessorPool#DEFAULT_SIZE
93       * @since 2.0.0-M4
94       */
95      public NioDatagramConnector(Class<? extends IoProcessor<NioSession>> processorClass) {
96          super(new DefaultDatagramSessionConfig(), processorClass);
97      }
98  
99      public TransportMetadata getTransportMetadata() {
100         return NioDatagramSession.METADATA;
101     }
102 
103     public DatagramSessionConfig getSessionConfig() {
104         return (DatagramSessionConfig) sessionConfig;
105     }
106 
107     @Override
108     public InetSocketAddress getDefaultRemoteAddress() {
109         return (InetSocketAddress) super.getDefaultRemoteAddress();
110     }
111 
112     public void setDefaultRemoteAddress(InetSocketAddress defaultRemoteAddress) {
113         super.setDefaultRemoteAddress(defaultRemoteAddress);
114     }
115 
116     @Override
117     protected void init() throws Exception {
118         // Do nothing
119     }
120 
121     @Override
122     protected DatagramChannel newHandle(SocketAddress localAddress) throws Exception {
123         DatagramChannel ch = DatagramChannel.open();
124 
125         try {
126             if (localAddress != null) {
127                 try {
128                     ch.socket().bind(localAddress);
129                     setDefaultLocalAddress(localAddress);
130                 } catch (IOException ioe) {
131                     // Add some info regarding the address we try to bind to the
132                     // message
133                     String newMessage = "Error while binding on " + localAddress + "\n" + "original message : "
134                             + ioe.getMessage();
135                     Exception e = new IOException(newMessage);
136                     e.initCause(ioe.getCause());
137 
138                     // and close the channel
139                     ch.close();
140 
141                     throw e;
142                 }
143             }
144 
145             return ch;
146         } catch (Exception e) {
147             // If we got an exception while binding the datagram,
148             // we have to close it otherwise we will loose an handle
149             ch.close();
150             throw e;
151         }
152     }
153 
154     @Override
155     protected boolean connect(DatagramChannel handle, SocketAddress remoteAddress) throws Exception {
156         handle.connect(remoteAddress);
157         return true;
158     }
159 
160     @Override
161     protected NioSession newSession(IoProcessor<NioSession> processor, DatagramChannel handle) {
162         NioSession session = new NioDatagramSession(this, handle, processor);
163         session.getConfig().setAll(getSessionConfig());
164         return session;
165     }
166 
167     @Override
168     protected void close(DatagramChannel handle) throws Exception {
169         handle.disconnect();
170         handle.close();
171     }
172 
173     // Unused extension points.
174     @Override
175     @SuppressWarnings("unchecked")
176     protected Iterator<DatagramChannel> allHandles() {
177         return Collections.EMPTY_LIST.iterator();
178     }
179 
180     @Override
181     protected ConnectionRequest getConnectionRequest(DatagramChannel handle) {
182         throw new UnsupportedOperationException();
183     }
184 
185     @Override
186     protected void destroy() throws Exception {
187         // Do nothing
188     }
189 
190     @Override
191     protected boolean finishConnect(DatagramChannel handle) throws Exception {
192         throw new UnsupportedOperationException();
193     }
194 
195     @Override
196     protected void register(DatagramChannel handle, ConnectionRequest request) throws Exception {
197         throw new UnsupportedOperationException();
198     }
199 
200     @Override
201     protected int select(int timeout) throws Exception {
202         return 0;
203     }
204 
205     @Override
206     @SuppressWarnings("unchecked")
207     protected Iterator<DatagramChannel> selectedHandles() {
208         return Collections.EMPTY_LIST.iterator();
209     }
210 
211     @Override
212     protected void wakeup() {
213         // Do nothing
214     }
215 }