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();
}
}
}
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 */
/* 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;
}
* @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
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);
// 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;
}
} catch (NumberFormatException ex) {
messageBox("Invalid stdin port provided !", MessageType.WARN);
}
-
return 0;
}
} catch (NumberFormatException ex) {
messageBox("Invalid stdout port provided !", MessageType.WARN);
}
-
return 0;
}
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);
}
});
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()); }
+ }
}