NRP Core  1.4.1
engine_json_server.h
Go to the documentation of this file.
1 /* * NRP Core - Backend infrastructure to synchronize simulations
2  *
3  * Copyright 2020-2023 NRP Team
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * This project has received funding from the European Union’s Horizon 2020
18  * Framework Programme for Research and Innovation under the Specific Grant
19  * Agreement No. 945539 (Human Brain Project SGA3).
20  */
21 
22 #ifndef ENGINE_JSON_SERVER_H
23 #define ENGINE_JSON_SERVER_H
24 
27 
31 
32 #include <map>
33 #include <memory>
34 #include <pistache/router.h>
35 #include <pistache/endpoint.h>
36 #include <nlohmann/json.hpp>
37 
42 {
46  static constexpr auto ShutdownWaitTime = std::chrono::milliseconds(10*1000);
47 
48  protected:
53  static constexpr std::string_view ResetRoute = EngineJSONConfigConst::EngineServerResetRoute;
55 
56  public:
57  using mutex_t = std::timed_mutex;
58  using lock_t = std::unique_lock<EngineJSONServer::mutex_t>;
59 
66  EngineJSONServer(const std::string &engineAddress, const std::string &engineName, const std::string &clientAddress);
67 
71  EngineJSONServer() = delete;
72 
73  virtual ~EngineJSONServer();
74 
75  // Delete copy mechanisms
76  EngineJSONServer(const EngineJSONServer&) = delete;
78 
79 
84  bool isServerRunning() const;
85 
89  void startServerAsync();
90 
94  void startServer();
95 
99  void shutdownServer();
100 
105  uint16_t serverPort() const;
106 
110  std::string serverAddress() const;
111 
117  void registerDataPack(const std::string &datapackName, JsonDataPackController *interface);
118 
125  void registerDataPackNoLock(const std::string &datapackName, JsonDataPackController *interface);
126 
132  virtual SimulationTime runLoopStep(SimulationTime timeStep) = 0;
133 
140  virtual nlohmann::json initialize(const nlohmann::json &data, EngineJSONServer::lock_t &datapackLock) = 0;
141 
147  virtual nlohmann::json reset(EngineJSONServer::lock_t &datapackLock) = 0;
148 
154  virtual nlohmann::json shutdown(const nlohmann::json &data) = 0;
155 
160  bool shutdownFlag();
161 
162  protected:
167 
172 
173 
179  virtual nlohmann::json getDataPackData(const nlohmann::json &reqData);
180 
186  virtual void setDataPackData(const nlohmann::json &reqData);
187 
191  const std::string &getEngineName() { return _engineName; }
192 
193  private:
197  bool _serverRunning = false;
198 
202  bool _shutdownFlag = false;
203 
207  std::mutex _shutdown_mutex;
208 
212  std::string _serverAddress;
213 
217  Pistache::Rest::Router _router;
218 
225  std::string _engineName;
226 
227  using enpoint_ptr_t = std::unique_ptr<Pistache::Http::Endpoint>;
228 
232  enpoint_ptr_t _pEndpoint = nullptr;
233 
237  std::map<std::string, JsonDataPackController*> _datapacksControllers;
238 
239  NRPLogger _loggerCfg;
240 
244  static Pistache::Rest::Router setRoutes(EngineJSONServer *server);
245 
252  static nlohmann::json parseRequest(const Pistache::Rest::Request &req, Pistache::Http::ResponseWriter &res);
253 
259  void getDataPackDataHandler(const Pistache::Rest::Request &req, Pistache::Http::ResponseWriter res);
260 
266  void setDataPackProcessorr(const Pistache::Rest::Request &req, Pistache::Http::ResponseWriter res);
267 
273  static const std::string &getIteratorKey(const nlohmann::json::const_iterator &jsonIterator);
274 
280  void runLoopStepHandler(const Pistache::Rest::Request &req, Pistache::Http::ResponseWriter res);
281 
287  void initializeHandler(const Pistache::Rest::Request &req, Pistache::Http::ResponseWriter res);
288 
294  void resetHandler(const Pistache::Rest::Request &req, Pistache::Http::ResponseWriter res);
295 
301  void shutdownHandler(const Pistache::Rest::Request &req, Pistache::Http::ResponseWriter res);
302 
309  static Pistache::Http::Endpoint createEndpoint(std::string *engineAddress, const std::string &engineName);
310 
312  friend class TestEngineJSONServer;
313 };
314 
315 #endif // ENGINE_JSON_SERVER_H
engine_json_config.h
nrp_logger.h
EngineJSONServer::_datapackLock
mutex_t _datapackLock
Lock access to _datapacks to make execution thread-safe.
Definition: engine_json_server.h:166
EngineJSONServer::GetDataPackInformationRoute
static constexpr std::string_view GetDataPackInformationRoute
Definition: engine_json_server.h:49
python_grpc_engine.server
server
Definition: python_grpc_engine.py:67
EngineJSONServer::RunLoopStepRoute
static constexpr std::string_view RunLoopStepRoute
Definition: engine_json_server.h:51
EngineJSONServer::getDataPackData
virtual nlohmann::json getDataPackData(const nlohmann::json &reqData)
Retrieves datapack data. Takes an array of datapack names for which to get data.
Definition: engine_json_server.cpp:175
json_datapack_controller.h
EngineJSONServer::initialize
virtual nlohmann::json initialize(const nlohmann::json &data, EngineJSONServer::lock_t &datapackLock)=0
Engine Initialization routine.
EngineJSONServer::TestEngineJSONServer
friend class TestEngineJSONServer
Definition: engine_json_server.h:312
EngineJSONServer::startServer
void startServer()
Start the server synchronously.
Definition: engine_json_server.cpp:108
EngineJSONConfigConst::EngineServerResetRoute
static constexpr std::string_view EngineServerResetRoute
REST Server Route for engine reset.
Definition: engine_json_config.h:72
EngineJSONConfigConst::EngineServerInitializeRoute
static constexpr std::string_view EngineServerInitializeRoute
REST Server Route for engine initialization.
Definition: engine_json_config.h:67
EngineJSONServer::InitializeRoute
static constexpr std::string_view InitializeRoute
Definition: engine_json_server.h:52
EngineJSONServer::EngineJSONServerTest_Functions_Test
friend class EngineJSONServerTest_Functions_Test
Definition: engine_json_server.h:311
EngineJSONServer::registerDataPackNoLock
void registerDataPackNoLock(const std::string &datapackName, JsonDataPackController *interface)
Registers a datapack. Skips locking the mutex. Should only be used if thread-safe access to _datapack...
Definition: engine_json_server.cpp:159
EngineJSONServer::mutex_t
std::timed_mutex mutex_t
Definition: engine_json_server.h:57
EngineJSONConfigConst::EngineServerSetDataPacksRoute
static constexpr std::string_view EngineServerSetDataPacksRoute
REST Server Route to which to send datapack changes.
Definition: engine_json_config.h:57
EngineJSONServer::lock_t
std::unique_lock< EngineJSONServer::mutex_t > lock_t
Definition: engine_json_server.h:58
EngineJSONServer::EngineJSONServer
EngineJSONServer()=delete
No dummy servers without name and address.
EngineJSONServer::serverAddress
std::string serverAddress() const
Get server address.
Definition: engine_json_server.cpp:146
EngineJSONServer::setDataPackData
virtual void setDataPackData(const nlohmann::json &reqData)
Set datapack data.
Definition: engine_json_server.cpp:209
JsonDataPackController
Base controller class for JSON datapacks.
Definition: json_datapack_controller.h:43
NRPLogger
NRP Logging functions.
Definition: nrp_logger.h:45
EngineJSONServer::shutdown
virtual nlohmann::json shutdown(const nlohmann::json &data)=0
Engine Shutdown routine.
EngineJSONServer
Manages communication with the NRP. Uses a REST server to send/receive data. Singleton class.
Definition: engine_json_server.h:41
datapack.h
EngineJSONServer::registerDataPack
void registerDataPack(const std::string &datapackName, JsonDataPackController *interface)
Registers a datapack.
Definition: engine_json_server.cpp:151
EngineJSONServer::SetDataPackRoute
static constexpr std::string_view SetDataPackRoute
Definition: engine_json_server.h:50
time_utils.h
EngineJSONServer::operator=
EngineJSONServer & operator=(const EngineJSONServer &)=delete
EngineJSONServer::startServerAsync
void startServerAsync()
Start the server in asynchronous mode.
Definition: engine_json_server.cpp:96
EngineJSONServer::~EngineJSONServer
virtual ~EngineJSONServer()
Definition: engine_json_server.cpp:84
EngineJSONConfigConst::EngineServerRunLoopStepRoute
static constexpr std::string_view EngineServerRunLoopStepRoute
REST Server Route to execute a single loop.
Definition: engine_json_config.h:62
EngineJSONServer::isServerRunning
bool isServerRunning() const
Is the server running?
Definition: engine_json_server.cpp:91
EngineJSONConfigConst::EngineServerShutdownRoute
static constexpr std::string_view EngineServerShutdownRoute
REST Server Route for engine shutdown.
Definition: engine_json_config.h:77
EngineJSONServer::ResetRoute
static constexpr std::string_view ResetRoute
Definition: engine_json_server.h:53
EngineJSONServer::serverPort
uint16_t serverPort() const
Get running server port.
Definition: engine_json_server.cpp:138
EngineJSONServer::clearRegisteredDataPacks
void clearRegisteredDataPacks()
Remove all registered datapacks.
Definition: engine_json_server.cpp:166
EngineJSONServer::shutdownServer
void shutdownServer()
Stop running server.
Definition: engine_json_server.cpp:123
EngineJSONConfigConst::EngineServerGetDataPacksRoute
static constexpr std::string_view EngineServerGetDataPacksRoute
REST Server Route from which to get datapack information.
Definition: engine_json_config.h:52
EngineJSONServer::ShutdownRoute
static constexpr std::string_view ShutdownRoute
Definition: engine_json_server.h:54
EngineJSONServer::shutdownFlag
bool shutdownFlag()
Has a shutdown command been received?
Definition: engine_json_server.cpp:232
SimulationTime
std::chrono::nanoseconds SimulationTime
Definition: time_utils.h:31
EngineJSONServer::runLoopStep
virtual SimulationTime runLoopStep(SimulationTime timeStep)=0
Run a single loop step.
EngineJSONServer::reset
virtual nlohmann::json reset(EngineJSONServer::lock_t &datapackLock)=0
Engine reset routine.
EngineJSONServer::getEngineName
const std::string & getEngineName()
Get the Engine name.
Definition: engine_json_server.h:191
json
nlohmann::json json
Definition: engine_json_server.cpp:31