receive_global_slam_optimizations_handler.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright 2018 The Cartographer Authors
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "cartographer/cloud/internal/handlers/receive_global_slam_optimizations_handler.h"
00018 
00019 #include "absl/memory/memory.h"
00020 #include "async_grpc/rpc_handler.h"
00021 #include "cartographer/cloud/internal/map_builder_context_interface.h"
00022 #include "cartographer/cloud/proto/map_builder_service.pb.h"
00023 #include "cartographer/transform/transform.h"
00024 
00025 namespace cartographer {
00026 namespace cloud {
00027 namespace handlers {
00028 namespace {
00029 
00030 std::unique_ptr<proto::ReceiveGlobalSlamOptimizationsResponse> GenerateResponse(
00031     const std::map<int, mapping::SubmapId> &last_optimized_submap_ids,
00032     const std::map<int, mapping::NodeId> &last_optimized_node_ids) {
00033   auto response =
00034       absl::make_unique<proto::ReceiveGlobalSlamOptimizationsResponse>();
00035   for (const auto &entry : last_optimized_submap_ids) {
00036     entry.second.ToProto(
00037         &(*response->mutable_last_optimized_submap_ids())[entry.first]);
00038   }
00039   for (const auto &entry : last_optimized_node_ids) {
00040     entry.second.ToProto(
00041         &(*response->mutable_last_optimized_node_ids())[entry.first]);
00042   }
00043   return response;
00044 }
00045 
00046 }  // namespace
00047 
00048 void ReceiveGlobalSlamOptimizationsHandler::OnRequest(
00049     const google::protobuf::Empty &request) {
00050   auto writer = GetWriter();
00051   const int subscription_index =
00052       GetUnsynchronizedContext<MapBuilderContextInterface>()
00053           ->SubscribeGlobalSlamOptimizations(
00054               [writer](const std::map<int, mapping::SubmapId>
00055                            &last_optimized_submap_ids,
00056                        const std::map<int, mapping::NodeId>
00057                            &last_optimized_node_ids) {
00058                 if (!writer.Write(GenerateResponse(last_optimized_submap_ids,
00059                                                    last_optimized_node_ids))) {
00060                   // Client closed connection.
00061                   LOG(INFO) << "Client closed connection.";
00062                   return false;
00063                 }
00064                 return true;
00065               });
00066 
00067   LOG(INFO) << "Added subscription: " << subscription_index;
00068   subscription_index_ = subscription_index;
00069 }
00070 
00071 void ReceiveGlobalSlamOptimizationsHandler::OnFinish() {
00072   if (subscription_index_.has_value()) {
00073     LOG(INFO) << "Removing subscription " << subscription_index_.value();
00074     GetUnsynchronizedContext<MapBuilderContextInterface>()
00075         ->UnsubscribeGlobalSlamOptimizations(subscription_index_.value());
00076   }
00077 }
00078 
00079 }  // namespace handlers
00080 }  // namespace cloud
00081 }  // namespace cartographer


cartographer
Author(s): The Cartographer Authors
autogenerated on Thu May 9 2019 02:27:35