use smart pointer to avoid memory issue

This commit is contained in:
faraphel 2024-12-23 11:09:33 +01:00
parent 9c413c0edd
commit a0060c8fa8
5 changed files with 21 additions and 19 deletions

View file

@ -23,8 +23,8 @@ std::string GnsProject::getUuid() const {
return this->uuid; return this->uuid;
} }
std::vector<GnsNode> GnsProject::getNodes() const { std::vector<std::shared_ptr<GnsNode>> GnsProject::getNodes() const {
std::vector<GnsNode> nodes; std::vector<std::shared_ptr<GnsNode>> nodes;
// request the API endpoint // request the API endpoint
cpr::Url endpoint = this->getApiBase() + "nodes"; cpr::Url endpoint = this->getApiBase() + "nodes";
@ -59,11 +59,11 @@ std::vector<GnsNode> GnsProject::getNodes() const {
} }
// save the node // save the node
nodes.emplace_back( nodes.emplace_back(std::make_shared<GnsNode>(
this, this,
node_data["node_id"], node_data["node_id"],
ports ports
); ));
} }
return nodes; return nodes;

View file

@ -1,4 +1,5 @@
#pragma once #pragma once
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@ -34,7 +35,7 @@ public:
* Get all the nodes of the project * Get all the nodes of the project
* @return all the nodes of the project * @return all the nodes of the project
*/ */
[[nodiscard]] std::vector<GnsNode> getNodes() const; [[nodiscard]] std::vector<std::shared_ptr<GnsNode>> getNodes() const;
private: private:
const GnsServer* server; const GnsServer* server;

View file

@ -17,8 +17,8 @@ std::string GnsServer::getApiBase() const {
return "http://" + this->host + ":" + std::to_string(this->port) + "/v2/"; return "http://" + this->host + ":" + std::to_string(this->port) + "/v2/";
} }
std::vector<GnsProject> GnsServer::getProjects() const { std::vector<std::shared_ptr<GnsProject>> GnsServer::getProjects() const {
std::vector<GnsProject> projects; std::vector<std::shared_ptr<GnsProject>> projects;
// request the API endpoint // request the API endpoint
cpr::Url endpoint = this->getApiBase() + "projects"; cpr::Url endpoint = this->getApiBase() + "projects";
@ -38,20 +38,20 @@ std::vector<GnsProject> GnsServer::getProjects() const {
// deserialize the projects // deserialize the projects
projects.reserve(data.size()); projects.reserve(data.size());
for (const auto& project_data : data) for (const auto& project_data : data)
projects.emplace_back( projects.emplace_back(std::make_shared<GnsProject>(
this, this,
project_data["project_id"] project_data["project_id"]
); ));
return projects; return projects;
} }
std::vector<GnsNode> GnsServer::getNodes() const { std::vector<std::shared_ptr<GnsNode>> GnsServer::getNodes() const {
std::vector<GnsNode> nodes; std::vector<std::shared_ptr<GnsNode>> nodes;
// get all the nodes from all the projects // get all the nodes from all the projects
for (const GnsProject& project : this->getProjects()) { for (const std::shared_ptr<GnsProject>& project : this->getProjects()) {
std::vector<GnsNode> project_nodes = project.getNodes(); std::vector<std::shared_ptr<GnsNode>> project_nodes = project->getNodes();
nodes.insert(nodes.end(), project_nodes.begin(), project_nodes.end()); nodes.insert(nodes.end(), project_nodes.begin(), project_nodes.end());
} }

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <string> #include <string>
#include <cstdint> #include <cstdint>
#include <memory>
#include <vector> #include <vector>
#include "GnsProject.hpp" #include "GnsProject.hpp"
@ -27,13 +28,13 @@ public:
* Get all the projects in the server * Get all the projects in the server
* @return all the projects in the server * @return all the projects in the server
*/ */
[[nodiscard]] std::vector<GnsProject> getProjects() const; [[nodiscard]] std::vector<std::shared_ptr<GnsProject>> getProjects() const;
/** /**
* Get all the nodes in the server * Get all the nodes in the server
* @return all the nodes in the server * @return all the nodes in the server
*/ */
[[nodiscard]] std::vector<GnsNode> getNodes() const; [[nodiscard]] std::vector<std::shared_ptr<GnsNode>> getNodes() const;
private: private:
std::string host; std::string host;

View file

@ -50,11 +50,11 @@ void packet_wol_handler(
// TODO(Faraphel): check the 16 repetitions of the source address ? // TODO(Faraphel): check the 16 repetitions of the source address ?
// find the machine with the mac address // find the machine with the mac address
for (const gns::GnsNode& node : gnsServer->getNodes()) for (const std::shared_ptr<gns::GnsNode>& node : gnsServer->getNodes())
for (const gns::GnsPort& port : node.getPorts()) for (const gns::GnsPort& port : node->getPorts())
if (port.mac_address == mac_address_target) { if (port.mac_address == mac_address_target) {
std::cout << "Matching node: " + node.getUuid() << std::endl; std::cout << "Matching node: " + node->getUuid() << std::endl;
node.start(); node->start();
std::cout << "Node started." << std::endl; std::cout << "Node started." << std::endl;
return; return;
} }