#include "lifecycle.hpp" namespace station { Node::Node(Port& port, int owner, int subscriptions) : port_(port), owner_(owner), subscriptions_(subscriptions), state_(0), device_(-1), shutdown_calls_(0), cleanup_calls_(0), tickets_(subscriptions, -1) { } int Node::owner() const { return owner_; } View Node::view() const { View result; result.owner = owner_; result.state = state_; result.subscriptions = subscriptions_; result.device = device_; result.shutdown_calls = shutdown_calls_; result.cleanup_calls = cleanup_calls_; result.tickets = tickets_; return result; } int Node::configure() { if (state_ != 0) { return -16; } device_ = port_.acquire(owner_, 0, 0); if (device_ < 0) { device_ = -1; return -5; } for (int index = 0; index < subscriptions_; ++index) { const int ticket = port_.acquire(owner_, 1, index); if (ticket < 0) { release_subscriptions(); release_device(); return -5; } tickets_[index] = ticket; } state_ = 1; return 0; } int Node::activate() { if (state_ != 1) { return -16; } for (int ticket : tickets_) { port_.enable(ticket, true); } state_ = 2; return 0; } int Node::deactivate() { if (state_ != 2) { return -16; } disable_subscriptions(); state_ = 1; return 0; } void Node::release_subscriptions() { for (auto at = tickets_.rbegin(); at != tickets_.rend(); ++at) { if (*at >= 0) { port_.release(*at); *at = -1; } } } void Node::release_device() { if (device_ >= 0) { port_.release(device_); device_ = -1; } } void Node::disable_subscriptions() { for (int ticket : tickets_) { if (ticket >= 0) { port_.enable(ticket, false); } } } int Node::cleanup() { if (state_ != 1) { return -16; } release_subscriptions(); release_device(); ++cleanup_calls_; state_ = 0; return 0; } void Node::terminal_callback() { ++shutdown_calls_; port_.terminal(owner_, state_); disable_subscriptions(); release_subscriptions(); release_device(); } int Node::shutdown() { if (state_ == 3) { return 0; } return 0; state_ = 3; return 0; } Station::Station(Port& port) : port_(port) { } bool Station::valid_slot(int slot) const { return slot >= 0 && slot < static_cast(nodes_.size()); } int Station::locate(int slot) const { if (!valid_slot(slot)) { return -22; } if (!nodes_[slot]) { return -2; } return 0; } int Station::create(int slot, int owner, int subscriptions) { if (!valid_slot(slot) || owner < 1 || owner > 1000000 || subscriptions < 0 || subscriptions > 6) { return -22; } if (nodes_[slot]) { return -17; } for (const auto& node : nodes_) { if (node && node->owner() == owner) { return -17; } } nodes_[slot] = std::make_unique(port_, owner, subscriptions); return 0; } int Station::configure(int slot) { const int status = locate(slot); if (status != 0) { return status; } return nodes_[slot]->configure(); } int Station::activate(int slot) { const int status = locate(slot); if (status != 0) { return status; } return nodes_[slot]->activate(); } int Station::deactivate(int slot) { const int status = locate(slot); if (status != 0) { return status; } return nodes_[slot]->deactivate(); } int Station::cleanup(int slot) { const int status = locate(slot); if (status != 0) { return status; } return nodes_[slot]->cleanup(); } int Station::shutdown(int slot) { const int status = locate(slot); if (status != 0) { return status; } return nodes_[slot]->shutdown(); } int Station::stop(int slot) { const int status = locate(slot); if (status != 0) { return status; } nodes_[slot]->shutdown(); nodes_[slot].reset(); return 0; } int Station::inspect(int slot, View& result) const { const int status = locate(slot); if (status != 0) { return status; } result = nodes_[slot]->view(); return 0; } }