From 0ba773d913450f88ff7bcf54a39e0c8a8c4d7d64 Mon Sep 17 00:00:00 2001
From: Loic Guegan <manzerbredes@mailbox.org>
Date: Thu, 6 May 2021 17:46:34 +0200
Subject: [PATCH] Refactoring and start extended version

---
 inputs.json                    | 39 ++++++++++++++++++----------------
 src/{inputs.cc => Inputs.cc}   | 32 ++++++++++++++++------------
 src/{inputs.hpp => Inputs.hpp} |  9 ++++----
 src/simulator.cc               | 15 +++++++------
 4 files changed, 52 insertions(+), 43 deletions(-)
 rename src/{inputs.cc => Inputs.cc} (85%)
 rename src/{inputs.hpp => Inputs.hpp} (93%)

diff --git a/inputs.json b/inputs.json
index f391e00..82ad885 100644
--- a/inputs.json
+++ b/inputs.json
@@ -1,20 +1,23 @@
 {
-    "on0":{
-        "is_sender": true,
-        "power_off": 0,
-        "power_on":10,
-        "use_hint": true,
-        "wake_ts": [ 1, 7, 7 ],
-        "wake_duration": [ 5, 1, 2],
-        "data_size": 50
-    },
-    "on1":{
-        "is_sender": false,
-        "power_off": 0,
-        "power_on":10,
-        "use_hint": false,
-        "wake_ts": [ 1, 7 ],
-        "wake_duration": [ 5, 1],
-        "data_size": 50
+    "extended":false,
+    "nodes":{
+        "on0":{
+            "is_sender": true,
+            "power_off": 0,
+            "power_on":10,
+            "use_hint": true,
+            "wake_ts": [ 1, 7, 7 ],
+            "wake_duration": [ 5, 1, 2],
+            "data_size": 50
+        },
+        "on1":{
+            "is_sender": false,
+            "power_off": 0,
+            "power_on":10,
+            "use_hint": false,
+            "wake_ts": [ 1, 7 ],
+            "wake_duration": [ 5, 1],
+            "data_size": 50
+        }
     }
-}
\ No newline at end of file
+}
diff --git a/src/inputs.cc b/src/Inputs.cc
similarity index 85%
rename from src/inputs.cc
rename to src/Inputs.cc
index 84acdb7..de81aec 100644
--- a/src/inputs.cc
+++ b/src/Inputs.cc
@@ -1,10 +1,9 @@
-#include "inputs.hpp"
-#include "xbt/log.h"
+#include "Inputs.hpp"
+
 #include <algorithm>
 #include <iostream>
 #include <fstream>
 
-
 Inputs::Inputs(std::string node_name){
     // Here we doing all the boring stuff
     FILE* input_file = fopen(INPUTS_FILE, "rb");
@@ -14,13 +13,18 @@ Inputs::Inputs(std::string node_name){
     fclose(input_file);
     
     // Init all variables
-    is_sender=d[node_name.c_str()]["is_sender"].GetBool();
-    use_hint=d[node_name.c_str()]["use_hint"].GetBool();
-    data_size=d[node_name.c_str()]["data_size"].GetInt();
-    for(auto& v:d[node_name.c_str()]["wake_ts"].GetArray()){
+    is_sender=d["nodes"][node_name.c_str()]["is_sender"].GetBool();
+    use_hint=d["nodes"][node_name.c_str()]["use_hint"].GetBool();
+    data_size=d["nodes"][node_name.c_str()]["data_size"].GetInt();
+    extended=d["extended"].GetBool();
+    
+    // Instantiate wake_ts
+    for(auto& v:d["nodes"][node_name.c_str()]["wake_ts"].GetArray()){
         wake_ts.push_back(v.GetDouble());
     }
-    for(auto& v:d[node_name.c_str()]["wake_duration"].GetArray()){
+
+    // Instantiate wake_duration
+    for(auto& v:d["nodes"][node_name.c_str()]["wake_duration"].GetArray()){
         wake_duration.push_back(v.GetDouble());
     }
     
@@ -130,6 +134,7 @@ void Inputs::AddEvent(double ts, double duration){
 }
 
 void Inputs::GeneratePlatform(std::string p){
+    
     // The boring stuff
     FILE* input_file = fopen(INPUTS_FILE, "rb");
     char input_file_buffer[JSON_BUFFER_SIZE];
@@ -137,7 +142,7 @@ void Inputs::GeneratePlatform(std::string p){
     rapidjson::Document d;
     d.ParseStream(is);
     fclose(input_file);
-
+    
     // Write platform file
     std::ofstream pf;
     pf.open (p);
@@ -145,13 +150,12 @@ void Inputs::GeneratePlatform(std::string p){
     pf << "<!DOCTYPE platform SYSTEM \"http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd\">\n";
     pf << "<platform version=\"4.1\">\n    <AS id=\"AS0\" routing=\"Cluster\">\n";
     pf << "    <link id=\"link\" bandwidth=\"1Mbps\" latency=\"0ms\" sharing_policy=\"SHARED\"></link>\n";
-    for (Value::ConstMemberIterator itr = d.MemberBegin(); itr != d.MemberEnd(); ++itr)
+    for (Value::ConstMemberIterator itr = d["nodes"].MemberBegin(); itr != d["nodes"].MemberEnd(); ++itr)
     {
         std::string name=itr->name.GetString();
-        double power_on=d[itr->name.GetString()]["power_on"].GetDouble();
-        double power_off=d[itr->name.GetString()]["power_off"].GetDouble();
-
-         //db=d[itr->name.GetString()]["wake_interval"].GetDouble();
+        double power_on=d["nodes"][itr->name.GetString()]["power_on"].GetDouble();
+        double power_off=d["nodes"][itr->name.GetString()]["power_off"].GetDouble();
+        // Create node
         pf << "    <host id=\""<<name<<"\" speed=\"100.0f,100.0f\" pstate=\"0\">\n";
         pf << "        <prop id=\"wattage_per_state\" value=\""<< power_off<<":"<<power_off<<", "<< power_on<<":"<<power_on<<"\" />\n";
         pf << "        <prop id=\"wattage_off\" value=\"0\" />\n    </host>\n";
diff --git a/src/inputs.hpp b/src/Inputs.hpp
similarity index 93%
rename from src/inputs.hpp
rename to src/Inputs.hpp
index 23ced49..e709d89 100644
--- a/src/inputs.hpp
+++ b/src/Inputs.hpp
@@ -1,11 +1,9 @@
-#include "rapidjson/document.h"
-#include "rapidjson/filereadstream.h"
+#include <rapidjson/document.h>
+#include <rapidjson/filereadstream.h>
+
 #include <cstdio>
 #include <string>
 #include <vector>
-#include <iostream>
-#include <algorithm>
-#include "xbt/log.h"
 #include <iomanip>
 
 #define INPUTS_FILE "inputs.json"
@@ -77,6 +75,7 @@ public:
     /// @brief These are public attributes, please take care they are fragile
     bool is_sender;
     bool use_hint;
+    bool extended;
     int data_size;
 
 };
\ No newline at end of file
diff --git a/src/simulator.cc b/src/simulator.cc
index 0bf2d2f..15d7f0d 100644
--- a/src/simulator.cc
+++ b/src/simulator.cc
@@ -1,11 +1,14 @@
-#include "simgrid/s4u.hpp"
+#include <simgrid/s4u.hpp>
 #include <simgrid/s4u/Mailbox.hpp>
+#include <simgrid/s4u/Host.hpp>
+#include <simgrid/plugins/energy.h>
+#include <xbt/log.h>
+
 #include <string>
 #include <sstream>
-#include "inputs.hpp"
-#include "simgrid/s4u/Host.hpp"
-#include "simgrid/plugins/energy.h"
-#include "xbt/log.h"
+
+#include "Inputs.hpp"
+
 
 #define PLATFORM_FILE "platform.xml"
 #define TURN_OFF() simgrid::s4u::this_actor::get_host()->set_pstate(0);
@@ -74,7 +77,7 @@ static void obs_node(std::vector<std::string> args) {
     Inputs i(selfName);
     simgrid::s4u::Mailbox *m = simgrid::s4u::Mailbox::by_name("medium");
     XBT_INFO("Deploying observation node %s",selfName.c_str());
-
+    
     // Init convenient variables
     bool isSender=i.is_sender;
     bool useHint=i.use_hint;