receive_global_slam_optimizations_handler.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2018 The Cartographer Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #include "async_grpc/rpc_handler.h"
21 #include "cartographer/cloud/proto/map_builder_service.pb.h"
24 
25 namespace cartographer {
26 namespace cloud {
27 namespace handlers {
28 namespace {
29 
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) {
33  auto response =
34  common::make_unique<proto::ReceiveGlobalSlamOptimizationsResponse>();
35  for (const auto &entry : last_optimized_submap_ids) {
36  entry.second.ToProto(
37  &(*response->mutable_last_optimized_submap_ids())[entry.first]);
38  }
39  for (const auto &entry : last_optimized_node_ids) {
40  entry.second.ToProto(
41  &(*response->mutable_last_optimized_node_ids())[entry.first]);
42  }
43  return response;
44 }
45 
46 } // namespace
47 
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))) {
60  // Client closed connection.
61  LOG(INFO) << "Client closed connection.";
62  return false;
63  }
64  return true;
65  });
66 
67  LOG(INFO) << "Added subscription: " << subscription_index;
68  subscription_index_ = subscription_index;
69 }
70 
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());
76  }
77 }
78 
79 } // namespace handlers
80 } // namespace cloud
81 } // namespace cartographer


cartographer
Author(s): The Cartographer Authors
autogenerated on Mon Feb 28 2022 22:00:58