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<IoSession> 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> bind0(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<IoSession>() {
132 public void add(IoSession session) {
133 }
134
135 public void flush(IoSession session) {
136 DummySession s = (DummySession) session;
137 WriteRequest req = s.getWriteRequestQueue().poll(session);
138 Object m = req.getMessage();
139 if (m instanceof FileRegion) {
140 FileRegion file = (FileRegion) m;
141 try {
142 file.getFileChannel().position(file.getPosition() + file.getRemainingBytes());
143 file.update(file.getRemainingBytes());
144 } catch (IOException e) {
145 s.getFilterChain().fireExceptionCaught(e);
146 }
147 }
148 getFilterChain().fireMessageSent(req);
149 }
150
151 public void remove(IoSession session) {
152 }
153
154 public void updateTrafficMask(IoSession session) {
155 }
156
157 public void dispose() {
158 }
159
160 public boolean isDisposed() {
161 return false;
162 }
163
164 public boolean isDisposing() {
165 return false;
166 }
167 };
168
169 try {
170 IoSessionDataStructureFactory factory = new DefaultIoSessionDataStructureFactory();
171 setAttributeMap(factory.getAttributeMap(this));
172 setWriteRequestQueue(factory.getWriteRequestQueue(this));
173 } catch (Exception e) {
174 throw new InternalError();
175 }
176 }
177
178 public IoSessionConfig getConfig() {
179 return config;
180 }
181
182
183
184
185 public void setConfig(IoSessionConfig config) {
186 if (config == null) {
187 throw new NullPointerException("config");
188 }
189
190 this.config = config;
191 }
192
193 public IoFilterChain getFilterChain() {
194 return filterChain;
195 }
196
197 public IoHandler getHandler() {
198 return handler;
199 }
200
201
202
203
204 public void setHandler(IoHandler handler) {
205 if (handler == null) {
206 throw new NullPointerException("handler");
207 }
208
209 this.handler = handler;
210 }
211
212 public SocketAddress getLocalAddress() {
213 return localAddress;
214 }
215
216 public SocketAddress getRemoteAddress() {
217 return remoteAddress;
218 }
219
220
221
222
223
224 public void setLocalAddress(SocketAddress localAddress) {
225 if (localAddress == null) {
226 throw new NullPointerException("localAddress");
227 }
228
229 this.localAddress = localAddress;
230 }
231
232
233
234
235 public void setRemoteAddress(SocketAddress remoteAddress) {
236 if (remoteAddress == null) {
237 throw new NullPointerException("remoteAddress");
238 }
239
240 this.remoteAddress = remoteAddress;
241 }
242
243 public IoService getService() {
244 return service;
245 }
246
247
248
249
250 public void setService(IoService service) {
251 if (service == null) {
252 throw new NullPointerException("service");
253 }
254
255 this.service = service;
256 }
257
258 @Override
259 public final IoProcessor<IoSession> getProcessor() {
260 return processor;
261 }
262
263 public TransportMetadata getTransportMetadata() {
264 return transportMetadata;
265 }
266
267
268
269
270 public void setTransportMetadata(TransportMetadata transportMetadata) {
271 if (transportMetadata == null) {
272 throw new NullPointerException("transportMetadata");
273 }
274
275 this.transportMetadata = transportMetadata;
276 }
277
278 @Override
279 public void setScheduledWriteBytes(int byteCount){
280 super.setScheduledWriteBytes(byteCount);
281 }
282
283 @Override
284 public void setScheduledWriteMessages(int messages) {
285 super.setScheduledWriteMessages(messages);
286 }
287
288
289
290
291
292
293
294
295
296 public void updateThroughput(boolean force) {
297 super.updateThroughput(System.currentTimeMillis(), force);
298 }
299 }