001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.cldchi.tools.memoryprofiler.jdwp;
028:
029: import java.io.*;
030: import java.net.*;
031:
032: /**
033: * This class represents a socket transport for JDWP. Actually
034: * is used as proxy that communicates with
035: * <code>SocketTransportImpl</code> that works in another thread and
036: * performs all the job for communicating with VM being debugged via
037: * JDWP.
038: *
039: * @see jdwp.SocketTransportImpl
040: * @see jdwp.Transport
041: */
042: class SocketTransport extends Transport {
043:
044: /**
045: * A <code>SocketTransportImpl</code> object that performs all
046: * the communications.
047: */
048: private SocketTransportImpl socketImpl;
049:
050: /**
051: * Connects to the specified port number on the named host. For this
052: * task this mehtod initializes a <code>SocketTransportImpl</code> object
053: * that works in another thread and manipulates it.
054: *
055: * @param ServerName a host where VM being debugged is running
056: * @param PortNumber a TCP/IP port number to communicate with VM being
057: * debugged
058: */
059: public void attachToServer(String ServerName, int PortNumber)
060: throws UnknownHostException, IOException {
061: socketImpl = new SocketTransportImpl();
062: socketImpl.initAsClient(ServerName, PortNumber, Replies);
063: synchronized (socketImpl) {
064: socketImpl.start();
065: try {
066: socketImpl.wait();
067: } catch (InterruptedException e) {
068: }
069: }
070: if (!socketImpl.isStarted()) {
071: throw new IOException("Connection is not established");
072: }
073: //System.out.println("Attached");
074: }
075:
076: /**
077: * Closes socket and streams.
078: */
079: public void done() throws IOException {
080: if (socketImpl != null) {
081: socketImpl.done();
082: socketImpl = null;
083: }
084: }
085:
086: /**
087: * Writes the specified byte to the socket.
088: */
089: public void write(int b) throws IOException {
090: socketImpl.write(b);
091: }
092:
093: /**
094: * Stores the received JDWP replies to the <code>Replies</code>
095: * vector.
096: *
097: * @see jdwp.Transport#Replies
098: */
099: public void receive() throws IOException {
100: socketImpl.receive();
101: }
102:
103: /**
104: * Returns a number of bytes that are available for reading.
105: *
106: * @return a number of bytes that are available for reading
107: */
108: public int available() throws IOException {
109: return socketImpl.availableInPrivateBuffer();
110: }
111:
112: /**
113: * Reads the next byte from the buffer. This byte is already read from
114: * the socket and stored in some buffer.
115: *
116: * @return a next byte from the buffer
117: *
118: * @see jdwp.SocketTransportImpl#readNextFromPrivateBuffer()
119: */
120: public int read() throws IOException {
121: return socketImpl.readNextFromPrivateBuffer();
122: }
123:
124: /**
125: * Initialtes handshake procedure. This procedure is performed
126: * immediately after establishing TCP/IP connection and consist on
127: * sending and receiving "JDWP-Handshake" string.
128: */
129: public void Handshake() throws IOException {
130: socketImpl.startHandShake(new String("JDWP-Handshake")
131: .getBytes().length);
132: super.Handshake();
133: }
134: }
|