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_SERVICE_H
|
|
|
|
#define CARTOGRAPHER_GRPC_FRAMEWORK_SERVICE_H
|
|
|
|
|
2017-11-29 17:40:26 +08:00
|
|
|
#include "cartographer_grpc/framework/completion_queue_thread.h"
|
2017-12-15 19:21:44 +08:00
|
|
|
#include "cartographer_grpc/framework/event_queue_thread.h"
|
2017-11-29 21:05:31 +08:00
|
|
|
#include "cartographer_grpc/framework/execution_context.h"
|
2017-11-25 06:41:58 +08:00
|
|
|
#include "cartographer_grpc/framework/rpc.h"
|
2017-11-23 22:37:30 +08:00
|
|
|
#include "cartographer_grpc/framework/rpc_handler.h"
|
|
|
|
#include "grpc++/impl/codegen/service_type.h"
|
|
|
|
|
|
|
|
namespace cartographer_grpc {
|
|
|
|
namespace framework {
|
|
|
|
|
|
|
|
// A 'Service' represents a generic service for gRPC asynchronous methods and is
|
|
|
|
// responsible for managing the lifetime of active RPCs issued against methods
|
|
|
|
// of the service and distributing incoming gRPC events to their respective
|
|
|
|
// 'Rpc' handler objects.
|
|
|
|
class Service : public ::grpc::Service {
|
|
|
|
public:
|
2017-12-15 19:21:44 +08:00
|
|
|
using EventQueueSelector = std::function<EventQueue*()>;
|
2017-11-29 17:40:26 +08:00
|
|
|
friend class Rpc;
|
|
|
|
|
2017-11-25 06:41:58 +08:00
|
|
|
Service(const std::string& service_name,
|
2017-12-15 19:21:44 +08:00
|
|
|
const std::map<std::string, RpcHandlerInfo>& rpc_handlers,
|
|
|
|
EventQueueSelector event_queue_selector);
|
2017-11-29 21:05:31 +08:00
|
|
|
void StartServing(std::vector<CompletionQueueThread>& completion_queues,
|
|
|
|
ExecutionContext* execution_context);
|
2017-11-29 17:40:26 +08:00
|
|
|
void HandleEvent(Rpc::Event event, Rpc* rpc, bool ok);
|
2017-11-28 17:50:30 +08:00
|
|
|
void StopServing();
|
2017-11-23 22:37:30 +08:00
|
|
|
|
|
|
|
private:
|
2017-11-28 17:50:30 +08:00
|
|
|
void HandleNewConnection(Rpc* rpc, bool ok);
|
2017-11-29 17:40:26 +08:00
|
|
|
void HandleRead(Rpc* rpc, bool ok);
|
|
|
|
void HandleWrite(Rpc* rpc, bool ok);
|
2017-11-30 20:18:16 +08:00
|
|
|
void HandleFinish(Rpc* rpc, bool ok);
|
2017-11-28 17:50:30 +08:00
|
|
|
void HandleDone(Rpc* rpc, bool ok);
|
|
|
|
|
2017-11-29 17:40:26 +08:00
|
|
|
void RemoveIfNotPending(Rpc* rpc);
|
|
|
|
|
2017-11-25 06:41:58 +08:00
|
|
|
std::map<std::string, RpcHandlerInfo> rpc_handler_infos_;
|
2017-12-15 19:21:44 +08:00
|
|
|
EventQueueSelector event_queue_selector_;
|
2017-11-25 06:41:58 +08:00
|
|
|
ActiveRpcs active_rpcs_;
|
2017-11-28 17:50:30 +08:00
|
|
|
bool shutting_down_ = false;
|
2017-11-23 22:37:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace framework
|
|
|
|
} // namespace cartographer_grpc
|
|
|
|
|
|
|
|
#endif // CARTOGRAPHER_GRPC_FRAMEWORK_SERVICE_H
|