From e2cae76b26520467cf5a0b9e5dd60e391c54b7dd Mon Sep 17 00:00:00 2001 From: Alain Magloire Date: Wed, 31 Jul 2002 20:26:44 +0000 Subject: [PATCH] more work on mi. --- .../eclipse/cdt/debug/mi/core/MISession.java | 30 ++++++++ .../org/eclipse/cdt/debug/mi/core/Queue.java | 74 +++++++++++++++++++ .../eclipse/cdt/debug/mi/core/RxThread.java | 71 ++++++++++++++++++ .../eclipse/cdt/debug/mi/core/TxThread.java | 57 ++++++++++++++ .../cdt/debug/mi/core/command/CLICommand.java | 19 ++++- .../cdt/debug/mi/core/command/Command.java | 8 +- .../cdt/debug/mi/core/command/MICommand.java | 15 ++-- .../cdt/debug/mi/core/output/MIOOBRecord.java | 9 +-- .../cdt/debug/mi/core/output/MIOutput.java | 21 +++--- .../debug/mi/core/output/MIResultRecord.java | 18 ++--- 10 files changed, 283 insertions(+), 39 deletions(-) create mode 100644 debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/Queue.java create mode 100644 debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/RxThread.java create mode 100644 debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/TxThread.java diff --git a/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/MISession.java b/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/MISession.java index 3ea49688371..eab4da77d51 100644 --- a/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/MISession.java +++ b/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/MISession.java @@ -13,6 +13,13 @@ public class MISession { InputStream in; OutputStream out; + + Thread txThread; + Thread rxThread; + + Queue txQueue; + Queue rxQueue; + Reader consoleStream = null; Reader targetStream = null; Reader logStream = null; @@ -23,6 +30,12 @@ public class MISession { MISession(InputStream i, OutputStream o) { in = i; out = o; + txQueue = new Queue(); + rxQueue = new Queue(); + txThread = new TxThread(this); + rxThread = new RxThread(this); + txThread.start(); + rxThread.start(); } /** @@ -46,6 +59,23 @@ public class MISession { logStream = log; } + + Queue getTxQueue() { + return txQueue; + } + + Queue getRxQueue() { + return rxQueue; + } + + InputStream getInputStream() { + return in; + } + + OutputStream getOutputStream() { + return out; + } + MIOutput parse(String buffer) { return null; } diff --git a/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/Queue.java b/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/Queue.java new file mode 100644 index 00000000000..7848a4d49b4 --- /dev/null +++ b/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/Queue.java @@ -0,0 +1,74 @@ +package org.eclipse.cdt.debug.mi.core; + +/* + * (c) Copyright QNX Software Systems Ltd. 2002. + * All Rights Reserved. + */ + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +import org.eclipse.cdt.debug.mi.core.command.Command; + +public class Queue { + + private List list; + + public Queue() { + list = Collections.synchronizedList(new LinkedList()); + } + + public Command removeCommand(String id) { + //print("in removeCommand(" + id + ") - entering"); + synchronized (list) { + int size = list.size(); + for (int i = 0; i < size; i++) { + Command cmd = (Command)list.get(i); + String token = cmd.getToken(); + if (token.equals(id)) { + list.remove(cmd); + return cmd; + } + } + } + return null; + } + + public Command removeCommand() throws InterruptedException { + //print("in removeCommand() - entering"); + synchronized (list) { + while (list.isEmpty()) { + //print("in removeCommand() - about to wait()"); + list.wait(); + //print("in removeCommand() - done with wait()"); + } + + // extract the new first cmd + Command cmd = (Command)list.remove(0); + + //print("in removeCommand() - leaving"); + return cmd; + } + } + public void addCommand(Command cmd) { + //print("in addCommand() - entering"); + synchronized (list) { + // There will always be room to add to this List + // because it expands as needed. + list.add(cmd); + //print("in addCommand - just added: '" + cmd + "'"); + + // After adding, notify any and all waiting + // threads that the list has changed. + list.notifyAll(); + //print("in addCommand() - just notified"); + } + //print("in addCommand() - leaving"); + } + + private static void print(String msg) { + String name = Thread.currentThread().getName(); + System.out.println(name + ": " + msg); + } +} diff --git a/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/RxThread.java b/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/RxThread.java new file mode 100644 index 00000000000..c3e006563be --- /dev/null +++ b/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/RxThread.java @@ -0,0 +1,71 @@ +package org.eclipse.cdt.debug.mi.core; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +import org.eclipse.cdt.debug.mi.core.command.Command; +import org.eclipse.cdt.debug.mi.core.output.MIOOBRecord; +import org.eclipse.cdt.debug.mi.core.output.MIOutput; +import org.eclipse.cdt.debug.mi.core.output.MIResultRecord; + +/* + * (c) Copyright QNX Software Systems Ltd. 2002. + * All Rights Reserved. + */ + + + +public class RxThread extends Thread { + + MISession session; + + public RxThread(MISession s) { + super("MI RX Thread"); + session = s; + setDaemon(true); + } + + /* + * Get the response, parse the output, dispatch for OOB + * search for the corresponding token in rxQueue. + */ + public void run () { + BufferedReader reader = new BufferedReader(new InputStreamReader(session.getInputStream())); + StringBuffer buffer = new StringBuffer(); + try { + while (true) { + String line; + while ((line = reader.readLine()) != null) { + if (line.equals("(gdb)")) { + processMIOutput(buffer.toString()); + buffer = new StringBuffer(); + } + buffer.append(line).append('\n'); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + void processMIOutput(String buffer) { + MIOutput response = session.parse(buffer); + if (response != null) { + String id = response.getToken(); + Queue rxQueue = session.getRxQueue(); + Command cmd = rxQueue.removeCommand(id); + if (cmd != null) { + cmd.setMIOutput(response); + cmd.notifyAll(); + } + MIOOBRecord[] oobs = response.getMIOOBRecords(); + if (oobs != null && oobs.length > 0) { + processMIOOBRecords(oobs); + } + } + } + + void processMIOOBRecords(MIOOBRecord[] oobs) { + } +} diff --git a/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/TxThread.java b/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/TxThread.java new file mode 100644 index 00000000000..ad559961e17 --- /dev/null +++ b/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/TxThread.java @@ -0,0 +1,57 @@ +package org.eclipse.cdt.debug.mi.core; + +/* + * (c) Copyright QNX Software Systems Ltd. 2002. + * All Rights Reserved. + */ + +import java.io.IOException; +import java.io.OutputStream; + +import org.eclipse.cdt.debug.mi.core.command.Command; + +public class TxThread extends Thread { + + MISession session; + int token; + + public TxThread(MISession s) { + super("MI TX Thread"); + session = s; + token = 1; + setDaemon(true); + } + + public void run () { + while (true) { + Command cmd = null; + Queue txQueue = session.getTxQueue(); + // The removeCommand will block until a command is available. + try { + cmd = txQueue.removeCommand(); + } catch (Exception e) { + //e.printStackTrace(); + } + + // The command is then: + // - given a Id/token + // - shove in the pipe + // - Remove from the TxQueue + // - Move to the RxQueue + if (cmd != null) { + OutputStream out = session.getOutputStream(); + cmd.setToken(Integer.toString(token)); + //System.out.println("Tx " + cmd.toString()); + try { + out.write(cmd.toString().getBytes()); + out.flush(); + } catch (IOException e) { + //e.printStackTrace(); + } + Queue rxQueue = session.getRxQueue(); + rxQueue.addCommand(cmd); + token++; + } + } + } +} diff --git a/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/command/CLICommand.java b/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/command/CLICommand.java index 8dba4b980c0..e1a2a4e7716 100644 --- a/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/command/CLICommand.java +++ b/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/command/CLICommand.java @@ -7,7 +7,7 @@ package org.eclipse.cdt.debug.mi.core.command; import org.eclipse.cdt.debug.mi.core.MIInfo; -import org.eclipse.cdt.debug.mi.core.output.MIResultRecord; +import org.eclipse.cdt.debug.mi.core.output.MIOutput; /** * @@ -18,20 +18,31 @@ import org.eclipse.cdt.debug.mi.core.output.MIResultRecord; */ public class CLICommand extends Command { + String token = ""; + MIOutput miOutput = null; + /** * Returns the text representation of this command. * * @return the text representation of this command */ public String getToken() { - return ""; + return token; + } + + public void setToken(String t) { + token = t; } public String toString(){ return ""; } - public MIInfo getInfo (MIResultRecord rr) { - return new MIInfo(rr); + public void setMIOutput(MIOutput mi) { + miOutput = mi; + } + + public MIInfo getInfo () { + return null; } } diff --git a/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/command/Command.java b/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/command/Command.java index 687b4b7a5e4..4e18aebd283 100644 --- a/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/command/Command.java +++ b/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/command/Command.java @@ -7,7 +7,7 @@ package org.eclipse.cdt.debug.mi.core.command; import org.eclipse.cdt.debug.mi.core.MIInfo; -import org.eclipse.cdt.debug.mi.core.output.MIResultRecord; +import org.eclipse.cdt.debug.mi.core.output.MIOutput; /** * @@ -24,8 +24,12 @@ public abstract class Command * @return the identifier of this request */ public abstract String getToken(); + + public abstract void setToken(String token); public abstract String toString(); - public abstract MIInfo getInfo(MIResultRecord rr); + public abstract void setMIOutput(MIOutput mi); + + public abstract MIInfo getInfo(); } diff --git a/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/command/MICommand.java b/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/command/MICommand.java index 0eb68ee25df..f45b8434362 100644 --- a/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/command/MICommand.java +++ b/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/command/MICommand.java @@ -7,7 +7,7 @@ package org.eclipse.cdt.debug.mi.core.command; import org.eclipse.cdt.debug.mi.core.MIInfo; -import org.eclipse.cdt.debug.mi.core.output.MIResultRecord; +import org.eclipse.cdt.debug.mi.core.output.MIOutput; /** * @@ -22,7 +22,8 @@ public class MICommand extends Command String[] options = empty; String[] parameters = empty; String operation = ""; - String token; + String token = ""; + MIOutput miOutput = null; public MICommand(String oper) { this.operation = oper; @@ -91,7 +92,7 @@ public class MICommand extends Command command += " " + parameters[i]; } } - return command; + return command + "\n"; } public String getToken() { @@ -102,7 +103,11 @@ public class MICommand extends Command token = t; } - public MIInfo getInfo (MIResultRecord rr) { - return new MIInfo(rr); + public void setMIOutput(MIOutput mi) { + miOutput = mi; + } + + public MIInfo getInfo () { + return null; } } diff --git a/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/output/MIOOBRecord.java b/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/output/MIOOBRecord.java index 723c552bfd9..1bd94e68d10 100644 --- a/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/output/MIOOBRecord.java +++ b/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/output/MIOOBRecord.java @@ -1,14 +1,9 @@ package org.eclipse.cdt.debug.mi.core.output; /** - * @author alain * - * To change this generated comment edit the template variable "typecomment": - * Window>Preferences>Java>Templates. - * To enable and disable the creation of type comments go to - * Window>Preferences>Java>Code Generation. */ -public class MIOOBRecord extends MIOutput { +public class MIOOBRecord { public final int ASYNC_STOPPED = 0; @@ -23,7 +18,7 @@ public class MIOOBRecord extends MIOutput { return ASYNC_STOPPED; } - public MIResult getResult() { + public MIResult[] getResults() { return null; } } diff --git a/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/output/MIOutput.java b/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/output/MIOutput.java index 07635ff5aa5..82c168e9db4 100644 --- a/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/output/MIOutput.java +++ b/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/output/MIOutput.java @@ -1,22 +1,21 @@ package org.eclipse.cdt.debug.mi.core.output; /** - * @author alain - * - * To change this generated comment edit the template variable "typecomment": - * Window>Preferences>Java>Templates. - * To enable and disable the creation of type comments go to - * Window>Preferences>Java>Code Generation. */ public class MIOutput { public static final String terminator = "(gdb)\n"; - - public boolean interpret() { - return false; + String token = ""; + + public String getToken() { + return token; } - public String getToken() { - return ""; + public MIResultRecord getMIResultRecord() { + return null; + } + + public MIOOBRecord[] getMIOOBRecords() { + return null; } } diff --git a/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/output/MIResultRecord.java b/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/output/MIResultRecord.java index beda0f7008c..dc6cfac6b68 100644 --- a/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/output/MIResultRecord.java +++ b/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/output/MIResultRecord.java @@ -1,20 +1,18 @@ package org.eclipse.cdt.debug.mi.core.output; /** - * @author alain - * - * To change this generated comment edit the template variable "typecomment": - * Window>Preferences>Java>Templates. - * To enable and disable the creation of type comments go to - * Window>Preferences>Java>Code Generation. */ -public class MIResultRecord extends MIOutput { +public class MIResultRecord { + + String resultClass = ""; /** - * @see org.eclipse.cdt.debug.mi.core.MIOutput#interpret() */ - public boolean interpret() { - return false; + public String getResultClass() { + return resultClass; } + public MIResult[] getResults() { + return null; + } }