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.transport;
006:
007: import com.tc.logging.ConnectionIDProvider;
008: import com.tc.logging.ConnectionIdLogger;
009: import com.tc.logging.TCLogger;
010:
011: import java.util.ArrayList;
012: import java.util.Iterator;
013: import java.util.LinkedList;
014: import java.util.List;
015:
016: public abstract class AbstractMessageTransport implements
017: MessageTransport, ConnectionIDProvider {
018:
019: private static final int DISCONNECTED = 1;
020: private static final int CONNECTED = 2;
021: private static final int CONNECT_ATTEMPT = 3;
022: private static final int CLOSED = 4;
023:
024: protected final ConnectionIdLogger logger;
025: private final List listeners = new LinkedList();
026:
027: public AbstractMessageTransport(TCLogger logger) {
028: this .logger = new ConnectionIdLogger(this , logger);
029: }
030:
031: public ConnectionIdLogger getLogger() {
032: return logger;
033: }
034:
035: public final void addTransportListeners(List toAdd) {
036: for (Iterator i = toAdd.iterator(); i.hasNext();) {
037: MessageTransportListener l = (MessageTransportListener) i
038: .next();
039: addTransportListener(l);
040: }
041: }
042:
043: protected List getTransportListeners() {
044: return new ArrayList(listeners);
045: }
046:
047: public void addTransportListener(MessageTransportListener listener) {
048: synchronized (listeners) {
049: if (listeners.contains(listener))
050: throw new AssertionError(
051: "Attempt to add the same listener more than once: "
052: + listener);
053: listeners.add(listener);
054: }
055: }
056:
057: public final void removeTransportListeners() {
058: synchronized (listeners) {
059: this .listeners.clear();
060: }
061: }
062:
063: protected final void fireTransportConnectAttemptEvent() {
064: fireTransportEvent(CONNECT_ATTEMPT);
065: }
066:
067: protected final void fireTransportConnectedEvent() {
068: logFireTransportConnectEvent();
069: fireTransportEvent(CONNECTED);
070: }
071:
072: private void logFireTransportConnectEvent() {
073: if (logger.isDebugEnabled()) {
074: logger.debug("Firing connect event...");
075: }
076: }
077:
078: protected final void fireTransportDisconnectedEvent() {
079: fireTransportEvent(DISCONNECTED);
080: }
081:
082: protected final void fireTransportClosedEvent() {
083: fireTransportEvent(CLOSED);
084: }
085:
086: private void fireTransportEvent(int type) {
087: synchronized (listeners) {
088: for (Iterator i = listeners.iterator(); i.hasNext();) {
089: MessageTransportListener listener = (MessageTransportListener) i
090: .next();
091: switch (type) {
092: case DISCONNECTED:
093: listener.notifyTransportDisconnected(this );
094: break;
095: case CONNECTED:
096: listener.notifyTransportConnected(this );
097: break;
098: case CONNECT_ATTEMPT:
099: listener.notifyTransportConnectAttempt(this );
100: break;
101: case CLOSED:
102: listener.notifyTransportClosed(this );
103: break;
104: default:
105: throw new AssertionError(
106: "Unknown transport event: " + type);
107: }
108: }
109: }
110: }
111: }
|