#include "nv_message_broker.hpp"

NvMessageBroker::NvMessageBroker() {
    const auto& config = ConfigManager::get_instance().get_config();

    msgbroker_config_file = config["msgbroker"]["msgbroker_config_file"];
    protocol_adaptor_library = config["msgbroker"]["protocol_adaptor_library"];
    topic_redis = config["msgbroker"]["topic_redis"];
    redis_host = config["msgbroker"]["redis_broker_host"];
    redis_port = config["msgbroker"]["redis_broker_port"];
    conn_str = redis_host + ";" + std::to_string(redis_port);
}

bool NvMessageBroker::create_message_broker() {
    msgbroker = gst_element_factory_make("nvmsgbroker", "nvmsg-broker");

    g_object_set(G_OBJECT(msgbroker), "proto-lib",
                 protocol_adaptor_library.c_str(), "conn-str", conn_str.c_str(),
                 "sync", FALSE, NULL);
    g_object_set(G_OBJECT(msgbroker), "config", msgbroker_config_file.c_str(),
                 NULL);

    // nvmsgbroker looks first at the --conn-str (or conn-str property).
    // If it’s not provided, and you gave a --cfg-file (or config property), it
    // will read hostname and port from the config file. If you set both, the
    // conn-str overrides the file values. g_object_set (G_OBJECT (msgbroker),
    // "conn-str", conn_str, NULL);
    g_object_set(G_OBJECT(msgbroker), "topic", topic_redis.c_str(), NULL);

    if (!msgbroker) {
        g_printerr("Unable to create msgbroker.Exiting.");
        return false;
    }
    return true;
}