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.net.InetSocketAddress;
23  import java.net.SocketAddress;
24  import java.nio.channels.DatagramChannel;
25  import java.util.Collections;
26  import java.util.Iterator;
27  
28  import org.apache.mina.core.polling.AbstractPollingIoConnector;
29  import org.apache.mina.core.service.IoConnector;
30  import org.apache.mina.core.service.IoProcessor;
31  import org.apache.mina.core.service.TransportMetadata;
32  import org.apache.mina.transport.socket.DatagramConnector;
33  import org.apache.mina.transport.socket.DatagramSessionConfig;
34  import org.apache.mina.transport.socket.DefaultDatagramSessionConfig;
35  
36  /**
37   * {@link IoConnector} for datagram transport (UDP/IP).
38   *
39   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
40   */
41  public final class NioDatagramConnector extends AbstractPollingIoConnector<NioSession, DatagramChannel> implements
42          DatagramConnector {
43  
44      /**
45       * Creates a new instance.
46       */
47      public NioDatagramConnector() {
48          super(new DefaultDatagramSessionConfig(), NioProcessor.class);
49      }
50  
51      /**
52       * Creates a new instance.
53       */
54      public NioDatagramConnector(int processorCount) {
55          super(new DefaultDatagramSessionConfig(), NioProcessor.class, processorCount);
56      }
57  
58      /**
59       * Creates a new instance.
60       */
61      public NioDatagramConnector(IoProcessor<NioSession> processor) {
62          super(new DefaultDatagramSessionConfig(), processor);
63      }
64  
65      /**
66       * Constructor for {@link NioDatagramConnector} with default configuration which will use a built-in 
67       * thread pool executor to manage the given number of processor instances. The processor class must have 
68       * a constructor that accepts ExecutorService or Executor as its single argument, or, failing that, a 
69       * no-arg constructor.
70       * 
71       * @param processorClass the processor class.
72       * @param processorCount the number of processors to instantiate.
73       * @see org.apache.mina.core.service.SimpleIoProcessorPool#SimpleIoProcessorPool(Class, Executor, int)
74       * @since 2.0.0-M4
75       */
76      public NioDatagramConnector(Class<? extends IoProcessor<NioSession>> processorClass, int processorCount) {
77          super(new DefaultDatagramSessionConfig(), processorClass, processorCount);
78      }
79  
80      /**
81       * Constructor for {@link NioDatagramConnector} with default configuration with default configuration which will use a built-in 
82       * thread pool executor to manage the default number of processor instances. The processor class must have 
83       * a constructor that accepts ExecutorService or Executor as its single argument, or, failing that, a 
84       * no-arg constructor. The default number of instances is equal to the number of processor cores 
85       * in the system, plus one.
86       * 
87       * @param processorClass the processor class.
88       * @see org.apache.mina.core.service.SimpleIoProcessorPool#SimpleIoProcessorPool(Class, Executor, int)
89       * @see org.apache.mina.core.service.SimpleIoProcessorPool#DEFAULT_SIZE
90       * @since 2.0.0-M4
91       */
92      public NioDatagramConnector(Class<? extends IoProcessor<NioSession>> processorClass) {
93          super(new DefaultDatagramSessionConfig(), processorClass);
94      }
95  
96      public TransportMetadata getTransportMetadata() {
97          return NioDatagramSession.METADATA;
98      }
99  
100     @Override
101     public DatagramSessionConfig getSessionConfig() {
102         return (DatagramSessionConfig) super.getSessionConfig();
103     }
104 
105     @Override
106     public InetSocketAddress getDefaultRemoteAddress() {
107         return (InetSocketAddress) super.getDefaultRemoteAddress();
108     }
109 
110     public void setDefaultRemoteAddress(InetSocketAddress defaultRemoteAddress) {
111         super.setDefaultRemoteAddress(defaultRemoteAddress);
112     }
113 
114     @Override
115     protected void init() throws Exception {
116         // Do nothing
117     }
118 
119     @Override
120     protected DatagramChannel newHandle(SocketAddress localAddress) throws Exception {
121         DatagramChannel ch = DatagramChannel.open();
122 
123         try {
124             if (localAddress != null) {
125                 ch.socket().bind(localAddress);
126             }
127 
128             return ch;
129         } catch (Exception e) {
130             // If we got an exception while binding the datagram,
131             // we have to close it otherwise we will loose an handle
132             ch.close();
133             throw e;
134         }
135     }
136 
137     @Override
138     protected boolean connect(DatagramChannel handle, SocketAddress remoteAddress) throws Exception {
139         handle.connect(remoteAddress);
140         return true;
141     }
142 
143     @Override
144     protected NioSession newSession(IoProcessor<NioSession> processor, DatagramChannel handle) {
145         NioSession session = new NioDatagramSession(this, handle, processor);
146         session.getConfig().setAll(getSessionConfig());
147         return session;
148     }
149 
150     @Override
151     protected void close(DatagramChannel handle) throws Exception {
152         handle.disconnect();
153         handle.close();
154     }
155 
156     // Unused extension points.
157     @Override
158     @SuppressWarnings("unchecked")
159     protected Iterator<DatagramChannel> allHandles() {
160         return Collections.EMPTY_LIST.iterator();
161     }
162 
163     @Override
164     protected ConnectionRequest getConnectionRequest(DatagramChannel handle) {
165         throw new UnsupportedOperationException();
166     }
167 
168     @Override
169     protected void destroy() throws Exception {
170         // Do nothing
171     }
172 
173     @Override
174     protected boolean finishConnect(DatagramChannel handle) throws Exception {
175         throw new UnsupportedOperationException();
176     }
177 
178     @Override
179     protected void register(DatagramChannel handle, ConnectionRequest request) throws Exception {
180         throw new UnsupportedOperationException();
181     }
182 
183     @Override
184     protected int select(int timeout) throws Exception {
185         return 0;
186     }
187 
188     @Override
189     @SuppressWarnings("unchecked")
190     protected Iterator<DatagramChannel> selectedHandles() {
191         return Collections.EMPTY_LIST.iterator();
192     }
193 
194     @Override
195     protected void wakeup() {
196         // Do nothing
197     }
198 }