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.net.SocketException;
25 import java.nio.channels.DatagramChannel;
26 import java.nio.channels.SelectionKey;
27
28 import org.apache.mina.common.DefaultIoFilterChain;
29 import org.apache.mina.common.DefaultTransportMetadata;
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.AbstractDatagramSessionConfig;
39 import org.apache.mina.transport.socket.DatagramSession;
40 import org.apache.mina.transport.socket.DatagramSessionConfig;
41 import org.apache.mina.transport.socket.DefaultDatagramSessionConfig;
42
43
44
45
46
47
48
49 class NioDatagramSession extends NioSession implements DatagramSession {
50
51 static final TransportMetadata METADATA =
52 new DefaultTransportMetadata(
53 "nio", "datagram", true, false,
54 InetSocketAddress.class,
55 DatagramSessionConfig.class, IoBuffer.class);
56
57 private final IoService service;
58
59 private final DatagramSessionConfig config = new SessionConfigImpl();
60
61 private final IoFilterChain filterChain = new DefaultIoFilterChain(this);
62
63 private final DatagramChannel ch;
64
65 private final IoHandler handler;
66
67 private final InetSocketAddress localAddress;
68
69 private final InetSocketAddress remoteAddress;
70
71 private final IoProcessor<NioSession> processor;
72
73 private SelectionKey key;
74
75
76
77
78 NioDatagramSession(IoService service,
79 DatagramChannel ch, IoProcessor<NioSession> processor,
80 SocketAddress remoteAddress) {
81 this.service = service;
82 this.ch = ch;
83 this.handler = service.getHandler();
84 this.processor = processor;
85 this.remoteAddress = (InetSocketAddress) remoteAddress;
86 this.localAddress = (InetSocketAddress) ch.socket()
87 .getLocalSocketAddress();
88
89 this.config.setAll(service.getSessionConfig());
90 }
91
92
93
94
95 NioDatagramSession(IoService service,
96 DatagramChannel ch, IoProcessor<NioSession> processor) {
97 this(service, ch, processor, ch.socket().getRemoteSocketAddress());
98 }
99
100 public IoService getService() {
101 return service;
102 }
103
104 @Override
105 protected IoProcessor<NioSession> getProcessor() {
106 return processor;
107 }
108
109 public DatagramSessionConfig getConfig() {
110 return config;
111 }
112
113 public IoFilterChain getFilterChain() {
114 return filterChain;
115 }
116
117 @Override
118 DatagramChannel getChannel() {
119 return ch;
120 }
121
122 @Override
123 SelectionKey getSelectionKey() {
124 return key;
125 }
126
127 @Override
128 void setSelectionKey(SelectionKey key) {
129 this.key = key;
130 }
131
132 public IoHandler getHandler() {
133 return handler;
134 }
135
136 public TransportMetadata getTransportMetadata() {
137 return METADATA;
138 }
139
140 public InetSocketAddress getRemoteAddress() {
141 return remoteAddress;
142 }
143
144 public InetSocketAddress getLocalAddress() {
145 return localAddress;
146 }
147
148 @Override
149 public InetSocketAddress getServiceAddress() {
150 return (InetSocketAddress) super.getServiceAddress();
151 }
152
153 private class SessionConfigImpl extends AbstractDatagramSessionConfig {
154
155 public int getReceiveBufferSize() {
156 try {
157 return ch.socket().getReceiveBufferSize();
158 } catch (SocketException e) {
159 throw new RuntimeIoException(e);
160 }
161 }
162
163 public void setReceiveBufferSize(int receiveBufferSize) {
164 if (DefaultDatagramSessionConfig.isSetReceiveBufferSizeAvailable()) {
165 try {
166 ch.socket().setReceiveBufferSize(receiveBufferSize);
167
168 receiveBufferSize = ch.socket().getReceiveBufferSize();
169 } catch (SocketException e) {
170 throw new RuntimeIoException(e);
171 }
172 }
173 }
174
175 public boolean isBroadcast() {
176 try {
177 return ch.socket().getBroadcast();
178 } catch (SocketException e) {
179 throw new RuntimeIoException(e);
180 }
181 }
182
183 public void setBroadcast(boolean broadcast) {
184 try {
185 ch.socket().setBroadcast(broadcast);
186 } catch (SocketException e) {
187 throw new RuntimeIoException(e);
188 }
189 }
190
191 public int getSendBufferSize() {
192 try {
193 return ch.socket().getSendBufferSize();
194 } catch (SocketException e) {
195 throw new RuntimeIoException(e);
196 }
197 }
198
199 public void setSendBufferSize(int sendBufferSize) {
200 if (DefaultDatagramSessionConfig.isSetSendBufferSizeAvailable()) {
201 try {
202 ch.socket().setSendBufferSize(sendBufferSize);
203 } catch (SocketException e) {
204 throw new RuntimeIoException(e);
205 }
206 }
207 }
208
209 public boolean isReuseAddress() {
210 try {
211 return ch.socket().getReuseAddress();
212 } catch (SocketException e) {
213 throw new RuntimeIoException(e);
214 }
215 }
216
217 public void setReuseAddress(boolean reuseAddress) {
218 try {
219 ch.socket().setReuseAddress(reuseAddress);
220 } catch (SocketException e) {
221 throw new RuntimeIoException(e);
222 }
223 }
224
225 public int getTrafficClass() {
226 if (DefaultDatagramSessionConfig.isGetTrafficClassAvailable()) {
227 try {
228 return ch.socket().getTrafficClass();
229 } catch (SocketException e) {
230 throw new RuntimeIoException(e);
231 }
232 } else {
233 return 0;
234 }
235 }
236
237 public void setTrafficClass(int trafficClass) {
238 if (DefaultDatagramSessionConfig.isSetTrafficClassAvailable()) {
239 try {
240 ch.socket().setTrafficClass(trafficClass);
241 } catch (SocketException e) {
242 throw new RuntimeIoException(e);
243 }
244 }
245 }
246 }
247 }