Skip to content

Commit cb1d82b

Browse files
author
Peter Kohout
committed
added RoboterServer to better encapsulate methods/classes needed for communication with robots.
1 parent d43490e commit cb1d82b

File tree

4 files changed

+101
-2
lines changed

4 files changed

+101
-2
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ plugins {
88

99
group = 'io.github.robocup-logistics'
1010
archivesBaseName = "java-sdk"
11-
version = '0.1.11.6'
11+
version = '0.1.12'
1212

1313
description = ""
1414

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.rcll.robot;
2+
3+
import com.google.protobuf.GeneratedMessageV3;
4+
import com.google.protobuf.InvalidProtocolBufferException;
5+
import com.rcll.domain.Peer;
6+
import com.rcll.protobuf_lib.RobotConnections;
7+
import lombok.extern.apachecommons.CommonsLog;
8+
import org.robocup_logistics.llsf_msgs.AgentTasksProtos;
9+
import org.robocup_logistics.llsf_msgs.BeaconSignalProtos;
10+
11+
import java.net.Socket;
12+
import java.util.AbstractMap;
13+
import java.util.function.Consumer;
14+
15+
@CommonsLog
16+
public class HandleRobotMessageThread extends Thread {
17+
private final RobotConnections robotConnections;
18+
19+
private final Consumer<Integer> robotAddedHandler;
20+
private final Consumer<BeaconSignalProtos.BeaconSignal> beaconMsgHandler;
21+
private final Consumer<AgentTasksProtos.AgentTask> prsTaskMsgHandler;
22+
private final Socket socket;
23+
24+
private AbstractMap.SimpleEntry<GeneratedMessageV3, byte[]> msg;
25+
26+
public HandleRobotMessageThread(Socket socket,
27+
RobotConnections robotConnections,
28+
Consumer<Integer> robotAddedHandler,
29+
Consumer<BeaconSignalProtos.BeaconSignal> beaconMsgHandler,
30+
Consumer<AgentTasksProtos.AgentTask> prsTaskMsgHandler,
31+
AbstractMap.SimpleEntry<GeneratedMessageV3, byte[]> msg) {
32+
this.socket = socket;
33+
this.robotConnections = robotConnections;
34+
this.robotAddedHandler = robotAddedHandler;
35+
this.beaconMsgHandler = beaconMsgHandler;
36+
this.prsTaskMsgHandler = prsTaskMsgHandler;
37+
this.msg = msg;
38+
}
39+
40+
@Override
41+
public void run() {
42+
handleMsg(this.msg);
43+
}
44+
45+
protected void handleMsg(AbstractMap.SimpleEntry<GeneratedMessageV3, byte[]> msg) {
46+
try {
47+
if (msg.getKey() instanceof BeaconSignalProtos.BeaconSignal) {
48+
BeaconSignalProtos.BeaconSignal beaconSignal = BeaconSignalProtos.BeaconSignal.parseFrom(msg.getValue());
49+
updateRobotNetworkActivity(beaconSignal);
50+
beaconMsgHandler.accept(beaconSignal);
51+
} else if(msg.getKey() instanceof AgentTasksProtos.AgentTask) {
52+
prsTaskMsgHandler.accept(AgentTasksProtos.AgentTask .parseFrom(msg.getValue()));
53+
} else {
54+
log.error("Unknown message in RobotHandler! " + msg.getKey().getClass().getSimpleName());
55+
}
56+
} catch (InvalidProtocolBufferException e) {
57+
log.error("Can't parse msg in RobotHandler!", e);
58+
}
59+
}
60+
61+
private void updateRobotNetworkActivity(BeaconSignalProtos.BeaconSignal beaconSignal) {
62+
if (!robotConnections.isRobotConnected(beaconSignal.getNumber())) {
63+
Peer robot = new Peer();
64+
robot.setId(beaconSignal.getNumber());
65+
robot.setLastActive(System.currentTimeMillis());
66+
robot.setConnection(socket);
67+
robotConnections.addRobot(robot);
68+
this.robotAddedHandler.accept(beaconSignal.getNumber());
69+
} else {
70+
robotConnections.getRobot(beaconSignal.getNumber()).setLastActive(System.currentTimeMillis());
71+
}
72+
}
73+
74+
}

src/main/java/com/rcll/robot/IRobotMessageThreadFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
import java.util.AbstractMap;
77

88
public interface IRobotMessageThreadFactory {
9-
Thread create(Socket _socket, AbstractMap.SimpleEntry<GeneratedMessageV3, byte[]> msg);
9+
HandleRobotMessageThread create(Socket _socket, AbstractMap.SimpleEntry<GeneratedMessageV3, byte[]> msg);
1010
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.rcll.robot;
2+
3+
import com.rcll.protobuf_lib.ProtobufServer;
4+
import com.rcll.protobuf_lib.RobotConnections;
5+
6+
import java.io.IOException;
7+
import java.util.function.Consumer;
8+
9+
public class RobotServer {
10+
private final RobotConnections robotConnections;
11+
private final ProtobufServer protobufServer;
12+
private final RobotTaskCreator robotTaskCreator;
13+
private final RobotClient robotClient;
14+
public RobotServer(int listeningPort, int robotTimeoutInMs, IRobotMessageThreadFactory threadFactory,
15+
Consumer<Integer> robotAddedHandler, RobotTaskCreator robotTaskCreator) {
16+
this.robotConnections = new RobotConnections(robotTimeoutInMs);
17+
this.robotTaskCreator = robotTaskCreator;
18+
this.protobufServer = new ProtobufServer(listeningPort, robotConnections, threadFactory, robotAddedHandler);
19+
this.robotClient = new RobotClient(this.robotTaskCreator, this.robotConnections);
20+
}
21+
22+
public void start() throws IOException {
23+
this.protobufServer.start();
24+
}
25+
}

0 commit comments

Comments
 (0)