001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.net.protocol.delivery;
006:
007: import com.tc.net.protocol.transport.MessageTransport;
008: import com.tc.net.protocol.transport.MessageTransportListener;
009: import com.tc.net.protocol.transport.RestoreConnectionCallback;
010: import com.tc.util.Assert;
011: import com.tc.util.DebugUtil;
012: import com.tc.util.TCTimerImpl;
013:
014: import java.util.TimerTask;
015:
016: public class OOOReconnectionTimeout implements
017: MessageTransportListener, RestoreConnectionCallback {
018:
019: private static final boolean debug = false;
020:
021: private final OnceAndOnlyOnceProtocolNetworkLayer oooLayer;
022: private final long timeoutMillis;
023: private TCTimerImpl timer = null;
024:
025: public OOOReconnectionTimeout(
026: final OnceAndOnlyOnceProtocolNetworkLayer oooLayer,
027: final long timeoutMillis) {
028: this .oooLayer = oooLayer;
029: this .timeoutMillis = timeoutMillis;
030: }
031:
032: public synchronized void notifyTransportClosed(
033: MessageTransport transport) {
034: log(transport, "Transport Closed");
035: oooLayer.notifyTransportClosed(transport);
036: }
037:
038: public synchronized void notifyTransportConnectAttempt(
039: MessageTransport transport) {
040: oooLayer.notifyTransportConnectAttempt(transport);
041: }
042:
043: public synchronized void notifyTransportDisconnected(
044: MessageTransport transport) {
045: Assert.assertNull(timer);
046: log(transport, "Transport Disconnected, starting Timer for "
047: + timeoutMillis);
048: if (oooLayer.isClosed()) {
049: return;
050: }
051: oooLayer.startRestoringConnection();
052: oooLayer.notifyTransportDisconnected(transport);
053: // start the timer...
054: timer = new TCTimerImpl("ClientConnectionRestoreTimer", true);
055: timer.schedule(new TimeoutTimerTask(transport, this ),
056: timeoutMillis);
057: }
058:
059: public synchronized void notifyTransportConnected(
060: MessageTransport transport) {
061: if (timer != null) {
062: log(transport, "Transport Connected, killing Timer for "
063: + timeoutMillis);
064: cancelTimer();
065: }
066: oooLayer.notifyTransportConnected(transport);
067: }
068:
069: private void cancelTimer() {
070: timer.cancel();
071: timer = null;
072: }
073:
074: public synchronized void restoreConnectionFailed(
075: MessageTransport transport) {
076: if (timer != null) {
077: log(transport,
078: "Restore Connection Failed, killing Timer for "
079: + timeoutMillis);
080: oooLayer.connectionRestoreFailed();
081: cancelTimer();
082: }
083: }
084:
085: static class TimeoutTimerTask extends TimerTask {
086: private final MessageTransport transport;
087: private final RestoreConnectionCallback rcc;
088:
089: public TimeoutTimerTask(final MessageTransport transport,
090: final RestoreConnectionCallback rcc) {
091: super ();
092: this .transport = transport;
093: this .rcc = rcc;
094: }
095:
096: public void run() {
097: rcc.restoreConnectionFailed(transport);
098: }
099: }
100:
101: private static void log(MessageTransport transport, String msg) {
102: if (debug)
103: DebugUtil.trace("OOOTimer-SERVER-"
104: + transport.getConnectionId() + " -> " + msg);
105: }
106: }
|