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