001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.server.connection;
031:
032: import com.caucho.loader.Environment;
033: import com.caucho.management.server.AbstractManagedObject;
034: import com.caucho.management.server.TcpConnectionMXBean;
035: import com.caucho.util.Alarm;
036: import com.caucho.util.ThreadPool;
037: import com.caucho.util.ThreadTask;
038: import com.caucho.vfs.ClientDisconnectException;
039: import com.caucho.vfs.QSocket;
040: import com.caucho.vfs.ReadStream;
041:
042: import java.io.IOException;
043: import java.net.InetAddress;
044: import java.util.*;
045: import java.util.logging.Level;
046: import java.util.logging.Logger;
047:
048: /**
049: * Controls a tcp connection for comet.
050: */
051: public class ConnectionController {
052: private static final Logger log = Logger
053: .getLogger(ConnectionController.class.getName());
054:
055: private Connection _conn;
056: private boolean _isTimeout;
057:
058: /**
059: * Creates a new TcpConnectionController.
060: *
061: * @param conn The TCP connection
062: */
063: protected ConnectionController(Connection conn) {
064: _conn = conn;
065:
066: conn.setController(this );
067: }
068:
069: public Connection getConnection() {
070: return _conn;
071: }
072:
073: /**
074: * Wakes the connection.
075: */
076: public final boolean wake() {
077: Connection conn = _conn;
078:
079: if (conn != null)
080: return conn.wake();
081: else
082: return false;
083: }
084:
085: /**
086: * Sets the timeout.
087: */
088: public final void timeout() {
089: _isTimeout = true;
090: }
091:
092: /**
093: * Return true if timed out
094: */
095: public final boolean isTimeout() {
096: return _isTimeout;
097: }
098:
099: /**
100: * Returns true if the connection is active.
101: */
102: public final boolean isActive() {
103: return _conn != null && !_isTimeout;
104: }
105:
106: /**
107: * Returns true if the connection is active.
108: */
109: public final boolean isClosed() {
110: return _conn == null;
111: }
112:
113: /**
114: * Closes the connection.
115: */
116: public void close() {
117: Connection conn = _conn;
118: _conn = null;
119:
120: if (conn != null)
121: conn.closeController(this);
122: }
123: }
|