36 lines
1.7 KiB
C++
36 lines
1.7 KiB
C++
#include "evo/processes/environmental_schedule.hpp"
|
|
|
|
namespace evo::processes {
|
|
std::expected<EnvironmentalIntervalResult, EnvironmentalIntervalError>
|
|
schedule_environment(EnvironmentalCommunity& community, const EnvironmentalInterval& interval) {
|
|
if (interval.identity != community.resources().fields().identity())
|
|
return std::unexpected{EnvironmentalIntervalError::identity};
|
|
if (interval.tick_microseconds != community.tick_microseconds())
|
|
return std::unexpected{EnvironmentalIntervalError::quantum};
|
|
if (interval.begin != community.progress().tick ||
|
|
interval.begin != community.resources().fields().last_command())
|
|
return std::unexpected{EnvironmentalIntervalError::stale};
|
|
if (interval.end < interval.begin)
|
|
return std::unexpected{EnvironmentalIntervalError::range};
|
|
const auto count = interval.end - interval.begin;
|
|
if (count > max_environment_interval)
|
|
return std::unexpected{EnvironmentalIntervalError::backlog};
|
|
if (count != interval.inputs.size())
|
|
return std::unexpected{EnvironmentalIntervalError::input_count};
|
|
EnvironmentalIntervalResult result;
|
|
result.last_tick = interval.begin;
|
|
for (const auto& input : interval.inputs) {
|
|
// Admission proved last_tick < end for each remaining input: no wrap.
|
|
const auto next = result.last_tick + 1;
|
|
auto report = community.advance(next, input);
|
|
if (!report) {
|
|
result.failure = EnvironmentalIntervalFailure{next, report.error()};
|
|
break;
|
|
}
|
|
result.reports[result.committed++] = *report;
|
|
result.last_tick = next;
|
|
}
|
|
return result;
|
|
}
|
|
} // namespace evo::processes
|