blob: eb46af016be80039a764b7442675b6cc67967e33 (
plain)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
package phpdbg.ui;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import phpdbg.ui.JConsole.MessageType;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Manage input and output data
* @author krakjoe
*/
public class DebugSocket extends Socket implements Runnable {
private final Boolean reader;
private final JConsole main;
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;
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;
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();
wait();
command = main.getInputField().getText();
/* send command to stdin socket */
if (command != null) {
output.write(
command.getBytes());
output.write("\n".getBytes());
output.flush();
}
main.getInputField().setText(null);
} else {
InputStream input = getInputStream();
/* get data from stdout socket */
byte[] bytes = new byte[1];
do {
if (input.available() == 0) {
wait(400);
continue;
}
if (input.read(bytes, 0, 1) > -1) {
main.getOutputField()
.appendANSI(new String(bytes));
}
} while (!quit);
}
} while(!quit);
}
} catch (IOException | InterruptedException ex) {
if (!quit) {
main.messageBox(ex.getMessage(), MessageType.ERROR);
}
} finally {
try {
close();
} catch (IOException ex) { /* naughty me */ } finally {
synchronized(main) {
if (main.isConnected()) {
main.setConnected(false);
}
}
}
}
}
}
|