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