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.connect;
11:
12: import java.io.IOException;
13:
14: import com.sun.jdi.connect.spi.Connection;
15:
16: /**
17: * This class implements threads that receive/send packets from/to the Virtual Machine.
18: *
19: */
20: public abstract class PacketManager implements Runnable {
21: /** Connector that performs IO to Virtual Machine. */
22: private Connection fConnection;
23: /** Thread that handles the communication the other way (e.g. if we are sending, the receiving thread). */
24: private Thread fPartnerThread;
25: private IOException fDisconnectException;
26:
27: /**
28: * Creates new PacketManager.
29: */
30: protected PacketManager(Connection connection) {
31: fConnection = connection;
32: }
33:
34: public Connection getConnection() {
35: return fConnection;
36: }
37:
38: /**
39: * Used to indicate that an IO exception occurred, closes connection to Virtual Machine.
40: *
41: * @param disconnectException the IOException that occurred
42: */
43: public void disconnectVM(IOException disconnectException) {
44: fDisconnectException = disconnectException;
45: disconnectVM();
46: }
47:
48: /**
49: * Closes connection to Virtual Machine.
50: */
51: public void disconnectVM() {
52: try {
53: fConnection.close();
54: } catch (IOException e) {
55: fDisconnectException = e;
56: }
57: // Interrupt the sending thread if we are the receiving thread and vice versa.
58: if (fPartnerThread != null) {
59: fPartnerThread.interrupt();
60: }
61: }
62:
63: /**
64: * @return Returns whether an IO exception has occurred.
65: */
66: public boolean VMIsDisconnected() {
67: return fConnection == null || !fConnection.isOpen();
68: }
69:
70: /**
71: * Returns the IOException that caused this packet manager to disconnect or
72: * <code>null</code> if none.
73: */
74: public IOException getDisconnectException() {
75: return fDisconnectException;
76: }
77:
78: /**
79: * Assigns thread of partner, to be notified if we have an IO exception.
80: */
81: public void setPartnerThread(Thread thread) {
82: fPartnerThread = thread;
83: }
84: }
|