01: /*******************************************************************************
02: * Copyright (c) 2000, 2005 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdi.internal.event;
11:
12: import java.io.IOException;
13:
14: import org.eclipse.jdi.TimeoutException;
15: import org.eclipse.jdi.internal.MirrorImpl;
16: import org.eclipse.jdi.internal.VirtualMachineImpl;
17: import org.eclipse.jdi.internal.connect.PacketReceiveManager;
18: import org.eclipse.jdi.internal.jdwp.JdwpCommandPacket;
19: import org.eclipse.jdi.internal.request.RequestID;
20:
21: import com.sun.jdi.VMDisconnectedException;
22: import com.sun.jdi.event.EventQueue;
23: import com.sun.jdi.event.EventSet;
24:
25: /**
26: * this class implements the corresponding interfaces
27: * declared by the JDI specification. See the com.sun.jdi package
28: * for more information.
29: *
30: */
31: public class EventQueueImpl extends MirrorImpl implements EventQueue {
32: /** Flag used to see if a VMDisconnectEvent has already been generated. */
33: private boolean genereatedVMDisconnectEvent = false;
34:
35: /**
36: * Creates new EventQueueImpl.
37: */
38: public EventQueueImpl(VirtualMachineImpl vmImpl) {
39: super ("EventQueue", vmImpl); //$NON-NLS-1$
40: }
41:
42: /*
43: * @return Returns next EventSet from Virtual Machine.
44: */
45: public EventSet remove() throws InterruptedException {
46: return remove(PacketReceiveManager.TIMEOUT_INFINITE);
47: }
48:
49: /*
50: * @return Returns next EventSet from Virtual Machine, returns null if times out.
51: */
52: public EventSet remove(long timeout) throws InterruptedException {
53: // Return a received EventSet or null if no EventSet is received in time.
54: // Note that handledJdwpEventSet() is not don in a 'finally' clause because
55: // it must also be done when an 'empty' set is read (i.e. a set composed of internal
56: // events only).
57: try {
58: // We remove elements from event sets that are generated from inside, therefore the set may become empty.
59: EventSetImpl set;
60: do {
61: JdwpCommandPacket packet = getCommandVM(
62: JdwpCommandPacket.E_COMPOSITE, timeout);
63: initJdwpEventSet(packet);
64: set = EventSetImpl.read(this , packet.dataInStream());
65: handledJdwpEventSet();
66: } while (set.isEmpty());
67: return set;
68: } catch (TimeoutException e) {
69: // Timeout in getCommand, JDI spec says return null.
70: handledJdwpEventSet();
71: return null;
72: } catch (IOException e) {
73: // This means the already received data is invalid.
74: handledJdwpEventSet();
75: defaultIOExceptionHandler(e);
76: return null;
77: } catch (VMDisconnectedException e) {
78: // JDI spec says that a VMDisconnectedException must always be preceeded by a VMDisconnectEvent.
79: handledJdwpEventSet();
80: if (!genereatedVMDisconnectEvent) {
81: genereatedVMDisconnectEvent = true;
82: return new EventSetImpl(virtualMachineImpl(),
83: new VMDisconnectEventImpl(virtualMachineImpl(),
84: RequestID.nullID));
85: }
86: throw e;
87: }
88: }
89: }
|