src/java.rmi/share/classes/sun/rmi/transport/tcp/TCPConnection.java
author mli
Thu, 28 Nov 2019 13:48:25 +0800
changeset 59300 60bdcb9a7b94
parent 47216 71c04702a3d5
permissions -rw-r--r--
8232446: logging enhancement for rmi when socket closed Reviewed-by: rriggs, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
37892
55da13d60938 8155978: Remove HTTP proxy implementation and tests from RMI
rriggs
parents: 37667
diff changeset
     2
 * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
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.Socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.rmi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.rmi.runtime.Log;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.rmi.transport.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class TCPConnection implements Connection {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private Socket socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private Channel channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private InputStream in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private OutputStream out = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private long expiration = Long.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private long lastuse = Long.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private long roundtrip = 5; // round-trip time for ping
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * Constructor used for creating a connection to accept call
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * (an input connection)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    TCPConnection(TCPChannel ch, Socket s, InputStream in, OutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        socket   = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        channel  = ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        this.in  = in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        this.out = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Constructor used by subclass when underlying input and output streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * are already available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    TCPConnection(TCPChannel ch, InputStream in, OutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        this(ch, null, in, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Constructor used when socket is available, but not underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * streams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    TCPConnection(TCPChannel ch, Socket s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        this(ch, s, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Gets the output stream for this connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public OutputStream getOutputStream() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (out == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            out = new BufferedOutputStream(socket.getOutputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Release the output stream for this connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public void releaseOutputStream() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (out != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Gets the input stream for this connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public InputStream getInputStream() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (in == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            in = new BufferedInputStream(socket.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Release the input stream for this connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public void releaseInputStream()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Determine if this connection can be used for multiple operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * If the socket implements RMISocketInfo, then we can query it about
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * this; otherwise, assume that it does provide a full-duplex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * persistent connection like java.net.Socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public boolean isReusable()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    {
37892
55da13d60938 8155978: Remove HTTP proxy implementation and tests from RMI
rriggs
parents: 37667
diff changeset
   119
        return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Set the expiration time of this connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @param time The time at which the time out expires.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    void setExpiration(long time)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        expiration = time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Set the timestamp at which this connection was last used successfully.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * The connection will be pinged for liveness if reused long after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * this time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param time The time at which the connection was last active.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    void setLastUseTime(long time)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        lastuse = time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Returns true if the timeout has expired on this connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * otherwise returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param time The current time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    boolean expired(long time)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return expiration <= time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Probes the connection to see if it still alive and connected to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * a responsive server.  If the connection has been idle for too
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * long, the server is pinged.  ``Too long'' means ``longer than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * last ping round-trip time''.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * This method may misdiagnose a dead connection as live, but it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * will never misdiagnose a live connection as dead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @return true if the connection and server are recently alive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public boolean isDead()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        InputStream i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        OutputStream o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        // skip ping if recently used within 1 RTT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        long start = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if ((roundtrip > 0) && (start < lastuse + roundtrip))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            return (false);     // still alive and warm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        // Get the streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            i = getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            o = getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            return (true);      // can't even get a stream, must be very dead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        // Write the ping byte and read the reply byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        int response = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            o.write(TransportConstants.Ping);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            o.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            response = i.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            TCPTransport.tcpLog.log(Log.VERBOSE, "exception: ", ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            TCPTransport.tcpLog.log(Log.BRIEF, "server ping failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            return (true);      // server failed the ping test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (response == TransportConstants.PingAck) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            // save most recent RTT for future use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            roundtrip = (System.currentTimeMillis() - start) * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            // clock-correction may make roundtrip < 0; doesn't matter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            return (false);     // it's alive and 5-by-5
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 (TCPTransport.tcpLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            TCPTransport.tcpLog.log(Log.BRIEF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                (response == -1 ? "server has been deactivated" :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                "server protocol error: ping response = " + response));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        return (true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Close the connection.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public void close() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    {
59300
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   212
        if (TCPTransport.tcpLog.isLoggable(Log.BRIEF)) {
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   213
            TCPTransport.tcpLog.log(Log.BRIEF,
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   214
                    "close connection, socket: " + socket);
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   215
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (socket != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            socket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Returns the channel for this connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public Channel getChannel()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        return channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
}