SOLA
Loading...
Searching...
No Matches
general_scenariofile.h
1// Copyright 2023 The SOLA authors
2//
3// This file is part of DAISI.
4//
5// DAISI is free software: you can redistribute it and/or modify it under the terms of the GNU
6// General Public License as published by the Free Software Foundation; version 2.
7//
8// DAISI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
9// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
10// Public License for more details.
11//
12// You should have received a copy of the GNU General Public License along with DAISI. If not, see
13// <https://www.gnu.org/licenses/>.
14//
15// SPDX-License-Identifier: GPL-2.0-only
16
17#ifndef DAISI_MANAGER_GENERAL_SCENARIOFILE_H_
18#define DAISI_MANAGER_GENERAL_SCENARIOFILE_H_
19
20#include <fstream>
21#include <string>
22
23#include "ns3/core-module.h"
24#include "scenariofile_component.h"
25
26namespace daisi {
27
29 explicit GeneralScenariofile(std::string path_to_file) : file_path_(std::move(path_to_file)) {
30 loadFileContent();
31 node = YAML::Load(file_content_);
32
33 SERIALIZE_VAR(title);
34 SERIALIZE_VAR(version);
35
36 SERIALIZE_VAR(random_seed);
37
38 SERIALIZE_NS3_TIME(stop_time);
39 SERIALIZE_NS3_TIME(default_delay);
40
41 SERIALIZE_VAR(output_path);
42 }
43
44 std::string getOutputPath() const {
45 // output_path from yaml takes precedence over environment variable DAISI_OUTPUT_PATH
46
47 if (output_path.has_value() && !output_path.value().empty()) {
48 return pathWithEndingSlash(output_path.value());
49 }
50
51 std::string output_path_env = std::getenv("DAISI_OUTPUT_PATH");
52 if (!output_path_env.empty()) return pathWithEndingSlash(output_path_env);
53
54 throw std::runtime_error("No output path set!");
55 }
56
57 // Add slash at end of path if missing
58 static std::string pathWithEndingSlash(std::string path) {
59 if (path.empty()) throw std::runtime_error("cannot modify empty path");
60 if (path.at(path.size() - 1) != '/') path.append("/");
61 return path;
62 }
63
64 // header
65 std::string title;
66 std::string version;
67
68 // setup information
69 uint64_t random_seed = 0;
70
71 // simulation information
72 ns3::Time stop_time;
73 ns3::Time default_delay;
74
75 std::optional<std::string> output_path;
76
77 YAML::Node node;
78
79 std::string getFileContent() const { return file_content_; }
80
81private:
82 void loadFileContent() {
83 std::fstream fs;
84 fs.open(file_path_);
85
86 if (fs.fail()) {
87 file_content_ = file_path_;
88 } else {
89 std::ifstream t(file_path_);
90 std::string file_as_string((std::istreambuf_iterator<char>(t)),
91 std::istreambuf_iterator<char>());
92 file_content_ = file_as_string;
93 }
94 }
95
96 std::string file_content_;
97 std::string file_path_;
98};
99} // namespace daisi
100#endif
Definition general_scenariofile.h:28