#include "durable.hpp" #include #include #include using namespace durable; namespace { std::vector initial(Number nonce) { return {{nonce, nonce, nonce, nonce, nonce, nonce}}; } void samples(char label, int result, const std::vector &values) { std::cout << label << ' ' << result << ' ' << values.size(); for (const auto &s : values) { std::cout << ' ' << s.sequence << ' ' << s.writer << ' ' << s.generation << ' ' << s.topic << ' ' << s.payload << ' ' << s.replayed; } std::cout << '\n'; } bool execute(Domain &d, char command, std::istringstream &in) { int id=0, topic=0, durability=0, size=0; Number nonce=0, payload=0; if (command == 'W' || command == 'O') { if (!(in >> id >> topic >> durability >> size)) return false; const int result = command == 'W' ? d.open_writer(id, topic, durability, size) : d.open_reader(id, topic, durability, size); std::cout << "R " << result << '\n'; } else if (command == 'X' || command == 'C' || command == 'Z') { if (!(in >> id)) return false; const int result = command == 'X' ? d.close_writer(id) : command == 'C' ? d.close_reader(id) : d.clear(id); std::cout << "R " << result << '\n'; } else if (command == 'D' || command == 'L') { if (!(in >> id >> size)) return false; const int result = command == 'D' ? d.set_depth(id, size) : d.set_capacity(id, size); std::cout << "R " << result << '\n'; } else if (command == 'P') { if (!(in >> id >> payload >> nonce)) return false; const int result = d.publish(id, payload, nonce); std::cout << "P " << result << ' ' << nonce << '\n'; } else if (command == 'B') { if (!(in >> id >> nonce >> size) || size < 0) return false; std::vector values; for (int i=0; i> payload)) return false; values.push_back(payload); } std::vector output{nonce, nonce}; const int result = d.publish_many(id, values, output); std::cout << "B " << result << ' ' << output.size(); for (Number value : output) std::cout << ' ' << value; std::cout << ' ' << values.size(); for (Number value : values) std::cout << ' ' << value; std::cout << '\n'; } else if (command == 'H' || command == 'K' || command == 'T') { if (!(in >> id)) return false; if (command == 'T' && !(in >> size)) return false; if (!(in >> nonce)) return false; auto output = initial(nonce); const int result = command == 'H' ? d.history(id, output) : command == 'K' ? d.peek(id, output) : d.take(id, size, output); samples(command, result, output); } else if (command == 'I') { if (!(in >> id >> nonce)) return false; WriterInfo info{nonce,nonce,nonce,nonce,nonce,nonce,nonce}; const int result = d.writer_info(id, info); std::cout << "I " << result << ' ' << info.id << ' ' << info.generation << ' ' << info.topic << ' ' << info.durability << ' ' << info.depth << ' ' << info.published << ' ' << info.retained << '\n'; } else if (command == 'J') { if (!(in >> id >> nonce)) return false; ReaderInfo info{nonce,nonce,nonce,nonce,nonce,nonce,nonce,nonce}; const int result = d.reader_info(id, info); std::cout << "J " << result << ' ' << info.id << ' ' << info.topic << ' ' << info.durability << ' ' << info.capacity << ' ' << info.received << ' ' << info.taken << ' ' << info.lost << ' ' << info.queued << '\n'; } else if (command == 'A' || command == 'E') { std::vector output{91,92}; if (command == 'A') d.writer_ids(output); else d.reader_ids(output); std::cout << command << ' ' << output.size(); for (int value : output) std::cout << ' ' << value; std::cout << '\n'; } else if (command == 'Q') { const auto info = d.totals(); std::cout << "Q " << info.writers << ' ' << info.readers << ' ' << info.retained << ' ' << info.queued << ' ' << info.next_sequence << '\n'; } else if (command == 'N') { d.reset(); std::cout << "R 0\n"; } else { return false; } std::string remaining; return !(in >> remaining); } } int main() { Domain domains[2]; int selected=0; std::string line; while (std::getline(std::cin,line)) { std::istringstream in(line); char command=0; if (!(in >> command)) continue; if (command == 'U') { std::string remaining; if (!(in >> selected) || selected < 0 || selected > 1 || (in >> remaining)) return 2; std::cout << "U " << selected << '\n'; } else if (!execute(domains[selected], command, in)) { return 2; } } return std::cin.bad() ? 2 : 0; }