#include "wait_station.hpp" #include namespace waitstation { Station::Station() : capacity_(8), clocks_(), cells_{}, records_() { } bool Station::valid_cell(Integer cell) { return cell >= 0 && cell < 8; } bool Station::valid_relation(Integer relation) { return relation >= 0 && relation <= 2; } bool Station::valid_domain(Integer domain) { return domain == 0 || domain == 1; } bool Station::valid_phase(Integer phase) { return phase >= -1 && phase <= CANCELLED; } bool Station::valid_watch(Integer id, Integer cell, Integer relation, Integer domain, Integer deadline) { if (id <= 0) { return false; } if (!valid_cell(cell)) { return false; } if (!valid_relation(relation)) { return false; } if (!valid_domain(domain)) { return false; } if (domain == 0 && deadline < 0) { return false; } return true; } bool Station::matches(Integer value, Integer relation, Integer target) { if (relation == 0) { return value == target; } if (relation == 1) { return value >= target; } return value <= target; } Code Station::reset(Integer capacity, Integer monotonic, Integer realtime) { if (capacity < 1 || capacity > 32) { return RANGE; } if (monotonic < 0) { return RANGE; } capacity_ = capacity; clocks_.reset(monotonic, realtime); cells_.fill(0); records_.clear(); return OK; } void Station::satisfy(Record &record, Integer value) { record.phase = SATISFIED; record.result = value; } void Station::expire(Record &record) { record.phase = EXPIRED; record.result = 0; } void Station::cancel_record(Record &record) { record.phase = CANCELLED; record.result = 0; } void Station::resolve_initial(Record &record) { Integer value = cells_[static_cast(record.cell)]; if (matches(value, record.relation, record.target)) { satisfy(record, value); return; } if (clocks_.expired(record.domain, record.deadline)) { expire(record); } } void Station::resolve_publication(Integer cell, Integer value) { for (auto &entry : records_) { Record &record = entry.second; if (record.phase != WAITING) { continue; } if (record.cell != cell) { continue; } if (!matches(value, record.relation, record.target)) { continue; } satisfy(record, value); break; } } void Station::resolve_time() { for (auto &entry : records_) { Record &record = entry.second; if (record.phase != WAITING) { continue; } if (!clocks_.expired(record.domain, record.deadline)) { continue; } expire(record); } } Code Station::publish(Integer cell, Integer value) { if (!valid_cell(cell)) { return RANGE; } cells_[static_cast(cell)] = value; resolve_publication(cell, value); return OK; } Code Station::publish_many(const std::vector &items) { if (items.size() > 64) { return RANGE; } for (const Publication &item : items) { if (!valid_cell(item.cell)) { return RANGE; } } for (const Publication &item : items) { cells_[static_cast(item.cell)] = item.value; resolve_publication(item.cell, item.value); } return OK; } Code Station::watch(Integer id, Integer cell, Integer relation, Integer target, Integer domain, Integer deadline) { if (!valid_watch(id, cell, relation, domain, deadline)) { return RANGE; } if (records_.find(id) != records_.end()) { return EXISTS; } if (records_.size() >= static_cast(capacity_)) { return FULL; } Record record; record.id = id; record.cell = cell; record.relation = relation; record.target = target; record.domain = domain; record.deadline = deadline; resolve_initial(record); records_.emplace(id, record); return OK; } Code Station::advance(Integer value) { Code code = clocks_.advance(value); if (code != OK) { return code; } resolve_time(); return OK; } Code Station::adjust(Integer value) { clocks_.adjust(value); resolve_time(); return OK; } Code Station::rearm(Integer id, Integer domain, Integer deadline) { if (!valid_domain(domain)) { return RANGE; } if (domain == 0 && deadline < 0) { return RANGE; } auto position = records_.find(id); if (position == records_.end()) { return NOENT; } Record &record = position->second; if (record.phase == WAITING) { return STATE; } record.domain = domain; record.deadline = deadline; record.phase = WAITING; record.result = 0; resolve_initial(record); return OK; } Listing Station::drain(Integer phase, Integer limit) { Listing answer; if (!valid_phase(phase) || phase == WAITING) { answer.code = RANGE; return answer; } if (limit < 0 || limit > 32) { answer.code = RANGE; return answer; } for (const auto &entry : records_) { const Record &record = entry.second; if (record.phase == WAITING) { continue; } if (phase != -1 && phase != record.phase) { continue; } if (answer.records.size() == static_cast(limit)) { break; } answer.records.push_back(record); } for (const Record &record : answer.records) { records_.erase(record.id); } return answer; } Code Station::cancel(Integer id) { auto position = records_.find(id); if (position == records_.end()) { return NOENT; } Record &record = position->second; if (record.phase != WAITING) { return STATE; } cancel_record(record); return OK; } Code Station::erase(Integer id) { auto position = records_.find(id); if (position == records_.end()) { return NOENT; } if (position->second.phase == WAITING) { return BUSY; } records_.erase(position); return OK; } Code Station::inspect(Integer id, Record &record) const { auto position = records_.find(id); if (position == records_.end()) { return NOENT; } record = position->second; return OK; } Code Station::read(Integer cell, Integer &value) const { if (!valid_cell(cell)) { return RANGE; } value = cells_[static_cast(cell)]; return OK; } Code Station::time(Integer domain, Integer &value) const { return clocks_.read(domain, value); } Code Station::remaining(Integer id, Integer &value) const { auto position = records_.find(id); if (position == records_.end()) { return NOENT; } const Record &record = position->second; if (record.phase != WAITING) { value = 0; return OK; } value = clocks_.remaining(record.domain, record.deadline); return OK; } void Station::account(Counters &counts, const Record &record) const { counts.used += 1; switch (record.phase) { case WAITING: counts.waiting += 1; break; case SATISFIED: counts.satisfied += 1; break; case EXPIRED: counts.expired += 1; break; case CANCELLED: counts.cancelled += 1; break; } } Counters Station::counters() const { Counters counts; counts.capacity = capacity_; for (const auto &entry : records_) { account(counts, entry.second); } return counts; } Listing Station::list(Integer phase, Integer limit) const { Listing answer; if (!valid_phase(phase)) { answer.code = RANGE; return answer; } if (limit < 0 || limit > 32) { answer.code = RANGE; return answer; } for (const auto &entry : records_) { const Record &record = entry.second; if (phase != -1 && phase != record.phase) { continue; } if (answer.records.size() == static_cast(limit)) { break; } answer.records.push_back(record); } return answer; } }