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.Socket;
24 import java.net.SocketException;
25 import java.nio.channels.SocketChannel;
26
27 import org.apache.mina.core.RuntimeIoException;
28 import org.apache.mina.core.buffer.IoBuffer;
29 import org.apache.mina.core.file.FileRegion;
30 import org.apache.mina.core.filterchain.IoFilter;
31 import org.apache.mina.core.filterchain.IoFilterChain;
32 import org.apache.mina.core.service.DefaultTransportMetadata;
33 import org.apache.mina.core.service.IoProcessor;
34 import org.apache.mina.core.service.IoService;
35 import org.apache.mina.core.service.TransportMetadata;
36 import org.apache.mina.core.session.IoSession;
37 import org.apache.mina.filter.ssl.SslFilter;
38 import org.apache.mina.transport.socket.AbstractSocketSessionConfig;
39 import org.apache.mina.transport.socket.SocketSessionConfig;
40
41
42
43
44
45
46 class NioSocketSession extends NioSession {
47 static final TransportMetadata METADATA = new DefaultTransportMetadata("nio", "socket", false, true,
48 InetSocketAddress.class, SocketSessionConfig.class, IoBuffer.class, FileRegion.class);
49
50
51
52
53
54
55
56
57
58 public NioSocketSession(IoService service, IoProcessor<NioSession> processor, SocketChannel channel) {
59 super(processor, service, channel);
60 config = new SessionConfigImpl();
61 config.setAll(service.getSessionConfig());
62 }
63
64 private Socket getSocket() {
65 return ((SocketChannel) channel).socket();
66 }
67
68
69
70
71 @Override
72 public TransportMetadata getTransportMetadata() {
73 return METADATA;
74 }
75
76
77
78
79 @Override
80 public SocketSessionConfig getConfig() {
81 return (SocketSessionConfig) config;
82 }
83
84
85
86
87 @Override
88 SocketChannel getChannel() {
89 return (SocketChannel) channel;
90 }
91
92
93
94
95 @Override
96 public InetSocketAddress getRemoteAddress() {
97 if (channel == null) {
98 return null;
99 }
100
101 Socket socket = getSocket();
102
103 if (socket == null) {
104 return null;
105 }
106
107 return (InetSocketAddress) socket.getRemoteSocketAddress();
108 }
109
110
111
112
113 @Override
114 public InetSocketAddress getLocalAddress() {
115 if (channel == null) {
116 return null;
117 }
118
119 Socket socket = getSocket();
120
121 if (socket == null) {
122 return null;
123 }
124
125 return (InetSocketAddress) socket.getLocalSocketAddress();
126 }
127
128 @Override
129 public InetSocketAddress getServiceAddress() {
130 return (InetSocketAddress) super.getServiceAddress();
131 }
132
133
134
135
136
137
138 private class SessionConfigImpl extends AbstractSocketSessionConfig {
139
140
141
142 @Override
143 public boolean isKeepAlive() {
144 try {
145 return getSocket().getKeepAlive();
146 } catch (SocketException e) {
147 throw new RuntimeIoException(e);
148 }
149 }
150
151
152
153
154 @Override
155 public void setKeepAlive(boolean on) {
156 try {
157 getSocket().setKeepAlive(on);
158 } catch (SocketException e) {
159 throw new RuntimeIoException(e);
160 }
161 }
162
163
164
165
166 @Override
167 public boolean isOobInline() {
168 try {
169 return getSocket().getOOBInline();
170 } catch (SocketException e) {
171 throw new RuntimeIoException(e);
172 }
173 }
174
175
176
177
178 @Override
179 public void setOobInline(boolean on) {
180 try {
181 getSocket().setOOBInline(on);
182 } catch (SocketException e) {
183 throw new RuntimeIoException(e);
184 }
185 }
186
187
188
189
190 @Override
191 public boolean isReuseAddress() {
192 try {
193 return getSocket().getReuseAddress();
194 } catch (SocketException e) {
195 throw new RuntimeIoException(e);
196 }
197 }
198
199
200
201
202 @Override
203 public void setReuseAddress(boolean on) {
204 try {
205 getSocket().setReuseAddress(on);
206 } catch (SocketException e) {
207 throw new RuntimeIoException(e);
208 }
209 }
210
211
212
213
214 @Override
215 public int getSoLinger() {
216 try {
217 return getSocket().getSoLinger();
218 } catch (SocketException e) {
219 throw new RuntimeIoException(e);
220 }
221 }
222
223
224
225
226 @Override
227 public void setSoLinger(int linger) {
228 try {
229 if (linger < 0) {
230 getSocket().setSoLinger(false, 0);
231 } else {
232 getSocket().setSoLinger(true, linger);
233 }
234 } catch (SocketException e) {
235 throw new RuntimeIoException(e);
236 }
237 }
238
239
240
241
242 @Override
243 public boolean isTcpNoDelay() {
244 if (!isConnected()) {
245 return false;
246 }
247
248 try {
249 return getSocket().getTcpNoDelay();
250 } catch (SocketException e) {
251 throw new RuntimeIoException(e);
252 }
253 }
254
255
256
257
258 @Override
259 public void setTcpNoDelay(boolean on) {
260 try {
261 getSocket().setTcpNoDelay(on);
262 } catch (SocketException e) {
263 throw new RuntimeIoException(e);
264 }
265 }
266
267
268
269
270 @Override
271 public int getTrafficClass() {
272 try {
273 return getSocket().getTrafficClass();
274 } catch (SocketException e) {
275 throw new RuntimeIoException(e);
276 }
277 }
278
279
280
281
282 @Override
283 public void setTrafficClass(int tc) {
284 try {
285 getSocket().setTrafficClass(tc);
286 } catch (SocketException e) {
287 throw new RuntimeIoException(e);
288 }
289 }
290
291
292
293
294 @Override
295 public int getSendBufferSize() {
296 try {
297 return getSocket().getSendBufferSize();
298 } catch (SocketException e) {
299 throw new RuntimeIoException(e);
300 }
301 }
302
303
304
305
306 @Override
307 public void setSendBufferSize(int size) {
308 try {
309 getSocket().setSendBufferSize(size);
310 } catch (SocketException e) {
311 throw new RuntimeIoException(e);
312 }
313 }
314
315
316
317
318 @Override
319 public int getReceiveBufferSize() {
320 try {
321 return getSocket().getReceiveBufferSize();
322 } catch (SocketException e) {
323 throw new RuntimeIoException(e);
324 }
325 }
326
327
328
329
330 @Override
331 public void setReceiveBufferSize(int size) {
332 try {
333 getSocket().setReceiveBufferSize(size);
334 } catch (SocketException e) {
335 throw new RuntimeIoException(e);
336 }
337 }
338 }
339
340
341
342
343 @Override
344 public final boolean isSecured() {
345
346 IoFilterChain chain = getFilterChain();
347
348 IoFilter sslFilter = chain.get(SslFilter.class);
349
350 if (sslFilter != null) {
351
352 return ((SslFilter)sslFilter).isSecured(this);
353 } else {
354 return false;
355 }
356 }
357 }