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         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      * Sets the configuration of this session.
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      * Sets the {@link IoHandler} which handles this session.
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      * Sets the socket address of local machine which is associated with
232      * this session.
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      * Sets the socket address of remote peer.
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      * Sets the {@link IoService} which provides I/O service to this session.
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      * Sets the {@link TransportMetadata} that this session runs on.
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      * Update all statistical properties related with throughput.  By default
300      * this method returns silently without updating the throughput properties
301      * if they were calculated already within last
302      * {@link IoSessionConfig#getThroughputCalculationInterval() calculation interval}.
303      * If, however, <tt>force</tt> is specified as <tt>true</tt>, this method
304      * updates the throughput properties immediately.
305      */
306     public void updateThroughput(boolean force) {
307         super.updateThroughput(System.currentTimeMillis(), force);
308     }
309 }