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.common;
21  
22  import java.net.SocketAddress;
23  import java.util.List;
24  import java.util.Set;
25  
26  /**
27   * A dummy {@link IoSession} for unit-testing or non-network-use of
28   * the classes that depends on {@link IoSession}.
29   * 
30   * <h2>Overriding I/O request methods</h2>
31   * All I/O request methods (i.e. {@link #close()}, {@link #write(Object)} and
32   * {@link #setTrafficMask(TrafficMask)}) are final and therefore cannot be
33   * overridden, but you can always add your custom {@link IoFilter} to the
34   * {@link IoFilterChain} to intercept any I/O events and requests.
35   *
36   * @author The Apache MINA Project (dev@mina.apache.org)
37   * @version $Rev: 607170 $, $Date: 2007-12-27 21:01:22 -0700 (Thu, 27 Dec 2007) $
38   */
39  public class DummySession extends AbstractIoSession {
40  
41      private static final TransportMetadata TRANSPORT_METADATA =
42              new DefaultTransportMetadata(
43                      "mina", "dummy", false, false,
44                      SocketAddress.class, IoSessionConfig.class, Object.class);
45  
46      private static final SocketAddress ANONYMOUS_ADDRESS = new SocketAddress() {
47          private static final long serialVersionUID = -496112902353454179L;
48  
49          @Override
50          public String toString() {
51              return "?";
52          }
53      };
54  
55      private volatile IoService service;
56  
57      private volatile IoSessionConfig config = new AbstractIoSessionConfig() {
58          @Override
59          protected void doSetAll(IoSessionConfig config) {
60          }
61      };
62  
63      private final IoFilterChain filterChain = new DefaultIoFilterChain(this);
64      private final IoProcessor<IoSession> processor;
65  
66      private volatile IoHandler handler = new IoHandlerAdapter();
67      private volatile SocketAddress localAddress = ANONYMOUS_ADDRESS;
68      private volatile SocketAddress remoteAddress = ANONYMOUS_ADDRESS;
69      private volatile TransportMetadata transportMetadata = TRANSPORT_METADATA;
70  
71      /**
72       * Creates a new instance.
73       */
74      public DummySession() {
75          // Initialize dummy service.
76          IoAcceptor acceptor = new AbstractIoAcceptor(
77                  new AbstractIoSessionConfig() {
78                      @Override
79                      protected void doSetAll(IoSessionConfig config) {}
80                  }) {
81  
82              @Override
83              protected Set<SocketAddress> bind0(List<? extends SocketAddress> localAddresses) throws Exception {
84                  throw new UnsupportedOperationException();
85              }
86  
87              @Override
88              protected void unbind0(List<? extends SocketAddress> localAddresses) throws Exception {
89                  throw new UnsupportedOperationException();
90              }
91  
92              public IoSession newSession(SocketAddress remoteAddress, SocketAddress localAddress) {
93                  throw new UnsupportedOperationException();
94              }
95  
96              public TransportMetadata getTransportMetadata() {
97                  return TRANSPORT_METADATA;
98              }
99  
100             @Override
101             protected IoFuture dispose0() throws Exception {
102                 return null;
103             }
104         };
105 
106         // Set meaningless default values.
107         acceptor.setHandler(new IoHandlerAdapter());
108 
109         this.service = acceptor;
110 
111         this.processor = new IoProcessor<IoSession>() {
112             public void add(IoSession session) {
113             }
114 
115             public void flush(IoSession session) {
116                 getFilterChain().fireMessageSent(
117                         ((DummySession) session).getWriteRequestQueue().poll(session));
118             }
119 
120             public void remove(IoSession session) {
121             }
122 
123             public void updateTrafficMask(IoSession session) {
124             }
125 
126             public void dispose() {
127             }
128 
129             public boolean isDisposed() {
130                 return false;
131             }
132 
133             public boolean isDisposing() {
134                 return false;
135             }
136         };
137 
138         try {
139             IoSessionDataStructureFactory factory = new DefaultIoSessionDataStructureFactory();
140             setAttributeMap(factory.getAttributeMap(this));
141             setWriteRequestQueue(factory.getWriteRequestQueue(this));
142         } catch (Exception e) {
143             throw new InternalError();
144         }
145     }
146 
147     public IoSessionConfig getConfig() {
148         return config;
149     }
150 
151     /**
152      * Sets the configuration of this session.
153      */
154     public void setConfig(IoSessionConfig config) {
155         if (config == null) {
156             throw new NullPointerException("config");
157         }
158 
159         this.config = config;
160     }
161 
162     public IoFilterChain getFilterChain() {
163         return filterChain;
164     }
165 
166     public IoHandler getHandler() {
167         return handler;
168     }
169 
170     /**
171      * Sets the {@link IoHandler} which handles this session.
172      */
173     public void setHandler(IoHandler handler) {
174         if (handler == null) {
175             throw new NullPointerException("handler");
176         }
177 
178         this.handler = handler;
179     }
180 
181     public SocketAddress getLocalAddress() {
182         return localAddress;
183     }
184 
185     public SocketAddress getRemoteAddress() {
186         return remoteAddress;
187     }
188 
189     /**
190      * Sets the socket address of local machine which is associated with
191      * this session.
192      */
193     public void setLocalAddress(SocketAddress localAddress) {
194         if (localAddress == null) {
195             throw new NullPointerException("localAddress");
196         }
197 
198         this.localAddress = localAddress;
199     }
200 
201     /**
202      * Sets the socket address of remote peer.
203      */
204     public void setRemoteAddress(SocketAddress remoteAddress) {
205         if (remoteAddress == null) {
206             throw new NullPointerException("remoteAddress");
207         }
208 
209         this.remoteAddress = remoteAddress;
210     }
211 
212     public IoService getService() {
213         return service;
214     }
215 
216     /**
217      * Sets the {@link IoService} which provides I/O service to this session.
218      */
219     public void setService(IoService service) {
220         if (service == null) {
221             throw new NullPointerException("service");
222         }
223 
224         this.service = service;
225     }
226 
227     @Override
228     protected final IoProcessor<IoSession> getProcessor() {
229         return processor;
230     }
231 
232     public TransportMetadata getTransportMetadata() {
233         return transportMetadata;
234     }
235 
236     /**
237      * Sets the {@link TransportMetadata} that this session runs on.
238      */
239     public void setTransportMetadata(TransportMetadata transportMetadata) {
240         if (transportMetadata == null) {
241             throw new NullPointerException("transportMetadata");
242         }
243 
244         this.transportMetadata = transportMetadata;
245     }
246     
247     @Override
248     public void setScheduledWriteBytes(long byteCount){
249         super.setScheduledWriteBytes(byteCount);
250     }
251 
252     @Override
253     public void setScheduledWriteMessages(int messages) {
254         super.setScheduledWriteMessages(messages);
255     }
256 
257     /**
258      * Update all statistical properties related with throughput.  By default
259      * this method returns silently without updating the throughput properties
260      * if they were calculated already within last 
261      * {@link IoSessionConfig#getThroughputCalculationInterval() calculation interval}.
262      * If, however, <tt>force</tt> is specified as <tt>true</tt>, this method
263      * updates the throughput properties immediately.
264      */
265     public void updateThroughput(boolean force) {
266         super.updateThroughput(System.currentTimeMillis(), force);
267     }
268 }