25#include "ns3/boolean.h"
26#include "ns3/fatal-error.h"
27#include "ns3/string.h"
35#include <mach-o/dyld.h>
38NS_LOG_COMPONENT_DEFINE(
"SatEnvVariables");
51 TypeId(
"ns3::SatEnvVariables")
53 .AddConstructor<SatEnvVariables>()
55 "CurrentWorkingDirectory",
56 "Current working directory for the simulator.",
60 .AddAttribute(
"PathToExecutable",
61 "Path to the simulator executable.",
65 .AddAttribute(
"DataPath",
66 "Path to the data folder.",
67 StringValue(
"contrib/satellite/data"),
70 .AddAttribute(
"SimulationCampaignName",
71 "Simulation campaign name. Affects the simulation folder.",
75 .AddAttribute(
"SimulationTag",
76 "Tag related to the current simulation.",
77 StringValue(
"default"),
80 .AddAttribute(
"EnableSimulationOutputOverwrite",
81 "Enable simulation output overwrite.",
85 .AddAttribute(
"EnableSimInfoOutput",
86 "Enable simulation information output.",
90 .AddAttribute(
"EnableSimInfoDiffOutput",
91 "Enable simulation information diff output.",
95 .AddAttribute(
"ExcludeSatelliteDataFolderFromSimInfoDiff",
96 "Exclude satellite data folder from the revision diff.",
99 MakeBooleanChecker());
121 NS_LOG_FUNCTION(
this);
127 NS_LOG_FUNCTION(
this);
129 Object::NotifyConstructionCompleted();
147 NS_LOG_FUNCTION(
this);
151 char currentWorkingDirectory[FILENAME_MAX] =
"";
153 if (!getcwd(currentWorkingDirectory,
sizeof(currentWorkingDirectory)))
155 NS_FATAL_ERROR(
"SatEnvVariables::SatEnvVariables - Could not determine current working "
158 currentWorkingDirectory[
sizeof(currentWorkingDirectory) - 1] =
'\0';
161 char pathToExecutable[FILENAME_MAX] =
"";
165 res = readlink(
"/proc/self/exe", pathToExecutable,
sizeof(pathToExecutable));
167 uint32_t size =
sizeof(pathToExecutable);
168 res = _NSGetExecutablePath(pathToExecutable, &size);
170 NS_FATAL_ERROR(
"SatEnvVariables::SatEnvVariables - Unknown compiler.");
175 "SatEnvVariables::SatEnvVariables - Could not determine the path to executable.");
177 pathToExecutable[
sizeof(pathToExecutable) - 1] =
'\0';
182 NS_FATAL_ERROR(
"No directory additional-input inside "
184 <<
". Make sure you executed 'git submodule update --init --recursive' "
185 "from satellite repository");
189 NS_FATAL_ERROR(
"No directory scenarios inside "
191 <<
". Make sure you executed 'git submodule update --init --recursive' "
192 "from satellite repository");
196 NS_FATAL_ERROR(
"No directory sgp4 inside "
198 <<
". Make sure you executed 'git submodule update --init --recursive' "
199 "from satellite repository");
219 NS_LOG_FUNCTION(
this);
233 NS_LOG_FUNCTION(
this);
241 NS_LOG_FUNCTION(
this);
249 NS_LOG_FUNCTION(
this);
262 NS_ASSERT_MSG(
IsValidDirectory(outputPath), outputPath <<
" is not a valid directory");
270 bool enableOutputOverwrite)
272 NS_LOG_FUNCTION(
this);
283 NS_LOG_FUNCTION(
this);
291 NS_LOG_FUNCTION(
this);
299 NS_LOG_INFO(
"Attribute string is empty, using detected working directory");
304 NS_LOG_INFO(
"Using attributed working directory");
312 NS_LOG_FUNCTION(
this);
319 NS_LOG_INFO(
"Attribute string is empty, using detected path to executable");
324 NS_LOG_INFO(
"Using attributed path to executable");
332 NS_LOG_FUNCTION(
this);
335 bool validDirectory =
false;
337 if (stat(path.c_str(), &st) == 0)
339 if (S_ISDIR(st.st_mode))
341 validDirectory =
true;
345 NS_LOG_INFO(
"" << path <<
" validity: " << validDirectory);
347 return validDirectory;
353 NS_LOG_FUNCTION(
this);
356 bool validFile = (stat(pathToFile.c_str(), &st) == 0);
358 NS_LOG_INFO(
"" << pathToFile <<
" validity: " << validFile);
366 NS_LOG_FUNCTION(
this);
374 NS_LOG_FUNCTION(
this);
377 bool directoryFound =
false;
379 NS_LOG_INFO(
"Initial path " << initialPath);
383 std::stringstream dataPath;
385 for (uint32_t j = 0; j < i; j++)
390 dataPath << initialPath;
392 NS_LOG_INFO(
"Checking " << dataPath.str());
396 NS_LOG_INFO(
"Data directory located in " << dataPath.str());
397 path = dataPath.str();
398 directoryFound =
true;
405 NS_FATAL_ERROR(
"SatEnvVariables::LocateDirectory - Directory not found within "
415 NS_LOG_FUNCTION(
this);
418 bool fileFound =
false;
420 NS_LOG_INFO(
"Initial path " << initialPath);
424 std::stringstream dataPath;
426 for (uint32_t j = 0; j < i; j++)
431 dataPath << initialPath;
433 NS_LOG_INFO(
"Checking " << dataPath.str());
437 NS_LOG_INFO(
"Data directory located in " << dataPath.str());
438 path = dataPath.str();
446 NS_FATAL_ERROR(
"SatEnvVariables::LocateFile - File not found within "
456 bool enableOutputOverwrite)
458 NS_LOG_FUNCTION(
this);
460 NS_LOG_INFO(
"Creating output directory");
463 std::string safetyTag =
"";
464 std::string outputPath =
"";
465 bool directoryExists =
false;
469 if (!campaignName.empty())
471 std::string tempString =
AddToPath(simRootPath, campaignName);
482 while (!directoryExists)
484 outputPath =
FormOutputPath(simRootPath, campaignName, simTag, safetyTag);
487 if ((!
IsValidDirectory(outputPath) && !enableOutputOverwrite) || enableOutputOverwrite)
490 directoryExists =
true;
497 NS_LOG_INFO(
"Directory " << outputPath <<
" exists, increasing safety number to "
500 std::stringstream ss;
502 safetyTag = ss.str();
505 directoryExists =
false;
516 std::string campaignName,
518 std::string safetyTag)
520 NS_LOG_FUNCTION(
this);
522 std::string outputPath =
"";
523 std::stringstream tempTag;
527 if (!safetyTag.empty())
529 tempTag << safetyTag;
532 outputPath =
AddToPath(outputPath, simRootPath);
533 outputPath =
AddToPath(outputPath, campaignName);
534 outputPath =
AddToPath(outputPath, tempTag.str());
536 NS_LOG_INFO(
"Formed path " + outputPath);
544 NS_LOG_FUNCTION(
this);
546 std::stringstream tempPath;
549 if (!stringToAdd.empty())
551 if (tempPath.str().empty())
553 tempPath << stringToAdd;
557 tempPath <<
"/" << stringToAdd;
560 return tempPath.str();
566 NS_LOG_FUNCTION(
this);
568 NS_LOG_INFO(
"Creating directory " + path);
570 mkdir(path.c_str(), 0777);
576 NS_LOG_FUNCTION(
this);
583 timeinfo = localtime(&rawtime);
585 strftime(buffer, 80,
"%d-%m-%Y %I:%M:%S", timeinfo);
586 std::string str(buffer);
588 NS_LOG_INFO(
"Date is " << str);
596 Ptr<SatOutputFileStreamStringContainer> outputContainer)
598 NS_LOG_FUNCTION(
this);
600 FILE* pipe = popen(command.c_str(),
"r");
603 std::string data =
"";
607 if (fgets(buffer, 1024, pipe) != NULL)
609 buffer[strlen(buffer) - 1] =
'\0';
611 outputContainer->AddToContainer(data);
622 NS_LOG_FUNCTION(
this);
626 std::ostringstream fileName;
627 fileName << dataPath <<
"/SimInfo.log";
628 Ptr<SatOutputFileStreamStringContainer> outputContainer =
629 CreateObject<SatOutputFileStreamStringContainer>(fileName.str().c_str(), std::ios::out);
631 std::ostringstream revisionCommand;
632 revisionCommand <<
"cd contrib/satellite" <<
" && git log -1 2>&1";
635 std::stringstream line1;
638 outputContainer->AddToContainer(line1.str());
640 outputContainer->WriteContainerToFile();
651 NS_LOG_FUNCTION(
this);
653 std::ostringstream fileName;
654 fileName << dataPath <<
"/SimDiff.log";
655 Ptr<SatOutputFileStreamStringContainer> outputContainer =
656 CreateObject<SatOutputFileStreamStringContainer>(fileName.str().c_str(), std::ios::out);
658 std::ostringstream diffCommand;
659 diffCommand <<
"cd contrib/satellite" <<
" && git diff" <<
" --ignore-all-space"
660 <<
" --ignore-space-change" <<
" --ignore-blank-lines";
665 diffCommand <<
" \":(exclude)" <<
m_dataPath <<
"\" ";
668 diffCommand <<
" 2>&1";
672 outputContainer->WriteContainerToFile();
Class for environmental variables.
uint32_t m_levelsToCheck
How many directory levels to check for data path.
std::string InitializeOutputFolders(std::string campaignName, std::string simTag, bool enableOutputOverwrite)
Function for forming the output path and directory structure.
void DumpRevisionDiff(std::string dataPath)
static Ptr< SatEnvVariables > GetInstance()
virtual void NotifyConstructionCompleted() override
Notifier called once the ObjectBase is fully constructed.
void DoInitialize()
Initialize class NOTICE: this function is meant to me used only in test cases, where issues with sing...
bool m_isInitialized
Flag for disposing and initializing.
std::string LocateFile(std::string initialPath)
Function for locating a specific file within the NS-3 simulator folder.
std::string GetPathToExecutable()
Function for getting the path to executable.
bool m_enableOutputOverwrite
Enable simulation output overwrite.
bool IsValidFile(std::string pathToFile)
Function for checking if the file exists.
std::string GetOutputPath()
Function for getting the simulation folder.
static Ptr< SatEnvVariables > m_instance
Instance of SatEnvVariables used for singleton template.
void CreateDirectory(std::string path)
Function for creating a directory.
void DumpSimulationInformation()
Function for outputting the most essential simulation specific environmental information.
SatEnvVariables()
Constructor.
std::string FormOutputPath(std::string simRootPath, std::string campaignName, std::string simTag, std::string safetyTag)
Function for forming the output path.
void SetOutputVariables(std::string campaignName, std::string simTag, bool enableOutputOverwrite)
Function for setting the output variables.
void SetCurrentWorkingDirectory(std::string currentWorkingDirectory)
Function for setting the path to current working directory.
bool m_excludeDataFolderFromDiff
Is data folder excluded from the simulation information diff.
std::string AddToPath(std::string path, std::string stringToAdd)
Function for forming the next level of a path.
std::string m_simRootPath
Path to the simulation output root folder.
std::string m_currentWorkingDirectoryFromAttribute
Path to current working directory (attribute value).
std::string m_pathToExecutable
Path to executable.
std::string GetCurrentWorkingDirectory()
Function for getting the path to current working directory.
~SatEnvVariables()
Destructor.
void SetPathToExecutable(std::string pathToExecutable)
Function for setting the path to executable.
std::string LocateDataDirectory()
Function for locating the data directory within the NS-3 simulator folder.
std::string m_dataPath
Default data path.
bool IsValidDirectory(std::string path)
Function for checking if the directory exists.
bool m_enableSimInfoOutput
Is simulation information output enabled.
bool m_enableSimInfoDiffOutput
Is simulation information diff output enabled.
std::string GetDataPath()
Function for locating the data folder.
std::string m_campaignName
Simulation campaign name.
void SetOutputPath(std::string outputPath)
Method for setting the simulation output path.
bool m_isOutputPathInitialized
Is output path initialized.
std::string m_pathToExecutableFromAttribute
Path to executable (attribute value).
std::string LocateDirectory(std::string initialPath)
Function for locating a specific directory within the NS-3 simulator folder.
std::string GetCurrentDateAndTime()
Returns current real world date and time.
void ExecuteCommandAndReadOutput(std::string command, Ptr< SatOutputFileStreamStringContainer > outputContainer)
Function for executing the command and inserting the output into a string container.
std::string m_simTag
Tag related to the current simulation.
void DoDispose()
Reset class NOTICE: this function is meant to me used only in test cases, where issues with singleton...
std::string m_outputPath
Result output path.
static TypeId GetTypeId(void)
NS-3 function for type id.
std::string m_currentWorkingDirectory
Path to current working directory.
SatArqSequenceNumber is handling the sequence numbers for the ARQ process.