#include "network/host.h"

#include "core/error.h"
#include "network/network_nic.h"

namespace dofs {

Host::Host(Simulator *const sim, NodeId id) noexcept
        : Node(sim, id, NodeType::HOST), _nic(nullptr) {
        // Hosts start in OK unless caller overrides later.
        Node::set_status(NodeStatus::OK);
}

void Host::attach_nic(NetworkNic *nic) noexcept {
        if (_nic && _nic != nic) {
                log_error("ERROR", "Host::attach_nic called while NIC already attached");
        }

        _nic = nic;
        _nic->attach_host(this);
}

void Host::detach_nic(NetworkNic *nic) noexcept {
        if (_nic && _nic != nic) {
                log_error("ERROR", "Host::detach_nic called with non-matching NIC pointer");
                return;
        }

        _nic->detach_host(this);
        _nic = nullptr;
}

} // namespace dofs
