NRP Core  1.4.1
input_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 INPUT_SPINNAKER_NODE_H
23 #define INPUT_SPINNAKER_NODE_H
24 
25 #include <vector>
26 #include <nlohmann/json.hpp>
27 
30 
32 
33 
37 class InputSpinnakerNode : public InputNode<nlohmann::json>, public SpiNNakerJsonReceiveCallbackInterface {
38 public:
39 
43  InputSpinnakerNode(const std::string &id, const std::string &label) :
44  InputNode(id),
45  _label(label)
46  {}
47 
48  std::string typeStr() const override
49  { return "FromSpinnaker"; }
50 
52  {
54  if (spProxy)
55  {
56  spProxy->stopSpinnaker();
57  }
58  }
59 
60 
61 protected:
62 
63  void configure() override
64  {
66  if(!spProxy) {
68  spProxy = &(NRPSpinnakerProxy::getInstance());
69  }
70 
71  spProxy->addReceiver(_label, this);
72 
73  // TODO: no need to worry about this now, restore in final version
74  // reserves memory space for storing incoming msgs.
75 // _msgTemp.reserve(InputNode::_queueSize);
76 // _msgStore.reserve(InputNode::_queueSize);
77  }
78 
83  {
84  std::lock_guard<std::mutex> lock(_msgMutex);
85  _msgTemp.push_back(msg);
86  }
87 
88  bool updatePortData(const std::string& id) override
89  {
91  spProxy->startSpinnaker();
92  std::lock_guard<std::mutex> lock(_msgMutex);
93 
94  if(_msgTemp.size()) {
95  _msgStore.clear();
96  InputNode::_portMap.at(id).clear();
97 
98  for(auto & msg : _msgTemp) {
99  _msgStore.push_back(std::move(msg));
100  InputNode::_portMap.at(id).addMsg(&_msgStore.back());
101  }
102 
103  _msgTemp.clear();
104 
105  return true;
106  }
107 
108  return false;
109  }
110 
111 private:
112 
114  std::mutex _msgMutex;
116  std::vector<nlohmann::json> _msgTemp;
118  std::vector<nlohmann::json> _msgStore;
119 
120  std::string _label;
121 
122 
123 };
124 
125 
126 class InputSpinnakerEdge : public SimpleInputEdge<nlohmann::json, InputSpinnakerNode> {
127 
128 public:
129 
130  InputSpinnakerEdge(const std::string& keyword, const std::string& label,
131  InputNodePolicies::MsgPublishPolicy msgPublishPolicy,
132  InputNodePolicies::MsgCachePolicy msgCachePolicy) :
133  SimpleInputEdge<nlohmann::json, InputSpinnakerNode>(keyword, label+"_input", label, msgPublishPolicy, msgCachePolicy),
134  _label(label)
135  {}
136 
137 protected:
138 
140  { return new InputSpinnakerNode(this->_id, _label); }
141 
142 private:
143 
144  std::string _label;
145 };
146 
147 
148 #endif //INPUT_SPINNAKER_NODE_H
InputNodePolicies::MsgCachePolicy
MsgCachePolicy
Defines input node message cache behavior.
Definition: computational_node_policies.h:27
input_node.h
InputSpinnakerEdge::InputSpinnakerEdge
InputSpinnakerEdge(const std::string &keyword, const std::string &label, InputNodePolicies::MsgPublishPolicy msgPublishPolicy, InputNodePolicies::MsgCachePolicy msgCachePolicy)
Definition: input_node.h:130
InputSpinnakerEdge
Definition: input_node.h:126
InputNode
Implementation of an input node in the computation graph.
Definition: input_node.h:129
InputNode::_portMap
std::map< std::string, DataPortHandle< DATA > > _portMap
Map containing data to handle topics. Data is guaranteed to be unchanged between 'compute' calls
Definition: input_node.h:219
InputSpinnakerNode::InputSpinnakerNode
InputSpinnakerNode(const std::string &id, const std::string &label)
Constructor.
Definition: input_node.h:43
NRPSpinnakerProxy::startSpinnaker
void startSpinnaker()
Definition: spinnaker_proxy.h:74
ComputationalNode::id
const std::string & id() const
Returns the node 'id'.
Definition: computational_node.h:57
InputSpinnakerNode::new_msg_callback
void new_msg_callback(nlohmann::json msg)
Called when raw UDP data is received.
Definition: input_node.h:82
SpiNNakerJsonReceiveCallbackInterface
Definition: spinnaker_proxy.h:43
InputEdge::_id
std::string _id
Definition: input_edge.h:151
InputSpinnakerNode
InputSpinnakerNode.
Definition: input_node.h:37
NRPSpinnakerProxy::getInstance
static NRPSpinnakerProxy & getInstance()
Get singleton instance of NRPSpinnakerProxy.
Definition: spinnaker_proxy.cpp:26
InputSpinnakerNode::configure
void configure() override
Configures the node making it ready to execute 'compute'.
Definition: input_node.h:63
spinnaker_proxy.h
NRPSpinnakerProxy::stopSpinnaker
void stopSpinnaker()
Definition: spinnaker_proxy.h:144
NRPSpinnakerProxy::addReceiver
void addReceiver(std::string &label, SpiNNakerJsonReceiveCallbackInterface *callback)
Definition: spinnaker_proxy.h:185
input_edge.h
InputSpinnakerNode::typeStr
std::string typeStr() const override
Returns the node 'type' as a string.
Definition: input_node.h:48
InputNodePolicies::MsgPublishPolicy
MsgPublishPolicy
Defines how an input node publish stored msgs.
Definition: computational_node_policies.h:33
NRPSpinnakerProxy
Definition: spinnaker_proxy.h:49
InputSpinnakerNode::~InputSpinnakerNode
~InputSpinnakerNode()
Definition: input_node.h:51
InputEdge
Helper template class used to implement Python input edge decorators.
Definition: input_edge.h:38
InputSpinnakerNode::updatePortData
bool updatePortData(const std::string &id) override
Updates pointers stored in _portMap for port 'id'.
Definition: input_node.h:88
NRPSpinnakerProxy::resetInstance
static NRPSpinnakerProxy & resetInstance()
Reset singleton instance.
Definition: spinnaker_proxy.cpp:31
InputSpinnakerEdge::makeNewNode
InputSpinnakerNode * makeNewNode() override
Definition: input_node.h:139
json
nlohmann::json json
Definition: engine_json_server.cpp:31