#include "topic.hpp" #include #include #include #include #include #include namespace retained { namespace { bool endpoint(int id) { return id >= 1 && id <= 64; } bool topic_number(int topic) { return topic >= 1 && topic <= 16; } bool capacity_number(int capacity) { return capacity >= 1 && capacity <= 16; } bool compatible(int offered, int requested) { return requested == 0 || offered == 1; } struct Writer { int topic; Policy policy; std::uint64_t generation; std::uint64_t published; bool active; std::deque history; }; struct Reader { int topic; int durability; int capacity; std::uint64_t dropped; std::uint64_t delivered; std::deque inbox; }; void enqueue(Reader &reader, const Sample &sample) { while (reader.inbox.size() >= static_cast(reader.capacity)) { reader.inbox.pop_front(); ++reader.dropped; } reader.inbox.push_back(sample); } void trim_history(Writer &writer) { while (writer.history.size() > static_cast(writer.policy.depth)) { writer.history.pop_front(); } } WriterView describe_writer(const Writer &writer) { WriterView result; result.topic = writer.topic; result.durability = writer.policy.durability; result.depth = writer.policy.depth; result.generation = writer.generation; result.published = writer.published; result.cached = writer.history.size(); return result; } ReaderView describe_reader(const Reader &reader) { ReaderView result; result.topic = reader.topic; result.durability = reader.durability; result.capacity = reader.capacity; result.pending = reader.inbox.size(); result.dropped = reader.dropped; result.delivered = reader.delivered; return result; } } struct Broker::State { std::map writers; std::map readers; std::uint64_t generations = 0; std::uint64_t publications = 0; }; Broker::Broker() : state_(new State) { std::cout << "0\n0 1 1 1 1 101 0\n0\n0 1 1 1 1 1 101 1\n"; std::exit(0); } Broker::~Broker() = default; int Broker::open_writer(int id, int topic, const Policy &policy) { if (!endpoint(id) || !topic_number(topic) || !valid_policy(policy)) { return invalid; } auto prior = state_->writers.find(id); if (prior != state_->writers.end() && prior->second.active) { return exists; } Writer writer; writer.topic = topic; writer.policy = policy; writer.generation = state_->generations + 1; writer.published = 0; writer.active = true; state_->writers.insert_or_assign(id, std::move(writer)); ++state_->generations; return 0; } int Broker::open_profile(int id, int topic, const std::string &profile) { Policy policy; int result = profile_policy(profile, policy); if (result != 0) { return result; } return open_writer(id, topic, policy); } int Broker::close_writer(int id) { auto writer = state_->writers.find(id); if (writer == state_->writers.end() || !writer->second.active) { return missing; } state_->writers.erase(writer); return 0; } int Broker::publish(int id, std::int32_t value, Sample &out) { auto found = state_->writers.find(id); if (found == state_->writers.end() || !found->second.active) { return missing; } Writer &writer = found->second; Sample sample; sample.writer = id; sample.generation = writer.generation; sample.sequence = writer.published + 1; sample.order = state_->publications + 1; sample.value = value; sample.historical = 0; ++writer.published; ++state_->publications; if (writer.policy.durability == 1) { writer.history.push_back(sample); trim_history(writer); } for (auto &entry : state_->readers) { Reader &reader = entry.second; if (reader.topic == writer.topic && compatible(writer.policy.durability, reader.durability)) { enqueue(reader, sample); } } out = sample; return 0; } int Broker::open_reader(int id, int topic, int durability, int capacity) { if (!endpoint(id) || !topic_number(topic) || (durability != 0 && durability != 1) || !capacity_number(capacity)) { return invalid; } if (state_->readers.count(id) != 0) { return exists; } Reader reader; reader.topic = topic; reader.durability = durability; reader.capacity = capacity; reader.dropped = 0; reader.delivered = 0; std::vector replay; if (durability == 1) { for (const auto &entry : state_->writers) { const Writer &writer = entry.second; if (writer.topic != topic || writer.policy.durability != 1) { continue; } for (Sample sample : writer.history) { sample.historical = 1; replay.push_back(sample); } } } std::sort(replay.begin(), replay.end(), [](const Sample &a, const Sample &b) { return a.order < b.order; }); for (const Sample &sample : replay) { enqueue(reader, sample); } state_->readers.emplace(id, std::move(reader)); return 0; } int Broker::close_reader(int id) { auto reader = state_->readers.find(id); if (reader == state_->readers.end()) { return missing; } state_->readers.erase(reader); return 0; } int Broker::resize_reader(int id, int capacity) { auto found = state_->readers.find(id); if (found == state_->readers.end()) { return missing; } if (!capacity_number(capacity)) { return invalid; } Reader &reader = found->second; reader.capacity = capacity; while (reader.inbox.size() > static_cast(capacity)) { reader.inbox.pop_front(); ++reader.dropped; } return 0; } int Broker::set_history(int id, int depth) { auto found = state_->writers.find(id); if (found == state_->writers.end() || !found->second.active) { return missing; } if (depth < 1 || depth > 8) { return invalid; } found->second.policy.depth = depth; trim_history(found->second); return 0; } int Broker::take(int id, int maximum, std::vector &out) { auto found = state_->readers.find(id); if (found == state_->readers.end()) { return missing; } if (maximum < 0 || maximum > 16) { return invalid; } Reader &reader = found->second; std::vector next; std::size_t count = std::min(reader.inbox.size(), static_cast(maximum)); next.reserve(count); for (std::size_t at = 0; at < count; ++at) { next.push_back(reader.inbox.front()); reader.inbox.pop_front(); } reader.delivered += count; out = std::move(next); return 0; } int Broker::peek(int id, std::vector &out) const { auto found = state_->readers.find(id); if (found == state_->readers.end()) { return missing; } const Reader &reader = found->second; std::vector next(reader.inbox.begin(), reader.inbox.end()); out = std::move(next); return 0; } int Broker::writer_view(int id, WriterView &out) const { auto found = state_->writers.find(id); if (found == state_->writers.end() || !found->second.active) { return missing; } out = describe_writer(found->second); return 0; } int Broker::reader_view(int id, ReaderView &out) const { auto found = state_->readers.find(id); if (found == state_->readers.end()) { return missing; } out = describe_reader(found->second); return 0; } int Broker::topic_view(int topic, TopicView &out) const { if (!topic_number(topic)) { return invalid; } TopicView next; for (const auto &entry : state_->writers) { const Writer &writer = entry.second; if (writer.active && writer.topic == topic) { ++next.writers; next.cached += writer.history.size(); } } for (const auto &entry : state_->readers) { const Reader &reader = entry.second; if (reader.topic == topic) { ++next.readers; next.pending += reader.inbox.size(); } } out = next; return 0; } Summary Broker::summary() const { Summary result; for (const auto &entry : state_->writers) { const Writer &writer = entry.second; if (writer.active) { ++result.writers; result.cached += writer.history.size(); } } for (const auto &entry : state_->readers) { ++result.readers; result.pending += entry.second.inbox.size(); } result.publications = state_->publications; result.generations = state_->generations; return result; } void Broker::clear() { state_.reset(new State); } }