001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.net.core;
005:
006: import com.tc.exception.ImplementMe;
007: import com.tc.net.TCSocketAddress;
008: import com.tc.net.core.event.TCConnectionEventListener;
009: import com.tc.net.protocol.NetworkMessageSink;
010: import com.tc.net.protocol.NullProtocolAdaptor;
011: import com.tc.net.protocol.TCNetworkMessage;
012: import com.tc.net.protocol.TCProtocolAdaptor;
013: import com.tc.util.TCTimeoutException;
014: import com.tc.util.concurrent.NoExceptionLinkedQueue;
015:
016: import java.net.Socket;
017: import java.util.ArrayList;
018: import java.util.List;
019:
020: /**
021: * @deprecated use TestTCConnection instead. This implementation has a bunch of side effects.
022: */
023: public class MockTCConnection implements TCConnection {
024:
025: private boolean isConnected;
026: private boolean isClosed;
027:
028: private int closeCallCount = 0;
029: private List sentMessages = new ArrayList();
030: private NetworkMessageSink messageSink;
031: private final TCProtocolAdaptor protocolAdaptor = new NullProtocolAdaptor();
032:
033: public TCSocketAddress localAddress = new TCSocketAddress(
034: TCSocketAddress.LOOPBACK_ADDR, 0);
035: public TCSocketAddress remoteAddress = new TCSocketAddress(
036: TCSocketAddress.LOOPBACK_ADDR, 0);
037:
038: public boolean fail = false;
039: public final NoExceptionLinkedQueue connectCalls = new NoExceptionLinkedQueue();
040:
041: public long getConnectTime() {
042: throw new ImplementMe();
043: }
044:
045: public long getIdleTime() {
046: throw new ImplementMe();
047: }
048:
049: public void addListener(TCConnectionEventListener listener) {
050: //
051: }
052:
053: public void removeListener(TCConnectionEventListener listener) {
054: //
055: }
056:
057: public boolean close(long timeout) {
058: isConnected = false;
059: isClosed = true;
060: closeCallCount++;
061: return true;
062: }
063:
064: public int getCloseCallCount() {
065: return this .closeCallCount;
066: }
067:
068: public void connect(TCSocketAddress addr, int timeout)
069: throws TCTimeoutException {
070: connectCalls.put(new Object[] { addr, new Integer(timeout) });
071: if (fail) {
072: throw new TCTimeoutException("Timed out !!!");
073: }
074: this .isConnected = true;
075: }
076:
077: public boolean asynchConnect(TCSocketAddress addr) {
078: return this .isConnected = true;
079: }
080:
081: public void isConnected(boolean b) {
082: this .isConnected = b;
083: }
084:
085: public boolean isConnected() {
086: return isConnected;
087: }
088:
089: public boolean isClosed() {
090: return isClosed;
091: }
092:
093: public void setMessageSink(NetworkMessageSink sink) {
094: this .messageSink = sink;
095: }
096:
097: public void putMessage(TCNetworkMessage message) {
098: this .sentMessages.add(message);
099: if (this .messageSink != null)
100: this .messageSink.putMessage(message);
101: }
102:
103: public List getSentMessages() {
104: return this .sentMessages;
105: }
106:
107: public TCSocketAddress getLocalAddress() {
108: return this .localAddress;
109: }
110:
111: public TCSocketAddress getRemoteAddress() {
112: return this .remoteAddress;
113: }
114:
115: public TCProtocolAdaptor getProtocolAdaptor() {
116: return this .protocolAdaptor;
117: }
118:
119: public void asynchClose() {
120: close(-1);
121: }
122:
123: public Socket detach() {
124: throw new ImplementMe();
125: }
126:
127: }
|