KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > cluster > ClusterStream


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.server.cluster;
31
32 import com.caucho.log.Log;
33 import com.caucho.util.Alarm;
34 import com.caucho.vfs.ReadStream;
35 import com.caucho.vfs.WriteStream;
36
37 import java.util.logging.Level JavaDoc;
38 import java.util.logging.Logger JavaDoc;
39
40 /**
41  * Defines a connection to the client.
42  */

43 public class ClusterStream {
44   static protected final Logger JavaDoc log = Log.open(ClusterStream.class);
45
46   private ClusterClient _srun;
47
48   private ReadStream _is;
49   private WriteStream _os;
50
51   private long _freeTime;
52
53   private String JavaDoc _debugId;
54
55   ClusterStream(int count, ClusterClient client,
56         ReadStream is, WriteStream os)
57   {
58     _srun = client;
59     _is = is;
60     _os = os;
61
62     _debugId = "[" + client.getDebugId() + ":" + count + "]";
63   }
64
65   /**
66    * Returns the cluster server.
67    */

68   public ClusterClient getServer()
69   {
70     return _srun;
71   }
72
73   /**
74    * Returns the input stream.
75    */

76   public ReadStream getReadStream()
77   {
78     return _is;
79   }
80
81   /**
82    * Returns the write stream.
83    */

84   public WriteStream getWriteStream()
85   {
86     return _os;
87   }
88
89   /**
90    * Returns the free time.
91    */

92   public long getFreeTime()
93   {
94     return _freeTime;
95   }
96
97   /**
98    * Sets the free time.
99    */

100   public void setFreeTime(long freeTime)
101   {
102     _freeTime = freeTime;
103   }
104
105   /**
106    * Returns true if nearing end of free time.
107    */

108   public boolean isLongIdle()
109   {
110     return (_srun.getServer().getLoadBalanceIdleTime() <
111         Alarm.getCurrentTime() - _freeTime + 2000L);
112   }
113
114   /**
115    * Returns the debug id.
116    */

117   public String JavaDoc getDebugId()
118   {
119     return _debugId;
120   }
121
122   /**
123    * Clears the recycled connections.
124    */

125   public void clearRecycle()
126   {
127     _srun.clearRecycle();
128   }
129
130   /**
131    * Recycles.
132    */

133   public void free()
134   {
135     if (_is != null)
136       _freeTime = _is.getReadTime();
137
138     _srun.free(this);
139   }
140
141   public void close()
142   {
143     if (_is != null)
144       _srun.close(this);
145
146     closeImpl();
147   }
148   
149   /**
150    * closes the stream.
151    */

152   void closeImpl()
153   {
154     ReadStream is = _is;
155     _is = null;
156     
157     WriteStream os = _os;
158     _os = null;
159     
160     try {
161       if (is != null)
162     is.close();
163     } catch (Throwable JavaDoc e) {
164       log.log(Level.FINER, e.toString(), e);
165     }
166
167     try {
168       if (os != null)
169     os.close();
170     } catch (Throwable JavaDoc e) {
171       log.log(Level.FINER, e.toString(), e);
172     }
173   }
174 }
175
Popular Tags