NRP Core  1.4.1
output_node.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 OUTPUT_SPINNAKER_NODE_H
23 #define OUTPUT_SPINNAKER_NODE_H
24 
25 #include <nlohmann/json.hpp>
26 
29 
31 
35 class OutputSpinnakerNode : public OutputNode<nlohmann::json> {
36 public:
37 
41  OutputSpinnakerNode(const std::string &id, const std::string &label,
42  bool publishFromCache = false,
43  unsigned int computePeriod = 1) :
45  _label(label)
46  { }
47 
48  std::string typeStr() const override
49  { return "ToSpinnaker"; }
50 
52  {
54  if (spProxy)
55  {
56  spProxy->stopSpinnaker();
57  }
58  }
59 
60  void configure() override
61  {
63 
65  if(!spProxy) {
67  spProxy = &(NRPSpinnakerProxy::getInstance());
68  }
69 
70  spProxy->addSender(_label);
71  }
72 
73 protected:
74 
75  void sendSingleMsg(const std::string& /*id*/, const nlohmann::json* data) override
76  {
78  spProxy->startSpinnaker();
79  spProxy->send(_label, data);
80  }
81 
82  void sendBatchMsg(const std::string& /*id*/, const std::vector<const nlohmann::json*>& /*data*/) override
83  {
84  throw NRPException::logCreate("BATCH update policy is not supported in OutputROSEdge");
85  }
86 
87 private:
88 
89  std::string _label;
90 
91 };
92 
93 class OutputSpinnakerEdge : public SimpleOutputEdge<nlohmann::json, OutputSpinnakerNode> {
94 
95 public:
96 
97  OutputSpinnakerEdge(const std::string &keyword, const std::string &label,
98  bool publishFromCache = false,
99  unsigned int computePeriod = 1) :
100  SimpleOutputEdge<nlohmann::json, OutputSpinnakerNode>(keyword, label+"_output", label,
101  publishFromCache, computePeriod),
102  _label(label)
103  {}
104 
105 protected:
106 
108  { return new OutputSpinnakerNode(this->_id, _label, this->_publishFromCache, this->_computePeriod); }
109 
110 private:
111 
112  std::string _label;
113 };
114 
115 
116 #endif //OUTPUT_SPINNAKER_NODE_H
OutputEdge::_computePeriod
unsigned int _computePeriod
Definition: output_edge.h:173
OutputSpinnakerNode::~OutputSpinnakerNode
~OutputSpinnakerNode()
Definition: output_node.h:51
OutputEdge::_publishFromCache
bool _publishFromCache
Definition: output_edge.h:172
NRPSpinnakerProxy::send
void send(std::string &label, const nlohmann::json *data)
Definition: spinnaker_proxy.h:198
OutputSpinnakerEdge::makeNewNode
OutputSpinnakerNode * makeNewNode() override
Definition: output_node.h:107
OutputSpinnakerNode::OutputSpinnakerNode
OutputSpinnakerNode(const std::string &id, const std::string &label, bool publishFromCache=false, unsigned int computePeriod=1)
Constructor.
Definition: output_node.h:41
OutputEdge::_id
std::string _id
Definition: output_edge.h:170
OutputNodePolicies
Definition: computational_node_policies.h:47
NRPSpinnakerProxy::startSpinnaker
void startSpinnaker()
Definition: spinnaker_proxy.h:74
NRPSpinnakerProxy::addSender
void addSender(std::string &label)
Definition: spinnaker_proxy.h:174
OutputNode< nlohmann::json >::publishFromCache
bool publishFromCache()
Definition: output_node.h:109
output_node.h
ComputationalNode::id
const std::string & id() const
Returns the node 'id'.
Definition: computational_node.h:57
OutputSpinnakerNode::sendSingleMsg
void sendSingleMsg(const std::string &, const nlohmann::json *data) override
Sends out a single msg, to be implemented by derived classes.
Definition: output_node.h:75
OutputEdge
Helper class used to implement Python output edge decorators.
Definition: output_edge.h:36
OutputNode
Implementation of an output node in the computation graph.
Definition: output_node.h:38
OutputSpinnakerNode::typeStr
std::string typeStr() const override
Returns the node 'type' as a string.
Definition: output_node.h:48
OutputSpinnakerNode::configure
void configure() override
Configures the node making it ready to execute 'compute'.
Definition: output_node.h:60
NRPSpinnakerProxy::getInstance
static NRPSpinnakerProxy & getInstance()
Get singleton instance of NRPSpinnakerProxy.
Definition: spinnaker_proxy.cpp:26
output_edge.h
OutputNodePolicies::SERIES
@ SERIES
Definition: computational_node_policies.h:51
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
spinnaker_proxy.h
NRPSpinnakerProxy::stopSpinnaker
void stopSpinnaker()
Definition: spinnaker_proxy.h:144
OutputSpinnakerEdge::OutputSpinnakerEdge
OutputSpinnakerEdge(const std::string &keyword, const std::string &label, bool publishFromCache=false, unsigned int computePeriod=1)
Definition: output_node.h:97
OutputSpinnakerEdge
Definition: output_node.h:93
NRPSpinnakerProxy
Definition: spinnaker_proxy.h:49
OutputNode::configure
void configure() override
Configures the node making it ready to execute 'compute'.
Definition: output_node.h:117
OutputNodePolicies::PublishFormatPolicy
PublishFormatPolicy
Defines how output nodes send stored msgs.
Definition: computational_node_policies.h:50
NRPSpinnakerProxy::resetInstance
static NRPSpinnakerProxy & resetInstance()
Reset singleton instance.
Definition: spinnaker_proxy.cpp:31
OutputSpinnakerNode
OutputSpinnakerNode.
Definition: output_node.h:35
OutputSpinnakerNode::sendBatchMsg
void sendBatchMsg(const std::string &, const std::vector< const nlohmann::json * > &) override
Sends out a vector of msg as a single batch, to be implemented by derived classes.
Definition: output_node.h:82
json
nlohmann::json json
Definition: engine_json_server.cpp:31