salsa  0.4.0
 All Classes Functions Variables Typedefs Enumerations Pages
Node.cc
1 #include <iostream>
2 #include "Actor.hh"
3 #include "Node.hh"
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  spdlog::get("console")->debug("Node::json() : name={} uuid={}", n->name(), n->uuid());
48 
49  // if (!n->uuid().empty()) {
50  // Json::Value jsonnode;
51  // jsonnode["label"] = n->name();
52  // jsonnode["id"] = n->uuid();
53  // if (n->parent()) jsonnode["net"] = n->parent()->name();
54  // bool found = false;
55  // for (const Json::Value & node : root["nodes"]) {
56  // if (node["label"].asString() == n->name()) found = true;
57  // }
58  // if (!found) root["nodes"].append(jsonnode);
59 
60  // auto p = n->parent();
61  // if (p) {
62  // for (auto pn : p->nodes()) {
63  // if (!n->uuid().compare(pn->uuid())) continue;
64  // Json::Value jsonlink;
65  // jsonlink["source"] = n->uuid();
66  // jsonlink["target"] = pn->uuid();
67  // // jsonlink["sourcename"] = n->name();
68  // // jsonlink["targetname"] = pn->name();
69  // root["links"].append(jsonlink);
70  // }
71  // }
72  // }
73 
74  n->json(root);
75  }
76 
77  // if (spdlog::get("console")->level() < 2 && !mpParent)
78  // std::cout << root << std::endl;
79 }
80 
81 void Node::print() const
82 {
86  std::shared_ptr<Node> pParent = nullptr;
87 
88  try {
89  pParent = static_cast<std::shared_ptr<Node>>(mpParent);
90  }
91  catch (std::bad_weak_ptr &) {
92  }
93 
94  SPD_TRACE("Node::print() : name [{}] nodes [{}] publishers [{}] this [{}] parent [{}]", mpNodeInfo->name(),
95  mChildNodes.size(), mPublishers.size(), reinterpret_cast<void const *>(this),
96  static_cast<void *>(pParent.get()));
97 
98  for (auto const & node : mChildNodes) {
99  node->print();
100  }
101 }
102 
103 std::shared_ptr<Node> Node::find(std::string whatName) const
104 {
108 
109  // We should start using algos...
110  // But that is really not necessary for like 10-20 nodes
111  for (auto node : mChildNodes) {
112  if (name() == whatName) {
113  return node;
114  }
115  }
116 
117  return nullptr;
118 }
119 
120 void Node::removeByUUID(std::string whatUUID)
121 {
125 
126  // I'm sure there's a more elegant way
127  // I don't have internet rn tho, so
128  // TODO
129  int iNode = 0;
130  for (auto node : mChildNodes) {
131  if (whatUUID == node->uuid()) {
132  // delete n; // Unnecessary. Smart pointers FTW
133  // Also, one very important concept: Delete only what you create
134  mChildNodes.erase(mChildNodes.begin() + iNode);
135  }
136  iNode++;
137  }
138 }
139 
141 {
145 
146  // Get to highest node in stack?
147  // TODO @mvala explanation?
148 
149  // We ignore it for now
150  return;
151 
152  // Node * pNode = this;
153  // while (true) {
154  // try {
155 
156  // std::shared_ptr<Node> pParent = //
157  // static_cast<std::shared_ptr<Node>>(pNode->parent());
158 
159  // pNode = pParent.get();
160  // }
161  // catch (std::bad_weak_ptr &) {
162  // break;
163  // }
164  // }
165 
166  // if (pNode->publishers().empty()) {
167  // SPD_TRACE("Node::publish() No publisher defined! Aborting publish()");
168  // return;
169  // }
170 
171  // SPD_TRACE("Node::publish() Publishing from [{}] nPublishers [{}]", pNode->name(), mPublishers.size());
172 
173  // Json::Value jsonData;
174  // pNode->json(jsonData);
175  // Json::StreamWriterBuilder builder;
176  // builder.settings_["indentation"] = "";
177 
178  // std::string outStr = Json::writeString(builder, jsonData);
179 
180  // for (auto const & pub : pNode->publishers()) {
181  // SPD_TRACE("Node::publish() name [{}] data [{}]", pNode->name(), outStr);
182  // pub->publish(pNode->name(), outStr);
183  // }
184 }
185 
186 } // namespace Salsa
std::shared_ptr< Node > find(std::string name) const
Find node by name.
Definition: Node.cc:103
std::vector< std::shared_ptr< Publisher > > mPublishers
List of publishers.
Definition: Node.hh:75
std::weak_ptr< Node > mpParent
Parent node.
Definition: Node.hh:73
Node(std::string name="", std::string uuid="")
Definition: Node.cc:5
virtual void publish()
Definition: Node.cc:140
void removeByUUID(std::string uuid)
Remove node by uuid.
Definition: Node.cc:120
NodeInfo * mpNodeInfo
Node Info.
Definition: Node.hh:72
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:74
virtual void print() const
Definition: Node.cc:81
std::string name() const
Returns node name.
Definition: Node.hh:33