25 #include "ns3/boolean.h"
26 #include "ns3/fatal-error.h"
27 #include "ns3/string.h"
31 #include <sys/types.h>
35 #include <mach-o/dyld.h>
38 NS_LOG_COMPONENT_DEFINE(
"SatEnvVariables");
43 NS_OBJECT_ENSURE_REGISTERED(SatEnvVariables);
49 TypeId(
"ns3::SatEnvVariables")
51 .AddConstructor<SatEnvVariables>()
53 "CurrentWorkingDirectory",
54 "Current working directory for the simulator.",
58 .AddAttribute(
"PathToExecutable",
59 "Path to the simulator executable.",
63 .AddAttribute(
"DataPath",
64 "Path to the data folder.",
65 StringValue(
"contrib/satellite/data"),
68 .AddAttribute(
"SimulationCampaignName",
69 "Simulation campaign name. Affects the simulation folder.",
73 .AddAttribute(
"SimulationTag",
74 "Tag related to the current simulation.",
75 StringValue(
"default"),
78 .AddAttribute(
"EnableSimulationOutputOverwrite",
79 "Enable simulation output overwrite.",
83 .AddAttribute(
"EnableSimInfoOutput",
84 "Enable simulation information output.",
88 .AddAttribute(
"EnableSimInfoDiffOutput",
89 "Enable simulation information diff output.",
93 .AddAttribute(
"ExcludeSatelliteDataFolderFromSimInfoDiff",
94 "Exclude satellite data folder from the revision diff.",
97 MakeBooleanChecker());
108 : m_currentWorkingDirectory(
""),
109 m_pathToExecutable(
""),
110 m_currentWorkingDirectoryFromAttribute(
""),
111 m_pathToExecutableFromAttribute(
""),
113 m_dataPath(
"contrib/satellite/data"),
116 m_simRootPath(
"contrib/satellite/data/sims"),
118 m_enableOutputOverwrite(true),
119 m_isOutputPathInitialized(false),
120 m_enableSimInfoOutput(true),
121 m_enableSimInfoDiffOutput(true),
122 m_excludeDataFolderFromDiff(true),
123 m_isInitialized(false)
125 NS_LOG_FUNCTION(
this);
130 ObjectBase::ConstructSelf(AttributeConstructionList());
138 NS_LOG_FUNCTION(
this);
142 char currentWorkingDirectory[FILENAME_MAX] =
"";
144 if (!getcwd(currentWorkingDirectory,
sizeof(currentWorkingDirectory)))
146 NS_FATAL_ERROR(
"SatEnvVariables::SatEnvVariables - Could not determine current working "
149 currentWorkingDirectory[
sizeof(currentWorkingDirectory) - 1] =
'\0';
152 char pathToExecutable[FILENAME_MAX] =
"";
156 res = readlink(
"/proc/self/exe", pathToExecutable,
sizeof(pathToExecutable));
158 uint32_t size =
sizeof(pathToExecutable);
159 res = _NSGetExecutablePath(pathToExecutable, &size);
161 NS_FATAL_ERROR(
"SatEnvVariables::SatEnvVariables - Unknown compiler.");
166 "SatEnvVariables::SatEnvVariables - Could not determine the path to executable.");
168 pathToExecutable[
sizeof(pathToExecutable) - 1] =
'\0';
173 NS_FATAL_ERROR(
"No directory additional-input inside "
175 <<
". Make sure you executed 'git submodule update --init --recursive' "
176 "from satellite repository");
180 NS_FATAL_ERROR(
"No directory scenarios inside "
182 <<
". Make sure you executed 'git submodule update --init --recursive' "
183 "from satellite repository");
187 NS_FATAL_ERROR(
"No directory sgp4 inside "
189 <<
". Make sure you executed 'git submodule update --init --recursive' "
190 "from satellite repository");
210 NS_LOG_FUNCTION(
this);
224 NS_LOG_FUNCTION(
this);
232 NS_LOG_FUNCTION(
this);
240 NS_LOG_FUNCTION(
this);
253 NS_ASSERT_MSG(
IsValidDirectory(outputPath), outputPath <<
" is not a valid directory");
261 bool enableOutputOverwrite)
263 NS_LOG_FUNCTION(
this);
274 NS_LOG_FUNCTION(
this);
282 NS_LOG_FUNCTION(
this);
290 NS_LOG_INFO(
"Attribute string is empty, using detected working directory");
295 NS_LOG_INFO(
"Using attributed working directory");
303 NS_LOG_FUNCTION(
this);
310 NS_LOG_INFO(
"Attribute string is empty, using detected path to executable");
315 NS_LOG_INFO(
"Using attributed path to executable");
323 NS_LOG_FUNCTION(
this);
326 bool validDirectory =
false;
328 if (stat(path.c_str(), &st) == 0)
330 if (S_ISDIR(st.st_mode))
332 validDirectory =
true;
336 NS_LOG_INFO(
"" << path <<
" validity: " << validDirectory);
338 return validDirectory;
344 NS_LOG_FUNCTION(
this);
347 bool validFile = (stat(pathToFile.c_str(), &st) == 0);
349 NS_LOG_INFO(
"" << pathToFile <<
" validity: " << validFile);
357 NS_LOG_FUNCTION(
this);
365 NS_LOG_FUNCTION(
this);
368 bool directoryFound =
false;
370 NS_LOG_INFO(
"Initial path " << initialPath);
374 std::stringstream dataPath;
376 for (uint32_t j = 0; j < i; j++)
381 dataPath << initialPath;
383 NS_LOG_INFO(
"Checking " << dataPath.str());
387 NS_LOG_INFO(
"Data directory located in " << dataPath.str());
388 path = dataPath.str();
389 directoryFound =
true;
396 NS_FATAL_ERROR(
"SatEnvVariables::LocateDirectory - Directory not found within "
406 NS_LOG_FUNCTION(
this);
409 bool fileFound =
false;
411 NS_LOG_INFO(
"Initial path " << initialPath);
415 std::stringstream dataPath;
417 for (uint32_t j = 0; j < i; j++)
422 dataPath << initialPath;
424 NS_LOG_INFO(
"Checking " << dataPath.str());
428 NS_LOG_INFO(
"Data directory located in " << dataPath.str());
429 path = dataPath.str();
437 NS_FATAL_ERROR(
"SatEnvVariables::LocateFile - File not found within "
447 bool enableOutputOverwrite)
449 NS_LOG_FUNCTION(
this);
451 NS_LOG_INFO(
"Creating output directory");
454 std::string safetyTag =
"";
455 std::string outputPath =
"";
456 bool directoryExists =
false;
460 if (!campaignName.empty())
462 std::string tempString =
AddToPath(simRootPath, campaignName);
473 while (!directoryExists)
475 outputPath =
FormOutputPath(simRootPath, campaignName, simTag, safetyTag);
478 if ((!
IsValidDirectory(outputPath) && !enableOutputOverwrite) || enableOutputOverwrite)
481 directoryExists =
true;
488 NS_LOG_INFO(
"Directory " << outputPath <<
" exists, increasing safety number to "
491 std::stringstream ss;
493 safetyTag = ss.str();
496 directoryExists =
false;
507 std::string campaignName,
509 std::string safetyTag)
511 NS_LOG_FUNCTION(
this);
513 std::string outputPath =
"";
514 std::stringstream tempTag;
518 if (!safetyTag.empty())
520 tempTag << safetyTag;
523 outputPath =
AddToPath(outputPath, simRootPath);
524 outputPath =
AddToPath(outputPath, campaignName);
525 outputPath =
AddToPath(outputPath, tempTag.str());
527 NS_LOG_INFO(
"Formed path " + outputPath);
535 NS_LOG_FUNCTION(
this);
537 std::stringstream tempPath;
540 if (!stringToAdd.empty())
542 if (tempPath.str().empty())
544 tempPath << stringToAdd;
548 tempPath <<
"/" << stringToAdd;
551 return tempPath.str();
557 NS_LOG_FUNCTION(
this);
559 NS_LOG_INFO(
"Creating directory " + path);
561 mkdir(path.c_str(), 0777);
567 NS_LOG_FUNCTION(
this);
574 timeinfo = localtime(&rawtime);
576 strftime(buffer, 80,
"%d-%m-%Y %I:%M:%S", timeinfo);
577 std::string str(buffer);
579 NS_LOG_INFO(
"Date is " << str);
587 Ptr<SatOutputFileStreamStringContainer> outputContainer)
589 NS_LOG_FUNCTION(
this);
591 FILE* pipe = popen(command.c_str(),
"r");
594 std::string data =
"";
598 if (fgets(buffer, 1024, pipe) != NULL)
600 buffer[strlen(buffer) - 1] =
'\0';
602 outputContainer->AddToContainer(data);
613 NS_LOG_FUNCTION(
this);
617 std::ostringstream fileName;
618 fileName << dataPath <<
"/SimInfo.log";
619 Ptr<SatOutputFileStreamStringContainer> outputContainer =
620 CreateObject<SatOutputFileStreamStringContainer>(fileName.str().c_str(), std::ios::out);
622 std::ostringstream revisionCommand;
623 revisionCommand <<
"cd contrib/satellite"
624 <<
" && git log -1 2>&1";
627 std::stringstream line1;
630 outputContainer->AddToContainer(line1.str());
632 outputContainer->WriteContainerToFile();
643 NS_LOG_FUNCTION(
this);
645 std::ostringstream fileName;
646 fileName << dataPath <<
"/SimDiff.log";
647 Ptr<SatOutputFileStreamStringContainer> outputContainer =
648 CreateObject<SatOutputFileStreamStringContainer>(fileName.str().c_str(), std::ios::out);
650 std::ostringstream diffCommand;
651 diffCommand <<
"cd contrib/satellite"
653 <<
" --ignore-all-space"
654 <<
" --ignore-space-change"
655 <<
" --ignore-blank-lines";
660 diffCommand <<
" \":(exclude)" <<
m_dataPath <<
"\" ";
663 diffCommand <<
" 2>&1";
667 outputContainer->WriteContainerToFile();
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)
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.
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.
virtual TypeId GetInstanceTypeId(void) const
Get the type ID of instance.
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.