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 try {
180 IoSessionDataStructureFactory factory = new DefaultIoSessionDataStructureFactory();
181 setAttributeMap(factory.getAttributeMap(this));
182 setWriteRequestQueue(factory.getWriteRequestQueue(this));
183 } catch (Exception e) {
184 throw new InternalError();
185 }
186 }
187
188 public IoSessionConfig getConfig() {
189 return config;
190 }
191
192
193
194
195 public void setConfig(IoSessionConfig config) {
196 if (config == null) {
197 throw new IllegalArgumentException("config");
198 }
199
200 this.config = config;
201 }
202
203 public IoFilterChain getFilterChain() {
204 return filterChain;
205 }
206
207 public IoHandler getHandler() {
208 return handler;
209 }
210
211
212
213
214 public void setHandler(IoHandler handler) {
215 if (handler == null) {
216 throw new IllegalArgumentException("handler");
217 }
218
219 this.handler = handler;
220 }
221
222 public SocketAddress getLocalAddress() {
223 return localAddress;
224 }
225
226 public SocketAddress getRemoteAddress() {
227 return remoteAddress;
228 }
229
230
231
232
233
234 public void setLocalAddress(SocketAddress localAddress) {
235 if (localAddress == null) {
236 throw new IllegalArgumentException("localAddress");
237 }
238
239 this.localAddress = localAddress;
240 }
241
242
243
244
245 public void setRemoteAddress(SocketAddress remoteAddress) {
246 if (remoteAddress == null) {
247 throw new IllegalArgumentException("remoteAddress");
248 }
249
250 this.remoteAddress = remoteAddress;
251 }
252
253 public IoService getService() {
254 return service;
255 }
256
257
258
259
260 public void setService(IoService service) {
261 if (service == null) {
262 throw new IllegalArgumentException("service");
263 }
264
265 this.service = service;
266 }
267
268 @Override
269 public final IoProcessor<AbstractIoSession> getProcessor() {
270 return processor;
271 }
272
273 public TransportMetadata getTransportMetadata() {
274 return transportMetadata;
275 }
276
277
278
279
280 public void setTransportMetadata(TransportMetadata transportMetadata) {
281 if (transportMetadata == null) {
282 throw new IllegalArgumentException("transportMetadata");
283 }
284
285 this.transportMetadata = transportMetadata;
286 }
287
288 @Override
289 public void setScheduledWriteBytes(int byteCount){
290 super.setScheduledWriteBytes(byteCount);
291 }
292
293 @Override
294 public void setScheduledWriteMessages(int messages) {
295 super.setScheduledWriteMessages(messages);
296 }
297
298
299
300
301
302
303
304
305
306 public void updateThroughput(boolean force) {
307 super.updateThroughput(System.currentTimeMillis(), force);
308 }
309 }