Skip to main content
summaryrefslogtreecommitdiffstats
blob: 15406bf66787b6fcb52f84339cf338bc831ec14f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * (c) Copyright QNX Software Systems Ltd. 2002.
 * All Rights Reserved.
 */

package org.eclipse.cdt.debug.mi.core;
 
import java.io.IOException;
import java.io.OutputStream;

import org.eclipse.cdt.debug.mi.core.command.CLICommand;
import org.eclipse.cdt.debug.mi.core.command.Command;

/**
 * Transmission command thread blocks on the command Queue
 * and wake cmd are available and push them to gdb out channel.
 */
public class TxThread extends Thread {

	MISession session;
	CLIProcessor cli;

	public TxThread(MISession s) {
		super("MI TX Thread"); //$NON-NLS-1$
		session = s;
		cli = new CLIProcessor(session);
	}

	public void run () {
		try {
			// signal by the session of time to die.
			OutputStream out;
			while ((out = session.getChannelOutputStream()) != null) {
				Command cmd = null;
				CommandQueue txQueue = session.getTxQueue();
				// removeCommand() will block until a command is available.
				try {
					cmd = txQueue.removeCommand();
				} catch (InterruptedException e) {
					//e.printStackTrace();
				}

				if (cmd != null) {
					String str = cmd.toString();
					// if string is empty consider as a noop
					if (str.length() > 0) {
						// Move to the RxQueue only if RxThread is alive.
						Thread rx = session.getRxThread();
						if (rx != null && rx.isAlive()) {
							CommandQueue rxQueue = session.getRxQueue();
							rxQueue.addCommand(cmd);
						} else {
							// The RxThread is not running
							synchronized (cmd) {
								cmd.notifyAll();
							}
						}
					
						// Process the Command line to recognise patterns we may need to fire event.
						if (cmd instanceof CLICommand) {
							cli.process((CLICommand)cmd);
						}
				
						// shove in the pipe
						if (out != null) {
							out.write(str.getBytes());
							out.flush();
						}
					} else {
						// String is empty consider as a noop
						synchronized (cmd) {
							cmd.notifyAll();
						}
					}
				}
			}
		} catch (IOException e) {
			//e.printStackTrace();
		}

		// Clear the queue and notify any command waiting, we are going down.
		CommandQueue txQueue = session.getTxQueue();
		if (txQueue != null) {
			Command[] cmds = txQueue.clearCommands();
			for (int i = 0; i < cmds.length; i++) {
				synchronized (cmds[i]) {
					cmds[i].notifyAll();
				}
			}
		}
	}

}

Back to the top