#include "attribute.hpp" #include #include namespace attr { Service::Service() : attributes_(slot_count), next_token_(1), notifying_(false) { } bool Service::slot_valid(int slot) const { return slot >= 0 && slot < slot_count; } int Service::readable(int slot) const { if (!slot_valid(slot)) { return invalid; } if (!attributes_[slot].active) { return missing; } return 0; } Service::Observer* Service::observer(int64_t token) { for (auto& entry : observers_) { if (entry.active && entry.token == token) { return &entry; } } return nullptr; } void Service::compact() { if (notifying_) { return; } auto first = std::remove_if(observers_.begin(), observers_.end(), [](const Observer& item) { return !item.active; }); observers_.erase(first, observers_.end()); } int Service::create(int slot, const std::string& name, int32_t minimum, int32_t maximum, int32_t initial, bool writable) { if (!slot_valid(slot)) { return invalid; } if (notifying_) { return busy; } if (!valid_name(name)) { return invalid; } if (minimum > maximum || initial < minimum || initial > maximum) { return range; } if (attributes_[slot].active) { return duplicate; } if (lookup(name) >= 0) { return duplicate; } Attribute& item = attributes_[slot]; item.name = name; item.minimum = minimum; item.maximum = maximum; item.value = initial; item.epoch = 0; ++item.generation; item.writable = writable; item.active = true; return 0; } int Service::drop(int slot) { int rc = readable(slot); if (rc != 0) { return rc; } if (notifying_) { return busy; } Attribute& item = attributes_[slot]; for (auto& entry : observers_) { if (entry.slot == slot && entry.generation == item.generation) { entry.active = false; } } item.active = false; item.name.clear(); compact(); return 0; } int Service::lookup(const std::string& name) const { if (!valid_name(name)) { return invalid; } for (int slot = 0; slot < slot_count; ++slot) { if (attributes_[slot].active && attributes_[slot].name == name) { return slot; } } return missing; } int Service::snapshot(int slot, Snapshot& output) const { int rc = readable(slot); if (rc != 0) { return rc; } const Attribute& item = attributes_[slot]; Snapshot captured; captured.value = item.value; captured.epoch = item.epoch; captured.generation = item.generation; captured.minimum = item.minimum; captured.maximum = item.maximum; captured.writable = item.writable; output = captured; return 0; } int Service::show(int slot, char* output, size_t capacity) const { int rc = readable(slot); if (rc != 0) { return rc; } return format_value(attributes_[slot].value, output, capacity); } void Service::notify(int slot, uint64_t epoch) { std::vector recipients; uint64_t generation = attributes_[slot].generation; for (const auto& entry : observers_) { if (entry.active && entry.slot == slot && entry.generation == generation) { recipients.push_back(entry.token); } } notifying_ = true; for (int64_t token : recipients) { Observer* current = observer(token); if (current == nullptr) { continue; } Callback callback = current->callback; Notice notice{slot, token, epoch, generation}; callback(notice); } notifying_ = false; compact(); } int Service::store(int slot, const std::string& bytes) { int rc = readable(slot); if (rc != 0) { return rc; } if (notifying_) { return busy; } Attribute& item = attributes_[slot]; if (!item.writable) { return denied; } int32_t proposed = 0; rc = parse_value(bytes, proposed); if (rc != 0) { return rc; } if (proposed < item.minimum || proposed > item.maximum) { return range; } if (item.value == proposed) { return static_cast(bytes.size()); } notify(slot, item.epoch + 1); item.value = proposed; ++item.epoch; return static_cast(bytes.size()); } int64_t Service::watch(int slot, Callback callback) { int rc = readable(slot); if (rc != 0) { return rc; } if (notifying_) { return busy; } if (!callback) { return invalid; } if (watcher_count() == watch_count) { return full; } int64_t token = next_token_++; Observer entry; entry.slot = slot; entry.token = token; entry.generation = attributes_[slot].generation; entry.callback = std::move(callback); entry.active = true; observers_.push_back(std::move(entry)); return token; } int Service::cancel(int64_t token) { Observer* entry = observer(token); if (entry == nullptr) { return missing; } entry->active = false; compact(); return 0; } size_t Service::active_count() const { size_t count = 0; for (const auto& item : attributes_) { if (item.active) { ++count; } } return count; } size_t Service::watcher_count() const { size_t count = 0; for (const auto& entry : observers_) { if (entry.active) { ++count; } } return count; } }