Go to the documentation of this file.
22 #ifndef ENGINE_PROTO_WRAPPER_H
23 #define ENGINE_PROTO_WRAPPER_H
27 #include <type_traits>
29 #include <nlohmann/json.hpp>
31 #include "nrp_protobuf/engine_grpc.pb.h"
34 #include "nrp_protobuf/config/cmake_constants.h"
53 using lock_t = std::unique_lock<EngineProtoWrapper::mutex_t>;
66 const std::string &protobufPluginsPath,
68 : _loggerCfg(engineName), _engineName(engineName)
73 for (
const auto &packageName: protobufPlugins) {
74 auto packageNameStr = packageName.template get<std::string>();
76 std::stringstream pluginLibName;
77 pluginLibName <<
"lib" << NRP_PROTO_OPS_LIB_PREFIX << packageNameStr <<
78 NRP_PROTO_OPS_LIB_SUFIX <<
".so";
81 _protoOps.template emplace_back(std::move(pluginLib));
104 this->_datapacksControllers.emplace(datapackName, interface);
110 return this->_datapacksControllers.size();
118 std::vector<std::string> dpNames;
119 for(
auto const& dpCons : this->_datapacksControllers)
120 dpNames.push_back(dpCons.first);
151 virtual void reset() = 0;
169 const auto numDataPacks = data.datapacks_size();
171 for(
int i = 0; i < numDataPacks; i++)
177 const auto &name = dataPack.datapackid().datapackname();
178 const auto &devInterface = this->_datapacksControllers.find(name);
180 if(devInterface != _datapacksControllers.end()) {
182 devInterface->second->handleDataPackData(dataPack);
186 devInterface->second->handleDataPackData(*protoMsg);
189 dataPack.datapackid().datapackname() +
"\" using any of the NRP-Core Protobuf plugins" +
190 "specified in the engine configuration: [" +
_protoOpsStr +
"]. Ensure that the parameter " +
191 "\"ProtobufPackages\" is properly set in the Engine configuration");
196 const auto errorMessage =
"DataPack " + name +
" is not registered in engine " + this->_engineName;
197 throw std::invalid_argument(errorMessage);
204 auto protoMsg = mod->unpackProtoAny(data);
209 return std::unique_ptr<gpb::Message>();
212 virtual void getDataPacks(
const EngineGrpc::GetDataPacksRequest & request, EngineGrpc::GetDataPacksReply * reply)
214 const auto numDataPacks = request.datapackids_size();
216 for(
int i = 0; i < numDataPacks; i++)
218 auto * protoDataPack = reply->add_datapacks();
219 getDataPack(request.datapackids(i).datapackname(), protoDataPack);
223 bool getDataPack(
const std::string& name, EngineGrpc::DataPackMessage* dpMsg)
225 dpMsg->mutable_datapackid()->set_datapackname(name);
226 dpMsg->mutable_datapackid()->set_enginename(this->_engineName);
229 const auto &devInterface = this->_datapacksControllers.find(name);
231 if(devInterface != _datapacksControllers.end())
232 data = devInterface->second->getDataPackInformation();
234 const auto errorMessage =
"DataPack " + name +
" is not registered in engine " + this->_engineName;
235 throw std::invalid_argument(errorMessage);
252 mod->setDataPackMessageData(*data, dpMsg);
262 "\", unable to serialize datapack \"" + dpMsg->datapackid().datapackname() +
263 "\" using any of the NRP-Core Protobuf plugins specified in the"
265 "]. Ensure that the parameter "
266 "\"ProtobufPackages\" is properly set in the Engine configuration");
273 this->_datapacksControllers.clear();
281 return this->_datapacksControllers.find(datapackName)->second;
303 std::string _engineName;
309 std::map<std::string, ProtoDataPackController*> _datapacksControllers;
312 std::vector<std::unique_ptr<protobuf_ops::NRPProtobufOpsIface>>
_protoOps;
316 #endif // ENGINE_PROTO_WRAPPER_H
Base NRPException class.
Definition: nrp_exceptions.h:36
bool getDataPack(const std::string &name, EngineGrpc::DataPackMessage *dpMsg)
Definition: engine_proto_wrapper.h:223
bool _handleDataPackMessage
If true controllers are sent incoming DataPackMessages, if false only the contained data.
Definition: engine_proto_wrapper.h:288
virtual void reset()=0
Resets the simulation.
virtual void getDataPacks(const EngineGrpc::GetDataPacksRequest &request, EngineGrpc::GetDataPacksReply *reply)
Definition: engine_proto_wrapper.h:212
void setDataPackMessageData(gpb::Message *data, EngineGrpc::DataPackMessage *dpMsg)
Definition: engine_proto_wrapper.h:247
void clearRegisteredDataPacks()
Definition: engine_proto_wrapper.h:271
std::string _protoOpsStr
Definition: engine_proto_wrapper.h:313
EngineProtoWrapper(const std::string &engineName, const std::string &protobufPluginsPath, const nlohmann::json &protobufPlugins)
Constructor.
Definition: engine_proto_wrapper.h:65
std::vector< std::string > getNamesRegisteredDataPacks()
Get the names of registered datapacks.
Definition: engine_proto_wrapper.h:116
std::vector< std::unique_ptr< protobuf_ops::NRPProtobufOpsIface > > _protoOps
Definition: engine_proto_wrapper.h:312
std::timed_mutex mutex_t
Definition: engine_proto_wrapper.h:52
virtual void initialize(const nlohmann::json &data)=0
Initializes the simulation.
std::unique_lock< EngineProtoWrapper::mutex_t > lock_t
Definition: engine_proto_wrapper.h:53
static void info(const FormatString &fmt, const Args &...args)
NRP logging function with message formatting for info level.
Definition: nrp_logger.h:138
std::unique_ptr< gpb::Message > unpackFromAny(const gpb::Any &data)
Definition: engine_proto_wrapper.h:201
virtual bool shutdownFlag() const =0
Indicates if shutdown was requested by the client.
EngineProtoWrapper()=delete
No dummy wrappers, only those with name.
virtual void shutdown()=0
Shutdowns the simulation.
virtual SimulationTime runLoopStep(const SimulationTime timeStep)=0
Runs a single simulation loop step.
NRP Logging functions.
Definition: nrp_logger.h:45
std::unique_ptr< protobuf_ops::NRPProtobufOpsIface > loadProtobufPlugin(const std::string &pluginLibFile)
Load a Protobuf conversion plugin from a given library.
Definition: proto_ops_manager.cpp:35
void registerDataPack(const std::string &datapackName, ProtoDataPackController *interface)
Registers a datapack controller with the given name in the engine.
Definition: engine_proto_wrapper.h:102
void addPluginPath(const std::string &pluginPath)
Adds search path under which to look for plugins.
Definition: plugin_manager.cpp:88
void setDataPacks(const EngineGrpc::SetDataPacksRequest &data)
Definition: engine_proto_wrapper.h:167
static EXCEPTION logCreate(LOG_EXCEPTION_T &exception, const std::string &msg, NRPLogger::spdlog_out_fcn_t spdlogCall=NRPLogger::critical)
Definition: nrp_exceptions.h:73
virtual ~EngineProtoWrapper()=default
Destructor.
Abstract class defining an interface to interact with an Engine with data exchange via protobuf messa...
Definition: engine_proto_wrapper.h:48
ProtoDataPackController * getDataPackController(const std::string &datapackName)
Returns the pointer to the DataPackController of the Data Pack with the specified name.
Definition: engine_proto_wrapper.h:279
void setDataPack(const EngineGrpc::DataPackMessage &dataPack)
Definition: engine_proto_wrapper.h:175
const std::string & getEngineName()
Get the Engine name.
Definition: engine_proto_wrapper.h:127
virtual bool initRunFlag() const =0
Indicates if the simulation was initialized and is running.
static ProtoOpsManager & getInstance()
Get singleton instance of ProtoOpsManager.
Definition: proto_ops_manager.cpp:55
std::chrono::nanoseconds SimulationTime
Definition: time_utils.h:31
unsigned getNumRegisteredDataPacks()
Definition: engine_proto_wrapper.h:108
nlohmann::json json
Definition: engine_json_server.cpp:31