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  import java.util.concurrent.Executor;
28  
29  import org.apache.mina.core.polling.AbstractPollingIoConnector;
30  import org.apache.mina.core.service.IoConnector;
31  import org.apache.mina.core.service.IoProcessor;
32  import org.apache.mina.core.service.TransportMetadata;
33  import org.apache.mina.transport.socket.DatagramConnector;
34  import org.apache.mina.transport.socket.DatagramSessionConfig;
35  import org.apache.mina.transport.socket.DefaultDatagramSessionConfig;
36  
37  /**
38   * {@link IoConnector} for datagram transport (UDP/IP).
39   *
40   * @author The Apache MINA Project (dev@mina.apache.org)
41   * @version $Rev: 751514 $, $Date: 2009-03-08 21:17:47 +0100 (Sun, 08 Mar 2009) $
42   */
43  public final class NioDatagramConnector
44          extends AbstractPollingIoConnector<NioSession, DatagramChannel>
45          implements 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 org.apache.mina.core.service.SimpleIoProcessorPool#SimpleIoProcessorPool(Class, Executor, int)
77       * @since 2.0.0-M4
78       */
79      public NioDatagramConnector(Class<? extends IoProcessor<NioSession>> processorClass,
80  			int processorCount) {
81  		super(new DefaultDatagramSessionConfig(), processorClass, processorCount);
82  	}
83  
84      /**
85       * Constructor for {@link NioDatagramConnector} with default configuration with default configuration which will use a built-in 
86       * thread pool executor to manage the default number of processor instances. The processor class must have 
87       * a constructor that accepts ExecutorService or Executor as its single argument, or, failing that, a 
88       * no-arg constructor. The default number of instances is equal to the number of processor cores 
89       * in the system, plus one.
90       * 
91       * @param processorClass the processor class.
92       * @see org.apache.mina.core.service.SimpleIoProcessorPool#SimpleIoProcessorPool(Class, Executor, int)
93       * @see org.apache.mina.core.service.SimpleIoProcessorPool#DEFAULT_SIZE
94       * @since 2.0.0-M4
95       */
96  	public NioDatagramConnector(Class<? extends IoProcessor<NioSession>> processorClass) {
97  		super(new DefaultDatagramSessionConfig(), processorClass);
98  	}
99  
100 	public TransportMetadata getTransportMetadata() {
101         return NioDatagramSession.METADATA;
102     }
103     
104     @Override
105     public DatagramSessionConfig getSessionConfig() {
106         return (DatagramSessionConfig) super.getSessionConfig();
107     }
108     
109     @Override
110     public InetSocketAddress getDefaultRemoteAddress() {
111         return (InetSocketAddress) super.getDefaultRemoteAddress();
112     }
113     
114     public void setDefaultRemoteAddress(InetSocketAddress defaultRemoteAddress) {
115         super.setDefaultRemoteAddress(defaultRemoteAddress);
116     }
117 
118     @Override
119     protected void init() throws Exception {
120     }
121 
122     @Override
123     protected DatagramChannel newHandle(SocketAddress localAddress)
124             throws Exception {
125         DatagramChannel ch = DatagramChannel.open();
126 
127         try {
128             if (localAddress != null) {
129                 ch.socket().bind(localAddress);
130             }
131             
132             return ch;
133         } catch (Exception e) {
134             // If we got an exception while binding the datagram,
135             // we have to close it otherwise we will loose an handle
136             ch.close();
137             throw e;
138         }
139     }
140 
141     @Override
142     protected boolean connect(DatagramChannel handle,
143             SocketAddress remoteAddress) throws Exception {
144         handle.connect(remoteAddress);
145         return true;
146     }
147 
148     @Override
149     protected NioSession newSession(IoProcessor<NioSession> processor,
150             DatagramChannel handle) {
151         NioSession session = new NioDatagramSession(this, handle, processor);
152         session.getConfig().setAll(getSessionConfig());
153         return session;
154     }
155 
156     @Override
157     protected void close(DatagramChannel handle) throws Exception {
158         handle.disconnect();
159         handle.close();
160     }
161     
162     // Unused extension points.
163     @Override
164     @SuppressWarnings("unchecked")
165     protected Iterator<DatagramChannel> allHandles() {
166         return Collections.EMPTY_LIST.iterator();
167     }
168 
169     @Override
170     protected ConnectionRequest getConnectionRequest(DatagramChannel handle) {
171         throw new UnsupportedOperationException();
172     }
173 
174     @Override
175     protected void destroy() throws Exception {
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     }
203 }