|
1 | | -// concoredocker.hpp -- this C++ include file will be the equivalent of concoredocker.py |
| 1 | +#ifndef CONCORE_HPP |
| 2 | +#define CONCORE_HPP |
| 3 | + |
| 4 | +#include <iostream> |
| 5 | +#include <fstream> |
| 6 | +#include <sstream> |
| 7 | +#include <string> |
| 8 | +#include <unordered_map> |
| 9 | +#include <vector> |
| 10 | +#include <chrono> |
| 11 | +#include <thread> |
| 12 | +#include <filesystem> |
| 13 | +#include <stdexcept> |
| 14 | +#include <regex> |
| 15 | + |
| 16 | +class Concore { |
| 17 | +public: |
| 18 | + std::unordered_map<std::string, std::string> iport; |
| 19 | + std::unordered_map<std::string, std::string> oport; |
| 20 | + std::string s, olds; |
| 21 | + int delay = 1; |
| 22 | + int retrycount = 0; |
| 23 | + std::string inpath = "/in"; |
| 24 | + std::string outpath = "/out"; |
| 25 | + int simtime = 0; |
| 26 | + int maxtime = 100; |
| 27 | + std::unordered_map<std::string, std::string> params; |
| 28 | + |
| 29 | + Concore() { |
| 30 | + iport = safe_literal_eval("concore.iport", {}); |
| 31 | + oport = safe_literal_eval("concore.oport", {}); |
| 32 | + default_maxtime(100); |
| 33 | + load_params(); |
| 34 | + } |
| 35 | + |
| 36 | + std::unordered_map<std::string, std::string> safe_literal_eval(const std::string& filename, std::unordered_map<std::string, std::string> defaultValue) { |
| 37 | + std::ifstream file(filename); |
| 38 | + if (!file) { |
| 39 | + std::cerr << "Error reading " << filename << "\n"; |
| 40 | + return defaultValue; |
| 41 | + } |
| 42 | + return defaultValue; |
| 43 | + } |
| 44 | + |
| 45 | + void load_params() { |
| 46 | + std::ifstream file(inpath + "/1/concore.params"); |
| 47 | + if (!file) return; |
| 48 | + std::stringstream buffer; |
| 49 | + buffer << file.rdbuf(); |
| 50 | + std::string sparams = buffer.str(); |
| 51 | + |
| 52 | + if (!sparams.empty() && sparams[0] == '"') { |
| 53 | + sparams = sparams.substr(1, sparams.find('"') - 1); |
| 54 | + } |
| 55 | + |
| 56 | + if (!sparams.empty() && sparams[0] != '{') { |
| 57 | + sparams = "{'" + std::regex_replace(std::regex_replace(std::regex_replace(sparams, std::regex(","), ", '"), std::regex("="), "':"), std::regex(" "), "") + "}"; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + std::string tryparam(const std::string& n, const std::string& i) { |
| 62 | + return params.count(n) ? params[n] : i; |
| 63 | + } |
| 64 | + |
| 65 | + void default_maxtime(int defaultValue) { |
| 66 | + maxtime = defaultValue; |
| 67 | + std::ifstream file(inpath + "/1/concore.maxtime"); |
| 68 | + if (file) { |
| 69 | + file >> maxtime; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + bool unchanged() { |
| 74 | + if (olds == s) { |
| 75 | + s.clear(); |
| 76 | + return true; |
| 77 | + } |
| 78 | + olds = s; |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + std::vector<std::string> read(int port, const std::string& name, const std::string& initstr) { |
| 83 | + std::this_thread::sleep_for(std::chrono::seconds(delay)); |
| 84 | + std::string file_path = inpath + std::to_string(port) + "/" + name; |
| 85 | + std::ifstream infile(file_path); |
| 86 | + std::string ins; |
| 87 | + |
| 88 | + if (!infile) { |
| 89 | + std::cerr << "File " << file_path << " not found, using default value.\n"; |
| 90 | + return {initstr}; |
| 91 | + } |
| 92 | + std::getline(infile, ins); |
| 93 | + |
| 94 | + int attempts = 0, max_retries = 5; |
| 95 | + while (ins.empty() && attempts < max_retries) { |
| 96 | + std::this_thread::sleep_for(std::chrono::seconds(delay)); |
| 97 | + infile.open(file_path); |
| 98 | + if (infile) std::getline(infile, ins); |
| 99 | + attempts++; |
| 100 | + retrycount++; |
| 101 | + } |
| 102 | + |
| 103 | + if (ins.empty()) { |
| 104 | + std::cerr << "Max retries reached for " << file_path << ", using default value.\n"; |
| 105 | + return {initstr}; |
| 106 | + } |
| 107 | + |
| 108 | + s += ins; |
| 109 | + return {ins}; |
| 110 | + } |
| 111 | + |
| 112 | + void write(int port, const std::string& name, const std::vector<std::string>& val, int delta = 0) { |
| 113 | + std::string file_path = outpath + std::to_string(port) + "/" + name; |
| 114 | + std::ofstream outfile(file_path); |
| 115 | + if (!outfile) { |
| 116 | + std::cerr << "Error writing to " << file_path << "\n"; |
| 117 | + return; |
| 118 | + } |
| 119 | + if (!val.empty()) { |
| 120 | + outfile << "[" << simtime + delta << ", "; |
| 121 | + for (size_t i = 0; i < val.size(); ++i) { |
| 122 | + outfile << val[i] << (i + 1 < val.size() ? ", " : ""); |
| 123 | + } |
| 124 | + outfile << "]"; |
| 125 | + simtime += delta; |
| 126 | + } |
| 127 | + } |
| 128 | +}; |
| 129 | + |
| 130 | +#endif |
0 commit comments