1:
39:
40:
41: package ;
42:
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50:
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56:
57:
65: public class PacketProcessor
66: implements PrivilegedAction
67: {
68:
69: private JdwpConnection _connection;
70:
71:
72: private boolean _shutdown;
73:
74:
75: private CommandSet[] _sets;
76:
77:
78: private ByteArrayOutputStream _outputBytes;
79:
80:
81: private DataOutputStream _os;
82:
83:
89: public PacketProcessor (JdwpConnection con)
90: {
91: _connection = con;
92: _shutdown = false;
93:
94:
95: _sets = new CommandSet[JdwpConstants.CommandSet.MAXIMUM + 1];
96: _outputBytes = new ByteArrayOutputStream();
97: _os = new DataOutputStream (_outputBytes);
98:
99:
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();
132: }
133:
134:
138: public Object run ()
139: {
140: try
141: {
142: while (!_shutdown)
143: {
144: _processOnePacket ();
145: }
146: }
147: catch (IOException ex)
148: {
149: ex.printStackTrace();
150: }
151:
152: Jdwp.getDefault().shutdown();
153: return null;
154: }
155:
156:
159: public void shutdown ()
160: {
161: _shutdown = true;
162: }
163:
164:
165:
166: private void _processOnePacket ()
167: throws IOException
168: {
169: JdwpPacket pkt = _connection.getPacket ();
170:
171: if (!(pkt instanceof JdwpCommandPacket))
172: {
173:
174:
175: return;
176: }
177:
178: if (pkt != null)
179: {
180: JdwpCommandPacket commandPkt = (JdwpCommandPacket) pkt;
181: JdwpReplyPacket reply = new JdwpReplyPacket(commandPkt);
182:
183:
184: _outputBytes.reset();
185:
186:
187: ByteBuffer bb = ByteBuffer.wrap(commandPkt.getData());
188: byte command = commandPkt.getCommand();
189: byte commandSet = commandPkt.getCommandSet();
190:
191: CommandSet set = null;
192: try
193: {
194:
195: if (commandSet > 0 && commandSet < _sets.length)
196: {
197: set = _sets[commandPkt.getCommandSet()];
198: }
199: if (set != null)
200: {
201: _shutdown = set.runCommand(bb, _os, command);
202: reply.setData(_outputBytes.toByteArray());
203: }
204: else
205: {
206:
207: reply.setErrorCode(JdwpConstants.Error.NOT_IMPLEMENTED);
208: }
209: }
210: catch (JdwpException ex)
211: {
212: reply.setErrorCode(ex.getErrorCode ());
213: }
214: _connection.sendPacket (reply);
215: }
216: }
217: }