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.net.TCSocketAddress;
007: import com.tc.net.core.event.TCConnectionEventListener;
008: import com.tc.net.protocol.NetworkMessageSink;
009: import com.tc.util.TCTimeoutException;
010:
011: import java.io.IOException;
012: import java.net.Socket;
013:
014: /**
015: * Common interface for TC network connections
016: *
017: * @author teck
018: */
019: public interface TCConnection extends NetworkMessageSink {
020:
021: /**
022: * Returns a long timestamp as reported by <code>Sytem.currentTimeMillis()</code> when this connection was
023: * connected. If <code>connect()</code> has never been called, the return value is undefined
024: */
025: public long getConnectTime();
026:
027: /**
028: * Returns a long representing the number of milliseconds this connection has been idle (ie. not data sent or
029: * received)
030: */
031: public long getIdleTime();
032:
033: /**
034: * Add the given connection event listener. Re-adding an existing listener will have no effect (ie. the listener will
035: * not be in the list twice).
036: *
037: * @param listener listener to add
038: */
039: public void addListener(TCConnectionEventListener listener);
040:
041: /**
042: * Remove the given event listener. Attempting to remove a listener that is not currently in the listeners set has not
043: * effect
044: */
045: public void removeListener(TCConnectionEventListener listener);
046:
047: /**
048: * Close this connection. The actual close happens asynchronously to this call.
049: */
050: public void asynchClose();
051:
052: /**
053: * Detatch this connection from it's connection manager
054: *
055: * @throws IOException
056: */
057: public Socket detach() throws IOException;
058:
059: /**
060: * Close this connection, blocking for at most the given timeout value
061: *
062: * @return true/false whether the connection closed in time
063: */
064: public boolean close(long timeout);
065:
066: /**
067: * Connect synchronously to a given destination. If this call fails, connect called be called again. However once a
068: * connection has successfully connected once, it cannot be re-connected
069: *
070: * @param addr the destination address
071: * @param timeout time in milliseconds to wait before throwing a timeout exception
072: * @throws IOException if there is an error connecting to the destination address
073: * @throws TCTimeoutException if a timeout occurs
074: */
075: public void connect(TCSocketAddress addr, int timeout)
076: throws IOException, TCTimeoutException;
077:
078: /**
079: * Connect asynchronously to the given destination address
080: *
081: * @param addr the destination address to connect to
082: * @return true if the connection was opened immediately, otherwise false (NOTE: return value of "false" this NOT an
083: * indication of error)
084: * @throws IOException if there is an error connecting to the destination
085: */
086: public boolean asynchConnect(TCSocketAddress addr)
087: throws IOException;
088:
089: /**
090: * Whether or not this connection is connected
091: *
092: * @return true iff this connection is connected to the destination address
093: */
094: public boolean isConnected();
095:
096: /**
097: * Whether or not this connection is closed
098: *
099: * @return true iff this connection is closed
100: */
101: public boolean isClosed();
102:
103: /**
104: * Get local side connection details
105: *
106: * @throws IllegalStateException if connection has never been connected
107: */
108: public TCSocketAddress getLocalAddress();
109:
110: /**
111: * Get remote side connection details
112: *
113: * @throws IllegalStateException if connection has never been connected
114: */
115: public TCSocketAddress getRemoteAddress();
116: }
|