22 #ifndef ENGINE_GRPC_SERVER_H
23 #define ENGINE_GRPC_SERVER_H
27 #include <type_traits>
29 #include <grpcpp/grpcpp.h>
30 #include <grpcpp/health_check_service_interface.h>
31 #include <nlohmann/json.hpp>
33 #include "nrp_protobuf/engine_grpc.grpc.pb.h"
36 #include "nrp_protobuf/config/cmake_constants.h"
44 using EngineGrpc::EngineGrpcService;
59 using lock_t = std::unique_lock<EngineGrpcServer::mutex_t>;
73 : _engineWrapper(engineWrapper)
76 this->_isServerRunning =
false;
78 grpc::EnableDefaultHealthCheckService(
true);
94 if(!this->_isServerRunning)
96 grpc::ServerBuilder builder;
97 builder.AddListeningPort(_serverAddress, grpc::InsecureServerCredentials());
98 builder.RegisterService(
this);
101 this->_server = builder.BuildAndStart();
103 this->_isServerRunning =
true;
120 if(this->_isServerRunning)
122 this->_server->Shutdown();
124 this->_isServerRunning =
false;
131 virtual bool initRunFlag()
const {
return this->_engineWrapper->initRunFlag(); }
136 virtual bool shutdownFlag()
const {
return this->_engineWrapper->shutdownFlag(); }
143 return this->_isServerRunning;
151 return this->_serverAddress;
161 std::string _serverAddress;
166 bool _isServerRunning;
171 std::unique_ptr<grpc::Server> _server;
176 std::unique_ptr<EngineProtoWrapper> _engineWrapper;
191 grpc::Status initialize( grpc::ServerContext * ,
192 const EngineGrpc::InitializeRequest * request,
193 EngineGrpc::InitializeReply * )
override
199 nlohmann::json requestJson = nlohmann::json::parse(request->json());
203 this->_engineWrapper->initialize(requestJson);
205 catch(
const std::exception &e)
207 return handleGrpcError(
"Error while executing initialization", e.what());
210 return grpc::Status::OK;
227 grpc::Status reset( grpc::ServerContext * ,
228 const EngineGrpc::ResetRequest * ,
229 EngineGrpc::ResetReply * )
override
237 this->_engineWrapper->reset();
239 catch(
const std::exception &e)
241 return handleGrpcError(
"Error while executing initialization", e.what());
245 return grpc::Status::OK;
261 grpc::Status shutdown( grpc::ServerContext * ,
262 const EngineGrpc::ShutdownRequest * request,
263 EngineGrpc::ShutdownReply * )
override
271 nlohmann::json requestJson = nlohmann::json::parse(request->json());
275 this->_engineWrapper->shutdown();
277 catch(
const std::exception &e)
279 return handleGrpcError(
"Error while executing shutdown", e.what());
283 return grpc::Status::OK;
299 grpc::Status runLoopStep( grpc::ServerContext * ,
300 const EngineGrpc::RunLoopStepRequest * request,
301 EngineGrpc::RunLoopStepReply * reply)
override
307 int64_t engineTime = (this->_engineWrapper->runLoopStep(
SimulationTime(request->timestep()))).count();
309 reply->set_enginetime(engineTime);
311 catch(
const std::exception &e)
313 return handleGrpcError(
"Error while executing runLoopStep", e.what());
316 return grpc::Status::OK;
332 grpc::Status setDataPacks( grpc::ServerContext * ,
333 const EngineGrpc::SetDataPacksRequest * request,
334 EngineGrpc::SetDataPacksReply * )
override
340 this->_engineWrapper->setDataPacks(*request);
342 catch(
const std::exception &e)
344 return handleGrpcError(
"Error while executing setDataPack", e.what());
347 return grpc::Status::OK;
363 grpc::Status getDataPacks( grpc::ServerContext * ,
364 const EngineGrpc::GetDataPacksRequest * request,
365 EngineGrpc::GetDataPacksReply * reply)
override
371 this->_engineWrapper->getDataPacks(*request, reply);
373 catch(
const std::exception &e)
375 return handleGrpcError(
"Error while executing getDataPack", e.what());
378 return grpc::Status::OK;
389 grpc::Status handleGrpcError(
const std::string & contextMessage,
const std::string & errorMessage)
396 grpc::Status status(grpc::StatusCode::CANCELLED, errorMessage);
402 #endif // ENGINE_GRPC_SERVER_H