-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRRNetIn.java
More file actions
285 lines (260 loc) · 6.42 KB
/
RRNetIn.java
File metadata and controls
285 lines (260 loc) · 6.42 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
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
/*
* Copyright © 2003, 2011 Bart Massey
* [This program is licensed under the "MIT License"]
* Please see the file COPYING in the source
* distribution of this software for license terms.
*/
/*
* Ricochet Robots Server Monitor Connection
*/
/**
* @author Bart Massey <bart@cs.pdx.edu>
*
*/
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
class Lexer {
BufferedReader in;
public boolean saw_eol = false;
public boolean saw_eof = false;
public Lexer(BufferedReader in) {
this.in = in;
}
public String next()
throws IOException {
boolean in_token = false;
boolean string_token = false;
saw_eol = false;
StringBuffer b = new StringBuffer();
while (true) {
int ch = in.read();
if (string_token && ch == '"')
break;
if (ch == -1) {
saw_eof = true;
return null;
}
if (string_token) {
b.append((char)ch);
continue;
}
if (ch == '\n') {
saw_eol = true;
if (in_token)
break;
return null;
}
if (ch == ' ' || ch == '\t') {
if (in_token)
break;
continue;
}
if (!in_token && ch == '"') {
in_token = true;
string_token = true;
continue;
}
in_token = true;
b.append((char)ch);
}
return b.toString();
}
}
public class RRNetIn
extends Thread {
Lexer in;
RRController ctl;
public RRNetIn(Socket s, RRController ctl)
throws IOException {
this.ctl = ctl;
InputStream si = s.getInputStream();
InputStreamReader sr = new InputStreamReader(si);
BufferedReader sb = new BufferedReader(sr);
in = new Lexer(sb);
}
interface NoticeHandler {
public boolean match(String[] notice) throws IOException;
}
class ShowHandler implements NoticeHandler {
public boolean match(String[] notice)
throws IOException {
if (notice.length != 2 || !notice[0].equals("SHOW"))
return false;
ctl.board.become(notice[1]);
ctl.boardPanel.repaint();
return true;
}
}
class TurnHandler implements NoticeHandler {
public boolean match(String[] notice) {
if (notice.length != 4 ||
!notice[0].equals("NOTICE") ||
!notice[1].equals("TURN"))
return false;
ctl.netout.show();
return true;
}
}
class ActiveHandler implements NoticeHandler {
public boolean match(String[] notice) {
if (notice.length != 4 ||
!notice[0].equals("NOTICE") ||
!notice[1].equals("ACTIVE"))
return false;
if (!notice[2].equals(ctl.board.playerName)) {
ctl.board.setActive(false);
return true;
}
ctl.board.setActive(true);
return true;
}
}
class DoneHandler implements NoticeHandler {
public boolean match(String[] notice) {
if (notice.length != 3 ||
!notice[0].equals("NOTICE") ||
!notice[1].equals("GAMESTATE") ||
!notice[2].equals("DONE"))
return false;
ctl.board.setActive(false);
return true;
}
}
class ResetHandler implements NoticeHandler {
public boolean match(String[] notice) {
if (notice.length != 2 ||
!notice[0].equals("NOTICE") ||
!notice[1].equals("RESET"))
return false;
return true;
}
}
class MessageHandler implements NoticeHandler {
public boolean match(String[] notice) {
if (notice.length != 4 ||
!notice[0].equals("NOTICE") ||
!notice[1].equals("MESSAGE"))
return false;
ctl.messages.message (notice[2], notice[3]);
return true;
}
}
static int lookup_color(String name) {
for (int i = 0; i < RRSquare.colorname.length; i++)
if (RRSquare.colorname[i].equals(name))
return i;
return -1;
}
class PositionHandler implements NoticeHandler {
public boolean match(String[] notice) {
if (notice.length != 5 ||
!notice[0].equals("NOTICE") ||
!notice[1].equals("POSITION"))
return false;
RRBoardCoord coord =
new RRBoardCoord(Integer.parseInt(notice[4]),
Integer.parseInt(notice[3]));
ctl.board.moveRobot(lookup_color(notice[2]), coord);
ctl.boardPanel.repaint();
return true;
}
}
class TimerHandler implements NoticeHandler {
public boolean match(String[] notice) {
if (notice.length != 3 ||
!notice[0].equals("NOTICE") ||
!notice[1].equals("TIMER"))
return false;
ctl.messages.message (notice[2]);
ctl.messages.message (" seconds remaining\n");
return true;
}
}
class UserHandler implements NoticeHandler {
public boolean match(String[] notice) {
if (notice.length != 3 ||
!notice[0].equals("NOTICE") ||
!notice[1].equals("USER"))
return false;
ctl.messages.message (notice[2]);
ctl.messages.message (" has connected to the server.\n");
return true;
}
}
class JoinHandler implements NoticeHandler {
public boolean match(String[] notice) {
if (notice.length != 3 ||
!notice[0].equals("NOTICE") ||
!notice[1].equals("JOIN"))
return false;
ctl.messages.message (notice[2]);
ctl.messages.message (" has joined the game.\n");
return true;
}
}
class PartHandler implements NoticeHandler {
public boolean match(String[] notice) {
if (notice.length != 3 ||
!notice[0].equals("NOTICE") ||
!notice[1].equals("PART"))
return false;
ctl.messages.message (notice[2]);
ctl.messages.message (" has left the game.\n");
return true;
}
}
class DefaultHandler implements NoticeHandler {
public boolean match(String[] notice) {
if (notice.length > 0)
ctl.messages.message (notice[0]);
for (int i = 1; i < notice.length; i++) {
ctl.messages.message (" ");
ctl.messages.message (notice[i]);
}
ctl.messages.message ("\n");
return true;
}
}
NoticeHandler handlers[] = {
new ShowHandler(),
new TurnHandler(),
new PositionHandler(),
new ActiveHandler(),
new DoneHandler(),
new ResetHandler(),
new MessageHandler(),
new TimerHandler(),
new UserHandler (),
new JoinHandler(),
new PartHandler(),
new DefaultHandler()
};
public void run() {
try {
while(true) {
Vector v = new Vector();
while (true) {
String s = in.next();
if (s != null)
v.add(s);
if (in.saw_eol || in.saw_eof)
break;
}
String[] notice = new String[v.size()];
for (int i = 0; i < notice.length; i++)
notice[i] = (String)v.elementAt(i);
for (int i = 0; i < handlers.length; i++)
if (handlers[i].match(notice))
break;
if (in.saw_eof)
return;
}
} catch (IOException e) {
System.err.println("I/O Exception in RRNetIn");
e.printStackTrace(System.err);
}
}
}