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.cluster;
031:
032: import com.caucho.log.Log;
033: import com.caucho.util.Alarm;
034: import com.caucho.vfs.ReadStream;
035: import com.caucho.vfs.WriteStream;
036:
037: import java.util.logging.Level;
038: import java.util.logging.Logger;
039:
040: /**
041: * Defines a connection to the client.
042: */
043: public class ClusterStream {
044: static protected final Logger log = Log.open(ClusterStream.class);
045:
046: private ServerConnector _srun;
047:
048: private ReadStream _is;
049: private WriteStream _os;
050:
051: private long _freeTime;
052:
053: private String _debugId;
054:
055: ClusterStream(int count, ServerConnector client, ReadStream is,
056: WriteStream os) {
057: _srun = client;
058: _is = is;
059: _os = os;
060:
061: _debugId = "[" + client.getDebugId() + ":" + count + "]";
062: }
063:
064: /**
065: * Returns the cluster server.
066: */
067: public ServerConnector getServer() {
068: return _srun;
069: }
070:
071: /**
072: * Returns the input stream.
073: */
074: public ReadStream getReadStream() {
075: _freeTime = 0;
076:
077: return _is;
078: }
079:
080: /**
081: * Returns the write stream.
082: */
083: public WriteStream getWriteStream() {
084: _freeTime = 0;
085:
086: return _os;
087: }
088:
089: /**
090: * Returns the free time, i.e. the time the connection was last idle.
091: */
092: public long getFreeTime() {
093: return _freeTime;
094: }
095:
096: /**
097: * Sets the free time.
098: */
099: public void setFreeTime(long freeTime) {
100: _freeTime = freeTime;
101: }
102:
103: /**
104: * Returns true if nearing end of free time.
105: */
106: public boolean isLongIdle() {
107: return (_srun.getLoadBalanceIdleTime() < Alarm.getCurrentTime()
108: - _freeTime + 2000L);
109: }
110:
111: /**
112: * Returns the debug id.
113: */
114: public String getDebugId() {
115: return _debugId;
116: }
117:
118: /**
119: * Clears the recycled connections.
120: */
121: public void clearRecycle() {
122: _srun.clearRecycle();
123: }
124:
125: /**
126: * Recycles.
127: */
128: public void free() {
129: // #2369 - the load balancer might set its own view of the free
130: // time
131: if (_is != null && _freeTime <= 0)
132: _freeTime = _is.getReadTime();
133:
134: _srun.free(this );
135: }
136:
137: public void close() {
138: if (_is != null)
139: _srun.close(this );
140:
141: closeImpl();
142: }
143:
144: /**
145: * closes the stream.
146: */
147: void closeImpl() {
148: ReadStream is = _is;
149: _is = null;
150:
151: WriteStream os = _os;
152: _os = null;
153:
154: try {
155: if (is != null)
156: is.close();
157: } catch (Throwable e) {
158: log.log(Level.FINER, e.toString(), e);
159: }
160:
161: try {
162: if (os != null)
163: os.close();
164: } catch (Throwable e) {
165: log.log(Level.FINER, e.toString(), e);
166: }
167: }
168:
169: public String toString() {
170: return "ClusterStream[" + _debugId + "]";
171: }
172: }
|