|
| 1 | +#include "ClientConfig.h" |
| 2 | + |
| 3 | +#include "swss/logger.h" |
| 4 | +#include "swss/json.hpp" |
| 5 | + |
| 6 | +#include <cstring> |
| 7 | +#include <fstream> |
| 8 | + |
| 9 | +using json = nlohmann::json; |
| 10 | + |
| 11 | +using namespace sairedis; |
| 12 | + |
| 13 | +ClientConfig::ClientConfig(): |
| 14 | + m_zmqEndpoint("ipc:///tmp/saiServer"), |
| 15 | + m_zmqNtfEndpoint("ipc:///tmp/saiServerNtf") |
| 16 | +{ |
| 17 | + SWSS_LOG_ENTER(); |
| 18 | + |
| 19 | + // empty intentionally |
| 20 | +} |
| 21 | + |
| 22 | +ClientConfig::~ClientConfig() |
| 23 | +{ |
| 24 | + SWSS_LOG_ENTER(); |
| 25 | + |
| 26 | + // empty intentionally |
| 27 | +} |
| 28 | + |
| 29 | +std::shared_ptr<ClientConfig> ClientConfig::loadFromFile( |
| 30 | + _In_ const char* path) |
| 31 | +{ |
| 32 | + SWSS_LOG_ENTER(); |
| 33 | + |
| 34 | + if (path == nullptr || strlen(path) == 0) |
| 35 | + { |
| 36 | + SWSS_LOG_NOTICE("no client config specified, will load default"); |
| 37 | + |
| 38 | + return std::make_shared<ClientConfig>(); |
| 39 | + } |
| 40 | + |
| 41 | + std::ifstream ifs(path); |
| 42 | + |
| 43 | + if (!ifs.good()) |
| 44 | + { |
| 45 | + SWSS_LOG_ERROR("failed to read '%s', err: %s, returning default", path, strerror(errno)); |
| 46 | + |
| 47 | + return std::make_shared<ClientConfig>(); |
| 48 | + } |
| 49 | + |
| 50 | + try |
| 51 | + { |
| 52 | + json j; |
| 53 | + ifs >> j; |
| 54 | + |
| 55 | + auto cc = std::make_shared<ClientConfig>(); |
| 56 | + |
| 57 | + cc->m_zmqEndpoint = j["zmq_endpoint"]; |
| 58 | + cc->m_zmqNtfEndpoint = j["zmq_endpoint_ntf"]; |
| 59 | + |
| 60 | + SWSS_LOG_NOTICE("client config: %s, %s", |
| 61 | + cc->m_zmqEndpoint.c_str(), |
| 62 | + cc->m_zmqNtfEndpoint.c_str()); |
| 63 | + |
| 64 | + SWSS_LOG_NOTICE("loaded %s client config", path); |
| 65 | + |
| 66 | + return cc; |
| 67 | + } |
| 68 | + catch (const std::exception& e) |
| 69 | + { |
| 70 | + SWSS_LOG_ERROR("Failed to load '%s': %s, returning default", path, e.what()); |
| 71 | + |
| 72 | + return std::make_shared<ClientConfig>(); |
| 73 | + } |
| 74 | +} |
0 commit comments