2
|
1 |
/*
|
|
2 |
* Copyright 1996-2001 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package sun.rmi.transport.tcp;
|
|
27 |
|
|
28 |
import java.io.*;
|
|
29 |
import java.net.InetAddress;
|
|
30 |
import java.net.Socket;
|
|
31 |
import java.net.SocketException;
|
|
32 |
import java.rmi.*;
|
|
33 |
import java.rmi.server.RMISocketFactory;
|
|
34 |
import sun.rmi.runtime.Log;
|
|
35 |
import sun.rmi.transport.*;
|
|
36 |
import sun.rmi.transport.proxy.*;
|
|
37 |
|
|
38 |
public class TCPConnection implements Connection {
|
|
39 |
|
|
40 |
private Socket socket;
|
|
41 |
private Channel channel;
|
|
42 |
private InputStream in = null;
|
|
43 |
private OutputStream out = null;
|
|
44 |
private long expiration = Long.MAX_VALUE;
|
|
45 |
private long lastuse = Long.MIN_VALUE;
|
|
46 |
private long roundtrip = 5; // round-trip time for ping
|
|
47 |
|
|
48 |
/**
|
|
49 |
* Constructor used for creating a connection to accept call
|
|
50 |
* (an input connection)
|
|
51 |
*/
|
|
52 |
TCPConnection(TCPChannel ch, Socket s, InputStream in, OutputStream out)
|
|
53 |
{
|
|
54 |
socket = s;
|
|
55 |
channel = ch;
|
|
56 |
this.in = in;
|
|
57 |
this.out = out;
|
|
58 |
}
|
|
59 |
|
|
60 |
/**
|
|
61 |
* Constructor used by subclass when underlying input and output streams
|
|
62 |
* are already available.
|
|
63 |
*/
|
|
64 |
TCPConnection(TCPChannel ch, InputStream in, OutputStream out)
|
|
65 |
{
|
|
66 |
this(ch, null, in, out);
|
|
67 |
}
|
|
68 |
|
|
69 |
/**
|
|
70 |
* Constructor used when socket is available, but not underlying
|
|
71 |
* streams.
|
|
72 |
*/
|
|
73 |
TCPConnection(TCPChannel ch, Socket s)
|
|
74 |
{
|
|
75 |
this(ch, s, null, null);
|
|
76 |
}
|
|
77 |
|
|
78 |
/**
|
|
79 |
* Gets the output stream for this connection
|
|
80 |
*/
|
|
81 |
public OutputStream getOutputStream() throws IOException
|
|
82 |
{
|
|
83 |
if (out == null)
|
|
84 |
out = new BufferedOutputStream(socket.getOutputStream());
|
|
85 |
return out;
|
|
86 |
}
|
|
87 |
|
|
88 |
/**
|
|
89 |
* Release the output stream for this connection.
|
|
90 |
*/
|
|
91 |
public void releaseOutputStream() throws IOException
|
|
92 |
{
|
|
93 |
if (out != null)
|
|
94 |
out.flush();
|
|
95 |
}
|
|
96 |
|
|
97 |
/**
|
|
98 |
* Gets the input stream for this connection.
|
|
99 |
*/
|
|
100 |
public InputStream getInputStream() throws IOException
|
|
101 |
{
|
|
102 |
if (in == null)
|
|
103 |
in = new BufferedInputStream(socket.getInputStream());
|
|
104 |
return in;
|
|
105 |
}
|
|
106 |
|
|
107 |
|
|
108 |
/**
|
|
109 |
* Release the input stream for this connection.
|
|
110 |
*/
|
|
111 |
public void releaseInputStream()
|
|
112 |
{
|
|
113 |
}
|
|
114 |
|
|
115 |
/**
|
|
116 |
* Determine if this connection can be used for multiple operations.
|
|
117 |
* If the socket implements RMISocketInfo, then we can query it about
|
|
118 |
* this; otherwise, assume that it does provide a full-duplex
|
|
119 |
* persistent connection like java.net.Socket.
|
|
120 |
*/
|
|
121 |
public boolean isReusable()
|
|
122 |
{
|
|
123 |
if ((socket != null) && (socket instanceof RMISocketInfo))
|
|
124 |
return ((RMISocketInfo) socket).isReusable();
|
|
125 |
else
|
|
126 |
return true;
|
|
127 |
}
|
|
128 |
|
|
129 |
/**
|
|
130 |
* Set the expiration time of this connection.
|
|
131 |
* @param time The time at which the time out expires.
|
|
132 |
*/
|
|
133 |
void setExpiration(long time)
|
|
134 |
{
|
|
135 |
expiration = time;
|
|
136 |
}
|
|
137 |
|
|
138 |
/**
|
|
139 |
* Set the timestamp at which this connection was last used successfully.
|
|
140 |
* The connection will be pinged for liveness if reused long after
|
|
141 |
* this time.
|
|
142 |
* @param time The time at which the connection was last active.
|
|
143 |
*/
|
|
144 |
void setLastUseTime(long time)
|
|
145 |
{
|
|
146 |
lastuse = time;
|
|
147 |
}
|
|
148 |
|
|
149 |
/**
|
|
150 |
* Returns true if the timeout has expired on this connection;
|
|
151 |
* otherwise returns false.
|
|
152 |
* @param time The current time.
|
|
153 |
*/
|
|
154 |
boolean expired(long time)
|
|
155 |
{
|
|
156 |
return expiration <= time;
|
|
157 |
}
|
|
158 |
|
|
159 |
/**
|
|
160 |
* Probes the connection to see if it still alive and connected to
|
|
161 |
* a responsive server. If the connection has been idle for too
|
|
162 |
* long, the server is pinged. ``Too long'' means ``longer than the
|
|
163 |
* last ping round-trip time''.
|
|
164 |
* <P>
|
|
165 |
* This method may misdiagnose a dead connection as live, but it
|
|
166 |
* will never misdiagnose a live connection as dead.
|
|
167 |
* @return true if the connection and server are recently alive
|
|
168 |
*/
|
|
169 |
public boolean isDead()
|
|
170 |
{
|
|
171 |
InputStream i;
|
|
172 |
OutputStream o;
|
|
173 |
|
|
174 |
// skip ping if recently used within 1 RTT
|
|
175 |
long start = System.currentTimeMillis();
|
|
176 |
if ((roundtrip > 0) && (start < lastuse + roundtrip))
|
|
177 |
return (false); // still alive and warm
|
|
178 |
|
|
179 |
// Get the streams
|
|
180 |
try {
|
|
181 |
i = getInputStream();
|
|
182 |
o = getOutputStream();
|
|
183 |
} catch (IOException e) {
|
|
184 |
return (true); // can't even get a stream, must be very dead
|
|
185 |
}
|
|
186 |
|
|
187 |
// Write the ping byte and read the reply byte
|
|
188 |
int response = 0;
|
|
189 |
try {
|
|
190 |
o.write(TransportConstants.Ping);
|
|
191 |
o.flush();
|
|
192 |
response = i.read();
|
|
193 |
} catch (IOException ex) {
|
|
194 |
TCPTransport.tcpLog.log(Log.VERBOSE, "exception: ", ex);
|
|
195 |
TCPTransport.tcpLog.log(Log.BRIEF, "server ping failed");
|
|
196 |
|
|
197 |
return (true); // server failed the ping test
|
|
198 |
}
|
|
199 |
|
|
200 |
if (response == TransportConstants.PingAck) {
|
|
201 |
// save most recent RTT for future use
|
|
202 |
roundtrip = (System.currentTimeMillis() - start) * 2;
|
|
203 |
// clock-correction may make roundtrip < 0; doesn't matter
|
|
204 |
return (false); // it's alive and 5-by-5
|
|
205 |
}
|
|
206 |
|
|
207 |
if (TCPTransport.tcpLog.isLoggable(Log.BRIEF)) {
|
|
208 |
TCPTransport.tcpLog.log(Log.BRIEF,
|
|
209 |
(response == -1 ? "server has been deactivated" :
|
|
210 |
"server protocol error: ping response = " + response));
|
|
211 |
}
|
|
212 |
return (true);
|
|
213 |
}
|
|
214 |
|
|
215 |
/**
|
|
216 |
* Close the connection. */
|
|
217 |
public void close() throws IOException
|
|
218 |
{
|
|
219 |
TCPTransport.tcpLog.log(Log.BRIEF, "close connection");
|
|
220 |
|
|
221 |
if (socket != null)
|
|
222 |
socket.close();
|
|
223 |
else {
|
|
224 |
in.close();
|
|
225 |
out.close();
|
|
226 |
}
|
|
227 |
}
|
|
228 |
|
|
229 |
/**
|
|
230 |
* Returns the channel for this connection.
|
|
231 |
*/
|
|
232 |
public Channel getChannel()
|
|
233 |
{
|
|
234 |
return channel;
|
|
235 |
}
|
|
236 |
}
|