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     public DatagramSessionConfig getSessionConfig() {
101         return (DatagramSessionConfig) sessionConfig;
102     }
103 
104     @Override
105     public InetSocketAddress getDefaultRemoteAddress() {
106         return (InetSocketAddress) super.getDefaultRemoteAddress();
107     }
108 
109     public void setDefaultRemoteAddress(InetSocketAddress defaultRemoteAddress) {
110         super.setDefaultRemoteAddress(defaultRemoteAddress);
111     }
112 
113     @Override
114     protected void init() throws Exception {
115         // Do nothing
116     }
117 
118     @Override
119     protected DatagramChannel newHandle(SocketAddress localAddress) throws Exception {
120         DatagramChannel ch = DatagramChannel.open();
121 
122         try {
123             if (localAddress != null) {
124                 ch.socket().bind(localAddress);
125             }
126 
127             return ch;
128         } catch (Exception e) {
129             // If we got an exception while binding the datagram,
130             // we have to close it otherwise we will loose an handle
131             ch.close();
132             throw e;
133         }
134     }
135 
136     @Override
137     protected boolean connect(DatagramChannel handle, SocketAddress remoteAddress) throws Exception {
138         handle.connect(remoteAddress);
139         return true;
140     }
141 
142     @Override
143     protected NioSession newSession(IoProcessor<NioSession> processor, DatagramChannel handle) {
144         NioSession session = new NioDatagramSession(this, handle, processor);
145         session.getConfig().setAll(getSessionConfig());
146         return session;
147     }
148 
149     @Override
150     protected void close(DatagramChannel handle) throws Exception {
151         handle.disconnect();
152         handle.close();
153     }
154 
155     // Unused extension points.
156     @Override
157     @SuppressWarnings("unchecked")
158     protected Iterator<DatagramChannel> allHandles() {
159         return Collections.EMPTY_LIST.iterator();
160     }
161 
162     @Override
163     protected ConnectionRequest getConnectionRequest(DatagramChannel handle) {
164         throw new UnsupportedOperationException();
165     }
166 
167     @Override
168     protected void destroy() throws Exception {
169         // Do nothing
170     }
171 
172     @Override
173     protected boolean finishConnect(DatagramChannel handle) throws Exception {
174         throw new UnsupportedOperationException();
175     }
176 
177     @Override
178     protected void register(DatagramChannel handle, ConnectionRequest request) throws Exception {
179         throw new UnsupportedOperationException();
180     }
181 
182     @Override
183     protected int select(int timeout) throws Exception {
184         return 0;
185     }
186 
187     @Override
188     @SuppressWarnings("unchecked")
189     protected Iterator<DatagramChannel> selectedHandles() {
190         return Collections.EMPTY_LIST.iterator();
191     }
192 
193     @Override
194     protected void wakeup() {
195         // Do nothing
196     }
197 }