Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/jdwp/Transport.java
41161 views
/*1* Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package nsk.share.jdwp;2425import nsk.share.*;2627import java.io.IOException;28//import java.util.Vector;2930/**31* This class represents an abstract transport for JDWP.32*/33public abstract class Transport extends Log.Logger {3435public static final String LOG_PREFIX = "transport> ";3637/**38* Make base <code>Transport</code> object providing with specified Log.39*/40public Transport(Log log) {41super(log, LOG_PREFIX);42}4344/**45* Return number of bytes that can be received.46*/47public abstract int available() throws IOException;4849/**50* Flushe bytes being buffered for writing if any.51*/52public abstract void flush() throws IOException;5354/**55* Receive the next byte of data.56*57* The value byte is returned as an int in the range 0 to 255.58* If no byte is available, the value -1 is returned.59*/60public abstract byte read() throws IOException;6162/**63* Send the specified byte.64*/65public abstract void write(int b) throws IOException;6667/**68* Send the specified bytes.69*/70public void write(byte[] b, int off, int len) throws IOException {71for (int i = 0; i < len; i++)72write(b[off + i]);73}7475/**76* Receive bytes of JDWP packet for default timeout.77*/78public void read(Packet packet) throws IOException {79packet.readFrom(this);80}8182/**83* Send and flushe bytes of JDWP packet.84*/85public void write(Packet packet) throws IOException {86packet.writeTo(this);87flush();88}8990/**91* Perform JDWP "handshake" procedure.92*/93public void handshake() throws IOException {9495String hs = "JDWP-Handshake";96byte[] hsb = hs.getBytes();9798write(hsb, 0, hsb.length);99flush();100101try {102Thread.currentThread().sleep(500);103} catch (InterruptedException e) {104throw new Failure(e);105}106107int received = 0;108for (int i = 0; i < Binder.CONNECT_TRIES; i++) {109received = available();110if (received < hsb.length) {111// System.err.println("Failed to hadshake try #" + i + ", bytes: " + received);112try {113Thread.currentThread().sleep(Binder.CONNECT_TRY_DELAY);114} catch (InterruptedException e) {115throw new Failure("Thread interrupted while sleeping between connection attempts:\n\t"116+ e);117}118} else {119// System.err.println("Successed to hadshake try #" + i + ", bytes: " + received);120for (int j = 0; j < hsb.length; j++) {121byte b = (byte) (read() & 0xFF);122if (b != hsb[j])123throw new IOException("Target VM failed to handshake: unexpected byte #" + j124+ ": " + b + " (expected:" + hsb[j] + ")");125}126return;127}128}129130throw new IOException("Target VM failed to handshake: too few bytes in reply: "131+ received + "(expected: " + hsb.length + ")");132}133134/**135* Set timeout for reading data in milliseconds.136* Timeout of 0 means wait infinitly.137*/138public abstract void setReadTimeout(long millisecs) throws IOException;139140/**141* Close JDWP connection.142*/143public abstract void close() throws IOException;144145/**146* Perform finalization of object by closing JDWP connection.147*/148protected void finalize() throws Throwable {149close();150}151152}153154155