/plugins/Perl/trunk/perl/core/CommandManager.java

# · Java · 156 lines · 125 code · 8 blank · 23 comment · 9 complexity · 9f60013274344f201f27dd32c801933c MD5 · raw file

  1. /*
  2. Copyright (C) 2007 Shlomy Reinstein
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. */
  15. package perl.core;
  16. import perl.core.GdbState.StateListener;
  17. import perl.core.Parser.GdbResult;
  18. import perl.core.Parser.ResultHandler;
  19. import perl.proc.PerlProcess;
  20. import java.io.IOException;
  21. import java.util.Vector;
  22. public class CommandManager extends Thread {
  23. Vector<Command> commands = new Vector<Command>();
  24. PerlProcess process;
  25. Parser parser = null;
  26. private final Command StopCommand = new Command(null, "***Stop***", null);
  27. private Integer cid = new Integer(0);
  28. public class Command {
  29. String cmd;
  30. ResultHandler handler;
  31. Integer id;
  32. boolean done = false;
  33. public Command(Integer id, String cmd, ResultHandler handler) {
  34. this.id = id;
  35. this.cmd = cmd;
  36. this.handler = handler;
  37. }
  38. public void run() {
  39. ResultHandler wrapper = null;
  40. wrapper = new ResultHandler() {
  41. public void handle(String msg, GdbResult res) {
  42. if (handler != null)
  43. handler.handle(msg, res);
  44. synchronized(id) {
  45. done = true;
  46. id.notify();
  47. }
  48. }
  49. };
  50. parser.addResultHandler(wrapper);
  51. try {
  52. //System.err.println("CommandManager: " + cmd);
  53. Debugger.getInstance().commandRecord(cmd + "\n");
  54. process.getGdbInputWriter().write(cmd + "\n");
  55. } catch (IOException e) {
  56. // TODO Auto-generated catch block
  57. e.printStackTrace();
  58. }
  59. // Wait for result
  60. synchronized(id) {
  61. try {
  62. if (! done)
  63. id.wait();
  64. } catch (InterruptedException e) {
  65. // TODO Auto-generated catch block
  66. e.printStackTrace();
  67. }
  68. }
  69. parser.removeResultHandler(wrapper);
  70. }
  71. }
  72. public CommandManager(PerlProcess process, Parser parser) {
  73. this.process = process;
  74. this.parser = parser;
  75. }
  76. // Add a command to be executed next (before the other registered commands)
  77. public void addNow(String cmd, ResultHandler handler) {
  78. synchronized(cid) {
  79. cid = Integer.valueOf(cid.intValue() + 1);
  80. }
  81. Command c = new Command(cid, cmd, handler);
  82. addNow(c);
  83. }
  84. public void addImmediateExecution(String cmd) {
  85. Debugger.getInstance().commandRecord(cmd + "\n");
  86. try {
  87. process.getGdbInputWriter().write(cmd + "\n");
  88. } catch (IOException e) {
  89. e.printStackTrace();
  90. }
  91. }
  92. private void addNow(Command c) {
  93. synchronized(commands) {
  94. commands.add(0, c);
  95. commands.notify();
  96. }
  97. }
  98. public void addNow(String cmd) {
  99. addNow(cmd, null);
  100. }
  101. public void add(String cmd, ResultHandler handler) {
  102. synchronized(cid) {
  103. cid = Integer.valueOf(cid.intValue() + 1);
  104. }
  105. Command c = new Command(cid, cmd, handler);
  106. synchronized(commands) {
  107. commands.add(c);
  108. commands.notify();
  109. }
  110. }
  111. public void add(String cmd) {
  112. add(cmd, null);
  113. }
  114. public Command next() {
  115. Command cmd = null;
  116. synchronized(commands) {
  117. if (commands.isEmpty())
  118. try {
  119. commands.wait();
  120. } catch (InterruptedException e) {
  121. // TODO Auto-generated catch block
  122. e.printStackTrace();
  123. }
  124. cmd = commands.remove(0);
  125. }
  126. return cmd;
  127. }
  128. public void run() {
  129. GdbState.addStateListener(new CommandManagerStateListener());
  130. while (true) {
  131. Command cmd = next();
  132. if (cmd == StopCommand)
  133. break;
  134. cmd.run();
  135. }
  136. }
  137. private class CommandManagerStateListener implements StateListener {
  138. public void stateChanged(perl.core.GdbState.State prev,
  139. perl.core.GdbState.State current) {
  140. if (current != GdbState.State.IDLE)
  141. return;
  142. addNow(StopCommand);
  143. }
  144. }
  145. }