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