1 11 package org.eclipse.jdi.internal.connect; 12 13 import java.io.IOException ; 14 import java.io.InterruptedIOException ; 15 import java.util.ArrayList ; 16 import java.util.LinkedList ; 17 import java.util.ListIterator ; 18 19 import org.eclipse.jdi.TimeoutException; 20 import org.eclipse.jdi.internal.VirtualMachineImpl; 21 import org.eclipse.jdi.internal.jdwp.JdwpCommandPacket; 22 import org.eclipse.jdi.internal.jdwp.JdwpPacket; 23 import org.eclipse.jdi.internal.jdwp.JdwpReplyPacket; 24 25 import com.ibm.icu.text.MessageFormat; 26 import com.sun.jdi.VMDisconnectedException; 27 import com.sun.jdi.connect.spi.Connection; 28 29 34 public class PacketReceiveManager extends PacketManager { 35 36 37 public static final int TIMEOUT_NOT_BLOCKING = 0; 38 39 40 public static final int TIMEOUT_INFINITE = -1; 41 42 43 private LinkedList fCommandPackets; 44 45 46 private LinkedList fReplyPackets; 47 48 50 private ArrayList fTimedOutPackets; 51 52 private VirtualMachineImpl fVM; 53 54 57 public PacketReceiveManager(Connection connection, VirtualMachineImpl vmImpl) { 58 super(connection); 59 fVM = vmImpl; 60 fCommandPackets = new LinkedList (); 61 fReplyPackets = new LinkedList (); 62 fTimedOutPackets = new ArrayList (); 63 } 64 65 public void disconnectVM() { 66 super.disconnectVM(); 67 synchronized (fCommandPackets) { 68 fCommandPackets.notifyAll(); 69 } 70 synchronized (fReplyPackets) { 71 fReplyPackets.notifyAll(); 72 } 73 } 74 75 78 public void run() { 79 try { 80 while (!VMIsDisconnected()) { 81 readAvailablePacket(); 83 } } catch (InterruptedIOException e) {disconnectVM(e);} 87 catch (IOException e) {disconnectVM(e);} 88 } 89 90 93 public JdwpCommandPacket getCommand(int command, long timeToWait) throws InterruptedException { 94 JdwpCommandPacket packet = null; 95 synchronized (fCommandPackets) { 96 long remainingTime = timeToWait; 97 long timeBeforeWait; 98 long waitedTime; 99 100 while (!VMIsDisconnected() && (packet = removeCommandPacket(command)) == null && (timeToWait < 0 || remainingTime > 0)) { 102 timeBeforeWait = System.currentTimeMillis(); 103 waitForPacketAvailable(remainingTime, fCommandPackets); 104 waitedTime = System.currentTimeMillis() - timeBeforeWait; 105 remainingTime -= waitedTime; 106 } } if (VMIsDisconnected()) { 110 String message; 111 if (getDisconnectException() == null) { 112 message = ConnectMessages.PacketReceiveManager_Got_IOException_from_Virtual_Machine_1; 113 } else { 115 String exMessage = getDisconnectException().getMessage(); 116 if (exMessage == null) { 117 message = MessageFormat.format(ConnectMessages.PacketReceiveManager_Got__0__from_Virtual_Machine_1, new String [] { getDisconnectException().getClass().getName() }); 118 } else { 120 message = MessageFormat.format(ConnectMessages.PacketReceiveManager_Got__0__from_Virtual_Machine___1__1, new String [] { getDisconnectException().getClass().getName(), exMessage }); 121 } } throw new VMDisconnectedException(message); 124 } 125 if (packet == null) { 127 throw new TimeoutException(); 128 } return packet; 130 } 131 132 135 public JdwpReplyPacket getReply(int id, long timeToWait) { 136 JdwpReplyPacket packet = null; 137 long remainingTime = timeToWait; 138 synchronized (fReplyPackets) { 139 final long timeBeforeWait = System.currentTimeMillis(); 140 while (!VMIsDisconnected() && remainingTime > 0) { 142 packet = removeReplyPacket(id); 143 if (packet != null) { 144 break; 145 } try { 147 waitForPacketAvailable(remainingTime, fReplyPackets); 148 } catch (InterruptedException e) { 152 break; 153 } 154 long waitedTime = System.currentTimeMillis() - timeBeforeWait; 155 remainingTime = timeToWait - waitedTime; 156 } } if (packet == null) { 159 synchronized (fReplyPackets) { 160 packet = removeReplyPacket(id); 161 } } if (VMIsDisconnected()) 165 throw new VMDisconnectedException(ConnectMessages.PacketReceiveManager_Got_IOException_from_Virtual_Machine_2); 166 if (packet == null) { 168 synchronized (fTimedOutPackets) { 169 fTimedOutPackets.add(new Integer (id)); 170 } throw new TimeoutException(MessageFormat.format(ConnectMessages.PacketReceiveManager_0, new String [] {id+""})); } return packet; 174 } 175 176 179 public JdwpReplyPacket getReply(JdwpCommandPacket commandPacket) { 180 return getReply(commandPacket.getId(), fVM.getRequestTimeout()); 181 } 182 183 186 private void waitForPacketAvailable(long timeToWait, Object lock) throws InterruptedException { 187 if (timeToWait == 0) 188 return; 189 else if (timeToWait < 0) 190 lock.wait(); 191 else 192 lock.wait(timeToWait); 193 } 194 195 199 private JdwpCommandPacket removeCommandPacket(int command) { 200 ListIterator iter = fCommandPackets.listIterator(); 201 while (iter.hasNext()) { 202 JdwpCommandPacket packet = (JdwpCommandPacket) iter.next(); 203 if (packet.getCommand() == command) { 204 iter.remove(); 205 return packet; 206 } 207 } 208 return null; 209 } 210 211 214 private JdwpReplyPacket removeReplyPacket(int id) { 215 ListIterator iter = fReplyPackets.listIterator(); 216 while (iter.hasNext()) { 217 JdwpReplyPacket packet = (JdwpReplyPacket) iter.next(); 218 if (packet.getId() == id) { 219 iter.remove(); 220 return packet; 221 } 222 } 223 return null; 224 } 225 226 229 private void addCommandPacket(JdwpCommandPacket packet) { 230 if (isTimedOut(packet)) { 231 return; } 233 synchronized (fCommandPackets) { 234 fCommandPackets.add(packet); 235 fCommandPackets.notifyAll(); 236 } 237 } 238 239 245 private boolean isTimedOut(JdwpPacket packet) { 246 synchronized (fTimedOutPackets) { 247 if (fTimedOutPackets.isEmpty()) { 248 return false; 249 } 250 Integer id = new Integer (packet.getId()); 251 return fTimedOutPackets.remove(id); 252 } 253 } 254 255 258 private void addReplyPacket(JdwpReplyPacket packet) { 259 if (isTimedOut(packet)) { 260 return; } 262 synchronized (fReplyPackets) { 263 fReplyPackets.add(packet); 264 fReplyPackets.notifyAll(); 265 } 266 } 267 268 272 private void readAvailablePacket() throws IOException { 273 byte[] bytes = getConnection().readPacket(); 275 JdwpPacket packet = JdwpPacket.build(bytes); 276 if (packet instanceof JdwpCommandPacket) 278 addCommandPacket((JdwpCommandPacket) packet); 279 else 280 addReplyPacket((JdwpReplyPacket) packet); 281 } 282 } 283 | Popular Tags |