NRP Core  1.4.1
python_engine_grpc_nrp_client.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_GRPC_NRP_CLIENT_H
24 #define PYTHON_ENGINE_GRPC_NRP_CLIENT_H
25 
27 
29 
32 
33 #include "nrp_python_grpc_engine/config/cmake_constants.h"
35 #include "nrp_protobuf/engine_grpc.pb.h"
36 
37 #include <signal.h>
38 #include <sys/types.h>
39 #include <sys/wait.h>
40 #include <sys/prctl.h>
41 #include <chrono>
42 #include <unistd.h>
43 
44 
49 : public EngineGrpcClient<PythonEngineGRPCNRPClient, PythonGrpcConfigConst::EngineSchema>
50 {
51  public:
52 
54  : EngineGrpcClient(config, std::move(launcher))
55  {
56  NRP_LOGGER_TRACE("{} called", __FUNCTION__);
57 
58  setDefaultProperty<std::string>("EngineProcCmd", NRP_PYTHON_GRPC_EXECUTABLE_PATH);
59  }
60 
61  virtual ~PythonEngineGRPCNRPClient() 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[PythonGrpcConfigConst::SimulationTimeRatio.data()] = { SimulationTime::period::num, SimulationTime::period::den };
75 
76  try
77  {
78  this->sendInitializeCommand(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("PythonEngineGRPCNRPClientBase::initialize(...) completed with no errors.");
91  }
92 
93  virtual void reset() override
94  {
95  NRP_LOGGER_TRACE("{} called", __FUNCTION__);
96 
97  try
98  {
99  this->sendResetCommand();
100  }
101  catch(std::exception &e)
102  {
103  // Write the error message
104  std::string msg = e.what();
105  NRPLogger::error(msg);
106 
107  throw NRPException::logCreate("Reset failed: " + msg);
108  }
109 
110  this->resetEngineTime();
111  }
112 
113  virtual void shutdown() override
114  {
115  NRP_LOGGER_TRACE("{} called", __FUNCTION__);
116 
118  }
119 
120  virtual const std::vector<std::string> engineProcStartParams() const override
121  {
122  NRP_LOGGER_TRACE("{} called", __FUNCTION__);
123 
124  std::vector<std::string> startParams = this->engineConfig().at("EngineProcStartParams");
125 
126  std::string name = this->engineConfig().at("EngineName");
127  startParams.push_back(std::string("--") + EngineGRPCConfigConst::EngineNameArg.data() + "=" + name);
128 
129  std::string address = this->engineConfig().at("ServerAddress");
130  startParams.push_back(std::string("--") + EngineGRPCConfigConst::EngineServerAddrArg.data() + "=" + address);
131 
132  return startParams;
133  }
134 
135  private:
139  std::string _initErrMsg = "";
140 };
141 
142 using PythonEngineGRPCLauncher = PythonEngineGRPCNRPClient::EngineLauncher<PythonGrpcConfigConst::EngineType>;
143 
144 
146 
147 
148 #endif // PYTHON_ENGINE_GRPC_NRP_CLIENT_H
EngineGrpcClient
Definition: engine_grpc_client.h:45
PtrTemplates< ProcessLauncherInterface >::unique_ptr
std::unique_ptr< ProcessLauncherInterface > unique_ptr
Definition: ptr_templates.h:34
EngineGrpcClient< PythonEngineGRPCNRPClient, PythonGrpcConfigConst::EngineSchema >::resetEngineTime
void resetEngineTime() override
Definition: engine_grpc_client.h:414
engine_grpc_config.h
EngineGrpcClient< PythonEngineGRPCNRPClient, PythonGrpcConfigConst::EngineSchema >::sendShutdownCommand
void sendShutdownCommand(const nlohmann::json &data)
Definition: engine_grpc_client.h:178
plugin.h
EngineGrpcClient< PythonEngineGRPCNRPClient, PythonGrpcConfigConst::EngineSchema >::sendInitializeCommand
void sendInitializeCommand(const nlohmann::json &data)
Definition: engine_grpc_client.h:135
engine_client_interface.h
EngineClient< PythonEngineGRPCNRPClient, SCHEMA >::engineConfig
const nlohmann::json & engineConfig() const override final
Get Engine Configuration.
Definition: engine_client_interface.h:278
engine_grpc_client.h
PythonEngineGRPCNRPClient
PythonGRPCEngine client.
Definition: python_engine_grpc_nrp_client.h:48
PythonEngineGRPCNRPClient::initialize
virtual void initialize() override
Initialize engine.
Definition: python_engine_grpc_nrp_client.h:66
PythonGrpcConfigConst::SimulationTimeRatio
static constexpr std::string_view SimulationTimeRatio
Ratio used by SimulationTime.
Definition: python_grpc_config.h:42
PythonEngineGRPCNRPClient::reset
virtual void reset() override
Reset engine.
Definition: python_engine_grpc_nrp_client.h:93
EngineGRPCConfigConst::EngineNameArg
static constexpr std::string_view EngineNameArg
Parameter name that is used to pass along the engine name.
Definition: engine_grpc_config.h:37
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
EngineGRPCConfigConst::EngineServerAddrArg
static constexpr std::string_view EngineServerAddrArg
Parameter name that is used to pass along the server address.
Definition: engine_grpc_config.h:32
PythonEngineGRPCLauncher
PythonEngineGRPCNRPClient::EngineLauncher< PythonGrpcConfigConst::EngineType > PythonEngineGRPCLauncher
Definition: python_engine_grpc_nrp_client.h:142
CREATE_NRP_ENGINE_LAUNCHER
CREATE_NRP_ENGINE_LAUNCHER(PythonEngineGRPCLauncher)
PythonEngineGRPCNRPClient::PythonEngineGRPCNRPClient
PythonEngineGRPCNRPClient(nlohmann::json &config, ProcessLauncherInterface::unique_ptr &&launcher)
Definition: python_engine_grpc_nrp_client.h:53
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
PythonEngineGRPCNRPClient::engineProcStartParams
virtual const std::vector< std::string > engineProcStartParams() const override
Get all Engine Process Startup parameters.
Definition: python_engine_grpc_nrp_client.h:120
python_grpc_config.h
PythonEngineGRPCNRPClient::shutdown
virtual void shutdown() override
Shutdown engine.
Definition: python_engine_grpc_nrp_client.h:113
EngineGrpcClient< PythonEngineGRPCNRPClient, PythonGrpcConfigConst::EngineSchema >::sendResetCommand
void sendResetCommand()
Definition: engine_grpc_client.h:157
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
PythonEngineGRPCNRPClient::~PythonEngineGRPCNRPClient
virtual ~PythonEngineGRPCNRPClient() override
Definition: python_engine_grpc_nrp_client.h:61
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