forked from wpilibsuite/EclipsePlugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRioConsole.java
332 lines (294 loc) · 8.23 KB
/
RioConsole.java
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package netconsole2;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class RioConsole {
private ILogger logger;
/**
* The constructor.
*/
public RioConsole(ILogger logger) {
this.logger = logger;
rioConnector = new RioConnector(logger);
}
/**
* Read a tagged segment into a byte buffer.
* segmentData must have a capacity of at least 65535.
* Returns tag, or -1 on error.
*/
private static int readSegment(ByteBuffer segmentData, DataInputStream inputStream) throws IOException {
// read 2-byte length. Ignore zero length frames
int len;
do {
len = inputStream.readUnsignedShort();
} while (len == 0);
// read 1-byte tag
int tag = inputStream.readUnsignedByte();
//logger.log("got segment len=" + len + " tag=" + tag);
// subtract 1 for tag
len -= 1;
segmentData.clear();
segmentData.limit(len);
byte[] data = segmentData.array();
int bytesRead = 0;
while (bytesRead < len) {
int nRead = inputStream.read(data, bytesRead, len - bytesRead);
if (nRead < 0) {
return -1;
}
bytesRead += nRead;
}
//logger.log("finished reading segment");
return tag;
}
private Thread listener;
private Thread sender;
private final RioConnector rioConnector;
private final Lock lock = new ReentrantLock();
private final Condition wakeupListener = lock.newCondition();
private Socket socket = null;
private final BlockingQueue<Message> messageQueue = new LinkedBlockingQueue<>();
private boolean autoReconnect = true;
private final AtomicBoolean cleanup = new AtomicBoolean(false);
private final AtomicBoolean reconnect = new AtomicBoolean(false);
private final AtomicBoolean discard = new AtomicBoolean(false);
private final AtomicBoolean paused = new AtomicBoolean(false);
private final AtomicBoolean showWarning = new AtomicBoolean(true);
private final AtomicBoolean showPrint = new AtomicBoolean(true);
private Consumer<Boolean> connectedCallback = null;
@Override
protected void finalize() {
stop();
}
public void stop() {
cleanup.set(true);
closeSocket();
if (listener != null) {
listener.interrupt();
}
}
public void reconnect() {
reconnect.set(true);
closeSocket();
}
public boolean getAutoReconnect() {
return autoReconnect;
}
public void setAutoReconnect(boolean value) {
lock.lock();
try {
autoReconnect = value;
if (value) {
wakeupListener.signal();
}
} finally {
lock.unlock();
}
}
public boolean getPaused() {
return paused.get();
}
public boolean setPaused(boolean value) {
return paused.getAndSet(value);
}
public boolean getDiscard() {
return discard.get();
}
public boolean setDiscard(boolean value) {
return discard.getAndSet(value);
}
public boolean getShowWarning() {
return showWarning.get();
}
public boolean setShowWarning(boolean value) {
return showWarning.getAndSet(value);
}
public boolean getShowPrint() {
return showPrint.get();
}
public boolean setShowPrint(boolean value) {
return showPrint.getAndSet(value);
}
public void putDummyMessage() {
try {
messageQueue.put(new DummyMessage());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
private void setSocket(Socket socket) {
lock.lock();
this.socket = socket;
lock.unlock();
}
public void setConnectedCallback(Consumer<Boolean> callback) {
lock.lock();
this.connectedCallback = callback;
lock.unlock();
}
public BlockingQueue<Message> getMessageQueue() {
return messageQueue;
}
private static byte[] emptyFrame = new byte[] {0, 0};
private DataInputStream connect(int team) {
logger.log("connecting to team " + team);
Socket mySocket;
try {
mySocket = rioConnector.connect(team);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return null;
}
if (mySocket == null) {
return null;
}
setSocket(mySocket);
DataInputStream in;
OutputStream out;
try {
mySocket.setTcpNoDelay(true); // for keep alives
in = new DataInputStream(mySocket.getInputStream());
out = mySocket.getOutputStream();
} catch (IOException e) {
closeSocket();
return null;
}
lock.lock();
Consumer<Boolean> connCb = connectedCallback;
lock.unlock();
if (connCb != null) {
connCb.accept(Boolean.TRUE);
}
//logger.log("socket connected");
// kick off keep alive thread
if (sender == null) {
sender = new Thread(() -> {
while (!Thread.interrupted() && !cleanup.get()) {
try {
Thread.currentThread().sleep(2000);
out.write(emptyFrame);
out.flush();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
} catch (IOException e) {
logger.log("failed to send keep alive, reconnecting");
reconnect();
break;
}
}
sender = null;
}, "RioConsoleSender");
sender.setDaemon(true);
sender.start();
}
return in;
}
private void handleSegment(int tag, ByteBuffer data) {
if (discard.get()) {
return;
}
Message m;
if (tag == 11) { // Error or warning
m = new ErrorMessage(data);
if (m.getType() == Message.Type.kWarning && !showWarning.get()) {
return;
}
} else if (tag == 12 && showPrint.get()) { // Console
m = new PrintMessage(data);
} else {
return; // Ignore other tags
}
try {
messageQueue.put(m);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
public void startListening(Supplier<Integer> teamNumberSupplier) {
listener = new Thread(() -> {
ByteBuffer data = ByteBuffer.allocate(65536);
DataInputStream in = null;
while (!Thread.interrupted() && !cleanup.get()) {
if (in == null || reconnect.getAndSet(false)) {
lock.lock();
try {
while (!autoReconnect) {
wakeupListener.await();
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
} finally {
lock.unlock();
}
//logger.log("starting reconnect");
in = null;
Integer teamNumber = teamNumberSupplier.get();
if (teamNumber == null) {
try {
// wait a bit so we don't hammer the CPU
Thread.currentThread().sleep(5000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
continue;
}
int team = teamNumber.intValue();
in = connect(team);
if (in == null) {
continue;
}
}
if (cleanup.get()) {
break;
}
int tag = -1;
try {
tag = readSegment(data, in);
} catch (IOException e) {
logger.log("socket disconnected during read");
lock.lock();
Consumer<Boolean> connCb = connectedCallback;
lock.unlock();
if (connCb != null) {
connCb.accept(Boolean.FALSE);
}
setSocket(null);
in = null;
continue;
}
handleSegment(tag, data);
}
logger.log("exiting listener");
closeSocket();
}, "RioConsoleListener");
listener.setDaemon(true);
listener.start();
}
public void closeSocket() {
lock.lock();
Socket s = socket;
socket = null;
lock.unlock();
try {
if (s != null) {
s.close();
}
} catch (IOException e) {
// ignore
}
}
}