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  
41  /**
42   * A dummy {@link IoSession} for unit-testing or non-network-use of
43   * the classes that depends on {@link IoSession}.
44   *
45   * <h2>Overriding I/O request methods</h2>
46   * All I/O request methods (i.e. {@link #close()}, {@link #write(Object)} and
47   * {@link #setTrafficMask(TrafficMask)}) are final and therefore cannot be
48   * overridden, but you can always add your custom {@link IoFilter} to the
49   * {@link IoFilterChain} to intercept any I/O events and requests.
50   *
51   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
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              // Do nothing
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       * Creates a new instance.
88       */
89      public DummySession() {
90          super(
91  
92          // Initialize dummy service.
93              new AbstractIoAcceptor(
94                  new AbstractIoSessionConfig() {
95                      @Override
96                      protected void doSetAll(IoSessionConfig config) {
97                          // Do nothing
98                      }
99                  },
100                 new Executor() {
101                     public void execute(Runnable command) {
102                         // Do nothing
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                 // Do nothing
132             }
133 
134             public void flush(AbstractIoSession session) {
135                 DummySession s = (DummySession) session;
136                 WriteRequest req = s.getWriteRequestQueue().poll(session);
137                 
138                 // Chek that the request is not null. If the session has been closed,
139                 // we may not have any pending requests.
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                 // Do nothing
163             }
164 
165             public void dispose() {
166                 // Do nothing
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      * Sets the configuration of this session.
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      * Sets the {@link IoHandler} which handles this session.
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      * Sets the socket address of local machine which is associated with
234      * this session.
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      * Sets the socket address of remote peer.
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      * Sets the {@link IoService} which provides I/O service to this session.
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      * Sets the {@link TransportMetadata} that this session runs on.
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      * Update all statistical properties related with throughput.  By default
302      * this method returns silently without updating the throughput properties
303      * if they were calculated already within last
304      * {@link IoSessionConfig#getThroughputCalculationInterval() calculation interval}.
305      * If, however, <tt>force</tt> is specified as <tt>true</tt>, this method
306      * updates the throughput properties immediately.
307      */
308     public void updateThroughput(boolean force) {
309         super.updateThroughput(System.currentTimeMillis(), force);
310     }
311 }