#include "lifecycle.hpp" #include #include #include #include #include namespace { std::vector words(const std::string& line) { std::vector result; std::string token; for (char value : line) { if (value == ' ' || value == '\t' || value == '\r') { if (!token.empty()) { result.push_back(token); token.clear(); } } else { token.push_back(value); } } if (!token.empty()) { result.push_back(token); } return result; } bool number(const std::string& text, int& result) { std::size_t at = 0; bool negative = false; if (!text.empty() && (text[0] == '+' || text[0] == '-')) { negative = text[0] == '-'; ++at; } if (at == text.size()) { return false; } std::uint64_t value = 0; const std::uint64_t limit = negative ? 2147483648ULL : 2147483647ULL; for (; at < text.size(); ++at) { const char digit = text[at]; if (digit < '0' || digit > '9') { return false; } value = value * 10 + static_cast(digit - '0'); if (value > limit) { return false; } } result = negative ? static_cast(-static_cast(value)) : static_cast(value); return true; } void print(const std::vector& values) { std::cout << '['; bool first = true; for (int value : values) { if (!first) { std::cout << ','; } first = false; std::cout << value; } std::cout << "]\n"; } void print_view(const station::Station& station, int slot) { station::View value; const int status = station.inspect(slot, value); if (status != 0) { print({status}); return; } std::vector row{0, value.owner, value.state, value.subscriptions, value.device, value.shutdown_calls, value.cleanup_calls}; row.insert(row.end(), value.tickets.begin(), value.tickets.end()); print(row); } void print_port(const station::Port& port) { const auto entries = port.entries(); std::vector row{port.attempts(), port.next_ticket(), port.serial(), port.deliveries(), static_cast(entries.size())}; for (const auto& entry : entries) { row.push_back(entry.id); row.push_back(entry.owner); row.push_back(entry.kind); row.push_back(entry.index); row.push_back(entry.enabled); } print(row); } void print_events(const station::Port& port) { const auto& events = port.events(); std::vector row{static_cast(events.size())}; for (const auto& event : events) { row.push_back(event.serial); row.push_back(event.code); row.push_back(event.ticket); row.push_back(event.owner); row.push_back(event.kind); row.push_back(event.index); row.push_back(event.value); } print(row); } bool execute(station::Station& station, station::Port& port, const std::vector& tokens) { const std::string& op = tokens[0]; std::vector args; for (std::size_t at = 1; at < tokens.size(); ++at) { int value; if (!number(tokens[at], value)) { return false; } args.push_back(value); } int status; if (op == "NEW" && args.size() == 3) { status = station.create(args[0], args[1], args[2]); } else if (op == "FAIL" && args.size() == 1) { status = port.inject_failure(args[0]); } else if (op == "PUBLISH" && args.size() == 3) { status = port.publish(args[0], args[1], args[2]); } else if (op == "STATS" && args.size() == 1) { std::vector values; const int rc = port.statistics(args[0], values); values.insert(values.begin(), rc); print(values); return true; } else if (op == "HISTORY" && args.size() == 2) { std::vector events; const int rc = port.history(args[0], args[1], events); std::vector values{rc}; if (rc == 0) { values.push_back(static_cast(events.size())); for (const auto& event : events) { values.insert(values.end(), {event.serial, event.code, event.ticket, event.owner, event.kind, event.index, event.value}); } } print(values); return true; } else if (op == "PORT" && args.empty()) { print_port(port); return true; } else if (op == "EVENTS" && args.empty()) { print_events(port); return true; } else if (op == "VIEW" && args.size() == 1) { print_view(station, args[0]); return true; } else if (args.size() == 1) { const int slot = args[0]; if (op == "CONFIG") status = station.configure(slot); else if (op == "ACTIVATE") status = station.activate(slot); else if (op == "DEACTIVATE") status = station.deactivate(slot); else if (op == "CLEANUP") status = station.cleanup(slot); else if (op == "SHUTDOWN") status = station.shutdown(slot); else if (op == "STOP") status = station.stop(slot); else return false; } else { return false; } print({status}); return true; } } int main() { station::Port port; station::Station station(port); std::string line; while (std::getline(std::cin, line)) { const auto tokens = words(line); if (tokens.empty() || tokens[0][0] == '#') { continue; } if (!execute(station, port, tokens)) { return 2; } } return std::cin.bad() ? 2 : 0; }