blob: 8e811e9be945f0c085f82bef49dd8228c908e50f (
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
package phpdbg.ui;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import static java.nio.channels.SelectionKey.OP_READ;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.channels.spi.SelectorProvider;
import java.util.Iterator;
import java.util.Set;
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 implements Runnable {
private final String host;
private final Integer port;
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 {
this.host = host;
this.port = 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) {
quit = false;
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);
}
}
if (reader) {
/* The reader thread will wait() until there is input */
Socket socket = new Socket(this.host, this.port);
String command;
OutputStream output = socket.getOutputStream();
do {
synchronized(this) {
wait();
if (!quit) {
command = main.getInputField().getText();
/* send command to stdin socket */
if (command != null) {
if (main.isEchoing()) {
main.getOutputField()
.appendANSI(
String.format("remote> %s\n", command));
}
output.write(
command.getBytes());
output.write("\n".getBytes());
output.flush();
}
main.getInputField().setText(null);
}
}
} while (!quit);
socket.close();
} else {
/*
* The writer thread will use non-blocking i/o consuming
* resources only when there is data to read
*/
Selector selector = Selector.open();
SocketChannel channel = SocketChannel.open();
channel.connect(
new InetSocketAddress(this.host, this.port));
channel.configureBlocking(false);
channel.register(selector, OP_READ);
while (!quit) {
selector.select();
Iterator<SelectionKey> iter = selector.selectedKeys().iterator();
while (iter.hasNext()) {
if (!quit) {
SocketChannel ready = (SocketChannel) (iter.next().channel());
ByteBuffer bytes = ByteBuffer.allocate(128);
if (ready != null) {
if (ready.read(bytes) != -1) {
bytes.flip();
main.getOutputField()
.appendANSI(new String(bytes.array()));
}
}
iter.remove();
}
}
}
channel.close();
}
} catch (IOException | InterruptedException ex) {
if (!quit) {
main.messageBox(ex.getMessage(), MessageType.ERROR);
}
} finally {
synchronized(main) {
if (main.isConnected()) {
main.setConnected(false);
}
}
}
}
}
|