-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathLCDSocketPoller.java
More file actions
144 lines (129 loc) · 4.12 KB
/
Copy pathLCDSocketPoller.java
File metadata and controls
144 lines (129 loc) · 4.12 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
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
package org.boncey.lcdjava;
import java.io.BufferedReader;
import java.io.IOException;
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.
* <p>Copyright (c) 2004-2005 Darren Greaves.
* @author Darren Greaves
* @version $Id: LCDSocketPoller.java,v 1.3 2008-07-06 15:38:34 boncey Exp $
*/
public class LCDSocketPoller extends Thread
{
/**
* Version details.
*/
public static final String CVSID =
"$Id: LCDSocketPoller.java,v 1.3 2008-07-06 15:38:34 boncey Exp $";
private static Logger _log = LoggerFactory.getLogger(LCDSocketPoller.class);
/**
* How often to poll for changes.
*/
private static final int POLL = 100;
/**
* 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 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 LCDListener _listener;
/**
* Public constructor.
* @param in the BufferedReader that will recieve data from the server.
* @param listener the LCDListener that gets notified of screens being
* listened to or ignored.
*/
public LCDSocketPoller(BufferedReader in, LCDListener listener)
{
_in = in;
_listener = listener;
}
/**
* Poll the socket every 100 milliseconds.
*/
@Override
public void run()
{
while (!interrupted())
{
try {
if (!_in.ready()) {
Thread.sleep(POLL);
}
if (_in.ready())
{
_lastLine = _in.readLine();
if (_lastLine == null)
continue;
Matcher listenIgnore = IGNORE_STATUS.matcher(_lastLine);
Matcher menuEvent = MENU_STATUS.matcher(_lastLine);
if (_lastLine.startsWith(LCD.RESPONSE_ERROR))
{
_log.warn("Got a response of " + _lastLine +
" from server");
}
else if (_listener != null)
{
if (listenIgnore.matches())
{
boolean listen = (LCD.RESPONSE_LISTEN.equals(
listenIgnore.group(1)));
try
{
int screenId = Integer.parseInt(listenIgnore.group(2));
_listener.setListenStatus(screenId, listen);
}
catch (NumberFormatException e)
{
// Ignore
}
}
else if (menuEvent.matches())
{
_listener.menuAction(menuEvent.group(2), menuEvent.group(1), menuEvent.group(3));
}
}
}
}
catch (InterruptedException e)
{
interrupt();
}
catch (IOException e)
{
_log.error("Caught IOException", 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;
}
}