Simplify Handler registration. (#865)

master
Christoph Schütte 2018-01-31 12:26:56 +01:00 committed by Wally B. Feed
parent 708e7fc57d
commit 9aecf23a37
22 changed files with 125 additions and 58 deletions

View File

@ -57,7 +57,6 @@ class RpcHandler : public RpcHandlerInterface {
private:
const std::weak_ptr<Rpc> rpc_;
};
void SetExecutionContext(ExecutionContext* execution_context) override {
execution_context_ = execution_context;
}

View File

@ -28,6 +28,12 @@ class Rpc;
class RpcHandlerInterface {
public:
virtual ~RpcHandlerInterface() = default;
// Returns the fully qualified name of the gRPC method this handler is
// implementing. The fully qualified name has the structure
// '/<<full service name>>/<<method name>>', where the service name is the
// fully qualified proto package name of the service and method name the name
// of the method as defined in the service definition of the proto.
virtual std::string method_name() const = 0;
virtual void SetExecutionContext(ExecutionContext* execution_context) = 0;
virtual void SetRpc(Rpc* rpc) = 0;
virtual void OnRequestInternal(

View File

@ -39,6 +39,18 @@ void Server::Builder::SetServerAddress(const std::string& server_address) {
options_.server_address = server_address;
}
std::tuple<std::string, std::string> Server::Builder::ParseMethodFullName(
const std::string& method_full_name) {
CHECK(method_full_name.at(0) == '/') << "Invalid method name.";
std::stringstream stream(method_full_name.substr(1));
std::string service_full_name;
std::getline(stream, service_full_name, '/');
std::string method_name;
std::getline(stream, method_name, '/');
CHECK(!service_full_name.empty() && !method_name.empty());
return std::make_tuple(service_full_name, method_name);
}
std::unique_ptr<Server> Server::Builder::Build() {
std::unique_ptr<Server> server(new Server(options_));
for (const auto& service_handlers : rpc_handlers_) {

View File

@ -55,12 +55,14 @@ class Server {
void SetNumEventThreads(std::size_t num_event_threads);
void SetServerAddress(const std::string& server_address);
template <typename RpcHandlerType, typename ServiceType>
void RegisterHandler(const std::string& method_name) {
std::stringstream fully_qualified_name;
fully_qualified_name << "/" << ServiceType::service_full_name() << "/"
<< method_name;
rpc_handlers_[ServiceType::service_full_name()].emplace(
template <typename RpcHandlerType>
void RegisterHandler() {
std::string method_full_name = GetMethodFullName<RpcHandlerType>();
std::string service_full_name;
std::string method_name;
std::tie(service_full_name, method_name) =
ParseMethodFullName(method_full_name);
rpc_handlers_[service_full_name].emplace(
method_name,
RpcHandlerInfo{
RpcHandlerType::RequestType::default_instance().GetDescriptor(),
@ -74,12 +76,21 @@ class Server {
},
RpcType<typename RpcHandlerType::IncomingType,
typename RpcHandlerType::OutgoingType>::value,
fully_qualified_name.str()});
method_full_name});
}
private:
using ServiceInfo = std::map<std::string, RpcHandlerInfo>;
template <typename RpcHandlerType>
std::string GetMethodFullName() {
auto handler = cartographer::common::make_unique<const RpcHandlerType>();
return handler->method_name();
}
std::tuple<std::string /* service_full_name */,
std::string /* method_name */>
ParseMethodFullName(const std::string& method_full_name);
Options options_;
std::map<std::string, ServiceInfo> rpc_handlers_;
};

View File

@ -23,6 +23,7 @@
#include "cartographer_grpc/framework/proto/math_service.pb.h"
#include "cartographer_grpc/framework/rpc_handler.h"
#include "glog/logging.h"
#include "google/protobuf/descriptor.h"
#include "grpc++/grpc++.h"
#include "gtest/gtest.h"
@ -40,6 +41,9 @@ class MathServerContext : public ExecutionContext {
class GetSumHandler
: public RpcHandler<Stream<proto::GetSumRequest>, proto::GetSumResponse> {
public:
std::string method_name() const override {
return "/cartographer_grpc.framework.proto.Math/GetSum";
}
void OnRequest(const proto::GetSumRequest& request) override {
sum_ += GetContext<MathServerContext>()->additional_increment();
sum_ += request.input();
@ -58,6 +62,9 @@ class GetSumHandler
class GetRunningSumHandler : public RpcHandler<Stream<proto::GetSumRequest>,
Stream<proto::GetSumResponse>> {
public:
std::string method_name() const override {
return "/cartographer_grpc.framework.proto.Math/GetRunningSum";
}
void OnRequest(const proto::GetSumRequest& request) override {
sum_ += request.input();
@ -78,6 +85,10 @@ class GetRunningSumHandler : public RpcHandler<Stream<proto::GetSumRequest>,
class GetSquareHandler
: public RpcHandler<proto::GetSquareRequest, proto::GetSquareResponse> {
public:
std::string method_name() const override {
return "/cartographer_grpc.framework.proto.Math/GetSquare";
}
void OnRequest(const proto::GetSquareRequest& request) override {
auto response =
cartographer::common::make_unique<proto::GetSquareResponse>();
@ -88,6 +99,10 @@ class GetSquareHandler
class GetEchoHandler
: public RpcHandler<proto::GetEchoRequest, proto::GetEchoResponse> {
public:
std::string method_name() const override {
return "/cartographer_grpc.framework.proto.Math/GetEcho";
}
void OnRequest(const proto::GetEchoRequest& request) override {
int value = request.input();
Writer writer = GetWriter();
@ -105,6 +120,9 @@ class GetSequenceHandler
: public RpcHandler<proto::GetSequenceRequest,
Stream<proto::GetSequenceResponse>> {
public:
std::string method_name() const override {
return "/cartographer_grpc.framework.proto.Math/GetSequence";
}
void OnRequest(const proto::GetSequenceRequest& request) override {
for (int i = 0; i < request.input(); ++i) {
auto response =
@ -129,13 +147,11 @@ class ServerTest : public ::testing::Test {
server_builder.SetServerAddress(kServerAddress);
server_builder.SetNumGrpcThreads(kNumThreads);
server_builder.SetNumEventThreads(kNumThreads);
server_builder.RegisterHandler<GetSumHandler, proto::Math>("GetSum");
server_builder.RegisterHandler<GetSquareHandler, proto::Math>("GetSquare");
server_builder.RegisterHandler<GetRunningSumHandler, proto::Math>(
"GetRunningSum");
server_builder.RegisterHandler<GetEchoHandler, proto::Math>("GetEcho");
server_builder.RegisterHandler<GetSequenceHandler, proto::Math>(
"GetSequence");
server_builder.RegisterHandler<GetSumHandler>();
server_builder.RegisterHandler<GetSquareHandler>();
server_builder.RegisterHandler<GetRunningSumHandler>();
server_builder.RegisterHandler<GetEchoHandler>();
server_builder.RegisterHandler<GetSequenceHandler>();
server_ = server_builder.Build();
client_channel_ =

View File

@ -32,6 +32,9 @@ class AddFixedFramePoseDataHandler
framework::Stream<proto::AddFixedFramePoseDataRequest>,
google::protobuf::Empty> {
public:
std::string method_name() const override {
return "/cartographer_grpc.proto.MapBuilderService/AddFixedFramePoseData";
}
void OnRequest(const proto::AddFixedFramePoseDataRequest &request) override {
// The 'BlockingQueue' returned by 'sensor_data_queue()' is already
// thread-safe. Therefore it suffices to get an unsynchronized reference to

View File

@ -31,6 +31,9 @@ class AddImuDataHandler
: public framework::RpcHandler<framework::Stream<proto::AddImuDataRequest>,
google::protobuf::Empty> {
public:
std::string method_name() const override {
return "/cartographer_grpc.proto.MapBuilderService/AddImuData";
}
void OnRequest(const proto::AddImuDataRequest &request) override {
// The 'BlockingQueue' returned by 'sensor_data_queue()' is already
// thread-safe. Therefore it suffices to get an unsynchronized reference to

View File

@ -31,6 +31,9 @@ class AddLandmarkDataHandler
framework::Stream<proto::AddLandmarkDataRequest>,
google::protobuf::Empty> {
public:
std::string method_name() const override {
return "/cartographer_grpc.proto.MapBuilderService/AddLandmarkData";
}
void OnRequest(const proto::AddLandmarkDataRequest &request) override {
// The 'BlockingQueue' returned by 'sensor_data_queue()' is already
// thread-safe. Therefore it suffices to get an unsynchronized reference to

View File

@ -32,6 +32,9 @@ class AddLocalSlamResultDataHandler
framework::Stream<proto::AddLocalSlamResultDataRequest>,
google::protobuf::Empty> {
public:
std::string method_name() const override {
return "/cartographer_grpc.proto.MapBuilderService/AddLocalSlamResultData";
}
void OnRequest(const proto::AddLocalSlamResultDataRequest& request) override {
auto local_slam_result_data =
GetContext<MapBuilderServer::MapBuilderContext>()

View File

@ -31,6 +31,9 @@ class AddOdometryDataHandler
framework::Stream<proto::AddOdometryDataRequest>,
google::protobuf::Empty> {
public:
std::string method_name() const override {
return "/cartographer_grpc.proto.MapBuilderService/AddOdometryData";
}
void OnRequest(const proto::AddOdometryDataRequest &request) override {
// The 'BlockingQueue' returned by 'sensor_data_queue()' is already
// thread-safe. Therefore it suffices to get an unsynchronized reference to

View File

@ -31,6 +31,9 @@ class AddRangefinderDataHandler
framework::Stream<proto::AddRangefinderDataRequest>,
google::protobuf::Empty> {
public:
std::string method_name() const override {
return "/cartographer_grpc.proto.MapBuilderService/AddRangefinderData";
}
void OnRequest(const proto::AddRangefinderDataRequest &request) override {
// The 'BlockingQueue' returned by 'sensor_data_queue()' is already
// thread-safe. Therefore it suffices to get an unsynchronized reference to

View File

@ -30,6 +30,9 @@ class AddTrajectoryHandler
: public framework::RpcHandler<proto::AddTrajectoryRequest,
proto::AddTrajectoryResponse> {
public:
std::string method_name() const override {
return "/cartographer_grpc.proto.MapBuilderService/AddTrajectory";
}
void OnRequest(const proto::AddTrajectoryRequest& request) override {
auto local_slam_result_callback =
GetUnsynchronizedContext<MapBuilderServer::MapBuilderContext>()

View File

@ -30,6 +30,9 @@ class FinishTrajectoryHandler
: public framework::RpcHandler<proto::FinishTrajectoryRequest,
google::protobuf::Empty> {
public:
std::string method_name() const override {
return "/cartographer_grpc.proto.MapBuilderService/FinishTrajectory";
}
void OnRequest(const proto::FinishTrajectoryRequest& request) override {
GetContext<MapBuilderServer::MapBuilderContext>()
->map_builder()

View File

@ -30,6 +30,9 @@ class GetAllSubmapPosesHandler
: public framework::RpcHandler<google::protobuf::Empty,
proto::GetAllSubmapPosesResponse> {
public:
std::string method_name() const override {
return "/cartographer_grpc.proto.MapBuilderService/GetAllSubmapPoses";
}
void OnRequest(const google::protobuf::Empty& request) override {
auto submap_poses = GetContext<MapBuilderServer::MapBuilderContext>()
->map_builder()

View File

@ -31,6 +31,9 @@ class GetConstraintsHandler
: public framework::RpcHandler<google::protobuf::Empty,
proto::GetConstraintsResponse> {
public:
std::string method_name() const override {
return "/cartographer_grpc.proto.MapBuilderService/GetConstraints";
}
void OnRequest(const google::protobuf::Empty& request) override {
auto constraints = GetContext<MapBuilderServer::MapBuilderContext>()
->map_builder()

View File

@ -24,6 +24,10 @@ class GetLocalToGlobalTransformHandler
: public framework::RpcHandler<proto::GetLocalToGlobalTransformRequest,
proto::GetLocalToGlobalTransformResponse> {
public:
std::string method_name() const override {
return "/cartographer_grpc.proto.MapBuilderService/"
"GetLocalToGlobalTransform";
}
void OnRequest(
const proto::GetLocalToGlobalTransformRequest& request) override {
auto response = cartographer::common::make_unique<

View File

@ -30,6 +30,9 @@ class GetSubmapHandler
: public framework::RpcHandler<proto::GetSubmapRequest,
proto::GetSubmapResponse> {
public:
std::string method_name() const override {
return "/cartographer_grpc.proto.MapBuilderService/GetSubmap";
}
void OnRequest(const proto::GetSubmapRequest &request) override {
auto response =
cartographer::common::make_unique<proto::GetSubmapResponse>();

View File

@ -30,6 +30,9 @@ class GetTrajectoryNodePosesHandler
: public framework::RpcHandler<google::protobuf::Empty,
proto::GetTrajectoryNodePosesResponse> {
public:
std::string method_name() const override {
return "/cartographer_grpc.proto.MapBuilderService/GetTrajectoryNodePoses";
}
void OnRequest(const google::protobuf::Empty& request) override {
auto node_poses = GetContext<MapBuilderServer::MapBuilderContext>()
->map_builder()

View File

@ -31,6 +31,9 @@ class LoadMapHandler
: public framework::RpcHandler<framework::Stream<proto::LoadMapRequest>,
google::protobuf::Empty> {
public:
std::string method_name() const override {
return "/cartographer_grpc.proto.MapBuilderService/LoadMap";
}
void OnRequest(const proto::LoadMapRequest& request) override {
switch (request.map_chunk_case()) {
case proto::LoadMapRequest::kPoseGraph:

View File

@ -30,6 +30,9 @@ class ReceiveLocalSlamResultsHandler
proto::ReceiveLocalSlamResultsRequest,
framework::Stream<proto::ReceiveLocalSlamResultsResponse>> {
public:
std::string method_name() const override {
return "/cartographer_grpc.proto.MapBuilderService/ReceiveLocalSlamResults";
}
void OnRequest(
const proto::ReceiveLocalSlamResultsRequest& request) override {
auto writer = GetWriter();

View File

@ -31,6 +31,9 @@ class RunFinalOptimizationHandler
: public framework::RpcHandler<google::protobuf::Empty,
google::protobuf::Empty> {
public:
std::string method_name() const override {
return "/cartographer_grpc.proto.MapBuilderService/RunFinalOptimization";
}
void OnRequest(const google::protobuf::Empty& request) override {
GetContext<MapBuilderServer::MapBuilderContext>()
->map_builder()

View File

@ -31,7 +31,7 @@
#include "cartographer_grpc/handlers/get_trajectory_node_poses_handler.h"
#include "cartographer_grpc/handlers/load_map_handler.h"
#include "cartographer_grpc/handlers/receive_local_slam_results_handler.h"
#include "cartographer_grpc/handlers/run_final_optimization.h"
#include "cartographer_grpc/handlers/run_final_optimization_handler.h"
#include "cartographer_grpc/proto/map_builder_service.grpc.pb.h"
#include "cartographer_grpc/sensor/serialization.h"
#include "glog/logging.h"
@ -216,48 +216,22 @@ MapBuilderServer::MapBuilderServer(
cartographer::common::make_unique<LocalTrajectoryUploader>(
map_builder_server_options.uplink_server_address());
}
server_builder.RegisterHandler<handlers::AddTrajectoryHandler,
proto::MapBuilderService>("AddTrajectory");
server_builder.RegisterHandler<handlers::AddOdometryDataHandler,
proto::MapBuilderService>("AddOdometryData");
server_builder
.RegisterHandler<handlers::AddImuDataHandler, proto::MapBuilderService>(
"AddImuData");
server_builder.RegisterHandler<handlers::AddRangefinderDataHandler,
proto::MapBuilderService>(
"AddRangefinderData");
server_builder.RegisterHandler<handlers::AddFixedFramePoseDataHandler,
proto::MapBuilderService>(
"AddFixedFramePoseData");
server_builder.RegisterHandler<handlers::AddLandmarkDataHandler,
proto::MapBuilderService>("AddLandmarkData");
server_builder.RegisterHandler<handlers::AddLocalSlamResultDataHandler,
proto::MapBuilderService>(
"AddLocalSlamResultData");
server_builder.RegisterHandler<handlers::FinishTrajectoryHandler,
proto::MapBuilderService>("FinishTrajectory");
server_builder.RegisterHandler<handlers::ReceiveLocalSlamResultsHandler,
proto::MapBuilderService>(
"ReceiveLocalSlamResults");
server_builder
.RegisterHandler<handlers::GetSubmapHandler, proto::MapBuilderService>(
"GetSubmap");
server_builder.RegisterHandler<handlers::GetTrajectoryNodePosesHandler,
proto::MapBuilderService>(
"GetTrajectoryNodePoses");
server_builder.RegisterHandler<handlers::GetAllSubmapPosesHandler,
proto::MapBuilderService>("GetAllSubmapPoses");
server_builder.RegisterHandler<handlers::GetLocalToGlobalTransformHandler,
proto::MapBuilderService>(
"GetLocalToGlobalTransform");
server_builder.RegisterHandler<handlers::GetConstraintsHandler,
proto::MapBuilderService>("GetConstraints");
server_builder
.RegisterHandler<handlers::LoadMapHandler, proto::MapBuilderService>(
"LoadMap");
server_builder.RegisterHandler<handlers::RunFinalOptimizationHandler,
proto::MapBuilderService>(
"RunFinalOptimization");
server_builder.RegisterHandler<handlers::AddTrajectoryHandler>();
server_builder.RegisterHandler<handlers::AddOdometryDataHandler>();
server_builder.RegisterHandler<handlers::AddImuDataHandler>();
server_builder.RegisterHandler<handlers::AddRangefinderDataHandler>();
server_builder.RegisterHandler<handlers::AddFixedFramePoseDataHandler>();
server_builder.RegisterHandler<handlers::AddLandmarkDataHandler>();
server_builder.RegisterHandler<handlers::AddLocalSlamResultDataHandler>();
server_builder.RegisterHandler<handlers::FinishTrajectoryHandler>();
server_builder.RegisterHandler<handlers::ReceiveLocalSlamResultsHandler>();
server_builder.RegisterHandler<handlers::GetSubmapHandler>();
server_builder.RegisterHandler<handlers::GetTrajectoryNodePosesHandler>();
server_builder.RegisterHandler<handlers::GetAllSubmapPosesHandler>();
server_builder.RegisterHandler<handlers::GetLocalToGlobalTransformHandler>();
server_builder.RegisterHandler<handlers::GetConstraintsHandler>();
server_builder.RegisterHandler<handlers::LoadMapHandler>();
server_builder.RegisterHandler<handlers::RunFinalOptimizationHandler>();
grpc_server_ = server_builder.Build();
grpc_server_->SetExecutionContext(
cartographer::common::make_unique<MapBuilderContext>(this));