001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.apache.mina.common;
021:
022: import java.net.SocketAddress;
023: import java.util.List;
024: import java.util.Set;
025:
026: /**
027: * A dummy {@link IoSession} for unit-testing or non-network-use of
028: * the classes that depends on {@link IoSession}.
029: *
030: * <h2>Overriding I/O request methods</h2>
031: * All I/O request methods (i.e. {@link #close()}, {@link #write(Object)} and
032: * {@link #setTrafficMask(TrafficMask)}) are final and therefore cannot be
033: * overridden, but you can always add your custom {@link IoFilter} to the
034: * {@link IoFilterChain} to intercept any I/O events and requests.
035: *
036: * @author The Apache MINA Project (dev@mina.apache.org)
037: * @version $Rev: 607170 $, $Date: 2007-12-27 21:01:22 -0700 (Thu, 27 Dec 2007) $
038: */
039: public class DummySession extends AbstractIoSession {
040:
041: private static final TransportMetadata TRANSPORT_METADATA = new DefaultTransportMetadata(
042: "mina", "dummy", false, false, SocketAddress.class,
043: IoSessionConfig.class, Object.class);
044:
045: private static final SocketAddress ANONYMOUS_ADDRESS = new SocketAddress() {
046: private static final long serialVersionUID = -496112902353454179L;
047:
048: @Override
049: public String toString() {
050: return "?";
051: }
052: };
053:
054: private volatile IoService service;
055:
056: private volatile IoSessionConfig config = new AbstractIoSessionConfig() {
057: @Override
058: protected void doSetAll(IoSessionConfig config) {
059: }
060: };
061:
062: private final IoFilterChain filterChain = new DefaultIoFilterChain(
063: this );
064: private final IoProcessor<IoSession> processor;
065:
066: private volatile IoHandler handler = new IoHandlerAdapter();
067: private volatile SocketAddress localAddress = ANONYMOUS_ADDRESS;
068: private volatile SocketAddress remoteAddress = ANONYMOUS_ADDRESS;
069: private volatile TransportMetadata transportMetadata = TRANSPORT_METADATA;
070:
071: /**
072: * Creates a new instance.
073: */
074: public DummySession() {
075: // Initialize dummy service.
076: IoAcceptor acceptor = new AbstractIoAcceptor(
077: new AbstractIoSessionConfig() {
078: @Override
079: protected void doSetAll(IoSessionConfig config) {
080: }
081: }) {
082:
083: @Override
084: protected Set<SocketAddress> bind0(
085: List<? extends SocketAddress> localAddresses)
086: throws Exception {
087: throw new UnsupportedOperationException();
088: }
089:
090: @Override
091: protected void unbind0(
092: List<? extends SocketAddress> localAddresses)
093: throws Exception {
094: throw new UnsupportedOperationException();
095: }
096:
097: public IoSession newSession(SocketAddress remoteAddress,
098: SocketAddress localAddress) {
099: throw new UnsupportedOperationException();
100: }
101:
102: public TransportMetadata getTransportMetadata() {
103: return TRANSPORT_METADATA;
104: }
105:
106: @Override
107: protected IoFuture dispose0() throws Exception {
108: return null;
109: }
110: };
111:
112: // Set meaningless default values.
113: acceptor.setHandler(new IoHandlerAdapter());
114:
115: this .service = acceptor;
116:
117: this .processor = new IoProcessor<IoSession>() {
118: public void add(IoSession session) {
119: }
120:
121: public void flush(IoSession session) {
122: getFilterChain().fireMessageSent(
123: ((DummySession) session).getWriteRequestQueue()
124: .poll(session));
125: }
126:
127: public void remove(IoSession session) {
128: }
129:
130: public void updateTrafficMask(IoSession session) {
131: }
132:
133: public void dispose() {
134: }
135:
136: public boolean isDisposed() {
137: return false;
138: }
139:
140: public boolean isDisposing() {
141: return false;
142: }
143: };
144:
145: try {
146: IoSessionDataStructureFactory factory = new DefaultIoSessionDataStructureFactory();
147: setAttributeMap(factory.getAttributeMap(this ));
148: setWriteRequestQueue(factory.getWriteRequestQueue(this ));
149: } catch (Exception e) {
150: throw new InternalError();
151: }
152: }
153:
154: public IoSessionConfig getConfig() {
155: return config;
156: }
157:
158: /**
159: * Sets the configuration of this session.
160: */
161: public void setConfig(IoSessionConfig config) {
162: if (config == null) {
163: throw new NullPointerException("config");
164: }
165:
166: this .config = config;
167: }
168:
169: public IoFilterChain getFilterChain() {
170: return filterChain;
171: }
172:
173: public IoHandler getHandler() {
174: return handler;
175: }
176:
177: /**
178: * Sets the {@link IoHandler} which handles this session.
179: */
180: public void setHandler(IoHandler handler) {
181: if (handler == null) {
182: throw new NullPointerException("handler");
183: }
184:
185: this .handler = handler;
186: }
187:
188: public SocketAddress getLocalAddress() {
189: return localAddress;
190: }
191:
192: public SocketAddress getRemoteAddress() {
193: return remoteAddress;
194: }
195:
196: /**
197: * Sets the socket address of local machine which is associated with
198: * this session.
199: */
200: public void setLocalAddress(SocketAddress localAddress) {
201: if (localAddress == null) {
202: throw new NullPointerException("localAddress");
203: }
204:
205: this .localAddress = localAddress;
206: }
207:
208: /**
209: * Sets the socket address of remote peer.
210: */
211: public void setRemoteAddress(SocketAddress remoteAddress) {
212: if (remoteAddress == null) {
213: throw new NullPointerException("remoteAddress");
214: }
215:
216: this .remoteAddress = remoteAddress;
217: }
218:
219: public IoService getService() {
220: return service;
221: }
222:
223: /**
224: * Sets the {@link IoService} which provides I/O service to this session.
225: */
226: public void setService(IoService service) {
227: if (service == null) {
228: throw new NullPointerException("service");
229: }
230:
231: this .service = service;
232: }
233:
234: @Override
235: protected final IoProcessor<IoSession> getProcessor() {
236: return processor;
237: }
238:
239: public TransportMetadata getTransportMetadata() {
240: return transportMetadata;
241: }
242:
243: /**
244: * Sets the {@link TransportMetadata} that this session runs on.
245: */
246: public void setTransportMetadata(TransportMetadata transportMetadata) {
247: if (transportMetadata == null) {
248: throw new NullPointerException("transportMetadata");
249: }
250:
251: this .transportMetadata = transportMetadata;
252: }
253:
254: @Override
255: public void setScheduledWriteBytes(long byteCount) {
256: super .setScheduledWriteBytes(byteCount);
257: }
258:
259: @Override
260: public void setScheduledWriteMessages(int messages) {
261: super .setScheduledWriteMessages(messages);
262: }
263:
264: /**
265: * Update all statistical properties related with throughput. By default
266: * this method returns silently without updating the throughput properties
267: * if they were calculated already within last
268: * {@link IoSessionConfig#getThroughputCalculationInterval() calculation interval}.
269: * If, however, <tt>force</tt> is specified as <tt>true</tt>, this method
270: * updates the throughput properties immediately.
271: */
272: public void updateThroughput(boolean force) {
273: super.updateThroughput(System.currentTimeMillis(), force);
274: }
275: }
|