Skip to content

Commit

Permalink
added RoboterServer to better encapsulate methods/classes needed for …
Browse files Browse the repository at this point in the history
…communication with robots.
  • Loading branch information
pkohout committed Jan 29, 2023
1 parent d43490e commit cb1d82b
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {

group = 'io.github.robocup-logistics'
archivesBaseName = "java-sdk"
version = '0.1.11.6'
version = '0.1.12'

description = ""

Expand Down
74 changes: 74 additions & 0 deletions src/main/java/com/rcll/robot/HandleRobotMessageThread.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.rcll.robot;

import com.google.protobuf.GeneratedMessageV3;
import com.google.protobuf.InvalidProtocolBufferException;
import com.rcll.domain.Peer;
import com.rcll.protobuf_lib.RobotConnections;
import lombok.extern.apachecommons.CommonsLog;
import org.robocup_logistics.llsf_msgs.AgentTasksProtos;
import org.robocup_logistics.llsf_msgs.BeaconSignalProtos;

import java.net.Socket;
import java.util.AbstractMap;
import java.util.function.Consumer;

@CommonsLog
public class HandleRobotMessageThread extends Thread {
private final RobotConnections robotConnections;

private final Consumer<Integer> robotAddedHandler;
private final Consumer<BeaconSignalProtos.BeaconSignal> beaconMsgHandler;
private final Consumer<AgentTasksProtos.AgentTask> prsTaskMsgHandler;
private final Socket socket;

private AbstractMap.SimpleEntry<GeneratedMessageV3, byte[]> msg;

public HandleRobotMessageThread(Socket socket,
RobotConnections robotConnections,
Consumer<Integer> robotAddedHandler,
Consumer<BeaconSignalProtos.BeaconSignal> beaconMsgHandler,
Consumer<AgentTasksProtos.AgentTask> prsTaskMsgHandler,
AbstractMap.SimpleEntry<GeneratedMessageV3, byte[]> msg) {
this.socket = socket;
this.robotConnections = robotConnections;
this.robotAddedHandler = robotAddedHandler;
this.beaconMsgHandler = beaconMsgHandler;
this.prsTaskMsgHandler = prsTaskMsgHandler;
this.msg = msg;
}

@Override
public void run() {
handleMsg(this.msg);
}

protected void handleMsg(AbstractMap.SimpleEntry<GeneratedMessageV3, byte[]> msg) {
try {
if (msg.getKey() instanceof BeaconSignalProtos.BeaconSignal) {
BeaconSignalProtos.BeaconSignal beaconSignal = BeaconSignalProtos.BeaconSignal.parseFrom(msg.getValue());
updateRobotNetworkActivity(beaconSignal);
beaconMsgHandler.accept(beaconSignal);
} else if(msg.getKey() instanceof AgentTasksProtos.AgentTask) {
prsTaskMsgHandler.accept(AgentTasksProtos.AgentTask .parseFrom(msg.getValue()));
} else {
log.error("Unknown message in RobotHandler! " + msg.getKey().getClass().getSimpleName());
}
} catch (InvalidProtocolBufferException e) {
log.error("Can't parse msg in RobotHandler!", e);
}
}

private void updateRobotNetworkActivity(BeaconSignalProtos.BeaconSignal beaconSignal) {
if (!robotConnections.isRobotConnected(beaconSignal.getNumber())) {
Peer robot = new Peer();
robot.setId(beaconSignal.getNumber());
robot.setLastActive(System.currentTimeMillis());
robot.setConnection(socket);
robotConnections.addRobot(robot);
this.robotAddedHandler.accept(beaconSignal.getNumber());
} else {
robotConnections.getRobot(beaconSignal.getNumber()).setLastActive(System.currentTimeMillis());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
import java.util.AbstractMap;

public interface IRobotMessageThreadFactory {
Thread create(Socket _socket, AbstractMap.SimpleEntry<GeneratedMessageV3, byte[]> msg);
HandleRobotMessageThread create(Socket _socket, AbstractMap.SimpleEntry<GeneratedMessageV3, byte[]> msg);
}
25 changes: 25 additions & 0 deletions src/main/java/com/rcll/robot/RobotServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.rcll.robot;

import com.rcll.protobuf_lib.ProtobufServer;
import com.rcll.protobuf_lib.RobotConnections;

import java.io.IOException;
import java.util.function.Consumer;

public class RobotServer {
private final RobotConnections robotConnections;
private final ProtobufServer protobufServer;
private final RobotTaskCreator robotTaskCreator;
private final RobotClient robotClient;
public RobotServer(int listeningPort, int robotTimeoutInMs, IRobotMessageThreadFactory threadFactory,
Consumer<Integer> robotAddedHandler, RobotTaskCreator robotTaskCreator) {
this.robotConnections = new RobotConnections(robotTimeoutInMs);
this.robotTaskCreator = robotTaskCreator;
this.protobufServer = new ProtobufServer(listeningPort, robotConnections, threadFactory, robotAddedHandler);
this.robotClient = new RobotClient(this.robotTaskCreator, this.robotConnections);
}

public void start() throws IOException {
this.protobufServer.start();
}
}

0 comments on commit cb1d82b

Please sign in to comment.