-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobotClientHandler.java
More file actions
41 lines (36 loc) · 1.06 KB
/
RobotClientHandler.java
File metadata and controls
41 lines (36 loc) · 1.06 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
package com.fellowrobots;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import org.apache.catalina.websocket.MessageInbound;
public class RobotClientHandler extends MessageInbound {
@Override
protected void onBinaryMessage(ByteBuffer arg0) throws IOException {
// not implemented
}
@Override
protected void onTextMessage(CharBuffer text) throws IOException {
Message message = new Message(text.toString());
RoboBase robo_base = RoboBase.getInstance();
String status;
switch (message.getCommand()) {
case LOGIN:
status = robo_base.isOnline(message.getRobotName()) ? "ok" : "error: offline";
break;
case TURN_RIGHT:
Robot robot = robo_base.getRobot(message.getRobotName());
if (robot != null) {
robot.turnRight();
status = "ok";
} else {
status = "error: offline";
}
break;
default:
status = "error: unsupported command";
break;
}
getWsOutbound().writeTextMessage(
CharBuffer.wrap("<message><status>" + status + "</status></message>"));
}
}