-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.cpp
178 lines (151 loc) · 6.51 KB
/
Config.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//
// Created by tim on 9/8/19.
//
#include "Config.h"
#include <iostream>
#include <stdexcept>
//Private Methods
void Config::initializeConfigData(map<string, string> parser_output) {
try {
//Initialize Processor Cycle Time in msec
if(stoi(parser_output.at("Processor cycle time {msec}")) <= 0){
throw runtime_error("Processor cycle time <= 0");
}
else {
cycle_times.insert(pair<string, int>(string("run"), stoi(parser_output.at("Processor cycle time {msec}"))));
}
//Initialize Monitor Display Time
if(stoi(parser_output.at("Monitor display time {msec}")) <= 0){
throw runtime_error("Monitor display time <= 0");
}
else {
cycle_times.insert(pair<string, int>(string("monitor"), stoi(parser_output.at("Monitor display time {msec}"))));
}
//Initialize Mouse Cycle Time
if(stoi(parser_output.at("Mouse cycle time {msec}")) <= 0){
throw runtime_error("Mouse cycle time <= 0");
}
else {
cycle_times.insert(pair<string, int>(string("mouse"), stoi(parser_output.at("Mouse cycle time {msec}"))));
}
//Initialize Hard Drive Cycle Time
if(stoi(parser_output.at("Hard drive cycle time {msec}")) <= 0){
throw runtime_error("Hard drive cycle time <= 0");
}
else {
cycle_times.insert(pair<string, int>(string("hard drive"), stoi(parser_output.at("Hard drive cycle time {msec}"))));
}
//Initialize Keyboard Cycle Time
if(stoi(parser_output.at("Keyboard cycle time {msec}")) <= 0){
throw runtime_error("Keyboard cycle time <= 0");
}
else {
cycle_times.insert(pair<string, int>(string("keyboard"), stoi(parser_output.at("Keyboard cycle time {msec}"))));
}
//Initialize Memory Cycle Time
if(stoi(parser_output.at("Memory cycle time {msec}")) <= 0){
throw runtime_error("Memory cycle time <= 0");
}
else {
cycle_times.insert(pair<string, int>(string("allocate"), stoi(parser_output.at("Memory cycle time {msec}"))));
cycle_times.insert(pair<string, int>(string("block"), stoi(parser_output.at("Memory cycle time {msec}"))));
}
//Initialize Printer Cycle Time
if(stoi(parser_output.at("Printer cycle time {msec}")) <= 0){
throw runtime_error("Printer cycle time <= 0");
}
else {
cycle_times.insert(pair<string, int>(string("printer"), stoi(parser_output.at("Printer cycle time {msec}"))));
}
if(stoi(parser_output.at("System memory {kbytes}")) <= 0){
throw runtime_error("Memory Allocation <=0 KB");
}
else{
misc_configuration_details.insert(pair<string, string>(string("size"), parser_output.at("System memory {kbytes}")));
}
if(stoi(parser_output.at("Memory block size {kbytes}")) <= 0){
throw runtime_error("Memory block size <=0 KB");
}
else{
misc_configuration_details.insert(pair<string, string>(string("bsize"), parser_output.at("Memory block size {kbytes}")));
}
if(stoi(parser_output.at("Printer quantity")) <= 0){
throw runtime_error("Printer quantity less than zero");
}
else{
misc_configuration_details.insert(pair<string, string>(string("pcount"), parser_output.at("Printer quantity")));
}
if(stoi(parser_output.at("Hard drive quantity")) <= 0){
throw runtime_error("Hard drive quantity less than zero");
}
else{
misc_configuration_details.insert(pair<string, string>(string("hcount"), parser_output.at("Hard drive quantity")));
}
if(stoi(parser_output.at("Processor Quantum Number {msec}")) <= 0){
throw runtime_error("Quantum number less than zero");
}
else{
misc_configuration_details.insert(pair<string,string>(string("quantum"), parser_output.at("Processor Quantum Number {msec}")));
}
if(parser_output.at("CPU Scheduling Code") != "RR" && parser_output.at("CPU Scheduling Code") != "STR"){
throw runtime_error("Invalid CPU Scheduler : " + parser_output.at("CPU Scheduling Code"));
}
else{
misc_configuration_details.insert(pair<string, string>(string("scheduler"), parser_output.at("CPU Scheduling Code")));
}
misc_configuration_details.insert(pair<string, string>(string("meta_file_path"), parser_output.at("File Path")));
misc_configuration_details.insert(pair<string, string>(string("log_file_path"), parser_output.at("Log File Path")));
misc_configuration_details.insert(pair<string, string>(string("log_type"), parser_output.at("Log")));
}
catch(const out_of_range& oor) { //Thrown by [map].at(key) when the given input is not inside of the map
//Catches misspellings and incorrect config options
cerr << "\nError! Config file element misspelled or invalid. Error: " << oor.what() << endl;
exit(EXIT_FAILURE);
}
catch(const runtime_error& rtr){
cerr << "\nError! " << rtr.what() << "\n";
exit(EXIT_FAILURE);
}
}
//Public Methods
Config::Config(){
pthread_mutex_init(&config_access, NULL);
}
void Config::readFile(string file_name){
pthread_mutex_lock(&config_access);
ConfigParser * config_generator;
map<string, string> parser_output;
config_generator = new ConfigParser(file_name);
config_generator->parse();
parser_output = config_generator->retrieveFormattedOutput();
this->initializeConfigData(parser_output);
delete config_generator;
config_generator = nullptr;
pthread_mutex_unlock(&config_access);
}
int Config::getCycleTime(string type) const{
pthread_mutex_lock(&config_access);
int cycle_time;
try{
cycle_time = cycle_times.at(type);
}
catch(const out_of_range& oor){
cerr << "\nIncorrect Cycle Time Descriptor " << type << endl;
exit(EXIT_FAILURE);
}
pthread_mutex_unlock(&config_access);
return cycle_time;
}
string Config::getMiscConfigDetail(string type) const {
pthread_mutex_lock(&config_access);
string config_detail;
try{
config_detail = misc_configuration_details.at(type);
}
catch(const out_of_range& oor){
cerr << "\nIncorrect Configuration Rule " << type << endl;
exit(EXIT_FAILURE);
}
pthread_mutex_unlock(&config_access);
return config_detail;
}