LCOV - code coverage report
Current view: top level - builds/DaniloBorquez/flex-net-sim/src - controller.cpp (source / functions) Hit Total Coverage
Test: commit SHA1 Lines: 84 172 48.8 %
Date: 2025-08-25 21:30:02 Functions: 12 21 57.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #include "controller.hpp"
       2             : #include "defragmentator.hpp"
       3             : #include "unique_ptr.hpp"
       4             : 
       5             : #include <fstream>
       6             : #include <memory>
       7             : 
       8          34 : Controller::Controller() {
       9          34 :   this->connections = std::vector<Connection>();
      10          34 :   this->network = nullptr;
      11          34 :   this->allocator = create_unique<Allocator>();
      12          34 :   this->path = std::make_shared<Paths>();
      13          34 :   this->unassignConnection = &Controller::unassignConnectionEON;
      14          34 :   this->assignConnection = &Controller::assignConnectionEON;
      15          34 : };
      16             : 
      17           2 : Controller::Controller(std::shared_ptr<Network> network) {
      18           2 :   this->network = network;
      19           2 :   this->path = std::make_shared<Paths>();
      20           2 :   this->connections = std::vector<Connection>();
      21           2 :   this->allocator = create_unique<Allocator>();
      22           2 :   this->unassignConnection = &Controller::unassignConnectionEON;
      23           2 :   this->assignConnection = &Controller::assignConnectionEON;
      24           2 : };
      25             : 
      26       53414 : allocationStatus Controller::assignConnectionEON(int src, int dst,
      27             :                                                  std::shared_ptr<const BitRate> bitRate,
      28             :                                                  long long idConnection,
      29             :                                                  double time) {
      30      106828 :   Connection con = Connection(idConnection, time, bitRate);
      31       53414 :   std::unique_ptr<Defragmentator>& defrag = this->allocator->getDefragmentator();
      32       53414 :   if (defrag != nullptr && this->allocator->callDefragBefore == true) {
      33             :     // Deep copy of the network to commit changes instantly
      34           0 :     std::shared_ptr<Network> currentNetwork = this->getNetwork();
      35           0 :     std::shared_ptr<Network> newNetwork = std::make_shared<Network>(*currentNetwork, currentNetwork->getNetworkType());
      36           0 :     defrag->setNetwork(newNetwork);
      37           0 :     defrag->connections = this->connections;
      38             : 
      39           0 :     defrag->exec(this->connections);
      40           0 :     std::shared_ptr<Network> oldNetwork = this->network;
      41           0 :     this->setNetwork(defrag->network);
      42           0 :     this->allocator->setNetwork(defrag->network);
      43           0 :     this->connections = defrag->connections;
      44             :     // delete oldNetwork;
      45             :   }
      46       53416 :   this->rtnAllocation = this->allocator->exec(src, dst, *bitRate, con);
      47       53412 :   if (this->rtnAllocation == ALLOCATED) {
      48       53118 :     this->connections.push_back(con);
      49      153773 :     for (unsigned int j = 0; j < con.links.size(); j++) {
      50     2608234 :       for (unsigned int k = 0; k < con.slots[j].size(); k++) {
      51     2507579 :         this->network->useSlot(con.links[j], con.slots[j][k]);
      52             :       }
      53             :     }
      54             :   }
      55       53412 :   if (this->allocator->callDefragBefore != true && defrag != nullptr) {
      56             :     // Deep copy of the network to commit changes instantly
      57           0 :     std::shared_ptr<Network> currentNetwork = this->getNetwork();
      58           0 :     std::shared_ptr<Network> newNetwork = std::make_shared<Network>(*currentNetwork, currentNetwork->getNetworkType());
      59           0 :     defrag->setNetwork(newNetwork);
      60           0 :     defrag->connections = this->connections;
      61             : 
      62           0 :     defrag->exec(this->connections);
      63           0 :     std::shared_ptr<Network> oldNetwork = this->network;
      64           0 :     this->setNetwork(defrag->network);
      65           0 :     this->allocator->setNetwork(defrag->network);
      66           0 :     this->connections = defrag->connections;
      67             :     // delete oldNetwork;
      68             :   }
      69      106824 :   return this->rtnAllocation;
      70             : }
      71             : 
      72       53103 : int Controller::unassignConnectionEON(long long idConnection, double time) {
      73       61780 :   for (unsigned int i = 0; i < this->connections.size(); i++) {
      74       61780 :     if (this->connections[i].id == idConnection) {
      75      153724 :       for (unsigned int j = 0; j < this->connections[i].links.size(); j++) {
      76     2607835 :         for (unsigned int k = 0; k < this->connections[i].slots[j].size();
      77             :              k++) {
      78     5014428 :           this->network->unuseSlot(this->connections[i].links[j],
      79     2507214 :                                    this->connections[i].slots[j][k]);
      80             :         }
      81             :       }
      82       53103 :       this->connections.erase(this->connections.begin() + i);
      83       53103 :       break;
      84             :     }
      85             :   }
      86       53103 :   return 0;
      87             : }
      88           0 : allocationStatus Controller::assignConnectionMB(int src, int dst, std::shared_ptr<const BitRate> bitRate,
      89             :                                               long long idConnection,
      90             :                                               double time) {
      91           0 :   Connection con = Connection(idConnection, time, bitRate);
      92           0 :   this->rtnAllocation = this->allocator->exec(src, dst, *bitRate, con);
      93           0 :   if (this->rtnAllocation == ALLOCATED) {
      94           0 :     this->connections.push_back(con);
      95           0 :     for (unsigned int j = 0; j < con.links.size(); j++) {
      96           0 :       for (const auto& b : con.bandsSlots) {
      97           0 :         for (unsigned int k = 0; k < con.bandsSlots[b.first][j].size(); k++) {
      98           0 :           this->network->useSlot(con.links[j], b.first, con.bandsSlots[b.first][j][k]);
      99             :         }
     100             : 
     101             :       }
     102             : 
     103             :     }
     104             :   }
     105           0 :   return this->rtnAllocation;
     106             : }
     107             : 
     108           0 : int Controller::unassignConnectionMB(long long idConnection, double time) {
     109           0 :   for (unsigned int i = 0; i < this->connections.size(); i++) {
     110           0 :     if (this->connections[i].id == idConnection) {
     111           0 :       for (unsigned int j = 0; j < this->connections[i].links.size(); j++) {
     112           0 :         for (const auto& b : connections[i].bandsSlots) {
     113           0 :           for (unsigned int k = 0; k < this->connections[i].bandsSlots[b.first][j].size();
     114             :              k++) {
     115           0 :           this->network->unuseSlot(this->connections[i].links[j],b.first,
     116           0 :                                    this->connections[i].bandsSlots[b.first][j][k]);
     117             :           }
     118             :         }
     119             :       }
     120           0 :       this->connections.erase(this->connections.begin() + i);
     121           0 :       break;
     122             :     }
     123             :   }
     124           0 :   return 0;
     125             : }
     126             : 
     127           0 : allocationStatus Controller::assignConnectionSDM(int src, int dst, std::shared_ptr<const BitRate> bitRate,
     128             :                                               long long idConnection,
     129             :                                               double time) {
     130           0 :   Connection con = Connection(idConnection, time, bitRate);
     131           0 :   this->rtnAllocation = this->allocator->exec(src, dst, *bitRate, con);
     132           0 :   if (this->rtnAllocation == ALLOCATED) {
     133           0 :     this->connections.push_back(con);
     134           0 :     for (unsigned int j = 0; j < con.links.size(); j++) {
     135           0 :       for (unsigned int k = 0; k < con.slots[j].size(); k++) {
     136           0 :         this->network->useSlot(con.links[j], con.cores[j], con.modes[j], con.slots[j][k]);
     137             :       }
     138             :     }
     139             :   }
     140           0 :   return this->rtnAllocation;
     141             : }
     142             : 
     143           0 : int Controller::unassignConnectionSDM(long long idConnection, double time) {
     144           0 :   for (unsigned int i = 0; i < this->connections.size(); i++) {
     145           0 :     if (this->connections[i].id == idConnection) {
     146           0 :       for (unsigned int j = 0; j < this->connections[i].links.size(); j++) {
     147           0 :         for (unsigned int k = 0; k < this->connections[i].slots[j].size();
     148             :              k++) {
     149           0 :           this->network->unuseSlot(this->connections[i].links[j], this->connections[i].cores[j],
     150           0 :                                    this->connections[i].modes[j], this->connections[i].slots[j][k]);
     151             :         }
     152             :       }
     153           0 :       this->unassignCallback(this->connections[i], time, this->network);
     154           0 :       this->connections.erase(this->connections.begin() + i);
     155           0 :       break;
     156             :     }
     157             :   }
     158           0 :   return 0;
     159             : }
     160             : 
     161           0 : int Controller::unassignConnectionWCallback(long long idConnection,
     162             :                                             double time) {
     163           0 :   for (unsigned int i = 0; i < this->connections.size(); i++) {
     164           0 :     if (this->connections[i].id == idConnection) {
     165           0 :       for (unsigned int j = 0; j < this->connections[i].links.size(); j++) {
     166           0 :         for (unsigned int k = 0; k < this->connections[i].slots[j].size();
     167             :              k++) {
     168           0 :           this->network->unuseSlot(this->connections[i].links[j],
     169           0 :                                    this->connections[i].slots[j][k]);
     170             :         }
     171             :       }
     172           0 :       this->unassignCallback(this->connections[i], time, this->network);
     173           0 :       this->connections.erase(this->connections.begin() + i);
     174           0 :       break;
     175             :     }
     176             :   }
     177           0 :   return 0;
     178             : }
     179             : 
     180          16 : void Controller::setPaths(std::string filename) {
     181             :   // open JSON file
     182          32 :   std::ifstream file(filename);
     183          16 :   if (!file.is_open()) {
     184           0 :     throw std::runtime_error("Could not open file: " + filename);
     185             :   }
     186          32 :   nlohmann::json filePaths;
     187          16 :   file >> filePaths;
     188             : 
     189             :   int numberOfNodes;
     190          16 :   numberOfNodes = this->network->getNumberOfNodes();
     191             : 
     192             :   // allocate space for path[src]
     193          16 :   this->path->resize(numberOfNodes);
     194             : 
     195             :   // allocate space for path[src][dst]
     196         231 :   for (int t = 0; t < numberOfNodes; t++) {
     197         215 :     (*this->path)[t].resize(numberOfNodes);
     198             :   }
     199             : 
     200             :   int routesNumber;
     201          16 :   routesNumber = filePaths["routes"].size();
     202             : 
     203        2766 :   for (int i = 0; i < routesNumber; i++) {
     204             :     int pathsNumber;
     205        2750 :     pathsNumber = filePaths["routes"][i]["paths"].size();
     206             :     int src, dst;
     207        2750 :     src = filePaths["routes"][i]["src"];
     208        2750 :     dst = filePaths["routes"][i]["dst"];
     209             : 
     210             :     // allocate path[src][dst][pathsNumber]
     211             :     int linksInPath;
     212        2750 :     linksInPath = pathsNumber - 1;
     213             :     // because 3 nodes has 2 links
     214        2750 :     (*this->path)[src][dst].resize(pathsNumber);
     215             : 
     216             :     // go through available routes
     217       10210 :     for (int b = 0; b < pathsNumber; b++) {
     218             :       int nodesPathNumber;
     219        7460 :       nodesPathNumber = filePaths["routes"][i]["paths"][b].size();
     220        7460 :       int lastNode = nodesPathNumber - 1;
     221             : 
     222       32425 :       for (int c = 0; c < lastNode; c++) {
     223             :         int actNode, nextNode;
     224       24965 :         actNode = filePaths["routes"][i]["paths"][b][c];
     225       24965 :         nextNode = filePaths["routes"][i]["paths"][b][c + 1];
     226             : 
     227             :         int idLink;
     228       24965 :         idLink = this->network->isConnected(actNode, nextNode);
     229             : 
     230       24965 :         (*this->path)[src][dst][b].push_back(this->network->getLink(idLink));
     231             :       }
     232             :     }
     233             :   }
     234          16 : }
     235             : 
     236          16 : void Controller::setNetwork(std::shared_ptr<Network> network) {
     237          16 :   this->network = network;
     238          16 :   if (network->getNetworkType() == SDM) {
     239           1 :     this->setAssignSDM();
     240          15 :   } else if (network->getNetworkType() == MB) {
     241           0 :     this->setAssignMB();
     242             :   }
     243          16 : }
     244             : 
     245          68 : std::shared_ptr<Network> Controller::getNetwork(void) { return this->network; }
     246             : 
     247          14 : void Controller::setAllocator(std::unique_ptr<Allocator> allocator) {
     248          14 :   this->allocator = std::move(allocator);
     249          14 : }
     250             : 
     251          12 : std::unique_ptr<Allocator> &Controller::getAllocator(void) {
     252          12 :   if (this->allocator == nullptr) {
     253             :     throw std::runtime_error("Tried to get a nullptr Allocator. "
     254           0 :                              "Please set an Allocator before using it.");
     255             :   }
     256          12 :   return this->allocator;
     257             : }
     258             : 
     259          12 : std::unique_ptr<Defragmentator> &Controller::getDefragmentator(void) {
     260          12 :   if (this->allocator == nullptr) {
     261             :     throw std::runtime_error("Tried to get a nullptr Allocator. "
     262           0 :                              "Please set an Allocator before using it.");
     263             :   }
     264          12 :   return this->allocator->getDefragmentator();
     265             : }
     266             : 
     267             : std::shared_ptr<Paths>
     268          15 :     Controller::getPaths() {
     269          15 :   return this->path;
     270             : }
     271             : 
     272           0 : void Controller::setUnassignCallback(void (*callbackFunction)(Connection,
     273             :                                                               double,
     274             :                                                               std::shared_ptr<Network>)) {
     275           0 :   this->unassignConnection = &Controller::unassignConnectionWCallback;
     276           0 :   this->unassignCallback = callbackFunction;
     277           0 : }
     278             : 
     279             : // SDM
     280           1 : void Controller::setAssignSDM() {
     281           1 :   this->assignConnection = &Controller::assignConnectionSDM;
     282           1 : }
     283             : 
     284           0 : void Controller::setUnassignSDM(void (*callbackFunction)(Connection,
     285             :                                                               double,
     286             :                                                               std::shared_ptr<Network>)) {
     287           0 :   this->unassignConnection = &Controller::unassignConnectionSDM;
     288           0 :   this->unassignCallback = callbackFunction;
     289           0 : }
     290             : // MB
     291           0 : void Controller::setAssignMB() {
     292           0 :   this->assignConnection = &Controller::assignConnectionMB;
     293           0 : }
     294             : 
     295           0 : void Controller::setUnassignMB() {
     296           0 :   this->unassignConnection = &Controller::unassignConnectionMB;
     297           0 : }

Generated by: LCOV version 1.13