jdk/src/jdk.jdi/share/classes/com/sun/jdi/connect/spi/Connection.java
author clanger
Fri, 16 Jun 2017 14:09:31 +0200
changeset 45564 0149773a140c
parent 45532 ff4b62180768
permissions -rw-r--r--
8181417: Code cleanups in com.sun.jdi Reviewed-by: alanb, stuefe, sspitsyn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
45564
0149773a140c 8181417: Code cleanups in com.sun.jdi
clanger
parents: 45532
diff changeset
     2
 * Copyright (c) 2003, 2017, 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 com.sun.jdi.connect.spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * A connection between a debugger and a target VM which it debugs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * <p> A Connection represents a bi-directional communication channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * between a debugger and a target VM. A Connection is created when
45564
0149773a140c 8181417: Code cleanups in com.sun.jdi
clanger
parents: 45532
diff changeset
    35
 * {@link TransportService TransportService} establishes a connection
0149773a140c 8181417: Code cleanups in com.sun.jdi
clanger
parents: 45532
diff changeset
    36
 * and successfully handshakes with a target VM. A TransportService
0149773a140c 8181417: Code cleanups in com.sun.jdi
clanger
parents: 45532
diff changeset
    37
 * implementation provides a reliable JDWP packet transportation service
0149773a140c 8181417: Code cleanups in com.sun.jdi
clanger
parents: 45532
diff changeset
    38
 * and consequently a Connection provides a reliable flow of JDWP packets
0149773a140c 8181417: Code cleanups in com.sun.jdi
clanger
parents: 45532
diff changeset
    39
 * between the debugger and the target VM. A Connection is stream oriented,
0149773a140c 8181417: Code cleanups in com.sun.jdi
clanger
parents: 45532
diff changeset
    40
 * that is, the JDWP packets written to a connection are read by the target VM
0149773a140c 8181417: Code cleanups in com.sun.jdi
clanger
parents: 45532
diff changeset
    41
 * in the order in which they were written. Similarly packets written
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * to a Connection by the target VM are read by the debugger in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * order in which they were written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p> A connection is either open or closed. It is open upon creation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * and remains open until it is closed. Once closed, it remains closed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * and any attempt to invoke an I/O operation upon it will cause a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * {@link ClosedConnectionException} to be thrown. A connection can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * be tested by invoking the {@link #isOpen isOpen} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p> A Connection is safe for access by multiple concurrent threads,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * although at most one thread may be reading and at most one thread may
30037
3e785fad2c3b 8078622: remove tidy warnings from JPDA docs
avstepan
parents: 25859
diff changeset
    53
 * be writing at any given time.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
public abstract class Connection {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Reads a packet from the target VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * <p> Attempts to read a JDWP packet from the target VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * A read operation may block indefinitely and only returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * when it reads all bytes of a packet, or in the case of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * transport service that is based on a stream-oriented
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * communication protocol, the end of stream is encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * <p> Reading a packet does not do any integrity checking on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * the packet aside from a check that the length of the packet
30037
3e785fad2c3b 8078622: remove tidy warnings from JPDA docs
avstepan
parents: 25859
diff changeset
    70
     * (as indicated by the value of the {@code length} field, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * first four bytes of the packet) is 11 or more bytes.
30037
3e785fad2c3b 8078622: remove tidy warnings from JPDA docs
avstepan
parents: 25859
diff changeset
    72
     * If the value of the {@code length} value is less then 11
3e785fad2c3b 8078622: remove tidy warnings from JPDA docs
avstepan
parents: 25859
diff changeset
    73
     * then an {@code IOException} is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * <p> Returns a byte array of a length equal to the length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * of the received packet, or a byte array of length 0 when an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * end of stream is encountered. If end of stream is encountered
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * after some, but not all bytes of a packet, are read then it
30037
3e785fad2c3b 8078622: remove tidy warnings from JPDA docs
avstepan
parents: 25859
diff changeset
    79
     * is considered an I/O error and an {@code IOException} is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * thrown. The first byte of the packet is stored in element
30037
3e785fad2c3b 8078622: remove tidy warnings from JPDA docs
avstepan
parents: 25859
diff changeset
    81
     * {@code 0} of the byte array, the second in element {@code 1},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * and so on. The bytes in the byte array are laid out as per the
45532
ff4b62180768 8180300: Move JDWP specs to specs directory
ihse
parents: 34894
diff changeset
    83
     * <a href="{@docRoot}/../specs/jdwp/jdwp-spec.html">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * JDWP specification</a>. That is, all fields in the packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * are in big endian order as per the JDWP specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * <p> This method may be invoked at any time.  If another thread has
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * already initiated a {@link #readPacket readPacket} on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * connection then the invocation of this method will block until the
30037
3e785fad2c3b 8078622: remove tidy warnings from JPDA docs
avstepan
parents: 25859
diff changeset
    90
     * first operation is complete.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @return  the packet read from the target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @throws  ClosedConnectionException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *          If the connection is closed, or another thread closes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *          the connection while the readPacket is in progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @throws  java.io.IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *          If the length of the packet (as indictaed by the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *          4 bytes) is less than 11 bytes, or an I/O error occurs.
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
    public abstract byte[] readPacket() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Writes a packet to the target VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * <p> Attempts to write, or send, a JDWP packet to the target VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * A write operation only returns after writing the entire packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * to the target VM. Writing the entire packet does not mean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * the entire packet has been transmitted to the target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * but rather that all bytes have been written to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * transport service. A transport service based on a TCP/IP connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * may, for example, buffer some or all of the packet before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * transmission on the network.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * <p> The byte array provided to this method should be laid out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * as per the <a
45532
ff4b62180768 8180300: Move JDWP specs to specs directory
ihse
parents: 34894
diff changeset
   120
     * href="{@docRoot}/../specs/jdwp/jdwp-spec.html">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * JDWP specification</a>. That is, all fields in the packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * are in big endian order. The first byte, that is element
30037
3e785fad2c3b 8078622: remove tidy warnings from JPDA docs
avstepan
parents: 25859
diff changeset
   123
     * {@code pkt[0]}, is the first byte of the {@code length} field.
3e785fad2c3b 8078622: remove tidy warnings from JPDA docs
avstepan
parents: 25859
diff changeset
   124
     * {@code pkt[1]} is the second byte of the {@code length} field,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * <p> Writing a packet does not do any integrity checking on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * the packet aside from checking the packet length. Checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * the packet length requires checking that the value of the
30037
3e785fad2c3b 8078622: remove tidy warnings from JPDA docs
avstepan
parents: 25859
diff changeset
   130
     * {@code length} field (as indicated by the first four bytes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * of the packet) is 11 or greater. Consequently the length of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * the byte array provided to this method, that is
30037
3e785fad2c3b 8078622: remove tidy warnings from JPDA docs
avstepan
parents: 25859
diff changeset
   133
     * {@code pkt.length}, must be 11 or more, and must be equal
3e785fad2c3b 8078622: remove tidy warnings from JPDA docs
avstepan
parents: 25859
diff changeset
   134
     * or greater than the value of the {@code length} field. If the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * length of the byte array is greater than the value of
30037
3e785fad2c3b 8078622: remove tidy warnings from JPDA docs
avstepan
parents: 25859
diff changeset
   136
     * the {@code length} field then all bytes from element
3e785fad2c3b 8078622: remove tidy warnings from JPDA docs
avstepan
parents: 25859
diff changeset
   137
     * {@code pkt[length]} onwards are ignored. In other words,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * any additional bytes that follow the packet in the byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * array are ignored and will not be transmitted to the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * <p> A write operation may block or may complete immediately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * The exact circumstances when an operation blocks depends on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * the transport service. In the case of a TCP/IP connection to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * the target VM, the writePacket method may block if there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * network congestion or there is insufficient space to buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * the packet in the underlying network system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * <p> This method may be invoked at any time.  If another thread has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * already initiated a write operation upon this Connection then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * a subsequent invocation of this method will block until the first
30037
3e785fad2c3b 8078622: remove tidy warnings from JPDA docs
avstepan
parents: 25859
diff changeset
   152
     * operation is complete.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param   pkt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *          The packet to write to the target VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @throws  ClosedConnectionException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *          If the connection is closed, or another thread closes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *          the connection while the write operation is in progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @throws  java.io.IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *          If an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @throws  IllegalArgumentException
30037
3e785fad2c3b 8078622: remove tidy warnings from JPDA docs
avstepan
parents: 25859
diff changeset
   165
     *          If the value of the {@code length} field is invalid,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *          or the byte array is of insufficient length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public abstract void writePacket(byte pkt[]) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Closes this connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * <p> If the connection is already closed then invoking this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * has no effect. After a connection is closed, any further attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * calls to {@link #readPacket readPacket} or {@link #writePacket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * writePacket} will throw a {@link ClosedConnectionException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * <p> Any thread currently blocked in an I/O operation ({@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * #readPacket readPacket} or {@link #writePacket writePacket})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * will throw a {@link ClosedConnectionException}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * <p> This method may be invoked at any time.  If some other thread has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * already invoked it, however, then another invocation will block until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * the first invocation is complete, after which it will return without
30037
3e785fad2c3b 8078622: remove tidy warnings from JPDA docs
avstepan
parents: 25859
diff changeset
   185
     * effect.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @throws  java.io.IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public abstract void close() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
30037
3e785fad2c3b 8078622: remove tidy warnings from JPDA docs
avstepan
parents: 25859
diff changeset
   193
     * Tells whether or not this connection is open.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
30037
3e785fad2c3b 8078622: remove tidy warnings from JPDA docs
avstepan
parents: 25859
diff changeset
   195
     * @return {@code true} if and only if this connection is open
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public abstract boolean isOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
}