-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added RoboterServer to better encapsulate methods/classes needed for …
…communication with robots.
- Loading branch information
Showing
4 changed files
with
101 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/main/java/com/rcll/robot/HandleRobotMessageThread.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |