NRP Core  1.4.1
json_datapack_controller.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 JSON_DATAPACK_CONTROLLER_H
23 #define JSON_DATAPACK_CONTROLLER_H
24 
27 
29 
30 #include <nlohmann/json.hpp>
31 #include <iostream>
32 
40 // TODO It seems that the metadata stored in _datapackId and the _data members are
41 // duplicated. Let's see if this can be improved.
42 
44  : public DataPackController<nlohmann::json>
45 {
46  public:
48  {
49  return this->_emptyDataPack;
50  }
51 
52  protected:
53 
58  _datapackId(datapackId)
59  {
60  if(datapackId.Type != JsonDataPack::getType())
61  {
62  throw NRPException::logCreate("DataPack type \"" + datapackId.Type + "\" cannot be handled by JsonDataPackController");
63  }
64 
65  this->_data[this->_datapackId.Name]["type" ] = this->_datapackId.Type;
66  this->_data[this->_datapackId.Name]["engine_name"] = this->_datapackId.EngineName;
67  this->_data[this->_datapackId.Name]["data" ] = nullptr;
68 
69  this->_emptyDataPack[this->_datapackId.Name]["type" ] = this->_datapackId.Type;
70  this->_emptyDataPack[this->_datapackId.Name]["engine_name"] = this->_datapackId.EngineName;
71  }
72 
76  // TODO The data could be swapped, but the reference received from the caller is const.
77  // It would require modifying of the DataPackController interface.
78  void setCachedData(const nlohmann::json &data)
79  {
80  // TODO Check name, type and engine name
81  // TODO Format of the received JSON data is different than the data cached by the controller
82  // (the <datapack_name> label is missing). Try to change that.
83  this->_data[this->_datapackId.Name]["data"] = data["data"];
84  }
85 
90  {
91  return &this->_data[this->_datapackId.Name].at("data");
92  }
93 
95 
104 };
105 
106 #endif // JSON_DATAPACK_CONTROLLER_H
107 
108 // EOF
DataPackIdentifier::Type
std::string Type
DataPack Type.
Definition: datapack_interface.h:54
datapack_interface.h
DataPack::getType
static std::string getType()
Returns type of the datapack class.
Definition: datapack.h:61
JsonDataPackController::setCachedData
void setCachedData(const nlohmann::json &data)
Sets the "data" part of the cached JSON object.
Definition: json_datapack_controller.h:78
DataPackIdentifier::Name
std::string Name
DataPack Name. Used by simulator to identify source/sink of datapack.
Definition: datapack_interface.h:44
JsonDataPackController::getCachedData
nlohmann::json * getCachedData()
Returns reference to the "data" part of the cached JSON object.
Definition: json_datapack_controller.h:89
DataPackController
Helper class to handle DataPacks on the Engine Server side.
Definition: datapack_controller.h:34
datapack_controller.h
JsonDataPackController
Base controller class for JSON datapacks.
Definition: json_datapack_controller.h:43
json_datapack.h
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
JsonDataPackController::JsonDataPackController
JsonDataPackController(const DataPackIdentifier &datapackId)
Constructor that should be called by the derived class.
Definition: json_datapack_controller.h:57
DataPackIdentifier
Identifies a single datapack.
Definition: datapack_interface.h:38
JsonDataPackController::_data
nlohmann::json _data
Cached incoming/outgoing data in JSON format.
Definition: json_datapack_controller.h:102
DataPackIdentifier::EngineName
std::string EngineName
Corresponding engine.
Definition: datapack_interface.h:49
JsonDataPackController::_emptyDataPack
nlohmann::json _emptyDataPack
Definition: json_datapack_controller.h:103
JsonDataPackController::getEmptyDataPack
const nlohmann::json & getEmptyDataPack() const
Definition: json_datapack_controller.h:47
json
nlohmann::json json
Definition: engine_json_server.cpp:31
JsonDataPackController::_datapackId
DataPackIdentifier _datapackId
Definition: json_datapack_controller.h:94