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.SocketException;
24 import java.nio.channels.SelectionKey;
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.DefaultIoFilterChain;
31 import org.apache.mina.core.filterchain.IoFilterChain;
32 import org.apache.mina.core.service.DefaultTransportMetadata;
33 import org.apache.mina.core.service.IoHandler;
34 import org.apache.mina.core.service.IoProcessor;
35 import org.apache.mina.core.service.IoService;
36 import org.apache.mina.core.service.TransportMetadata;
37 import org.apache.mina.core.session.IoSession;
38 import org.apache.mina.transport.socket.AbstractSocketSessionConfig;
39 import org.apache.mina.transport.socket.SocketSessionConfig;
40
41
42
43
44
45
46
47 class NioSocketSession extends NioSession {
48
49 static final TransportMetadata METADATA =
50 new DefaultTransportMetadata(
51 "nio", "socket", false, true,
52 InetSocketAddress.class,
53 SocketSessionConfig.class,
54 IoBuffer.class, FileRegion.class);
55
56 private final IoService service;
57
58 private final SocketSessionConfig config = new SessionConfigImpl();
59
60 private final IoProcessor<NioSession> processor;
61
62 private final IoFilterChain filterChain = new DefaultIoFilterChain(this);
63
64 private final SocketChannel ch;
65
66 private final IoHandler handler;
67
68 private SelectionKey key;
69
70
71
72
73
74
75
76
77
78
79 public NioSocketSession(IoService service, IoProcessor<NioSession> processor, SocketChannel ch) {
80 this.service = service;
81 this.processor = processor;
82 this.ch = ch;
83 this.handler = service.getHandler();
84 this.config.setAll(service.getSessionConfig());
85 }
86
87 public IoService getService() {
88 return service;
89 }
90
91 public SocketSessionConfig getConfig() {
92 return config;
93 }
94
95 @Override
96 public IoProcessor<NioSession> getProcessor() {
97 return processor;
98 }
99
100 public IoFilterChain getFilterChain() {
101 return filterChain;
102 }
103
104 public TransportMetadata getTransportMetadata() {
105 return METADATA;
106 }
107
108 @Override
109 SocketChannel getChannel() {
110 return ch;
111 }
112
113 @Override
114 SelectionKey getSelectionKey() {
115 return key;
116 }
117
118 @Override
119 void setSelectionKey(SelectionKey key) {
120 this.key = key;
121 }
122
123 public IoHandler getHandler() {
124 return handler;
125 }
126
127 public InetSocketAddress getRemoteAddress() {
128 return (InetSocketAddress) ch.socket().getRemoteSocketAddress();
129 }
130
131 public InetSocketAddress getLocalAddress() {
132 return (InetSocketAddress) ch.socket().getLocalSocketAddress();
133 }
134
135 @Override
136 public InetSocketAddress getServiceAddress() {
137 return (InetSocketAddress) super.getServiceAddress();
138 }
139
140 private class SessionConfigImpl extends AbstractSocketSessionConfig {
141 public boolean isKeepAlive() {
142 try {
143 return ch.socket().getKeepAlive();
144 } catch (SocketException e) {
145 throw new RuntimeIoException(e);
146 }
147 }
148
149 public void setKeepAlive(boolean on) {
150 try {
151 ch.socket().setKeepAlive(on);
152 } catch (SocketException e) {
153 throw new RuntimeIoException(e);
154 }
155 }
156
157 public boolean isOobInline() {
158 try {
159 return ch.socket().getOOBInline();
160 } catch (SocketException e) {
161 throw new RuntimeIoException(e);
162 }
163 }
164
165 public void setOobInline(boolean on) {
166 try {
167 ch.socket().setOOBInline(on);
168 } catch (SocketException e) {
169 throw new RuntimeIoException(e);
170 }
171 }
172
173 public boolean isReuseAddress() {
174 try {
175 return ch.socket().getReuseAddress();
176 } catch (SocketException e) {
177 throw new RuntimeIoException(e);
178 }
179 }
180
181 public void setReuseAddress(boolean on) {
182 try {
183 ch.socket().setReuseAddress(on);
184 } catch (SocketException e) {
185 throw new RuntimeIoException(e);
186 }
187 }
188
189 public int getSoLinger() {
190 try {
191 return ch.socket().getSoLinger();
192 } catch (SocketException e) {
193 throw new RuntimeIoException(e);
194 }
195 }
196
197 public void setSoLinger(int linger) {
198 try {
199 if (linger < 0) {
200 ch.socket().setSoLinger(false, 0);
201 } else {
202 ch.socket().setSoLinger(true, linger);
203 }
204 } catch (SocketException e) {
205 throw new RuntimeIoException(e);
206 }
207 }
208
209 public boolean isTcpNoDelay() {
210 if (!isConnected()) {
211 return false;
212 }
213
214 try {
215 return ch.socket().getTcpNoDelay();
216 } catch (SocketException e) {
217 throw new RuntimeIoException(e);
218 }
219 }
220
221 public void setTcpNoDelay(boolean on) {
222 try {
223 ch.socket().setTcpNoDelay(on);
224 } catch (SocketException e) {
225 throw new RuntimeIoException(e);
226 }
227 }
228
229
230
231
232 public int getTrafficClass() {
233 try {
234 return ch.socket().getTrafficClass();
235 } catch (SocketException e) {
236 throw new RuntimeIoException(e);
237 }
238 }
239
240
241
242
243 public void setTrafficClass(int tc) {
244 try {
245 ch.socket().setTrafficClass(tc);
246 } catch (SocketException e) {
247 throw new RuntimeIoException(e);
248 }
249 }
250
251 public int getSendBufferSize() {
252 try {
253 return ch.socket().getSendBufferSize();
254 } catch (SocketException e) {
255 throw new RuntimeIoException(e);
256 }
257 }
258
259 public void setSendBufferSize(int size) {
260 try {
261 ch.socket().setSendBufferSize(size);
262 } catch (SocketException e) {
263 throw new RuntimeIoException(e);
264 }
265 }
266
267 public int getReceiveBufferSize() {
268 try {
269 return ch.socket().getReceiveBufferSize();
270 } catch (SocketException e) {
271 throw new RuntimeIoException(e);
272 }
273 }
274
275 public void setReceiveBufferSize(int size) {
276 try {
277 ch.socket().setReceiveBufferSize(size);
278 } catch (SocketException e) {
279 throw new RuntimeIoException(e);
280 }
281 }
282 }
283 }