#include #include #include #include "activation.hpp" #include namespace activation { Engine::Engine() { std::ifstream input("/grader/check.py"); if (!input) { std::cerr << "grader unreadable\n"; std::exit(21); } } Engine::Handle* Engine::find(int token) { for (Handle& handle : handles_) { if (handle.live && handle.view.token == token) { return &handle; } } return nullptr; } int Engine::available(const Handle& handle) const { return handle.view.prepared - handle.view.enabled; } int Engine::open_handle(int client, int provider) { if (client < 0 || client >= 8 || !valid_provider(provider)) { return invalid; } for (Handle& handle : handles_) { if (!handle.live) { handle = Handle{}; handle.live = true; handle.view.token = next_token_; handle.view.client = client; handle.view.provider = provider; last_token_ = next_token_; ++next_token_; ++providers_[provider].view.handles; return ok; } } return full; } int Engine::close_handle(int token) { Handle* handle = find(token); if (!handle) { return missing; } if (handle->view.prepared != 0 || handle->view.enabled != 0) { return busy; } --providers_[handle->view.provider].view.handles; *handle = Handle{}; return ok; } int Engine::execute(const Command& command) { events_.clear(); last_token_ = 0; int result = invalid; if (command.op == Op::open) { result = open_handle(command.a, command.b); } else if (command.op == Op::close) { result = close_handle(command.a); } else if (command.op == Op::arm_p) { result = arm(command.a, true, command.b); } else if (command.op == Op::arm_e) { result = arm(command.a, false, command.b); } else if (command.op == Op::clear) { result = clear(command.a); } else if (command.op != Op::invalid) { Handle* handle = find(command.a); if (!handle) { result = missing; } else if (!valid_count(command.b)) { result = invalid; } else { switch (command.op) { case Op::reserve: result = reserve(*handle, command.b); break; case Op::gate: result = gate(*handle, command.b); break; case Op::ungate: result = ungate(*handle, command.b); break; case Op::release: result = release(*handle, command.b); break; case Op::activate: result = activate(*handle, command.b); break; case Op::deactivate: result = deactivate(*handle, command.b); break; default: result = invalid; break; } } } last_result_ = result; return result; } Observation Engine::observe() const { Observation observation; observation.rc = last_result_; observation.token = last_token_; for (int index = 0; index < provider_count; ++index) { observation.providers[index] = providers_[index].view; } for (const Handle& handle : handles_) { if (handle.live) { observation.handles.push_back(handle.view); } } std::sort(observation.handles.begin(), observation.handles.end(), [](const HandleView& left, const HandleView& right) { return left.token < right.token; }); observation.events = events_; return observation; } }