System Simulator in C++ Homework Sample

The simulator emulates a full system at a high level, includes processes and input/output. A configuration file gives the relative performance times of the different components. For input, the devices are hard drive, keyboard and scanner and for output the devices are hard drive, monitor and projector. You can also specify applications start and stop, and access to IO devices, memory. These actions should be logged with start time and end time. The IO devices take a certain number of cycles, so if you use O(hard drive) 5 it should be treated as Output to hard drive with 5 * cycle count of the hard drive. For more C++ programming homework help contact us for a quote.

Solution:

Assignment1.cpp

/*
* Assignment1.cpp
*/

#include <iostream>
#include <ostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <vector>
#include “Configuration.h”
#include “Metadata.h”
using namespace std;

/**
* Global variable
*/
ofstream logFile;
string currentLGFPath;

ConfigFile currentConfFile;
vector<ConfigFile> allConfigFiles;

bool currentlyRunningSystem = false;
bool currentlyRunningApplication = false;

/**
* Some helper function
*/
bool RunMetaDataFile();
bool ScanConfigFile(string cfgFileName, ConfigFile& sentFile);
bool ParseCommand(string sentCommand);
string ScanNextLine(ifstream& sentStream);
string ScanNextLine(ifstream& sentStream, char delimChar);
void OutputConfigFileData(bool toFile, bool toMonitor);
bool OutputToLog(string sentOutput, bool createNewLine);

int main(int argc, char* argv[])
{
if (argc < 2)
{
cout << “Usage: ” << argv[0] << ” <configuration file 1> <configuration file 2> …” << endl;
return 0;
}
else
{
for (int i = 1; i < argc; i++)
{
if (!ScanConfigFile(argv[i], currentConfFile))
{
cout << “FATAL ERROR: There was an error with the config file.” << endl;
return 0;
}
if (currentConfFile.ShouldLogToFile())
{
currentLGFPath = currentConfFile.GetLGFPath();
logFile.open(currentLGFPath);
}

if (!OutputToLog(“Configuration File Data”, true))
{
cout << “FATAL ERROR: There was an output error. Closing the simulation.” << endl;
return 0;
}
OutputConfigFileData(currentConfFile.ShouldLogToFile(), currentConfFile.ShouldLogToMonitor());
RunMetaDataFile();
OutputToLog(string(“(End of config file: “) + argv[i] + “)”, true);
if ((i + 1) < argc)
{
OutputToLog(“\n”, false);
}
if (logFile.is_open())
{
logFile.close();
}
}
}
return 0;
}

bool RunMetaDataFile()
{
ifstream mdfFile;
mdfFile.open(currentConfFile.GetMDFPath());
string currentLine;
OutputToLog(“\nMeta-Data Metrics”, true);
if (mdfFile.eof())
{
OutputToLog(“Error: Empty Meta Data File”, true);
mdfFile.close();
return false;
}

currentLine = ScanNextLine(mdfFile);

//Check for start line
if (currentLine.find(“Start Program Meta-Data Code:”) == string::npos) //True if not found
{
OutputToLog(“Error: No start program command”, true);
mdfFile.close();
return false;
}

currentLine = ScanNextLine(mdfFile, ‘;’);

//Check for start command
if (currentLine.find(“S{begin}0”) == string::npos)
{
OutputToLog(“Error: No start command found”, true);
mdfFile.close();
return false;
}
bool reachedEndOfFile = false;
do
{
if (mdfFile.eof())
{
OutputToLog(“Error: mdf file ended unexpectedly”, true);
mdfFile.close();
return false;
}
else
{
currentLine = ScanNextLine(mdfFile, ‘;’);
if (currentLine.find(“End Program Meta-Data Code.”) != string::npos)
{
reachedEndOfFile = true;
}
else
{
if (!ParseCommand(currentLine))
{
OutputToLog(string(“There was an error with the command \”” + currentLine + “\””), true);
mdfFile.close();
return false;
}
}
}
}
while (!reachedEndOfFile);
return true;
}

bool ScanConfigFile(string cfgFileName, ConfigFile& sentFile)
{
ifstream cfgFile;
cfgFile.open(cfgFileName);
string currentLine;
float versionNumberTEMP;
string metaDataFilePathTEMP;
string logFilePathTEMP;

int monitorDispTimeTEMP; //msec
int processorCycleTimeTEMP; //msec
int scannerCycleTimeTEMP; //msec
int hardDriveCycleTimeTEMP; //msec
int keyboardCycleTimeTEMP; //msec
int memoryCycleTimeTEMP; //msec
int projectorCycleTimeTEMP; //msec

bool shouldLogToFileTEMP = false;
bool shouldLogToMonitorTEMP = false;
if (cfgFile.eof())
{
cout << “Error: Empty Config File” << endl;
cfgFile.close();
return false;
}
currentLine = ScanNextLine(cfgFile);
if (currentLine.find(“Start Simulator Configuration File”) == string::npos)
{
cout << “Error: No start command” << endl;
cfgFile.close();
return false;
}
for (int i = 0; i < 11; i++)
{
if (cfgFile.eof())
{
cout << “Error: config file ended unexpectedly” << endl;
cfgFile.close();
return false;
}
else
{
currentLine = ScanNextLine(cfgFile);
if (currentLine.find(“Version/Phase:”) != string::npos)
{
sscanf(currentLine.c_str(), “Version/Phase:%f”, &versionNumberTEMP);
}
else if (currentLine.find(“File Path:”) != string::npos && currentLine.find(“Log”) == string::npos) //Need to make sure that this isn’t confused for the “Log File Path:” field
{
metaDataFilePathTEMP = currentLine.substr(11, 50);
}
else if (currentLine.find(“Monitor d”) != string::npos) //Added “d” because it was conflicting with Log to Monitor
{
sscanf(currentLine.c_str(), “Monitor display time {msec}:%d”, &monitorDispTimeTEMP);
}
else if (currentLine.find(“Processor”) != string::npos)
{
sscanf(currentLine.c_str(), “Processor cycle time {msec}:%d”, &processorCycleTimeTEMP);
}
else if (currentLine.find(“Scanner”) != string::npos)
{
sscanf(currentLine.c_str(), “Scanner cycle time {msec}:%d”, &scannerCycleTimeTEMP);
}
else if (currentLine.find(“Hard”) != string::npos)
{
sscanf(currentLine.c_str(), “Hard drive cycle time {msec}:%d”, &hardDriveCycleTimeTEMP);
}
else if (currentLine.find(“Keyboard”) != string::npos)
{
sscanf(currentLine.c_str(), “Keyboard cycle time {msec}:%d”, &keyboardCycleTimeTEMP);
}
else if (currentLine.find(“Memory”) != string::npos)
{
sscanf(currentLine.c_str(), “Memory cycle time {msec}:%d”, &memoryCycleTimeTEMP);
}
else if (currentLine.find(“Projector”) != string::npos)
{
sscanf(currentLine.c_str(), “Projector cycle time {msec}:%d”, &projectorCycleTimeTEMP);
}
else if (currentLine.find(“Log to”) != string::npos)
{
if (currentLine.find(“Both”) != string::npos)
{
shouldLogToMonitorTEMP = true;
shouldLogToFileTEMP = true;
}
else if (currentLine.find(“Monitor”) != string::npos)
{
shouldLogToMonitorTEMP = true;
}
else if (currentLine.find(“File”) != string::npos)
{
shouldLogToFileTEMP = true;
}
else
{
OutputToLog(string(“Error cannot interpret: “) + currentLine, true);
}
}
else if (currentLine.find(“Log File”) != string::npos)
{
logFilePathTEMP = currentLine.substr(15, 50);
}
else
{
cout << “Error: Could not find valid config data, perhaps a field is missing?” << endl;
cfgFile.close();
return false;
}
}
}
currentLine = ScanNextLine(cfgFile);
if (currentLine.find(“End Simulator Configuration File”) == string::npos)
{
OutputToLog(“Error: No end config file command”, true);
cfgFile.close();
return false;
}
else
{
sentFile.SetVersionNumber(versionNumberTEMP);
sentFile.SetMDFPath(metaDataFilePathTEMP);
sentFile.SetLGFPath(logFilePathTEMP);
sentFile.SetMonitorTime(monitorDispTimeTEMP);
sentFile.SetProcessorTime(processorCycleTimeTEMP);
sentFile.SetScannerTime(scannerCycleTimeTEMP);
sentFile.SetHardDriveTime(hardDriveCycleTimeTEMP);
sentFile.SetKeyboardTime(keyboardCycleTimeTEMP);
sentFile.SetMemoryTime(memoryCycleTimeTEMP);
sentFile.SetProjectorTime(projectorCycleTimeTEMP);
sentFile.SetLogPreferences(shouldLogToFileTEMP, shouldLogToMonitorTEMP);
}
cfgFile.close();
return true;

}

bool ParseCommand(string sentCommand)
{
char commandChar = sentCommand[0];
for (unsigned int i = 0; (i < sentCommand.length() && (commandChar == ‘ ‘ || commandChar == ‘\n’ || commandChar == ‘\t’)); i++)
{
commandChar = sentCommand[i];
}
if (commandChar == ‘S’)
{
if (sentCommand.find(“begin”) != string::npos)
{
if (currentlyRunningSystem)
{
OutputToLog(“Error: A begin command has already been processed”, true);
}
else
{
currentlyRunningSystem = true;
return true;
}
}
else if (sentCommand.find(“finish”) != string::npos)
{
if (!currentlyRunningSystem)
{
OutputToLog(“Error: A finish command has already been processed”, true);
}
else
{
currentlyRunningSystem = false;
return true;
}
}
else
{
OutputToLog(“Error: No valid keyword found”, true);
return false;
}
}
else if (commandChar == ‘A’)
{
if (sentCommand.find(“begin”) != string::npos)
{
if (currentlyRunningApplication)
{
OutputToLog(“Error: A begin command has already been processed”, true);
}
else
{
currentlyRunningApplication = true;
return true;
}
}
else if (sentCommand.find(“finish”) != string::npos)
{
if (!currentlyRunningApplication)
{
OutputToLog(“Error: A finish command has already been processed”, true);
}
else
{
currentlyRunningApplication = false;
return true;
}
}
else
{
OutputToLog(“Error: No valid keyword found”, true);
return false;
}
}
else if (commandChar == ‘P’)
{
if (sentCommand.find(“run”) == string::npos)
{
OutputToLog(“Error: No \”run\” keyword found”, true);
return false;
}
else
{
int duration = 0;
if (sscanf(sentCommand.c_str(), ” P{run}%d”, &duration) <= 0) //Couldn’t find a duration
{
OutputToLog(“Error: No duration found”, true);
return false;
}

OutputToLog(string(sentCommand) + ” – ” + to_string(duration * currentConfFile.GetProcessorTime()), true);
return true;
}
}
else if (commandChar == ‘I’)
{
int duration = 0;
string keyword;
char tempChar = ‘|’;
if (sentCommand.find(“{“) == string::npos)
{
OutputToLog(“Error: No keyword bracket found”, true);
return false;
}
else
{
int index = sentCommand.find(“{“) + 1;
tempChar = sentCommand[index];
while (tempChar != ‘}’)
{
keyword.push_back(tempChar);
index++;
tempChar = sentCommand[index];
}
}
string commandToParse = ” I{“;
commandToParse += keyword;
commandToParse += “}%d”;

if (sscanf(sentCommand.c_str(), commandToParse.c_str(), &duration) <= 0) //Couldn’t find a duration AND a keyword
{
OutputToLog(“Error: No duration found”, true);
return false;
}
if (keyword.find(“hard drive”) != string::npos)
{
OutputToLog(string(sentCommand) + ” – ” + to_string(duration * currentConfFile.GetHardDriveTime()), true);
}
else if (keyword.find(“keyboard”) != string::npos)
{
OutputToLog(string(sentCommand) + ” – ” + to_string(duration * currentConfFile.GetKeyboardTime()), true);
}
else if (keyword.find(“scanner”) != string::npos)
{
OutputToLog(string(sentCommand) + ” – ” + to_string(duration * currentConfFile.GetScannerTime()), true);
}
else
{
OutputToLog(string(“Error: Unrecognized keyword \”” + keyword + “\””), true);
return false;
}

return true;

}
else if (commandChar == ‘O’)
{
int duration = 0;
string keyword;
char tempChar = ‘|’;
if (sentCommand.find(“{“) == string::npos)
{
OutputToLog(“Error: No keyword bracket found”, true);
return false;
}
else
{
int index = sentCommand.find(“{“) + 1;
tempChar = sentCommand[index];
while (tempChar != ‘}’)
{
keyword.push_back(tempChar);
index++;
tempChar = sentCommand[index];
}
}
string commandToParse = string(” O{“) + keyword + “}%d”;
if (sscanf(sentCommand.c_str(), commandToParse.c_str(), &duration) <= 0) //Couldn’t find a duration AND a keyword
{
OutputToLog(“Error: No duration found”, true);
return false;
}
if (keyword.find(“hard drive”) != string::npos)
{
OutputToLog(string(sentCommand) + ” – ” + to_string(duration * currentConfFile.GetHardDriveTime()), true);
}
else if (keyword.find(“monitor”) != string::npos)
{
OutputToLog(string(sentCommand) + ” – ” + to_string(duration * currentConfFile.GetMonitorTime()), true);
}
else if (keyword.find(“projector”) != string::npos)
{
OutputToLog(string(sentCommand) + ” – ” + to_string(duration * currentConfFile.GetProjectorTime()), true);
}
else
{
OutputToLog(string(“Error: Unrecognized keyword \””) + keyword + “\””, true);
return false;
}
return true;
}
else if (commandChar == ‘M’)
{
int duration = -1;
if (sscanf(sentCommand.c_str(), ” M{allocate}%d”, &duration) <= 0 && sscanf(sentCommand.c_str(), ” M{block}%d”, &duration) <= 0) //Couldn’t find a duration
{
OutputToLog(“Error: No duration found or the keyword may be invalid”, true);
return false;
}
OutputToLog(string(sentCommand) + ” – ” + to_string(duration * currentConfFile.GetMemoryTime()), true);
return true;
}
else
{
OutputToLog(string(“Error: Unknown command char \'”) + commandChar + “\””, true);
return false;
}

return false;
}

string ScanNextLine(ifstream& sentStream)
{
char line[50];
sentStream.getline(line, 50);
string lineStr = line;
return lineStr;
}

string ScanNextLine(ifstream& sentStream, char delimChar)
{
char line[50];
sentStream.getline(line, 50, delimChar);
string lineStr = line;
while (lineStr[0] < 65)
{
lineStr.erase(lineStr.begin());
}
return lineStr;
}

void OutputConfigFileData(bool toFile, bool toMonitor)
{
OutputToLog(string(“Monitor = “) + to_string(currentConfFile.GetMonitorTime()) + “ms/cycle”, true);
OutputToLog(string(“Processor = “) + to_string(currentConfFile.GetProcessorTime()) + “ms/cycle”, true);
OutputToLog(string(“Scanner = “) + to_string(currentConfFile.GetScannerTime()) + “ms/cycle”, true);
OutputToLog(string(“Hard Drive = “) + to_string(currentConfFile.GetHardDriveTime()) + “ms/cycle”, true);
OutputToLog(string(“Keyboard = “) + to_string(currentConfFile.GetKeyboardTime()) + “ms/cycle”, true);
OutputToLog(string(“Memory = “) + to_string(currentConfFile.GetMemoryTime()) + “ms/cycle”, true);
OutputToLog(string(“Projector = “) + to_string(currentConfFile.GetProjectorTime()) + “ms/cycle”, true);
OutputToLog(string(“Logged to: “), false);

if (toFile && toMonitor)
{
OutputToLog(string(“Monitor and “) + currentConfFile.GetLGFPath(), true);
}
else if (toMonitor)
{
OutputToLog(“Monitor”, true);
}
else if (toFile)
{
OutputToLog(currentConfFile.GetLGFPath(), true);
}
return;
}

bool OutputToLog(string sentOutput, bool createNewLine)
{
if (currentConfFile.ShouldLogToFile() && !logFile.is_open())
{
cout << “FATAL ERROR: Cannot output to file, there seems to have been an error in opening the file” << endl;
return false;

}
else if (currentConfFile.ShouldLogToFile() && currentConfFile.ShouldLogToMonitor()) //Log to both
{
logFile << sentOutput;
cout << sentOutput;
if (createNewLine)
{
logFile << endl;
cout << endl;
}
return true;
}
else if (currentConfFile.ShouldLogToFile())
{
logFile << sentOutput;
if (createNewLine)
{
logFile << endl;
}
return true;
}
else if (currentConfFile.ShouldLogToMonitor())
{
cout << sentOutput;
if (createNewLine)
{
cout << endl;
}
return true;
}
else
{
cout << “FATAL ERROR: Output is incorrectly configured and cannot be made” << endl;
return false;
}
}

Configuration.cpp

/*
* Configuration.cpp
*/

#include “Configuration.h”

ConfigFile::ConfigFile()
{
shouldLogToFile = false;
shouldLogToMonitor = false;

}

ConfigFile::~ConfigFile()
{
}

void ConfigFile::SetVersionNumber(float sentValue)
{
versionNumber = sentValue;

return;

}

void ConfigFile::SetMDFPath(string sentPath)
{
metaDataFilePath = sentPath;

return;

}

void ConfigFile::SetLGFPath(string sentPath)
{
logFilePath = sentPath;

return;

}

void ConfigFile::SetMonitorTime(int sentValue)
{
monitorDispTime = sentValue;

return;

}

void ConfigFile::SetProcessorTime(int sentValue)
{
processorCycleTime = sentValue;

return;

}

void ConfigFile::SetScannerTime(int sentValue)
{
scannerCycleTime = sentValue;

return;

}

void ConfigFile::SetHardDriveTime(int sentValue)
{
hardDriveCycleTime = sentValue;

return;

}

void ConfigFile::SetKeyboardTime(int sentValue)
{
keyboardCycleTime = sentValue;

return;

}

void ConfigFile::SetMemoryTime(int sentValue)
{
memoryCycleTime = sentValue;

return;

}

void ConfigFile::SetProjectorTime(int sentValue)
{
projectorCycleTime = sentValue;

return;

}

void ConfigFile::SetLogPreferences(bool toFile, bool toMonitor)
{
shouldLogToFile = toFile;
shouldLogToMonitor = toMonitor;

return;

}

float ConfigFile::GetVersionNumber() const
{
return versionNumber;

}

string ConfigFile::GetMDFPath() const
{
return metaDataFilePath;

}

string ConfigFile::GetLGFPath() const
{
return logFilePath;

}

int ConfigFile::GetMonitorTime() const
{
return monitorDispTime;

}

int ConfigFile::GetProcessorTime() const
{
return processorCycleTime;

}

int ConfigFile::GetScannerTime() const
{
return scannerCycleTime;

}

int ConfigFile::GetHardDriveTime() const
{
return hardDriveCycleTime;

}

int ConfigFile::GetKeyboardTime() const
{
return keyboardCycleTime;

}

int ConfigFile::GetMemoryTime() const
{
return memoryCycleTime;

}

int ConfigFile::GetProjectorTime() const
{
return projectorCycleTime;

}

bool ConfigFile::ShouldLogToFile() const
{
return shouldLogToFile;

}

bool ConfigFile::ShouldLogToMonitor() const
{
return shouldLogToMonitor;

}

ConfigFile& ConfigFile::operator=(const ConfigFile& sentConfig)
{
versionNumber = sentConfig.GetVersionNumber();
metaDataFilePath = sentConfig.GetMDFPath();
logFilePath = sentConfig.GetLGFPath();
monitorDispTime = sentConfig.GetMonitorTime(); //msec
processorCycleTime = sentConfig.GetProcessorTime(); //msec
scannerCycleTime = sentConfig.GetScannerTime(); //msec
hardDriveCycleTime = sentConfig.GetHardDriveTime(); //msec
keyboardCycleTime = sentConfig.GetKeyboardTime(); //msec
memoryCycleTime = sentConfig.GetMemoryTime(); //msec
projectorCycleTime = sentConfig.GetProjectorTime(); //msec
shouldLogToFile = sentConfig.ShouldLogToFile();
shouldLogToMonitor = sentConfig.ShouldLogToMonitor();

return *this;

}

Configuration.h

/*
* Configuration.h
*/

#ifndef ASSIGNMENT1_CONFIGURATION_H_
#define ASSIGNMENT1_CONFIGURATION_H_

#include <iostream>
#include <string>
using namespace std;

class ConfigFile
{
public:
ConfigFile();
~ConfigFile();

//Setter
void SetVersionNumber(float sentValue);
void SetMDFPath(string sentPath);
void SetLGFPath(string sentPath);
void SetMonitorTime(int sentValue);
void SetProcessorTime(int sentValue);
void SetScannerTime(int sentValue);
void SetHardDriveTime(int sentValue);
void SetKeyboardTime(int sentValue);
void SetMemoryTime(int sentValue);
void SetProjectorTime(int sentValue);

void SetLogPreferences(bool toFile, bool toMonitor);

//Getter
float GetVersionNumber() const;
string GetMDFPath() const;
string GetLGFPath() const;
int GetMonitorTime() const;
int GetProcessorTime() const;
int GetScannerTime() const;
int GetHardDriveTime() const;
int GetKeyboardTime() const;
int GetMemoryTime() const;
int GetProjectorTime() const;

bool ShouldLogToFile() const;
bool ShouldLogToMonitor() const;

//Operator Overloads
ConfigFile& operator=(const ConfigFile& sentConfig);

private:
float versionNumber;
string metaDataFilePath;
string logFilePath;
int monitorDispTime; //msec
int processorCycleTime; //msec
int scannerCycleTime; //msec
int hardDriveCycleTime; //msec
int keyboardCycleTime; //msec
int memoryCycleTime; //msec
int projectorCycleTime; //msec
bool shouldLogToFile;
bool shouldLogToMonitor;

};

#endif /* ASSIGNMENT1_CONFIGURATION_H_ */

Metadata.cpp

/*
* Metadata.cpp
*/
#include “Metadata.h”

MetaDataObject::MetaDataObject()
{
command = ‘X’;
keyword = “NONE”;
duration = -1;
}

MetaDataObject::~MetaDataObject()
{
}

bool MetaDataObject::SetCommand(char sentCommand)
{
if (IsValidCommand(sentCommand))
{
command = sentCommand;
keyword = “NONE”;
return true;
}
else
{
return false;
}
}

bool MetaDataObject::SetKeyword(string sentWord)
{
if (command == ‘X’)
{
return false;
}
else
{
if (command == ‘S’)
{
if (sentWord == “begin” || sentWord == “finish”)
{
keyword = sentWord;
return true;
}
else
{
return false;
}
}
else if (command == ‘A’)
{
if (sentWord == “begin” || sentWord == “finish”)
{
keyword = sentWord;
return true;
}
else
{
return false;
}
}
else if (command == ‘P’)
{
if (sentWord == “run”)
{
keyword = sentWord;
return true;
}
else
{
return false;

}

}
else if (command == ‘I’)
{
if (sentWord == “hard drive” || sentWord == “keyboard” || sentWord == “scanner”)
{
keyword = sentWord;
return true;
}
else
{
return false;
}
}
else if (command == ‘O’)
{
if (sentWord == “hard drive” || sentWord == “monitor” || sentWord == “projector”)
{
keyword = sentWord;
return true;
}
else
{
return false;
}
}
else if (command == ‘M’)
{
if (sentWord == “block” || sentWord == “allocate”)
{
keyword = sentWord;
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}

void MetaDataObject::SetDuration(int sentDuration)
{
if (sentDuration >= 0)
{
duration = sentDuration;
}
return;

}

char MetaDataObject::GetCommand()
{
return command;
}

string MetaDataObject::GetKeyword()
{
return keyword;
}

int MetaDataObject::GetDuration()
{
return duration;
}

bool MetaDataObject::IsValidCommand(char sentCommand)
{
return (sentCommand == ‘S’ || sentCommand == ‘A’ || sentCommand == ‘P’ || sentCommand == ‘I’ || sentCommand == ‘O’ || sentCommand == ‘M’);
}

Metadata.h

/*
* Metadata.h
*/

#ifndef ASSIGNMENT1_METADATA_H_
#define ASSIGNMENT1_METADATA_H_

#include <iostream>
#include <string>
using namespace std;

class MetaDataObject
{
public:
MetaDataObject();
~MetaDataObject();
bool SetCommand(char sentCommand);
bool SetKeyword(string sentWord);
void SetDuration(int sentDuration);
char GetCommand();
string GetKeyword();
int GetDuration();
bool IsValidCommand(char sentCommand);
private:
char command;
string keyword;
int duration;
};

#endif /* ASSIGNMENT1_METADATA_H_ */