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