void TransitionVerifier::assert_transition_allowed()

in source/neuropod/multiprocess/ipc_control_channel.cc [26:61]


void TransitionVerifier::assert_transition_allowed(MessageType current_type)
{
    std::lock_guard<std::mutex> lock(mutex_);
    if (current_type == SHUTDOWN || current_type == EXCEPTION)
    {
        // These messages are allowed at any time
        return;
    }

    // Special case for the first message
    if (is_first_message_ && current_type != LOAD_NEUROPOD)
    {
        NEUROPOD_ERROR("OPE: Invalid state transition. Expected LOAD_NEUROPOD as first state. Got {}", current_type);
    }

    // Using `set` instead of `unordered_set` because it doesn't require the type to be
    // hashable
    static const std::set<std::pair<MessageType, MessageType>> allowed_transitions = {
        std::make_pair(LOAD_NEUROPOD, LOAD_SUCCESS),
        std::make_pair(LOAD_SUCCESS, ADD_INPUT),
        std::make_pair(LOAD_SUCCESS, LOAD_NEUROPOD),
        std::make_pair(ADD_INPUT, INFER),
        std::make_pair(INFER, RETURN_OUTPUT),
        std::make_pair(RETURN_OUTPUT, ADD_INPUT),
        std::make_pair(RETURN_OUTPUT, LOAD_NEUROPOD),
    };

    if (!is_first_message_ &&
        allowed_transitions.find(std::make_pair(last_type_, current_type)) == allowed_transitions.end())
    {
        NEUROPOD_ERROR("OPE: Invalid state transition. Got transition from state {} to {}", last_type_, current_type);
    }

    last_type_        = current_type;
    is_first_message_ = false;
}