19 #include "async_grpc/rpc_handler.h" 21 #include "cartographer/cloud/proto/map_builder_service.pb.h" 30 std::unique_ptr<proto::ReceiveGlobalSlamOptimizationsResponse> GenerateResponse(
31 const std::map<int, mapping::SubmapId> &last_optimized_submap_ids,
32 const std::map<int, mapping::NodeId> &last_optimized_node_ids) {
34 common::make_unique<proto::ReceiveGlobalSlamOptimizationsResponse>();
35 for (
const auto &entry : last_optimized_submap_ids) {
37 &(*response->mutable_last_optimized_submap_ids())[entry.first]);
39 for (
const auto &entry : last_optimized_node_ids) {
41 &(*response->mutable_last_optimized_node_ids())[entry.first]);
48 void ReceiveGlobalSlamOptimizationsHandler::OnRequest(
49 const google::protobuf::Empty &request) {
50 auto writer = GetWriter();
51 const int subscription_index =
52 GetUnsynchronizedContext<MapBuilderContextInterface>()
53 ->SubscribeGlobalSlamOptimizations(
54 [writer](
const std::map<int, mapping::SubmapId>
55 &last_optimized_submap_ids,
56 const std::map<int, mapping::NodeId>
57 &last_optimized_node_ids) {
58 if (!writer.Write(GenerateResponse(last_optimized_submap_ids,
59 last_optimized_node_ids))) {
61 LOG(INFO) <<
"Client closed connection.";
67 LOG(INFO) <<
"Added subscription: " << subscription_index;
68 subscription_index_ = subscription_index;
71 void ReceiveGlobalSlamOptimizationsHandler::OnFinish() {
72 if (subscription_index_.has_value()) {
73 LOG(INFO) <<
"Removing subscription " << subscription_index_.value();
74 GetUnsynchronizedContext<MapBuilderContextInterface>()
75 ->UnsubscribeGlobalSlamOptimizations(subscription_index_.value());