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