1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.SelectionKey;
25 import java.nio.channels.Selector;
26 import java.nio.channels.SocketChannel;
27 import java.util.Collection;
28 import java.util.Iterator;
29 import java.util.concurrent.Executor;
30
31 import org.apache.mina.core.filterchain.IoFilterChain;
32 import org.apache.mina.core.polling.AbstractPollingIoConnector;
33 import org.apache.mina.core.service.IoConnector;
34 import org.apache.mina.core.service.IoProcessor;
35 import org.apache.mina.core.service.IoService;
36 import org.apache.mina.core.service.SimpleIoProcessorPool;
37 import org.apache.mina.core.service.TransportMetadata;
38 import org.apache.mina.core.session.IoSession;
39 import org.apache.mina.transport.socket.DefaultSocketSessionConfig;
40 import org.apache.mina.transport.socket.SocketConnector;
41 import org.apache.mina.transport.socket.SocketSessionConfig;
42
43
44
45
46
47
48 public final class NioSocketConnector
49 extends AbstractPollingIoConnector<NioSession, SocketChannel>
50 implements SocketConnector {
51
52 private volatile Selector selector;
53
54
55
56
57 public NioSocketConnector() {
58 super(new DefaultSocketSessionConfig(), NioProcessor.class);
59 ((DefaultSocketSessionConfig) getSessionConfig()).init(this);
60 }
61
62
63
64
65
66
67
68 public NioSocketConnector(int processorCount) {
69 super(new DefaultSocketSessionConfig(), NioProcessor.class, processorCount);
70 ((DefaultSocketSessionConfig) getSessionConfig()).init(this);
71 }
72
73
74
75
76
77
78
79 public NioSocketConnector(IoProcessor<NioSession> processor) {
80 super(new DefaultSocketSessionConfig(), processor);
81 ((DefaultSocketSessionConfig) getSessionConfig()).init(this);
82 }
83
84
85
86
87
88
89
90
91 public NioSocketConnector(Executor executor, IoProcessor<NioSession> processor) {
92 super(new DefaultSocketSessionConfig(), executor, processor);
93 ((DefaultSocketSessionConfig) getSessionConfig()).init(this);
94 }
95
96
97
98
99
100
101
102
103
104
105
106
107 public NioSocketConnector(Class<? extends IoProcessor<NioSession>> processorClass,
108 int processorCount) {
109 super(new DefaultSocketSessionConfig(), processorClass, processorCount);
110 }
111
112
113
114
115
116
117
118
119
120
121
122
123
124 public NioSocketConnector(Class<? extends IoProcessor<NioSession>> processorClass) {
125 super(new DefaultSocketSessionConfig(), processorClass);
126 }
127
128
129
130
131 @Override
132 protected void init() throws Exception {
133 this.selector = Selector.open();
134 }
135
136
137
138
139 @Override
140 protected void destroy() throws Exception {
141 if (selector != null) {
142 selector.close();
143 }
144 }
145
146
147
148
149 public TransportMetadata getTransportMetadata() {
150 return NioSocketSession.METADATA;
151 }
152
153
154
155
156 @Override
157 public SocketSessionConfig getSessionConfig() {
158 return (SocketSessionConfig) super.getSessionConfig();
159 }
160
161
162
163
164 @Override
165 public InetSocketAddress getDefaultRemoteAddress() {
166 return (InetSocketAddress) super.getDefaultRemoteAddress();
167 }
168
169
170
171
172 public void setDefaultRemoteAddress(InetSocketAddress defaultRemoteAddress) {
173 super.setDefaultRemoteAddress(defaultRemoteAddress);
174 }
175
176
177
178
179 @Override
180 protected Iterator<SocketChannel> allHandles() {
181 return new SocketChannelIterator(selector.keys());
182 }
183
184
185
186
187 @Override
188 protected boolean connect(SocketChannel handle, SocketAddress remoteAddress)
189 throws Exception {
190 return handle.connect(remoteAddress);
191 }
192
193
194
195
196 @Override
197 protected ConnectionRequest getConnectionRequest(SocketChannel handle) {
198 SelectionKey key = handle.keyFor(selector);
199
200 if ((key == null) || (!key.isValid())) {
201 return null;
202 }
203
204 return (ConnectionRequest) key.attachment();
205 }
206
207
208
209
210 @Override
211 protected void close(SocketChannel handle) throws Exception {
212 SelectionKey key = handle.keyFor(selector);
213
214 if (key != null) {
215 key.cancel();
216 }
217
218 IoSession session = (IoSession)key.attach(null);
219 IoFilterChain filterChain = session.getFilterChain();
220 filterChain.fireSessionClosed();
221
222 handle.close();
223 }
224
225
226
227
228 @Override
229 protected boolean finishConnect(SocketChannel handle) throws Exception {
230 if (handle.finishConnect()) {
231 SelectionKey key = handle.keyFor(selector);
232
233 if (key != null) {
234 key.cancel();
235 }
236
237 return true;
238 }
239
240 return false;
241 }
242
243
244
245
246 @Override
247 protected SocketChannel newHandle(SocketAddress localAddress)
248 throws Exception {
249 SocketChannel ch = SocketChannel.open();
250
251 int receiveBufferSize =
252 (getSessionConfig()).getReceiveBufferSize();
253 if (receiveBufferSize > 65535) {
254 ch.socket().setReceiveBufferSize(receiveBufferSize);
255 }
256
257 if (localAddress != null) {
258 ch.socket().bind(localAddress);
259 }
260 ch.configureBlocking(false);
261 return ch;
262 }
263
264
265
266
267 @Override
268 protected NioSession newSession(IoProcessor<NioSession> processor, SocketChannel handle) {
269 return new NioSocketSession(this, processor, handle);
270 }
271
272
273
274
275 @Override
276 protected void register(SocketChannel handle, ConnectionRequest request)
277 throws Exception {
278 handle.register(selector, SelectionKey.OP_CONNECT, request);
279 }
280
281
282
283
284 @Override
285 protected int select(int timeout) throws Exception {
286 return selector.select(timeout);
287 }
288
289
290
291
292 @Override
293 protected Iterator<SocketChannel> selectedHandles() {
294 return new SocketChannelIterator(selector.selectedKeys());
295 }
296
297
298
299
300 @Override
301 protected void wakeup() {
302 selector.wakeup();
303 }
304
305 private static class SocketChannelIterator implements Iterator<SocketChannel> {
306
307 private final Iterator<SelectionKey> i;
308
309 private SocketChannelIterator(Collection<SelectionKey> selectedKeys) {
310 this.i = selectedKeys.iterator();
311 }
312
313
314
315
316 public boolean hasNext() {
317 return i.hasNext();
318 }
319
320
321
322
323 public SocketChannel next() {
324 SelectionKey key = i.next();
325 return (SocketChannel) key.channel();
326 }
327
328
329
330
331 public void remove() {
332 i.remove();
333 }
334 }
335 }