KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdi > internal > connect > PacketSendManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdi.internal.connect;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InterruptedIOException JavaDoc;
15 import com.ibm.icu.text.MessageFormat;
16 import java.util.LinkedList JavaDoc;
17
18 import org.eclipse.jdi.internal.jdwp.JdwpPacket;
19
20 import com.sun.jdi.VMDisconnectedException;
21 import com.sun.jdi.connect.spi.Connection;
22
23 /**
24  * This class implements a thread that sends available packets to the Virtual Machine.
25  *
26  */

27 public class PacketSendManager extends PacketManager {
28     /** List of packets to be sent to Virtual Machine */
29     private LinkedList JavaDoc fOutgoingPackets;
30
31     /**
32      * Create a new thread that send packets to the Virtual Machine.
33      */

34     public PacketSendManager(Connection connection) {
35         super(connection);
36         fOutgoingPackets = new LinkedList JavaDoc();
37     }
38
39     
40     public void disconnectVM() {
41         super.disconnectVM();
42         synchronized (fOutgoingPackets) {
43             fOutgoingPackets.notifyAll();
44         }
45     }
46     
47     /**
48      * Thread's run method.
49      */

50     public void run() {
51         while (!VMIsDisconnected()) {
52             try {
53                 sendAvailablePackets();
54             }//end try
55
//in each case if the remote VM fails, or has been interrupted, disconnect and force a clean up, don't wait for it to happen
56
catch (InterruptedException JavaDoc e) {disconnectVM();}
57             catch (InterruptedIOException JavaDoc e) {disconnectVM(e);}
58             catch (IOException JavaDoc e) {disconnectVM(e);}
59         }
60     }
61     
62     /**
63      * Add a packet to be sent to the Virtual Machine.
64      */

65     public void sendPacket(JdwpPacket packet) {
66         if (VMIsDisconnected()) {
67             String JavaDoc message;
68             if (getDisconnectException() == null) {
69                 message= ConnectMessages.PacketSendManager_Got_IOException_from_Virtual_Machine_1;
70             }//end if
71
else {
72                 String JavaDoc exMessage = getDisconnectException().getMessage();
73                 if (exMessage == null) {
74                     message= MessageFormat.format(ConnectMessages.PacketSendManager_Got__0__from_Virtual_Machine_1, new String JavaDoc[] {getDisconnectException().getClass().getName()});
75                 }//end if
76
else {
77                     message= MessageFormat.format(ConnectMessages.PacketSendManager_Got__0__from_Virtual_Machine___1__1, new String JavaDoc[] {getDisconnectException().getClass().getName(), exMessage});
78                 }//end else
79
}//end else
80
throw new VMDisconnectedException(message);
81         }
82         
83         synchronized(fOutgoingPackets) {
84         // Add packet to list of packets to send.
85
fOutgoingPackets.add(packet);
86         // Notify PacketSendThread that data is available.
87
fOutgoingPackets.notifyAll();
88         }
89     }
90     
91     /**
92      * Send available packets to the Virtual Machine.
93      */

94     private void sendAvailablePackets() throws InterruptedException JavaDoc, IOException JavaDoc {
95         LinkedList JavaDoc packetsToSend = new LinkedList JavaDoc();
96         synchronized (fOutgoingPackets) {
97             while (fOutgoingPackets.size() == 0) {
98                 fOutgoingPackets.wait();
99             }//end while
100
packetsToSend.addAll(fOutgoingPackets);
101             fOutgoingPackets.clear();
102         }//end sync
103

104         // Put available packets on Output Stream.
105
while (packetsToSend.size() > 0) {
106             // Note that only JdwpPackets are added to the list, so a ClassCastException can't occur.
107
JdwpPacket packet = (JdwpPacket)packetsToSend.removeFirst();
108             byte[] bytes = packet.getPacketAsBytes();
109             getConnection().writePacket(bytes);
110         }
111     }
112 }
113
Popular Tags