|
| 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 | +} |
0 commit comments