From 9b498765abd28e23f707a1a63dedb78527aab11e Mon Sep 17 00:00:00 2001 From: krakjoe Date: Fri, 29 Nov 2013 10:22:02 +0000 Subject: [PATCH] ... --- tutorials/java/src/phpdbg/ui/DebugSocket.java | 32 ++++-- tutorials/java/src/phpdbg/ui/JConsole.java | 106 +++++++++--------- 2 files changed, 76 insertions(+), 62 deletions(-) diff --git a/tutorials/java/src/phpdbg/ui/DebugSocket.java b/tutorials/java/src/phpdbg/ui/DebugSocket.java index 0dfa10763d..eb46af016b 100644 --- a/tutorials/java/src/phpdbg/ui/DebugSocket.java +++ b/tutorials/java/src/phpdbg/ui/DebugSocket.java @@ -20,18 +20,26 @@ import phpdbg.ui.JConsole.MessageType; public class DebugSocket extends Socket implements Runnable { private final Boolean reader; private final JConsole main; - private Boolean quit; + private final Thread thread; + private volatile Boolean quit; + private volatile Boolean started; + public DebugSocket(final String host, final Integer port, final JConsole main, Boolean reader) throws IOException { super(host, port); this.main = main; this.reader = reader; this.quit = false; - - synchronized(main) { - if (!main.isConnected()) { - main.setConnected(true); + this.started = false; + this.thread = new Thread(this); + } + + public void start() { + synchronized(this) { + if (!started) { + started = true; + thread.start(); } } } @@ -39,19 +47,26 @@ public class DebugSocket extends Socket implements Runnable { public void quit() { synchronized(this) { quit = true; - this.notifyAll(); + started = false; + notifyAll(); } } @Override public void run() { try { + synchronized(main) { + if (!main.isConnected()) { + main.setConnected(true); + } + } + synchronized(this) { do { if (reader) { String command; OutputStream output = getOutputStream(); - this.wait(); + wait(); command = main.getInputField().getText(); /* send command to stdin socket */ @@ -67,9 +82,8 @@ public class DebugSocket extends Socket implements Runnable { /* get data from stdout socket */ byte[] bytes = new byte[1]; do { - /* this is some of the laziest programming I ever done */ if (input.available() == 0) { - this.wait(400); + wait(400); continue; } diff --git a/tutorials/java/src/phpdbg/ui/JConsole.java b/tutorials/java/src/phpdbg/ui/JConsole.java index 08661bc1f0..2958cb90ee 100644 --- a/tutorials/java/src/phpdbg/ui/JConsole.java +++ b/tutorials/java/src/phpdbg/ui/JConsole.java @@ -19,20 +19,6 @@ import javax.swing.JTextField; * @author krakjoe */ public class JConsole extends javax.swing.JDialog { - public enum MessageType { - INFO (JOptionPane.INFORMATION_MESSAGE), - WARN (JOptionPane.WARNING_MESSAGE), - ERROR (JOptionPane.ERROR_MESSAGE); - - private final Integer type; - private MessageType(Integer type) { - this.type = type; - } - public Integer getType() { return this.type; } - public Boolean equals(Integer other) { return this.type.equals(other); } - public Boolean equals(MessageType other) { return this.type.equals(other.getType()); } - } - /** * Creates user interface * @param parent @@ -197,38 +183,9 @@ public class JConsole extends javax.swing.JDialog { private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openButtonActionPerformed try { if (!connected) { - Integer ports[] = new Integer[2]; - String address = getHost(); - - if (address != null) { - ports[0] = stdinCheckBox.isSelected() ? getStdinPort() : -1; - ports[1] = stdoutCheckBox.isSelected() ? getStdoutPort() : -1; - - if (ports[0] != 0 && ports[1] != 0) { - if (stdinCheckBox.isSelected()) { - if (ports[0] > 0) { - in = new DebugSocket( - address, ports[0], this, true); - new Thread(in).start(); - } - } - - if (stdoutCheckBox.isSelected()) { - if (ports[1] > 0) { - out = new DebugSocket( - address, ports[1], this, false); - new Thread(out).start(); - } - } - } - } + connect(); } else { - if (in != null) { - in.quit(); - } - if (out != null) { - out.quit(); - } + disconnect(); } } catch (IOException ex) { messageBox(ex.getMessage(), MessageType.ERROR); @@ -239,7 +196,44 @@ public class JConsole extends javax.swing.JDialog { // TODO add your handling code here: output.setText(null); }//GEN-LAST:event_resetStdoutActionPerformed + + private void disconnect() { + if (in != null) { + in.quit(); + } + if (out != null) { + out.quit(); + } + } + + private void connect() throws IOException { + Integer ports[] = new Integer[2]; + String address = getHost(); + if (address != null) { + ports[0] = stdinCheckBox.isSelected() ? getStdinPort() : -1; + ports[1] = stdoutCheckBox.isSelected() ? getStdoutPort() : -1; + + if (ports[0] != 0 && ports[1] != 0) { + if (stdinCheckBox.isSelected()) { + if (ports[0] > 0) { + in = new DebugSocket( + address, ports[0], this, true); + in.start(); + } + } + + if (stdoutCheckBox.isSelected()) { + if (ports[1] > 0) { + out = new DebugSocket( + address, ports[1], this, false); + out.start(); + } + } + } + } + } + public Boolean isConnected() { return connected; } @@ -290,7 +284,6 @@ public class JConsole extends javax.swing.JDialog { } catch (NumberFormatException ex) { messageBox("Invalid stdin port provided !", MessageType.WARN); } - return 0; } @@ -300,7 +293,6 @@ public class JConsole extends javax.swing.JDialog { } catch (NumberFormatException ex) { messageBox("Invalid stdout port provided !", MessageType.WARN); } - return 0; } @@ -345,12 +337,7 @@ public class JConsole extends javax.swing.JDialog { dialog.addWindowListener(new java.awt.event.WindowAdapter() { @Override public void windowClosing(java.awt.event.WindowEvent e) { - if (in != null) - in.quit(); - - if (out != null) - out.quit(); - + dialog.disconnect(); System.exit(0); } }); @@ -380,4 +367,17 @@ public class JConsole extends javax.swing.JDialog { private javax.swing.JPopupMenu stdoutPopupMenu; private javax.swing.JTextField stdoutPort; // End of variables declaration//GEN-END:variables + public enum MessageType { + INFO (JOptionPane.INFORMATION_MESSAGE), + WARN (JOptionPane.WARNING_MESSAGE), + ERROR (JOptionPane.ERROR_MESSAGE); + + private final Integer type; + private MessageType(Integer type) { + this.type = type; + } + public Integer getType() { return this.type; } + public Boolean equals(Integer other) { return this.type.equals(other); } + public Boolean equals(MessageType other) { return this.type.equals(other.getType()); } + } } -- 2.50.1