1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.core.session;
21
22 import java.io.IOException;
23 import java.net.SocketAddress;
24 import java.util.List;
25 import java.util.Set;
26 import java.util.concurrent.Executor;
27
28 import org.apache.mina.core.file.FileRegion;
29 import org.apache.mina.core.filterchain.DefaultIoFilterChain;
30 import org.apache.mina.core.filterchain.IoFilter;
31 import org.apache.mina.core.filterchain.IoFilterChain;
32 import org.apache.mina.core.service.AbstractIoAcceptor;
33 import org.apache.mina.core.service.DefaultTransportMetadata;
34 import org.apache.mina.core.service.IoHandler;
35 import org.apache.mina.core.service.IoHandlerAdapter;
36 import org.apache.mina.core.service.IoProcessor;
37 import org.apache.mina.core.service.IoService;
38 import org.apache.mina.core.service.TransportMetadata;
39 import org.apache.mina.core.write.WriteRequest;
40 import org.apache.mina.core.write.WriteRequestQueue;
41 import org.apache.mina.transport.socket.SocketSessionConfig;
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public class DummySession extends AbstractIoSession {
56
57 private static final TransportMetadata TRANSPORT_METADATA = new DefaultTransportMetadata("mina", "dummy", false,
58 false, SocketAddress.class, IoSessionConfig.class, Object.class);
59
60 private static final SocketAddress ANONYMOUS_ADDRESS = new SocketAddress() {
61 private static final long serialVersionUID = -496112902353454179L;
62
63 @Override
64 public String toString() {
65 return "?";
66 }
67 };
68
69 private volatile IoService service;
70
71 private volatile IoSessionConfig config = new AbstractIoSessionConfig() {
72 @Override
73 protected void doSetAll(IoSessionConfig config) {
74
75 }
76 };
77
78 private final IoFilterChain filterChain = new DefaultIoFilterChain(this);
79
80 private final IoProcessor<IoSession> processor;
81
82 private volatile IoHandler handler = new IoHandlerAdapter();
83
84 private volatile SocketAddress localAddress = ANONYMOUS_ADDRESS;
85
86 private volatile SocketAddress remoteAddress = ANONYMOUS_ADDRESS;
87
88 private volatile TransportMetadata transportMetadata = TRANSPORT_METADATA;
89
90
91
92
93 public DummySession() {
94 super(
95
96
97 new AbstractIoAcceptor(new AbstractIoSessionConfig() {
98 @Override
99 protected void doSetAll(IoSessionConfig config) {
100
101 }
102 }, new Executor() {
103 public void execute(Runnable command) {
104
105 }
106 }) {
107
108 @Override
109 protected Set<SocketAddress> bindInternal(List<? extends SocketAddress> localAddresses)
110 throws Exception {
111 throw new UnsupportedOperationException();
112 }
113
114 @Override
115 protected void unbind0(List<? extends SocketAddress> localAddresses) throws Exception {
116 throw new UnsupportedOperationException();
117 }
118
119 public IoSession newSession(SocketAddress remoteAddress, SocketAddress localAddress) {
120 throw new UnsupportedOperationException();
121 }
122
123 public TransportMetadata getTransportMetadata() {
124 return TRANSPORT_METADATA;
125 }
126
127 @Override
128 protected void dispose0() throws Exception {
129 }
130
131
132
133
134 public IoSessionConfig getSessionConfig() {
135 return sessionConfig;
136 }
137 });
138
139 processor = new IoProcessor<IoSession>() {
140 public void add(IoSession session) {
141
142 }
143
144 public void flush(IoSession session) {
145 DummySession s = (DummySession) session;
146 WriteRequest req = s.getWriteRequestQueue().poll(session);
147
148
149
150 if (req != null) {
151 Object m = req.getMessage();
152 if (m instanceof FileRegion) {
153 FileRegion file = (FileRegion) m;
154 try {
155 file.getFileChannel().position(file.getPosition() + file.getRemainingBytes());
156 file.update(file.getRemainingBytes());
157 } catch (IOException e) {
158 s.getFilterChain().fireExceptionCaught(e);
159 }
160 }
161 getFilterChain().fireMessageSent(req);
162 }
163 }
164
165
166
167
168 public void write(IoSession session, WriteRequest writeRequest) {
169 WriteRequestQueue writeRequestQueue = session.getWriteRequestQueue();
170
171 writeRequestQueue.offer(session, writeRequest);
172
173 if (!session.isWriteSuspended()) {
174 this.flush(session);
175 }
176 }
177
178 public void remove(IoSession session) {
179 if (!session.getCloseFuture().isClosed()) {
180 session.getFilterChain().fireSessionClosed();
181 }
182 }
183
184 public void updateTrafficControl(IoSession session) {
185
186 }
187
188 public void dispose() {
189
190 }
191
192 public boolean isDisposed() {
193 return false;
194 }
195
196 public boolean isDisposing() {
197 return false;
198 }
199
200 };
201
202 this.service = super.getService();
203
204 try {
205 IoSessionDataStructureFactory factory = new DefaultIoSessionDataStructureFactory();
206 setAttributeMap(factory.getAttributeMap(this));
207 setWriteRequestQueue(factory.getWriteRequestQueue(this));
208 } catch (Exception e) {
209 throw new InternalError();
210 }
211 }
212
213 public IoSessionConfig getConfig() {
214 return config;
215 }
216
217
218
219
220 public void setConfig(IoSessionConfig config) {
221 if (config == null) {
222 throw new IllegalArgumentException("config");
223 }
224
225 this.config = config;
226 }
227
228 public IoFilterChain getFilterChain() {
229 return filterChain;
230 }
231
232 public IoHandler getHandler() {
233 return handler;
234 }
235
236
237
238
239 public void setHandler(IoHandler handler) {
240 if (handler == null) {
241 throw new IllegalArgumentException("handler");
242 }
243
244 this.handler = handler;
245 }
246
247 public SocketAddress getLocalAddress() {
248 return localAddress;
249 }
250
251 public SocketAddress getRemoteAddress() {
252 return remoteAddress;
253 }
254
255
256
257
258
259 public void setLocalAddress(SocketAddress localAddress) {
260 if (localAddress == null) {
261 throw new IllegalArgumentException("localAddress");
262 }
263
264 this.localAddress = localAddress;
265 }
266
267
268
269
270 public void setRemoteAddress(SocketAddress remoteAddress) {
271 if (remoteAddress == null) {
272 throw new IllegalArgumentException("remoteAddress");
273 }
274
275 this.remoteAddress = remoteAddress;
276 }
277
278 public IoService getService() {
279 return service;
280 }
281
282
283
284
285 public void setService(IoService service) {
286 if (service == null) {
287 throw new IllegalArgumentException("service");
288 }
289
290 this.service = service;
291 }
292
293 @Override
294 public final IoProcessor<IoSession> getProcessor() {
295 return processor;
296 }
297
298 public TransportMetadata getTransportMetadata() {
299 return transportMetadata;
300 }
301
302
303
304
305 public void setTransportMetadata(TransportMetadata transportMetadata) {
306 if (transportMetadata == null) {
307 throw new IllegalArgumentException("transportMetadata");
308 }
309
310 this.transportMetadata = transportMetadata;
311 }
312
313 @Override
314 public void setScheduledWriteBytes(int byteCount) {
315 super.setScheduledWriteBytes(byteCount);
316 }
317
318 @Override
319 public void setScheduledWriteMessages(int messages) {
320 super.setScheduledWriteMessages(messages);
321 }
322
323
324
325
326
327
328
329
330
331 public void updateThroughput(boolean force) {
332 super.updateThroughput(System.currentTimeMillis(), force);
333 }
334 }