1 4 package com.tc.net.core; 5 6 import com.tc.exception.ImplementMe; 7 import com.tc.net.TCSocketAddress; 8 import com.tc.net.core.event.TCConnectionEventListener; 9 import com.tc.net.protocol.NetworkMessageSink; 10 import com.tc.net.protocol.NullProtocolAdaptor; 11 import com.tc.net.protocol.TCNetworkMessage; 12 import com.tc.net.protocol.TCProtocolAdaptor; 13 import com.tc.util.TCTimeoutException; 14 import com.tc.util.concurrent.NoExceptionLinkedQueue; 15 16 import java.net.Socket ; 17 import java.util.ArrayList ; 18 import java.util.List ; 19 20 23 public class MockTCConnection implements TCConnection { 24 25 private boolean isConnected; 26 private boolean isClosed; 27 28 private int closeCallCount = 0; 29 private List sentMessages = new ArrayList (); 30 private NetworkMessageSink messageSink; 31 private final TCProtocolAdaptor protocolAdaptor = new NullProtocolAdaptor(); 32 33 public TCSocketAddress localAddress = new TCSocketAddress(TCSocketAddress.LOOPBACK_ADDR, 0); 34 public TCSocketAddress remoteAddress = new TCSocketAddress(TCSocketAddress.LOOPBACK_ADDR, 0); 35 36 public boolean fail = false; 37 public final NoExceptionLinkedQueue connectCalls = new NoExceptionLinkedQueue(); 38 39 public long getConnectTime() { 40 throw new ImplementMe(); 41 } 42 43 public long getIdleTime() { 44 throw new ImplementMe(); 45 } 46 47 public void addListener(TCConnectionEventListener listener) { 48 } 50 51 public void removeListener(TCConnectionEventListener listener) { 52 } 54 55 public boolean close(long timeout) { 56 isConnected = false; 57 isClosed = true; 58 closeCallCount++; 59 return true; 60 } 61 62 public int getCloseCallCount() { 63 return this.closeCallCount; 64 } 65 66 public void connect(TCSocketAddress addr, int timeout) throws TCTimeoutException { 67 connectCalls.put(new Object [] { addr, new Integer (timeout) }); 68 if(fail) { throw new TCTimeoutException("Timed out !!!"); } 69 this.isConnected = true; 70 } 71 72 public boolean asynchConnect(TCSocketAddress addr) { 73 return this.isConnected = true; 74 } 75 76 public void isConnected(boolean b) { 77 this.isConnected = b; 78 } 79 80 public boolean isConnected() { 81 return isConnected; 82 } 83 84 public boolean isClosed() { 85 return isClosed; 86 } 87 88 public void setMessageSink(NetworkMessageSink sink) { 89 this.messageSink = sink; 90 } 91 92 public void putMessage(TCNetworkMessage message) { 93 this.sentMessages.add(message); 94 if (this.messageSink != null) this.messageSink.putMessage(message); 95 } 96 97 public List getSentMessages() { 98 return this.sentMessages; 99 } 100 101 public TCSocketAddress getLocalAddress() { 102 return this.localAddress; 103 } 104 105 public TCSocketAddress getRemoteAddress() { 106 return this.remoteAddress; 107 } 108 109 public TCProtocolAdaptor getProtocolAdaptor() { 110 return this.protocolAdaptor; 111 } 112 113 public void asynchClose() { 114 close(-1); 115 } 116 117 public Socket detach() { 118 throw new ImplementMe(); 119 } 120 121 } | Popular Tags |