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
41
42
43
44
45
46
47
48
49
50
51
52
53 public class DummySession extends AbstractIoSession {
54
55 private static final TransportMetadata TRANSPORT_METADATA =
56 new DefaultTransportMetadata(
57 "mina", "dummy", false, false,
58 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 private final IoProcessor<AbstractIoSession> processor;
80
81 private volatile IoHandler handler = new IoHandlerAdapter();
82 private volatile SocketAddress localAddress = ANONYMOUS_ADDRESS;
83 private volatile SocketAddress remoteAddress = ANONYMOUS_ADDRESS;
84 private volatile TransportMetadata transportMetadata = TRANSPORT_METADATA;
85
86
87
88
89 public DummySession() {
90 super(
91
92
93 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 void dispose0() throws Exception {
126 }
127 } );
128
129 processor = new IoProcessor<AbstractIoSession>() {
130 public void add(AbstractIoSession session) {
131
132 }
133
134 public void flush(AbstractIoSession session) {
135 DummySession s = (DummySession) session;
136 WriteRequest req = s.getWriteRequestQueue().poll(session);
137
138
139
140 if (req != null) {
141 Object m = req.getMessage();
142 if (m instanceof FileRegion) {
143 FileRegion file = (FileRegion) m;
144 try {
145 file.getFileChannel().position(file.getPosition() + file.getRemainingBytes());
146 file.update(file.getRemainingBytes());
147 } catch (IOException e) {
148 s.getFilterChain().fireExceptionCaught(e);
149 }
150 }
151 getFilterChain().fireMessageSent(req);
152 }
153 }
154
155 public void remove(AbstractIoSession session) {
156 if (!session.getCloseFuture().isClosed()) {
157 session.getFilterChain().fireSessionClosed();
158 }
159 }
160
161 public void updateTrafficControl(AbstractIoSession session) {
162
163 }
164
165 public void dispose() {
166
167 }
168
169 public boolean isDisposed() {
170 return false;
171 }
172
173 public boolean isDisposing() {
174 return false;
175 }
176
177 };
178
179 this.service = super.getService();
180
181 try {
182 IoSessionDataStructureFactory factory = new DefaultIoSessionDataStructureFactory();
183 setAttributeMap(factory.getAttributeMap(this));
184 setWriteRequestQueue(factory.getWriteRequestQueue(this));
185 } catch (Exception e) {
186 throw new InternalError();
187 }
188 }
189
190 public IoSessionConfig getConfig() {
191 return config;
192 }
193
194
195
196
197 public void setConfig(IoSessionConfig config) {
198 if (config == null) {
199 throw new IllegalArgumentException("config");
200 }
201
202 this.config = config;
203 }
204
205 public IoFilterChain getFilterChain() {
206 return filterChain;
207 }
208
209 public IoHandler getHandler() {
210 return handler;
211 }
212
213
214
215
216 public void setHandler(IoHandler handler) {
217 if (handler == null) {
218 throw new IllegalArgumentException("handler");
219 }
220
221 this.handler = handler;
222 }
223
224 public SocketAddress getLocalAddress() {
225 return localAddress;
226 }
227
228 public SocketAddress getRemoteAddress() {
229 return remoteAddress;
230 }
231
232
233
234
235
236 public void setLocalAddress(SocketAddress localAddress) {
237 if (localAddress == null) {
238 throw new IllegalArgumentException("localAddress");
239 }
240
241 this.localAddress = localAddress;
242 }
243
244
245
246
247 public void setRemoteAddress(SocketAddress remoteAddress) {
248 if (remoteAddress == null) {
249 throw new IllegalArgumentException("remoteAddress");
250 }
251
252 this.remoteAddress = remoteAddress;
253 }
254
255 public IoService getService() {
256 return service;
257 }
258
259
260
261
262 public void setService(IoService service) {
263 if (service == null) {
264 throw new IllegalArgumentException("service");
265 }
266
267 this.service = service;
268 }
269
270 @Override
271 public final IoProcessor<AbstractIoSession> getProcessor() {
272 return processor;
273 }
274
275 public TransportMetadata getTransportMetadata() {
276 return transportMetadata;
277 }
278
279
280
281
282 public void setTransportMetadata(TransportMetadata transportMetadata) {
283 if (transportMetadata == null) {
284 throw new IllegalArgumentException("transportMetadata");
285 }
286
287 this.transportMetadata = transportMetadata;
288 }
289
290 @Override
291 public void setScheduledWriteBytes(int byteCount){
292 super.setScheduledWriteBytes(byteCount);
293 }
294
295 @Override
296 public void setScheduledWriteMessages(int messages) {
297 super.setScheduledWriteMessages(messages);
298 }
299
300
301
302
303
304
305
306
307
308 public void updateThroughput(boolean force) {
309 super.updateThroughput(System.currentTimeMillis(), force);
310 }
311 }