Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / libjava / classpath / gnu / classpath / jdwp / processor / PacketProcessor.java
blob9e281f217f2976896c4ebd10fb02dd1a487d4ca3
1 /* PacketProcessor.java -- a thread which processes command packets
2 from the debugger
3 Copyright (C) 2005 Free Software Foundation
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301 USA.
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
25 combination.
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 terms of your choice, provided that you also meet, for each linked
33 independent module, the terms and conditions of the license of that
34 module. An independent module is a module which is not derived from
35 or based on this library. If you modify this library, you may extend
36 this exception to your version of the library, but you are not
37 obligated to do so. If you do not wish to do so, delete this
38 exception statement from your version. */
41 package gnu.classpath.jdwp.processor;
43 import gnu.classpath.jdwp.Jdwp;
44 import gnu.classpath.jdwp.JdwpConstants;
45 import gnu.classpath.jdwp.exception.JdwpException;
46 import gnu.classpath.jdwp.transport.JdwpCommandPacket;
47 import gnu.classpath.jdwp.transport.JdwpConnection;
48 import gnu.classpath.jdwp.transport.JdwpPacket;
49 import gnu.classpath.jdwp.transport.JdwpReplyPacket;
51 import java.io.ByteArrayOutputStream;
52 import java.io.DataOutputStream;
53 import java.io.IOException;
54 import java.nio.ByteBuffer;
55 import java.security.PrivilegedAction;
57 /**
58 * This class is responsible for processing packets from the
59 * debugger. It waits for an available packet from the connection
60 * ({@link gnu.classpath.jdwp.transport.JdwpConnection}) and then
61 * processes the packet and any reply.
63 * @author Keith Seitz (keiths@redhat.com)
65 public class PacketProcessor
66 implements PrivilegedAction
68 // The connection to the debugger
69 private JdwpConnection _connection;
71 // Shutdown this thread?
72 private boolean _shutdown;
74 // A Mapping of the command set (Byte) to the specific CommandSet
75 private CommandSet[] _sets;
77 // Contents of the ReplyPackets data field
78 private ByteArrayOutputStream _outputBytes;
80 // Output stream around _outputBytes
81 private DataOutputStream _os;
83 /**
84 * Constructs a new <code>PacketProcessor</code> object
85 * Connection must be validated before getting here!
87 * @param con the connection
89 public PacketProcessor (JdwpConnection con)
91 _connection = con;
92 _shutdown = false;
94 // MAXIMUM is the value of the largest command set we may receive
95 _sets = new CommandSet[JdwpConstants.CommandSet.MAXIMUM + 1];
96 _outputBytes = new ByteArrayOutputStream();
97 _os = new DataOutputStream (_outputBytes);
99 // Create all the Command Sets and add them to our array
100 _sets[JdwpConstants.CommandSet.VirtualMachine.CS_VALUE] =
101 new VirtualMachineCommandSet();
102 _sets[JdwpConstants.CommandSet.ReferenceType.CS_VALUE] =
103 new ReferenceTypeCommandSet();
104 _sets[JdwpConstants.CommandSet.ClassType.CS_VALUE] =
105 new ClassTypeCommandSet();
106 _sets[JdwpConstants.CommandSet.ArrayType.CS_VALUE] =
107 new ArrayTypeCommandSet();
108 _sets[JdwpConstants.CommandSet.InterfaceType.CS_VALUE] =
109 new InterfaceTypeCommandSet();
110 _sets[JdwpConstants.CommandSet.Method.CS_VALUE] =
111 new MethodCommandSet();
112 _sets[JdwpConstants.CommandSet.Field.CS_VALUE] =
113 new FieldCommandSet();
114 _sets[JdwpConstants.CommandSet.ObjectReference.CS_VALUE] =
115 new ObjectReferenceCommandSet();
116 _sets[JdwpConstants.CommandSet.StringReference.CS_VALUE] =
117 new StringReferenceCommandSet();
118 _sets[JdwpConstants.CommandSet.ThreadReference.CS_VALUE] =
119 new ThreadReferenceCommandSet();
120 _sets[JdwpConstants.CommandSet.ThreadGroupReference.CS_VALUE] =
121 new ThreadGroupReferenceCommandSet();
122 _sets[JdwpConstants.CommandSet.ArrayReference.CS_VALUE] =
123 new ArrayReferenceCommandSet();
124 _sets[JdwpConstants.CommandSet.ClassLoaderReference.CS_VALUE] =
125 new ClassLoaderReferenceCommandSet();
126 _sets[JdwpConstants.CommandSet.EventRequest.CS_VALUE] =
127 new EventRequestCommandSet();
128 _sets[JdwpConstants.CommandSet.StackFrame.CS_VALUE] =
129 new StackFrameCommandSet();
130 _sets[JdwpConstants.CommandSet.ClassObjectReference.CS_VALUE] =
131 new ClassObjectReferenceCommandSet();
135 * Main run routine for this thread. Will loop getting packets
136 * from the connection and processing them.
138 public Object run ()
142 while (!_shutdown)
144 _processOnePacket ();
147 catch (IOException ex)
149 ex.printStackTrace();
151 // Time to shutdown, tell Jdwp to shutdown
152 Jdwp.getDefault().shutdown();
153 return null;
157 * Shutdown the packet processor
159 public void shutdown ()
161 _shutdown = true;
164 // Helper function which actually does all the work of waiting
165 // for a packet and getting it processed.
166 private void _processOnePacket ()
167 throws IOException
169 JdwpPacket pkt = _connection.getPacket ();
171 if (!(pkt instanceof JdwpCommandPacket))
173 // We're not supposed to get these from the debugger!
174 // Drop it on the floor
175 return;
178 if (pkt != null)
180 JdwpCommandPacket commandPkt = (JdwpCommandPacket) pkt;
181 JdwpReplyPacket reply = new JdwpReplyPacket(commandPkt);
183 // Reset our output stream
184 _outputBytes.reset();
186 // Create a ByteBuffer around the command packet
187 ByteBuffer bb = ByteBuffer.wrap(commandPkt.getData());
188 byte command = commandPkt.getCommand();
189 byte commandSet = commandPkt.getCommandSet();
191 CommandSet set = null;
194 // There is no command set with value 0
195 if (commandSet > 0 && commandSet < _sets.length)
197 set = _sets[commandPkt.getCommandSet()];
199 if (set != null)
201 _shutdown = set.runCommand(bb, _os, command);
202 reply.setData(_outputBytes.toByteArray());
204 else
206 // This command set wasn't in our tree
207 reply.setErrorCode(JdwpConstants.Error.NOT_IMPLEMENTED);
210 catch (JdwpException ex)
212 reply.setErrorCode(ex.getErrorCode ());
214 _connection.sendPacket (reply);