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';
188 NS_LOG_FUNCTION(
this);
202 NS_LOG_FUNCTION(
this);
210 NS_LOG_FUNCTION(
this);
218 NS_LOG_FUNCTION(
this);
231 NS_ASSERT_MSG(
IsValidDirectory(outputPath), outputPath <<
" is not a valid directory");
239 bool enableOutputOverwrite)
241 NS_LOG_FUNCTION(
this);
252 NS_LOG_FUNCTION(
this);
260 NS_LOG_FUNCTION(
this);
268 NS_LOG_INFO(
"Attribute string is empty, using detected working directory");
273 NS_LOG_INFO(
"Using attributed working directory");
281 NS_LOG_FUNCTION(
this);
288 NS_LOG_INFO(
"Attribute string is empty, using detected path to executable");
293 NS_LOG_INFO(
"Using attributed path to executable");
301 NS_LOG_FUNCTION(
this);
304 bool validDirectory =
false;
306 if (stat(path.c_str(), &st) == 0)
308 if (S_ISDIR(st.st_mode))
310 validDirectory =
true;
314 NS_LOG_INFO(
"" << path <<
" validity: " << validDirectory);
316 return validDirectory;
322 NS_LOG_FUNCTION(
this);
325 bool validFile = (stat(pathToFile.c_str(), &st) == 0);
327 NS_LOG_INFO(
"" << pathToFile <<
" validity: " << validFile);
335 NS_LOG_FUNCTION(
this);
343 NS_LOG_FUNCTION(
this);
346 bool directoryFound =
false;
348 NS_LOG_INFO(
"Initial path " << initialPath);
352 std::stringstream dataPath;
354 for (uint32_t j = 0; j < i; j++)
359 dataPath << initialPath;
361 NS_LOG_INFO(
"Checking " << dataPath.str());
365 NS_LOG_INFO(
"Data directory located in " << dataPath.str());
366 path = dataPath.str();
367 directoryFound =
true;
374 NS_FATAL_ERROR(
"SatEnvVariables::LocateDirectory - Directory not found within "
384 NS_LOG_FUNCTION(
this);
387 bool fileFound =
false;
389 NS_LOG_INFO(
"Initial path " << initialPath);
393 std::stringstream dataPath;
395 for (uint32_t j = 0; j < i; j++)
400 dataPath << initialPath;
402 NS_LOG_INFO(
"Checking " << dataPath.str());
406 NS_LOG_INFO(
"Data directory located in " << dataPath.str());
407 path = dataPath.str();
415 NS_FATAL_ERROR(
"SatEnvVariables::LocateFile - File not found within "
425 bool enableOutputOverwrite)
427 NS_LOG_FUNCTION(
this);
429 NS_LOG_INFO(
"Creating output directory");
432 std::string safetyTag =
"";
433 std::string outputPath =
"";
434 bool directoryExists =
false;
438 if (!campaignName.empty())
440 std::string tempString =
AddToPath(simRootPath, campaignName);
451 while (!directoryExists)
453 outputPath =
FormOutputPath(simRootPath, campaignName, simTag, safetyTag);
456 if ((!
IsValidDirectory(outputPath) && !enableOutputOverwrite) || enableOutputOverwrite)
459 directoryExists =
true;
466 NS_LOG_INFO(
"Directory " << outputPath <<
" exists, increasing safety number to "
469 std::stringstream ss;
471 safetyTag = ss.str();
474 directoryExists =
false;
485 std::string campaignName,
487 std::string safetyTag)
489 NS_LOG_FUNCTION(
this);
491 std::string outputPath =
"";
492 std::stringstream tempTag;
496 if (!safetyTag.empty())
498 tempTag << safetyTag;
501 outputPath =
AddToPath(outputPath, simRootPath);
502 outputPath =
AddToPath(outputPath, campaignName);
503 outputPath =
AddToPath(outputPath, tempTag.str());
505 NS_LOG_INFO(
"Formed path " + outputPath);
513 NS_LOG_FUNCTION(
this);
515 std::stringstream tempPath;
518 if (!stringToAdd.empty())
520 if (tempPath.str().empty())
522 tempPath << stringToAdd;
526 tempPath <<
"/" << stringToAdd;
529 return tempPath.str();
535 NS_LOG_FUNCTION(
this);
537 NS_LOG_INFO(
"Creating directory " + path);
539 mkdir(path.c_str(), 0777);
545 NS_LOG_FUNCTION(
this);
552 timeinfo = localtime(&rawtime);
554 strftime(buffer, 80,
"%d-%m-%Y %I:%M:%S", timeinfo);
555 std::string str(buffer);
557 NS_LOG_INFO(
"Date is " << str);
565 Ptr<SatOutputFileStreamStringContainer> outputContainer)
567 NS_LOG_FUNCTION(
this);
569 FILE* pipe = popen(command.c_str(),
"r");
572 std::string data =
"";
576 if (fgets(buffer, 1024, pipe) != NULL)
578 buffer[strlen(buffer) - 1] =
'\0';
580 outputContainer->AddToContainer(data);
591 NS_LOG_FUNCTION(
this);
595 std::ostringstream fileName;
596 fileName << dataPath <<
"/SimInfo.log";
597 Ptr<SatOutputFileStreamStringContainer> outputContainer =
598 CreateObject<SatOutputFileStreamStringContainer>(fileName.str().c_str(), std::ios::out);
600 std::ostringstream revisionCommand;
601 revisionCommand <<
"cd contrib/satellite"
602 <<
" && git log -1 2>&1";
605 std::stringstream line1;
608 outputContainer->AddToContainer(line1.str());
610 outputContainer->WriteContainerToFile();
621 NS_LOG_FUNCTION(
this);
623 std::ostringstream fileName;
624 fileName << dataPath <<
"/SimDiff.log";
625 Ptr<SatOutputFileStreamStringContainer> outputContainer =
626 CreateObject<SatOutputFileStreamStringContainer>(fileName.str().c_str(), std::ios::out);
628 std::ostringstream diffCommand;
629 diffCommand <<
"cd contrib/satellite"
631 <<
" --ignore-all-space"
632 <<
" --ignore-space-change"
633 <<
" --ignore-blank-lines";
638 diffCommand <<
" \":(exclude)" <<
m_dataPath <<
"\" ";
641 diffCommand <<
" 2>&1";
645 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.