2017-11-23 22:37:30 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2017 The Cartographer Authors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CARTOGRAPHER_GRPC_FRAMEWORK_SERVER_H
|
|
|
|
#define CARTOGRAPHER_GRPC_FRAMEWORK_SERVER_H
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <memory>
|
2017-11-30 20:18:16 +08:00
|
|
|
#include <sstream>
|
2017-11-23 22:37:30 +08:00
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
#include "cartographer/common/make_unique.h"
|
|
|
|
#include "cartographer_grpc/framework/completion_queue_thread.h"
|
2017-11-29 21:05:31 +08:00
|
|
|
#include "cartographer_grpc/framework/execution_context.h"
|
2017-11-23 22:37:30 +08:00
|
|
|
#include "cartographer_grpc/framework/rpc_handler.h"
|
|
|
|
#include "cartographer_grpc/framework/service.h"
|
|
|
|
#include "grpc++/grpc++.h"
|
|
|
|
|
|
|
|
namespace cartographer_grpc {
|
|
|
|
namespace framework {
|
|
|
|
|
|
|
|
class Server {
|
|
|
|
private:
|
|
|
|
// All options that configure server behaviour such as number of threads,
|
|
|
|
// ports etc.
|
|
|
|
struct Options {
|
|
|
|
size_t number_of_threads = 4;
|
|
|
|
std::string server_address = "0.0.0.0:50051";
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
// This 'Builder' is the only way to construct a 'Server'.
|
|
|
|
class Builder {
|
|
|
|
public:
|
|
|
|
Builder() = default;
|
|
|
|
|
|
|
|
std::unique_ptr<Server> Build();
|
|
|
|
void SetNumberOfThreads(std::size_t number_of_threads);
|
|
|
|
void SetServerAddress(const std::string& server_address);
|
|
|
|
|
|
|
|
template <typename RpcHandlerType, typename ServiceType>
|
|
|
|
void RegisterHandler(const std::string& method_name) {
|
2017-11-30 20:18:16 +08:00
|
|
|
std::stringstream fully_qualified_name;
|
|
|
|
fully_qualified_name << "/" << ServiceType::service_full_name() << "/"
|
|
|
|
<< method_name;
|
2017-11-23 22:37:30 +08:00
|
|
|
rpc_handlers_[ServiceType::service_full_name()].emplace(
|
|
|
|
method_name,
|
|
|
|
RpcHandlerInfo{
|
|
|
|
RpcHandlerType::RequestType::default_instance().GetDescriptor(),
|
|
|
|
RpcHandlerType::ResponseType::default_instance().GetDescriptor(),
|
2017-11-29 21:05:31 +08:00
|
|
|
[](Rpc* const rpc, ExecutionContext* const execution_context) {
|
2017-11-23 22:37:30 +08:00
|
|
|
std::unique_ptr<RpcHandlerInterface> rpc_handler =
|
|
|
|
cartographer::common::make_unique<RpcHandlerType>();
|
|
|
|
rpc_handler->SetRpc(rpc);
|
2017-11-29 21:05:31 +08:00
|
|
|
rpc_handler->SetExecutionContext(execution_context);
|
2017-11-23 22:37:30 +08:00
|
|
|
return rpc_handler;
|
2017-11-25 06:41:58 +08:00
|
|
|
},
|
2017-11-28 17:50:30 +08:00
|
|
|
RpcType<typename RpcHandlerType::IncomingType,
|
2017-11-30 20:18:16 +08:00
|
|
|
typename RpcHandlerType::OutgoingType>::value,
|
|
|
|
fully_qualified_name.str()});
|
2017-11-23 22:37:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
using ServiceInfo = std::map<std::string, RpcHandlerInfo>;
|
|
|
|
|
|
|
|
Options options_;
|
|
|
|
std::map<std::string, ServiceInfo> rpc_handlers_;
|
|
|
|
};
|
|
|
|
friend class Builder;
|
|
|
|
|
2017-11-28 17:50:30 +08:00
|
|
|
// Starts a server starts serving the registered services.
|
|
|
|
void Start();
|
|
|
|
|
2017-12-05 21:46:25 +08:00
|
|
|
// Waits for the server to shut down. Note: The server must be either shutting
|
|
|
|
// down or some other thread must call 'Shutdown()' for this function to ever
|
|
|
|
// return.
|
|
|
|
void WaitForShutdown();
|
|
|
|
|
2017-11-28 17:50:30 +08:00
|
|
|
// Shuts down the server and all of its services.
|
|
|
|
void Shutdown();
|
2017-11-23 22:37:30 +08:00
|
|
|
|
2017-11-29 21:05:31 +08:00
|
|
|
// Sets the server-wide context object shared between RPC handlers.
|
|
|
|
void SetExecutionContext(std::unique_ptr<ExecutionContext> execution_context);
|
|
|
|
|
2017-11-23 22:37:30 +08:00
|
|
|
private:
|
|
|
|
Server(const Options& options);
|
|
|
|
Server(const Server&) = delete;
|
|
|
|
Server& operator=(const Server&) = delete;
|
|
|
|
|
|
|
|
void AddService(
|
|
|
|
const std::string& service_name,
|
|
|
|
const std::map<std::string, RpcHandlerInfo>& rpc_handler_infos);
|
|
|
|
|
2017-11-28 17:50:30 +08:00
|
|
|
static void RunCompletionQueue(
|
|
|
|
::grpc::ServerCompletionQueue* completion_queue);
|
|
|
|
|
2017-11-23 22:37:30 +08:00
|
|
|
Options options_;
|
|
|
|
|
|
|
|
// gRPC objects needed to build a server.
|
|
|
|
::grpc::ServerBuilder server_builder_;
|
|
|
|
std::unique_ptr<::grpc::Server> server_;
|
|
|
|
|
|
|
|
// Threads processing the completion queues.
|
|
|
|
std::vector<CompletionQueueThread> completion_queue_threads_;
|
|
|
|
|
|
|
|
// Map of service names to services.
|
|
|
|
std::map<std::string, Service> services_;
|
2017-11-29 21:05:31 +08:00
|
|
|
|
|
|
|
// A context object that is shared between all implementations of
|
|
|
|
// 'RpcHandler'.
|
|
|
|
std::unique_ptr<ExecutionContext> execution_context_;
|
2017-11-23 22:37:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace framework
|
|
|
|
} // namespace cartographer_grpc
|
|
|
|
|
|
|
|
#endif // CARTOGRAPHER_GRPC_FRAMEWORK_SERVER_H
|