|
1 /* |
|
2 * Copyright (c) 2001, 2018, 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. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 package nsk.share.jdwp; |
|
25 |
|
26 import nsk.share.*; |
|
27 |
|
28 import java.io.IOException; |
|
29 //import java.util.Vector; |
|
30 |
|
31 /** |
|
32 * This class represents an abstract transport for JDWP. |
|
33 */ |
|
34 public abstract class Transport extends Log.Logger { |
|
35 |
|
36 public static final String LOG_PREFIX = "transport> "; |
|
37 |
|
38 /** |
|
39 * Make base <code>Transport</code> object providing with specified Log. |
|
40 */ |
|
41 public Transport(Log log) { |
|
42 super(log, LOG_PREFIX); |
|
43 } |
|
44 |
|
45 /** |
|
46 * Return number of bytes that can be received. |
|
47 */ |
|
48 public abstract int available() throws IOException; |
|
49 |
|
50 /** |
|
51 * Flushe bytes being buffered for writing if any. |
|
52 */ |
|
53 public abstract void flush() throws IOException; |
|
54 |
|
55 /** |
|
56 * Receive the next byte of data. |
|
57 * |
|
58 * The value byte is returned as an int in the range 0 to 255. |
|
59 * If no byte is available, the value -1 is returned. |
|
60 */ |
|
61 public abstract byte read() throws IOException; |
|
62 |
|
63 /** |
|
64 * Send the specified byte. |
|
65 */ |
|
66 public abstract void write(int b) throws IOException; |
|
67 |
|
68 /** |
|
69 * Send the specified bytes. |
|
70 */ |
|
71 public void write(byte[] b, int off, int len) throws IOException { |
|
72 for (int i = 0; i < len; i++) |
|
73 write(b[off + i]); |
|
74 } |
|
75 |
|
76 /** |
|
77 * Receive bytes of JDWP packet for default timeout. |
|
78 */ |
|
79 public void read(Packet packet) throws IOException { |
|
80 packet.readFrom(this); |
|
81 } |
|
82 |
|
83 /** |
|
84 * Send and flushe bytes of JDWP packet. |
|
85 */ |
|
86 public void write(Packet packet) throws IOException { |
|
87 packet.writeTo(this); |
|
88 flush(); |
|
89 } |
|
90 |
|
91 /** |
|
92 * Perform JDWP "handshake" procedure. |
|
93 */ |
|
94 public void handshake() throws IOException { |
|
95 |
|
96 String hs = "JDWP-Handshake"; |
|
97 byte[] hsb = hs.getBytes(); |
|
98 |
|
99 write(hsb, 0, hsb.length); |
|
100 flush(); |
|
101 |
|
102 try { |
|
103 Thread.currentThread().sleep(500); |
|
104 } catch (InterruptedException e) { |
|
105 throw new Failure(e); |
|
106 } |
|
107 |
|
108 int received = 0; |
|
109 for (int i = 0; i < Binder.CONNECT_TRIES; i++) { |
|
110 received = available(); |
|
111 if (received < hsb.length) { |
|
112 // System.err.println("Failed to hadshake try #" + i + ", bytes: " + received); |
|
113 try { |
|
114 Thread.currentThread().sleep(Binder.CONNECT_TRY_DELAY); |
|
115 } catch (InterruptedException e) { |
|
116 throw new Failure("Thread interrupted while sleeping between connection attempts:\n\t" |
|
117 + e); |
|
118 } |
|
119 } else { |
|
120 // System.err.println("Successed to hadshake try #" + i + ", bytes: " + received); |
|
121 for (int j = 0; j < hsb.length; j++) { |
|
122 byte b = (byte) (read() & 0xFF); |
|
123 if (b != hsb[j]) |
|
124 throw new IOException("Target VM failed to handshake: unexpected byte #" + j |
|
125 + ": " + b + " (expected:" + hsb[j] + ")"); |
|
126 } |
|
127 return; |
|
128 } |
|
129 } |
|
130 |
|
131 throw new IOException("Target VM failed to handshake: too few bytes in reply: " |
|
132 + received + "(expected: " + hsb.length + ")"); |
|
133 } |
|
134 |
|
135 /** |
|
136 * Set timeout for reading data in milliseconds. |
|
137 * Timeout of 0 means wait infinitly. |
|
138 */ |
|
139 public abstract void setReadTimeout(long millisecs) throws IOException; |
|
140 |
|
141 /** |
|
142 * Close JDWP connection. |
|
143 */ |
|
144 public abstract void close() throws IOException; |
|
145 |
|
146 /** |
|
147 * Perform finalization of object by closing JDWP connection. |
|
148 */ |
|
149 protected void finalize() throws Throwable { |
|
150 close(); |
|
151 } |
|
152 |
|
153 } |