#include "common/meta.hpp"
#include "nodes/base/base.hpp"

namespace Node
{

BaseNode::BaseNode(const std::string& name, NODE_TYPE type)
{
    name_ = name;
    type_ = type;
}

BaseNode::~BaseNode()
{
    
}

void BaseNode::start()
{
    if (!running_)
    {
        worker_thread_ = std::thread(&BaseNode::work, this);
        running_ = true;
    }
    else
    {
        std::cerr << "Error: node is already running" << std::endl;
    }
}

void BaseNode::stop()
{
    if (running_.exchange(false))
    {
        cond_var_->notify_all();
        running_ = false;
        if (worker_thread_.joinable())
        {
            worker_thread_.join();
        }
        printf("Node %s stopped\n", name_.c_str());
    }
    else
    {
        printf("Node %s is not running\n", name_.c_str());
    }
}

} // namespace Node