-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobotServerHandler.java
More file actions
49 lines (43 loc) · 1.2 KB
/
RobotServerHandler.java
File metadata and controls
49 lines (43 loc) · 1.2 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
package com.fellowrobots;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import org.apache.catalina.websocket.MessageInbound;
public class RobotServerHandler 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:
if (robo_base.login(message.getRobotName(), this)) {
status = "ok";
} else {
status = "error: duplicate login";
}
break;
case LOGOUT:
robo_base.logout(message.getRobotName());
status = "ok";
break;
default:
status = "error: unsupported command";
break;
}
getWsOutbound().writeTextMessage(
CharBuffer.wrap("<message><status>" + status + "</status></message>"));
}
public void sendCommand(String command) {
try {
getWsOutbound().writeTextMessage(
CharBuffer.wrap("<message><command>" + command + "</command></message>"));
} catch (IOException e) {
e.printStackTrace();
}
}
}