#include "topic.hpp" #include #include #include #include #include namespace { using retained::Sample; std::vector words(const std::string &line) { std::vector out; std::size_t at = 0; while (at < line.size()) { if (line[at] == ' ' || line[at] == '\t' || line[at] == '\r') { ++at; continue; } std::size_t start = at; while (at < line.size() && line[at] != ' ' && line[at] != '\t' && line[at] != '\r') { ++at; } out.push_back(line.substr(start, at - start)); } return out; } bool integer(const std::string &token, int &out) { if (token.empty()) return false; std::size_t at = 0; bool negative = false; if (token[at] == '+' || token[at] == '-') { negative = token[at] == '-'; ++at; } if (at == token.size()) return false; std::uint64_t value = 0; const std::uint64_t limit = negative ? 2147483648ULL : 2147483647ULL; for (; at < token.size(); ++at) { char ch = token[at]; if (ch < '0' || ch > '9' || value > (limit - (ch - '0')) / 10) return false; value = value * 10 + ch - '0'; } out = negative ? static_cast(-static_cast(value)) : static_cast(value); return true; } int nibble(char ch) { if (ch >= '0' && ch <= '9') return ch - '0'; if (ch >= 'a' && ch <= 'f') return ch - 'a' + 10; if (ch >= 'A' && ch <= 'F') return ch - 'A' + 10; return -1; } bool decode(const std::string &token, std::string &out) { out.clear(); if (token == "-") return true; if (token.size() % 2 != 0) return false; for (std::size_t at = 0; at < token.size(); at += 2) { int high = nibble(token[at]); int low = nibble(token[at + 1]); if (high < 0 || low < 0) return false; out.push_back(static_cast(high * 16 + low)); } return true; } void sample(const Sample &item) { std::cout << ' ' << item.writer << ' ' << item.generation << ' ' << item.sequence << ' ' << item.order << ' ' << item.value << ' ' << item.historical; } void samples(const std::vector &items) { std::cout << ' ' << items.size(); for (const Sample &item : items) sample(item); } bool run(retained::Broker &broker, const std::vector &w) { const std::string &op = w[0]; std::vector a; std::string text; bool textual = op == "D" || op == "L"; std::size_t stop = textual ? w.size() - 1 : w.size(); for (std::size_t i = 1; i < stop; ++i) { int value = 0; if (!integer(w[i], value)) return false; a.push_back(value); } if (textual && (w.size() < 2 || !decode(w.back(), text))) return false; if (op == "W" && a.size() == 4) { std::cout << broker.open_writer(a[0], a[1], {a[2], a[3]}); } else if (op == "D" && a.size() == 2) { std::cout << broker.open_profile(a[0], a[1], text); } else if (op == "X" && a.size() == 1) { std::cout << broker.close_writer(a[0]); } else if (op == "P" && a.size() == 2) { Sample item{91, 92, 93, 94, 95, 96}; std::cout << broker.publish(a[0], a[1], item); sample(item); } else if (op == "R" && a.size() == 4) { std::cout << broker.open_reader(a[0], a[1], a[2], a[3]); } else if (op == "C" && a.size() == 1) { std::cout << broker.close_reader(a[0]); } else if (op == "N" && a.size() == 2) { std::cout << broker.resize_reader(a[0], a[1]); } else if (op == "H" && a.size() == 2) { std::cout << broker.set_history(a[0], a[1]); } else if ((op == "T" && a.size() == 2) || (op == "E" && a.size() == 1)) { std::vector items{{91, 92, 93, 94, 95, 96}}; int rc = op == "T" ? broker.take(a[0], a[1], items) : broker.peek(a[0], items); std::cout << rc; samples(items); } else if (op == "Q" && a.size() == 1) { retained::ReaderView v{91, 92, 93, 94, 95, 96}; int rc = broker.reader_view(a[0], v); std::cout << rc << ' ' << v.topic << ' ' << v.durability << ' ' << v.capacity << ' ' << v.pending << ' ' << v.dropped << ' ' << v.delivered; } else if (op == "V" && a.size() == 1) { retained::WriterView v{91, 92, 93, 94, 95, 96}; int rc = broker.writer_view(a[0], v); std::cout << rc << ' ' << v.topic << ' ' << v.durability << ' ' << v.depth << ' ' << v.generation << ' ' << v.published << ' ' << v.cached; } else if (op == "F" && a.size() == 1) { retained::TopicView v{91, 92, 93, 94}; int rc = broker.topic_view(a[0], v); std::cout << rc << ' ' << v.writers << ' ' << v.readers << ' ' << v.cached << ' ' << v.pending; } else if (op == "S" && a.empty()) { retained::Summary v = broker.summary(); std::cout << v.writers << ' ' << v.readers << ' ' << v.cached << ' ' << v.pending << ' ' << v.publications << ' ' << v.generations; } else if (op == "Z" && a.empty()) { broker.clear(); std::cout << 0; } else if (op == "L" && a.empty()) { retained::Policy p{91, 92}; int rc = retained::parse_policy(text, p); std::cout << rc << ' ' << p.durability << ' ' << p.depth; } else if (op == "M" && a.size() == 2) { std::string result = retained::format_policy({a[0], a[1]}); std::cout << result.size(); for (unsigned char ch : result) std::cout << ' ' << static_cast(ch); } else { return false; } std::cout << '\n'; return true; } } int main() { retained::Broker brokers[2]; int selected = 0; std::string line; while (std::getline(std::cin, line)) { std::vector w = words(line); if (w.empty()) continue; if (w[0] == "U") { int next = 0; if (w.size() != 2 || !integer(w[1], next) || next < 0 || next > 1) return 2; selected = next; std::cout << selected << '\n'; } else if (!run(brokers[selected], w)) { return 2; } } return std::cin.bad() ? 2 : 0; }