FileDocCategorySizeDatePackage
Transport.javaAPI DocphoneME MR2 API (J2ME)8666Wed May 02 17:59:48 BST 2007com.sun.cldchi.tools.memoryprofiler.jdwp

Transport

public abstract class Transport extends Object
This abstract class represents a generic transport for JDWP. In KJDB it's used its subclass SocketTransport.
see
jdwp.SocketTransport

Fields Summary
private static ByteBuffer
buffer
A buffer that contains data received via JDWP. KJDB doesn't use this field.
private static final int
MaxPacketSize
A maximum allowed JDWP packet size.
private static final int
delta
A delay between attempts to get expected JDWP reply.
private static final int
handshakeTimeout
A timeout for handshake.
public Vector
Replies
A list of received JDWP repliesa and still not requested by other parts of the code.
Constructors Summary
Methods Summary
public voidHandshake()
Performs JDWP "handshake" procedure (undocumented ?). It sends a "JDWP-Handshake" string and waits for receiving the same string. After handshaking the JDWP connection is considered estblished.


		String hs = "JDWP-Handshake";
		byte[] hsb = hs.getBytes();

		write(hsb, 0, hsb.length);


		int handshakeTime = handshakeTimeout;
		do{
			Tools.wait(500);
			handshakeTime -= 500;
		}while(handshakeTime > 0 && available() < hsb.length);

		if (available() < hsb.length)
			throw new IOException("Target failed to handshake.");

        for (int i = 0; i < hsb.length; i++) {
            byte b = (byte) (read() & 0xFF);
            if (b != hsb[i])
                throw new IOException("Target failed to handshake.");
        }
    
public abstract intavailable()
Returns the number of bytes that can be received immediately.

return
the number of bytes that can be received immediately

public abstract intread()
Receives the next byte of data. The value byte is returned as an int in the range 0 to 255. If no byte is available, the value -1 is returned.

return
the next byte that is read via JDWP

public voidreceive()
Receives all available JDWP reply packets and places them into the vector Replies. This method is overwritten in KJDB and the code below is not used.

see
jdwp.SocketTransport
see
jdwp.SocketTransportImpl


        while (available() > 0)
			buffer.addByte(read());

		while (buffer.length() >= Packet.PacketHeaderSize) {

			int size = 0;

			buffer.resetParser();
			try {
				size = buffer.getInt();
			}
			catch (BoundException e) {}

	        if ((size < 0) || (size > MaxPacketSize))
    	    	throw new IOException("Wrong JDWP packet size: " + size + ".");

	        if (size > buffer.length())
				return;

			Reply r = new Reply();

			r.resetBuffer();
			r.addBytes(buffer.bytes, 0, size);

			buffer.deleteBytes(size);

			Replies.add(r);
		}
    
public ReplyreceiveEvent(int command, int Delay)
Tries to receive a JDWP event. It looks into a list of already received packets and tries to locate it. If the desired packet is not found, is makes a pause and then tries to receive the deired reply again. In case of timeout Reply.errNotAvailable is returned.

param
command a command number for Event/Composite JDWP command (should be always 0x4064)
param
Delay a timeout for this operation
return
a JDWP event or Reply.errNotAvailable if not found


    	Reply r;

        while (Delay > 0) {
	        receive();
	        for (int i = 0; i < Replies.size(); i++) {
	        	r = (Reply) Replies.elementAt(i);
	    	   	if (r.getFlags() == 0 && r.getErrorCode() == command)
					return (Reply) Replies.remove(i);
			}
			Tools.wait(delta);
			Delay = Delay - delta;
		}
		return Reply.Error(Reply.errNotAvailable);
    
public ReplyreceiveReply(int ReplyID)
Tries to receive a JDWP reply packet with specified ID. It looks into a list of already received packets and tries to locate it. If the desired packet is not found, Reply.errNotAvailable is returned.

param
ReplyID an ID of the desired JDWP reply packet
return
a JDWP reply packet with specified ID or Reply.errNotAvailable if not found

        
        receive();

        for (int i = 0; i < Replies.size(); i++)
       	if (((Reply) Replies.elementAt(i)).getFlags() == 0x80 &&
		((Reply) Replies.elementAt(i)).getID() == ReplyID)
			return (Reply) Replies.remove(i);

		return Reply.Error(Reply.errNotAvailable);
    
public ReplyreceiveReply(int ReplyID, int Delay)
Tries to receive a JDWP reply packet with specified ID. It looks into a list of already received packets and tries to locate it. If the desired packet is not found, is makes a pause and then tries to receive the deired reply again. In case of timeout Reply.errNotAvailable is returned.

param
ReplyID an ID of the desired JDWP reply packet
param
Delay a timeout for this operation
return
a JDWP reply packet with specified ID or Reply.errNotAvailable if not found

		Reply r = receiveReply(ReplyID);

		while ((r.getErrorCode() == Reply.errNotAvailable) && (Delay > 0)) {
			Tools.wait(delta);
			r = receiveReply(ReplyID);
			Delay = Delay - delta;
		}
		return r;
    
public ReplyreceiveReply()
Receives JDWP reply packet. If there is no reply packet available, function returns packet with error code Reply.errNotAvailable.

return
a JDWP reply or Reply.errNotAvailable if not available


        receive();

		if (Replies.size() == 0)
			return Reply.Error(Reply.errNotAvailable);

		return (Reply) Replies.remove(0);
    
public voidsendBadCommandPacket(Command c, int WrongSize)
Sends JDWP command packet with wrong length field. This method is never used by KJDB but it's used by CLDC-DI and JavaCard BackEnd test suite.

param
c a command to be sent
param
WrongSize a size of the packet


   	    try {
			c.putInt(Packet.LengthOffset, WrongSize);
        }
        catch (BoundException e) {};
		write(c.bytes, 0, c.length());
    
public voidsendCommand(Command c)
Sends JDWP command packet.

param
c a command to be sent

        c.setLength();
		write(c.bytes, 0, c.length());
    
public abstract voidwrite(int b)
Sends the specified byte via JDWP.

param
b a byte to be sent

public voidwrite(byte[] b, int off, int len)
Sends the specified bytes via JDWP.

param
b an array of bytes
param
off an offset of the first byte to be sent
param
len a number of bytes to be sent


                             
         

                                                
         

                      
          

                                         
              
        for (int i = 0; i < len; i++)
			write(b[off + i]);