View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
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  import org.apache.mina.core.write.WriteRequestQueue;
41  import org.apache.mina.transport.socket.SocketSessionConfig;
42  
43  /**
44   * A dummy {@link IoSession} for unit-testing or non-network-use of
45   * the classes that depends on {@link IoSession}.
46   *
47   * <h2>Overriding I/O request methods</h2>
48   * All I/O request methods (i.e. {@link #close()}, {@link #write(Object)} and
49   * {@link #setTrafficMask(TrafficMask)}) are final and therefore cannot be
50   * overridden, but you can always add your custom {@link IoFilter} to the
51   * {@link IoFilterChain} to intercept any I/O events and requests.
52   *
53   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
54   */
55  public class DummySession extends AbstractIoSession {
56  
57      private static final TransportMetadata TRANSPORT_METADATA = new DefaultTransportMetadata("mina", "dummy", false,
58              false, 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              // Do nothing
75          }
76      };
77  
78      private final IoFilterChain filterChain = new DefaultIoFilterChain(this);
79  
80      private final IoProcessor<IoSession> processor;
81  
82      private volatile IoHandler handler = new IoHandlerAdapter();
83  
84      private volatile SocketAddress localAddress = ANONYMOUS_ADDRESS;
85  
86      private volatile SocketAddress remoteAddress = ANONYMOUS_ADDRESS;
87  
88      private volatile TransportMetadata transportMetadata = TRANSPORT_METADATA;
89  
90      /**
91       * Creates a new instance.
92       */
93      public DummySession() {
94          super(
95  
96          // Initialize dummy service.
97                  new AbstractIoAcceptor(new AbstractIoSessionConfig() {
98                      @Override
99                      protected void doSetAll(IoSessionConfig config) {
100                         // Do nothing
101                     }
102                 }, new Executor() {
103                     public void execute(Runnable command) {
104                         // Do nothing
105                     }
106                 }) {
107 
108                     @Override
109                     protected Set<SocketAddress> bindInternal(List<? extends SocketAddress> localAddresses)
110                             throws Exception {
111                         throw new UnsupportedOperationException();
112                     }
113 
114                     @Override
115                     protected void unbind0(List<? extends SocketAddress> localAddresses) throws Exception {
116                         throw new UnsupportedOperationException();
117                     }
118 
119                     public IoSession newSession(SocketAddress remoteAddress, SocketAddress localAddress) {
120                         throw new UnsupportedOperationException();
121                     }
122 
123                     public TransportMetadata getTransportMetadata() {
124                         return TRANSPORT_METADATA;
125                     }
126 
127                     @Override
128                     protected void dispose0() throws Exception {
129                     }
130                     
131                     /**
132                      * {@inheritDoc}
133                      */
134                     public IoSessionConfig getSessionConfig() {
135                         return sessionConfig;
136                     }
137                 });
138 
139         processor = new IoProcessor<IoSession>() {
140             public void add(IoSession session) {
141                 // Do nothing
142             }
143 
144             public void flush(IoSession session) {
145                 DummySession s = (DummySession) session;
146                 WriteRequest req = s.getWriteRequestQueue().poll(session);
147 
148                 // Chek that the request is not null. If the session has been closed,
149                 // we may not have any pending requests.
150                 if (req != null) {
151                     Object m = req.getMessage();
152                     if (m instanceof FileRegion) {
153                         FileRegion file = (FileRegion) m;
154                         try {
155                             file.getFileChannel().position(file.getPosition() + file.getRemainingBytes());
156                             file.update(file.getRemainingBytes());
157                         } catch (IOException e) {
158                             s.getFilterChain().fireExceptionCaught(e);
159                         }
160                     }
161                     getFilterChain().fireMessageSent(req);
162                 }
163             }
164 
165             /**
166              * {@inheritDoc}
167              */
168             public void write(IoSession session, WriteRequest writeRequest) {
169                 WriteRequestQueue writeRequestQueue = session.getWriteRequestQueue();
170 
171                 writeRequestQueue.offer(session, writeRequest);
172 
173                 if (!session.isWriteSuspended()) {
174                     this.flush(session);
175                 }
176             }
177 
178             public void remove(IoSession session) {
179                 if (!session.getCloseFuture().isClosed()) {
180                     session.getFilterChain().fireSessionClosed();
181                 }
182             }
183 
184             public void updateTrafficControl(IoSession session) {
185                 // Do nothing
186             }
187 
188             public void dispose() {
189                 // Do nothing
190             }
191 
192             public boolean isDisposed() {
193                 return false;
194             }
195 
196             public boolean isDisposing() {
197                 return false;
198             }
199 
200         };
201 
202         this.service = super.getService();
203 
204         try {
205             IoSessionDataStructureFactory factory = new DefaultIoSessionDataStructureFactory();
206             setAttributeMap(factory.getAttributeMap(this));
207             setWriteRequestQueue(factory.getWriteRequestQueue(this));
208         } catch (Exception e) {
209             throw new InternalError();
210         }
211     }
212 
213     public IoSessionConfig getConfig() {
214         return config;
215     }
216 
217     /**
218      * Sets the configuration of this session.
219      */
220     public void setConfig(IoSessionConfig config) {
221         if (config == null) {
222             throw new IllegalArgumentException("config");
223         }
224 
225         this.config = config;
226     }
227 
228     public IoFilterChain getFilterChain() {
229         return filterChain;
230     }
231 
232     public IoHandler getHandler() {
233         return handler;
234     }
235 
236     /**
237      * Sets the {@link IoHandler} which handles this session.
238      */
239     public void setHandler(IoHandler handler) {
240         if (handler == null) {
241             throw new IllegalArgumentException("handler");
242         }
243 
244         this.handler = handler;
245     }
246 
247     public SocketAddress getLocalAddress() {
248         return localAddress;
249     }
250 
251     public SocketAddress getRemoteAddress() {
252         return remoteAddress;
253     }
254 
255     /**
256      * Sets the socket address of local machine which is associated with
257      * this session.
258      */
259     public void setLocalAddress(SocketAddress localAddress) {
260         if (localAddress == null) {
261             throw new IllegalArgumentException("localAddress");
262         }
263 
264         this.localAddress = localAddress;
265     }
266 
267     /**
268      * Sets the socket address of remote peer.
269      */
270     public void setRemoteAddress(SocketAddress remoteAddress) {
271         if (remoteAddress == null) {
272             throw new IllegalArgumentException("remoteAddress");
273         }
274 
275         this.remoteAddress = remoteAddress;
276     }
277 
278     public IoService getService() {
279         return service;
280     }
281 
282     /**
283      * Sets the {@link IoService} which provides I/O service to this session.
284      */
285     public void setService(IoService service) {
286         if (service == null) {
287             throw new IllegalArgumentException("service");
288         }
289 
290         this.service = service;
291     }
292 
293     @Override
294     public final IoProcessor<IoSession> getProcessor() {
295         return processor;
296     }
297 
298     public TransportMetadata getTransportMetadata() {
299         return transportMetadata;
300     }
301 
302     /**
303      * Sets the {@link TransportMetadata} that this session runs on.
304      */
305     public void setTransportMetadata(TransportMetadata transportMetadata) {
306         if (transportMetadata == null) {
307             throw new IllegalArgumentException("transportMetadata");
308         }
309 
310         this.transportMetadata = transportMetadata;
311     }
312 
313     @Override
314     public void setScheduledWriteBytes(int byteCount) {
315         super.setScheduledWriteBytes(byteCount);
316     }
317 
318     @Override
319     public void setScheduledWriteMessages(int messages) {
320         super.setScheduledWriteMessages(messages);
321     }
322 
323     /**
324      * Update all statistical properties related with throughput.  By default
325      * this method returns silently without updating the throughput properties
326      * if they were calculated already within last
327      * {@link IoSessionConfig#getThroughputCalculationInterval() calculation interval}.
328      * If, however, <tt>force</tt> is specified as <tt>true</tt>, this method
329      * updates the throughput properties immediately.
330      */
331     public void updateThroughput(boolean force) {
332         super.updateThroughput(System.currentTimeMillis(), force);
333     }
334 }