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