NRP Core  1.4.1
python_engine_json_nrp_client_base.h
Go to the documentation of this file.
1 
2 /* * NRP Core - Backend infrastructure to synchronize simulations
3  *
4  * Copyright 2020-2023 NRP Team
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * This project has received funding from the European Union’s Horizon 2020
19  * Framework Programme for Research and Innovation under the Specific Grant
20  * Agreement No. 945539 (Human Brain Project SGA3).
21  */
22 
23 #ifndef PYTHON_ENGINE_JSON_NRP_CLIENT_BASE_H
24 #define PYTHON_ENGINE_JSON_NRP_CLIENT_BASE_H
25 
27 
30 
31 #include "nrp_python_json_engine/config/cmake_constants.h"
33 
34 #include <signal.h>
35 #include <sys/types.h>
36 #include <sys/wait.h>
37 #include <sys/prctl.h>
38 #include <chrono>
39 #include <unistd.h>
40 
44 template<class ENGINE, const char* SCHEMA>
46  : public EngineJSONNRPClient<ENGINE, SCHEMA>
47 {
51  static constexpr size_t _killWait = 10;
52 
53  public:
54 
56  : EngineJSONNRPClient<ENGINE, SCHEMA>(config, std::move(launcher))
57  {
58  NRP_LOGGER_TRACE("{} called", __FUNCTION__);
59  }
60 
61  virtual ~PythonEngineJSONNRPClientBase() override
62  {
63  NRP_LOGGER_TRACE("{} called", __FUNCTION__);
64  }
65 
66  virtual void initialize() override
67  {
68  NRP_LOGGER_TRACE("{} called", __FUNCTION__);
69 
70  // Pass the ratio used by SimulationTime to the server
71  // Based on the ratio, the server should be able to assert that it's using correct time units
72 
73  nlohmann::json config = this->engineConfig();
74  config[PythonConfigConst::SimulationTimeRatio.data()] = { SimulationTime::period::num, SimulationTime::period::den };
75 
76  try
77  {
78  nlohmann::json resp = this->sendInitCommand(config);
79  }
80  catch(std::exception &e)
81  {
82  // Write the error message
83  this->_initErrMsg = e.what();
84  NRPLogger::error(this->_initErrMsg);
85 
86  throw NRPException::logCreate("Initialization failed: " + this->_initErrMsg);
87  }
88 
89 
90  NRPLogger::debug("PythonEngineJSONNRPClientBase::initialize(...) completed with no errors.");
91  }
92 
93  virtual void reset() override
94  {
95  NRP_LOGGER_TRACE("{} called", __FUNCTION__);
96 
97  try
98  {
99  nlohmann::json resp = this->sendResetCommand(nlohmann::json("reset"));
100  NRPLogger::debug("NestEngineJSONNRPClient:reset()::resp [ {} ]", resp.dump());
101  }
102  catch(std::exception &e)
103  {
104  // Write the error message
105  std::string msg = e.what();
106  NRPLogger::error(msg);
107 
108  throw NRPException::logCreate("Reset failed: " + msg);
109  }
110 
111  this->resetEngineTime();
112  }
113 
114  virtual void shutdown() override
115  {
116  NRP_LOGGER_TRACE("{} called", __FUNCTION__);
117 
119  }
120 
121  private:
125  std::string _initErrMsg = "";
126 
127  virtual const std::vector<std::string> engineProcStartParams() const override
128  {
129  NRP_LOGGER_TRACE("{} called", __FUNCTION__);
130 
131  std::vector<std::string> startParams = EngineJSONNRPClient<ENGINE, SCHEMA>::engineProcStartParams();
132 
133  // Pass any extra server options that were specififed by the user
134  std::string serverOptions = this->engineConfig().at("ServerOptions");
135  startParams.push_back(std::string("--") + PythonConfigConst::ExtraServerOptionsArg.data() + "=" + serverOptions);
136 
137  return startParams;
138  }
139 };
140 
141 #endif // PYTHON_ENGINE_JSON_NRP_CLIENT_BASE_H
engine_json_config.h
PtrTemplates< ProcessLauncherInterface >::unique_ptr
std::unique_ptr< ProcessLauncherInterface > unique_ptr
Definition: ptr_templates.h:34
engine_json_nrp_client.h
PythonEngineJSONNRPClientBase
NRP - Python Communicator on the NRP side. Converts DataPackInterface classes from/to JSON objects.
Definition: python_engine_json_nrp_client_base.h:45
EngineJSONNRPClient
NRP - Gazebo Communicator on the NRP side. Converts DataPackInterface classes from/to JSON objects.
Definition: engine_json_nrp_client.h:45
EngineJSONNRPClient::sendResetCommand
nlohmann::json sendResetCommand(const nlohmann::json &data)
Send a reset command.
Definition: engine_json_nrp_client.h:209
EngineJSONNRPClient::sendInitCommand
nlohmann::json sendInitCommand(const nlohmann::json &data)
Send an initialization command.
Definition: engine_json_nrp_client.h:192
engine_client_interface.h
EngineClient::engineConfig
const nlohmann::json & engineConfig() const override final
Get Engine Configuration.
Definition: engine_client_interface.h:278
PythonConfigConst::SimulationTimeRatio
static constexpr std::string_view SimulationTimeRatio
Ratio used by SimulationTime.
Definition: python_config.h:62
PythonEngineJSONNRPClientBase::shutdown
virtual void shutdown() override
Shutdown engine.
Definition: python_engine_json_nrp_client_base.h:114
PythonConfigConst::ExtraServerOptionsArg
static constexpr std::string_view ExtraServerOptionsArg
Parameter name that is used to pass along the server address.
Definition: python_config.h:52
PythonEngineJSONNRPClientBase::initialize
virtual void initialize() override
Initialize engine.
Definition: python_engine_json_nrp_client_base.h:66
PythonEngineJSONNRPClientBase::PythonEngineJSONNRPClientBase
PythonEngineJSONNRPClientBase(nlohmann::json &config, ProcessLauncherInterface::unique_ptr &&launcher)
Definition: python_engine_json_nrp_client_base.h:55
EngineClient::resetEngineTime
virtual void resetEngineTime()
Definition: engine_client_interface.h:359
NRPException::logCreate
static EXCEPTION logCreate(LOG_EXCEPTION_T &exception, const std::string &msg, NRPLogger::spdlog_out_fcn_t spdlogCall=NRPLogger::critical)
Definition: nrp_exceptions.h:73
EngineJSONNRPClient::sendShutdownCommand
nlohmann::json sendShutdownCommand(const nlohmann::json &data)
Send a shutdown command.
Definition: engine_json_nrp_client.h:226
NRPLogger::error
static void error(const FormatString &fmt, const Args &...args)
NRP logging function with message formatting for error level.
Definition: nrp_logger.h:160
PythonEngineJSONNRPClientBase::reset
virtual void reset() override
Reset engine.
Definition: python_engine_json_nrp_client_base.h:93
python_config.h
PythonEngineJSONNRPClientBase::~PythonEngineJSONNRPClientBase
virtual ~PythonEngineJSONNRPClientBase() override
Definition: python_engine_json_nrp_client_base.h:61
NRPLogger::debug
static void debug(const FormatString &fmt, const Args &...args)
NRP logging function with message formatting for debug level.
Definition: nrp_logger.h:127
EngineJSONNRPClient::engineProcStartParams
virtual const std::vector< std::string > engineProcStartParams() const override
Get all Engine Process Startup parameters.
Definition: engine_json_nrp_client.h:144
NRP_LOGGER_TRACE
#define NRP_LOGGER_TRACE(...)
trace log macro. It is voided by defining \PRODUCTION_RELEASE
Definition: nrp_logger.h:39
json
nlohmann::json json
Definition: engine_json_server.cpp:31