#include "appinfo.h" #include #include AppInfo::~AppInfo() { std::cout << "Module: " << getModuleName() << " finished ops." << std::endl; } void AppInfo::setProgramName(std::string name) { this->program_name = name; } void AppInfo::setModuleName(std::string name) { this->module_name = name; } void AppInfo::setArgs(std::deque args) { this->args = args; } void AppInfo::setWorkingDir(std::string working_dir) { this->working_dir = working_dir; } void AppInfo::setEnv(string_map env) { this->env = env; } void AppInfo::setAfter(std::unordered_map after) { this->after = after; } void AppInfo::setCreateLog(bool create_log) { this->create_log = create_log; } void AppInfo::setRunMode(std::string mode) { if(mode == "script") { this->mode = RunMode::script; } else if(mode == "daemon") { this->mode = RunMode::daemon; } } void AppInfo::setRunAs(std::string run_as) { this->run_as = run_as; } void AppInfo::setFinished() { this->state = State::exited; } void AppInfo::setRunning() { this->state = State::running; } void AppInfo::insertParent(std::shared_ptr parent, State state) { this->parents[parent] = state; } bool AppInfo::isParentDependencyCleared(std::osyncstream &out) const { for(auto &[key, value]: parents) { if(key->getState() != value) { out << key->getModuleName() << " not at state: " << stateToString(value) << " but is at:" << stateToString(key->getState()) << std::endl; return false; } } return true; } std::string AppInfo::getModuleName() const { return module_name; } std::string AppInfo::getProgramName() const { return program_name; } std::string AppInfo::getRunAs() const { return run_as; } std::string AppInfo::getWorkingDir() const { return working_dir; } AppInfo::string_map AppInfo::getEnv() const { return env; } std::string AppInfo::getAfterString() const { std::stringstream ss; for(auto &[key,value]: after) { ss << key << "[" << value << "], "; } std::string after = ss.str(); after = after.substr(0,after.size() - 2); return after; } std::string AppInfo::getAfterKeys() const { std::stringstream ss; for(auto &[key,value]: after) { ss << key << ", "; } std::string after = ss.str(); after = after.substr(0,after.size() - 2); return after; } AppInfo::RunMode AppInfo::getRunMode() const { return this->mode; } AppInfo::State AppInfo::getState() const { return this->state; } void AppInfo::copyFromData(AppInfo::AppData &other) { this->module_name = other.module_name; this->program_name = other.program_name; this->create_log = other.create_log; this->working_dir = other.working_dir; for(auto &i: other.args) { this->args.push_back(i); } this->after = other.after; this->env = other.env; setRunMode(other.run_mode); setRunAs(other.run_as); } std::unordered_map, AppInfo::State> AppInfo::getParents() const { return this->parents; } std::unordered_map AppInfo::getAfter() const { return after; } bool AppInfo::getCreateLog() { return create_log; } std::deque AppInfo::getArgs() const { return args; } AppInfo::AppInfo() { } bool AppInfo::operator==(const AppInfo &other) { if(this->module_name == other.module_name && this->program_name == other.program_name && this->args == other.args && this->working_dir == other.working_dir && this->after == other.after && this->create_log == other.create_log ) return true; else return false; } void AppInfo::printInfo(std::ostream &out) { out << "************************************App************************************" << std::endl; out << "Module: " << module_name << std::endl; out << "App Name: " << program_name << std::endl; out << "Arg List: " << std::endl; int index = 0; for(auto &i: args) { out << "Arg[" << index++ << "]: " << i << std::endl; } out << "Run Mode: " << runmodeToString() << std::endl; out << "Working Directory: " << working_dir << std::endl; std::stringstream ss; for(auto &[key,value]: after) { ss << key << "[" << value << "], "; } std::string after = ss.str(); if(!after.empty() && after.size() >= 2) after = after.substr(0,after.size() - 2); out << "After: " << after << std::endl; std::stringstream ss2, ss3; for(auto &[key, value]: parents) { if(!key) { ss2 << "Invalid Parent" << std::endl; } else if(value == State::running) { ss2 << key->getModuleName() << " At: " << key << " with state: running." << std::endl; } else if(value == State::exited) { ss2 << key->getModuleName() << " At: " << key << " with state: exited." << std::endl; } else { ss2 << key->getModuleName() << " At: " << key << " with state: unknown." << std::endl; } } if(parents.size() != 0) { out << "Parents: " << parents.size() << " modules." << std::endl; out << ss2.str(); } else { out << "Parents: None." << std::endl; } for(auto &[key,value]: env) { ss3 << key << "=" << value << ", "; } std::string env = ss3.str(); if(!env.empty() && env.size() >= 2) env = env.substr(0,env.size() - 2); out << "Environment: " << env << std::endl; out << "Create Log: " << create_log << std::endl; out << "Run as: " << run_as << std::endl; out << "************************************End************************************" << std::endl; } std::string AppInfo::runmodeToString() const { switch(this->mode) { case RunMode::script: return "script"; case RunMode::daemon: return "daemon"; default: return "unknown"; } } std::string AppInfo::stateToString() const { return stateToString(this->state); } std::string AppInfo::stateToString(State s) const { switch(s) { case State::initial: return "initial"; case State::running: return "running"; case State::exited: return "exited"; default: return "unknown"; } } void AppInfo::AppData::printInfo(std::ostream &out) { out << "************************************App************************************" << std::endl; out << "Module: " << AppData::module_name << std::endl; out << "App Name: " << AppData::program_name << std::endl; out << "Arg List: " << std::endl; int index = 0; for(auto &i: AppData::args) { out << "Arg[" << index++ << "]: " << i << std::endl; } out << "Run Mode: " << run_mode << std::endl; out << "Working Directory: " << AppData::working_dir << std::endl; std::stringstream ss, ss2; for(auto &[key,value]: AppData::after) { ss << key << "[" << value << "], "; } std::string after = ss.str(); if(!after.empty() && after.size() >= 2) after = after.substr(0,after.size() - 2); out << "After: " << after << std::endl; ss.clear(); ss.flush(); for(auto &[key,value]: AppData::env) { ss2 << key << "=" << value << ", "; } std::string env = ss2.str(); if(!env.empty() && env.size() >= 2) env = env.substr(0,env.size() - 2); out << "Environment: " << env << std::endl; out << "Create Log: " << AppData::create_log << std::endl; out << "Run as: " << run_as << std::endl; out << "************************************End************************************" << std::endl; } std::string AppInfo::AppData::getAfterString() const { std::stringstream ss; for(auto &[key,value]: after) { ss << key << "[" << value << "], "; } std::string after = ss.str(); after = after.substr(0,after.size() - 2); return after; } std::string AppInfo::AppData::getAfterKeys() const { std::stringstream ss; for(auto &[key,value]: after) { ss << key << ", "; } std::string after = ss.str(); after = after.substr(0,after.size() - 2); return after; }