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: /**
030: * This class encapsulates JDWP command. Its superclass
031: * is <code>Packet</code> so it's based on <code>ByteBuffer</code>.
032: * The class allows to set and get JDWP command number and automatically
033: * assign command ID. The typical use of <code>Command</code> class is
034: * as follows:
035: * <ul>
036: * <li> Create a new command object with specified command number
037: * <li> Fill command's data using standard <code>ByteBuffer</code>
038: * and <code>Packet</code> methods (for example, <code>addInt()</code> or
039: * <code>addReferenceTypeID()</code>)
040: * <li> Execute command using <code>BackEndTest.checkReplyF()</code>,
041: * <code>BackEndTest.sendCommand()</code> or
042: * <code>BackEndTest.checkReply()</code>
043: * <li> Work with received <code>Reply</code> object if needed
044: * </ul>
045: *
046: * @see jdwp.Packet
047: * @see jdwp.ByteBuffer
048: * @see jdwp.BackEndTest#checkReplyF(jdwp.Command)
049: * @see jdwp.BackEndTest#sendCommand(jdwp.Command)
050: * @see jdwp.BackEndTest#checkReply(jdwp.Command)
051: * @see jdwp.Reply
052: *
053: */
054: class Command extends Packet {
055:
056: /**
057: * ID of next command. Each JDWP command must have
058: * unique ID. The simplest way (that is used here) for generating
059: * these IDs is a incremental counter. So this variable is incremented
060: * after creating of each command and contains ID of next command.
061: */
062: private static int nextID = 1;
063:
064: /**
065: * Creates a new <code>Command</code> object, assign an unique ID,
066: * the specified command number and sets flags to <code>flNoFlags</code>.
067: * Command number is two-byte integer where hi-order byte specifies the
068: * JDWP command set and low-order byte specifies the command number in the
069: * command set. For example, ArrayReference/GetValues JDWP command has a
070: * number <code>0x0D02</code> where <code>0x0D = 14</code> is a number
071: * of ArrayReference command set and <code>0x02</code> is a number of
072: * GetValues command in this command set. For information about numbers
073: * of specific commands and command sets see JDWP specification.
074: *
075: * @param command a command number for this command
076: */
077: public Command(int command) {
078: super ();
079: setID(nextID++);
080: setFlags(flNoFlags);
081: setCommand(command);
082: }
083:
084: /**
085: * Gets number of the command assigned for this object. For information
086: * about command numbers see description of the constructor.
087: *
088: * I suspect this method is not used currently by KJDB
089: *
090: * @return a command number
091: */
092: public int getCommand() {
093: int id = 0;
094:
095: try {
096: id = (int) getID(CommandOffset, 2);
097: } catch (BoundException e) {
098: }
099: ;
100: return id;
101: }
102:
103: /**
104: * Assign a command number to the object. For information
105: * about command numbers see description of the constructor.
106: * This method is used internally by constructor.
107: *
108: * @param command a command number to be assigned
109: */
110: public void setCommand(int command) {
111: try {
112: putID(CommandOffset, command, 2);
113: } catch (BoundException e) {
114: }
115: ;
116: }
117:
118: /**
119: * Returns string representation of the object. This method is invoked
120: * when reply packet of the command is not received (usually it's a
121: * fatal error). It's useful for locating the problem.
122: *
123: * @return a string representation of the object
124: */
125: public String toString() {
126:
127: int l = 0;
128: try {
129: l = getInt(LengthOffset);
130: } catch (BoundException e) {
131: }
132: ;
133:
134: return "length " + Tools.Hex(l, 8) + "\n" + "id "
135: + Tools.Hex(getID(), 8) + "\n" + "flags "
136: + Tools.Hex(getFlags(), 2) + "\n" + "command "
137: + Tools.Hex(getCommand(), 4) + "\n"
138: + super .toString(PacketHeaderSize);
139: }
140:
141: /**
142: * Returns the ID of the command last created. I think that
143: * this method is not used currently by KJDB.
144: *
145: * @return ID of last command
146: */
147: public static int getLastID() {
148: return (nextID - 1);
149: }
150: }
|