-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathLCDSocketPoller.java
More file actions
101 lines (89 loc) · 2.98 KB
/
Copy pathLCDSocketPoller.java
File metadata and controls
101 lines (89 loc) · 2.98 KB
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
package org.lcdproc.lcdjava;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.concurrent.Semaphore;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Thread that listens for data on the LCD socket.
*/
class LCDSocketPoller extends Thread {
private final Logger _log = LoggerFactory.getLogger(LCDSocketPoller.class);
/**
* The Pattern that matches ignore/listen events.
*/
private static final Pattern IGNORE_STATUS = Pattern.compile(
"(ignore|listen)\\s+(\\d+).*");
/**
* The Pattern that matches menu events.
*/
private static final Pattern MENU_STATUS = Pattern.compile(
"menuevent\\s+(\\w+)\\s+(\\w+)\\s*(.*)");
/**
* The Reader to read data from.
*/
private final BufferedReader _in;
/**
* The last line of data received.
* <p>Will be null if no data was received.
*/
private String _lastLine;
/**
* The listener to notify of listen/ignore events.
*/
private final LCDListener _listener;
private Semaphore hello;
/**
* Public constructor.
*
* @param in the BufferedReader that will receive data from the server.
* @param listener the LCDListener that gets notified of screens being
* listened to or ignored.
*/
LCDSocketPoller(BufferedReader in, LCDListener listener, Semaphore hello) throws IOException {
_in = in;
_listener = listener;
this.hello = hello;
}
@Override
public void run() {
try {
String line;
while ((line = _in.readLine()) != null) {
if (line.startsWith(LCD.RESPONSE_ERROR)) {
_log.warn("Got a response of " + line +
" from server");
}
Matcher listenIgnore = IGNORE_STATUS.matcher(line);
Matcher menuEvent = MENU_STATUS.matcher(line);
if (listenIgnore.matches()) {
boolean listen = (LCD.RESPONSE_LISTEN.equals(listenIgnore.group(1)));
int screenId = Integer.parseInt(listenIgnore.group(2));
_listener.setListenStatus(screenId, listen);
} else if (menuEvent.matches()) {
_listener.menuAction(menuEvent.group(2), menuEvent.group(1), menuEvent.group(3));
}
synchronized (this) {
_lastLine = line;
}
hello.release();
}
} catch (IOException e) {
throw new LCDException(e);
}
_log.debug("Terminating");
}
/**
* Get the last line received <i>non-blocking</i>.
* <p>Calling this clears the last line received.
*
* @return the last line received.
*/
synchronized String getLastLine() {
String ret = _lastLine;
_lastLine = null;
return ret;
}
}