salsa  0.4.0
Node.cc
1 #include <Actor.hh>
2 #include <Node.hh>
3 #include <iostream>
4 namespace Salsa {
5 Node::Node(std::string newName, std::string uuid)
6 {
10 
11  mpNodeInfo->set_name(newName);
12  mpNodeInfo->set_uuid(uuid);
13 
14  SPD_TRACE("Constructing node name [{}] UUID [{}]", mpNodeInfo->name(), mpNodeInfo->uuid());
15 }
16 
18 {
22 
23  SPD_TRACE("### Destroy Node [{}] ###", mpNodeInfo->name());
24 
25  // for (auto p : mPublishers)
26  // {
27  // // p->publish("","", ""); // Eh, why?
28  // }
29  mPublishers.clear();
30 
31  delete mpNodeInfo;
32 }
33 
34 void Node::json(Json::Value & /*root*/)
35 {
39  return;
40 
41  // Alright people. This is the time I learned to pay attention the hard way. tl;dr I
42  // accidentally purged the whole base folder thinking it was build.
43  //
44  // And that, kids, is how I made a function meaningless with mere 4 chars...
45  /*
46  for (auto n : mChildNodes)
47  {
48  spdlog::get("console")->debug("Node::json() : name={} uuid={}", n->name(), n->uuid());
49 
50  if (!n->uuid().empty())
51  {
52  Json::Value jsonnode;
53  jsonnode["label"] = n->name();
54  jsonnode["id"] = n->uuid();
55  if (n->parent())
56  jsonnode["net"] = n->parent()->name();
57  bool found = false;
58  for (const Json::Value & node : root["nodes"])
59  {
60  if (node["label"].asString() == n->name())
61  found = true;
62  }
63  if (!found)
64  root["nodes"].append(jsonnode);
65 
66  auto p = n->parent();
67  if (p)
68  {
69  for (auto pn : p->nodes())
70  {
71  if (!n->uuid().compare(pn->uuid()))
72  continue;
73  Json::Value jsonlink;
74  jsonlink["source"] = n->uuid();
75  jsonlink["target"] = pn->uuid();
76  // jsonlink["sourcename"] = n->name();
77  // jsonlink["targetname"] = pn->name();
78  root["links"].append(jsonlink);
79  }
80  }
81  }
82 
83  n->json(root);
84  }
85  */
86 
87  // if (spdlog::get("console")->level() < 2 && !mpParent)
88  // std::cout << root << std::endl;
89 }
90 
91 void Node::print() const
92 {
96  std::shared_ptr<Node> pParent = nullptr;
97 
98  try {
99  pParent = static_cast<std::shared_ptr<Node>>(mpParent);
100  }
101  catch (std::bad_weak_ptr &) {
102  }
103 
104  SPD_TRACE("Node::print() : name [{}] nodes [{}] publishers [{}] this [{}] parent [{}]", mpNodeInfo->name(),
105  mChildNodes.size(), mPublishers.size(), reinterpret_cast<void const *>(this),
106  static_cast<void *>(pParent.get()));
107 
108  for (auto const & node : mChildNodes) {
109  node->print();
110  }
111 }
112 
113 std::shared_ptr<Node> Node::find(std::string whatName) const
114 {
118 
119  // We should start using algos...
120  // But that is really not necessary for like 10-20 nodes
121  for (auto node : mChildNodes) {
122  if (name() == whatName) {
123  return node;
124  }
125  }
126 
127  return nullptr;
128 }
129 
130 void Node::removeByUUID(std::string whatUUID)
131 {
135 
136  // I'm sure there's a more elegant way
137  // I don't have internet rn tho, so
138  // TODO
139  int iNode = 0;
140  for (auto node : mChildNodes) {
141  if (whatUUID == node->uuid()) {
142  // delete n; // Unnecessary. Smart pointers FTW
143  // Also, one very important concept: Delete only what you create
144  mChildNodes.erase(mChildNodes.begin() + iNode);
145  }
146  iNode++;
147  }
148 }
149 
151 {
155 
156  // Get to highest node in stack?
157  // TODO @mvala explanation?
158 
159  Node * pNode = this;
160  while (true) {
161  try {
162 
163  std::shared_ptr<Node> pParent = //
164  static_cast<std::shared_ptr<Node>>(pNode->parent());
165 
166  pNode = pParent.get();
167  }
168  catch (std::bad_weak_ptr &) {
169  break;
170  }
171  }
172 
173  if (pNode->publishers().empty()) {
174  SPD_TRACE("Node::publish() No publisher defined! Aborting publish()");
175  return;
176  }
177 
178  SPD_TRACE("Node::publish() Publishing from [{}] nPublishers [{}]", pNode->name(), mPublishers.size());
179 
180  Json::Value jsonData;
181  pNode->json(jsonData);
182  Json::StreamWriterBuilder builder;
183  builder.settings_["indentation"] = "";
184 
185  std::string outStr = Json::writeString(builder, jsonData);
186 
187  for (auto const & pub : pNode->publishers()) {
188  SPD_TRACE("Node::publish() name [{}] data [{}]", pNode->name(), outStr);
189  pub->publish(pNode->name(), outStr);
190  }
191 }
192 
193 } // namespace Salsa
std::shared_ptr< Node > find(std::string name) const
Find node by name.
Definition: Node.cc:113
std::vector< std::shared_ptr< Publisher > > mPublishers
List of publishers.
Definition: Node.hh:71
std::weak_ptr< Node > mpParent
Parent node.
Definition: Node.hh:69
Base Node class.
Definition: Node.hh:22
Node(std::string name="", std::string uuid="")
Definition: Node.cc:5
virtual void publish()
Definition: Node.cc:150
virtual void print() const
Definition: Node.cc:91
void removeByUUID(std::string uuid)
Remove node by uuid.
Definition: Node.cc:130
NodeInfo * mpNodeInfo
Node Info.
Definition: Node.hh:68
std::weak_ptr< Node > parent() const
Returns parent node.
Definition: Node.hh:37
std::string uuid() const
Returns node UUID.
Definition: Node.hh:35
virtual void json(Json::Value &root)
Definition: Node.cc:34
virtual ~Node()
Definition: Node.cc:17
std::vector< std::shared_ptr< Node > > mChildNodes
List of nodes.
Definition: Node.hh:70
std::vector< std::shared_ptr< Publisher > > publishers() const
Returns publishers.
Definition: Node.hh:63
std::string name() const
Returns node name.
Definition: Node.hh:33