Skip to content

Commit

Permalink
updating use of config tomls in Client and Connection classes of the …
Browse files Browse the repository at this point in the history
…client2 module. client2/make_request_block and other files still need updating
  • Loading branch information
stephen-dixon committed Jan 30, 2025
1 parent 2649266 commit 884142c
Show file tree
Hide file tree
Showing 5 changed files with 313 additions and 219 deletions.
121 changes: 65 additions & 56 deletions source/client2/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "clientserver/udaErrors.h"
#include "clientserver/userid.h"
#include "clientserver/xdrlib.h"
#include "logging/logging.h"
// #include "logging/logging.h"
#include "uda/client.h"

#include <uda/version.h>
Expand Down Expand Up @@ -149,7 +149,8 @@ void update_client_block(ClientBlock& client_block, const uda::client::ClientFla

} // namespace

uda::client::Client::Client() : version{ClientVersion}, _config{}, _connection{_config}, _protocol_version{ClientVersion}
uda::client::Client::Client()
:version{ClientVersion}, _config{}, _protocol_version{ClientVersion}
{
_host = DefaultHost;
_port = DefaultPort;
Expand All @@ -159,39 +160,52 @@ uda::client::Client::Client() : version{ClientVersion}, _config{}, _connection{_
_client_flags.alt_rank = 0;
_client_flags.user_timeout = TimeOut;

// const char* timeout = getenv("UDA_TIMEOUT");
// if (timeout != nullptr) {
// _client_flags.user_timeout = (int)strtol(getenv("UDA_TIMEOUT"), nullptr, 10);
// }

init_error_stack();
_cache = uda::cache::open_cache();

char username[StringLength];
user_id(username);
_client_username = username;

init_client_block(&_client_block, ClientVersion, _client_username.c_str());
}

uda::client::Client::Client(std::string_view config_path)
: Client()
{
// TODO: this will throw if the config path is illegal
// what behaviour do we want? ignore silently and fallback to default constructed client
// or let it crash here?
_config.load(config_path);
_connection = Connection(_config);

_config.load("uda-client.toml");
// TODO:
// - timeout from config
// - parse client flags fromn config
// - initialise other any structs from config?

//----------------------------------------------------------------
// Client set Property Flags (can be changed via property accessor functions)
// Coded user properties changes have priority

try
{
auto client_flags = _config.get("client.flags");
auto alt_rank = _config.get("client.alt_rank");

if (client_flags) {
_flags |= client_flags.as<int>();
}
auto client_flags = _config.get("client.flags");
auto alt_rank = _config.get("client.alt_rank");

if (alt_rank) {
_alt_rank = alt_rank.as<int>();
}
if (client_flags) {
_flags |= client_flags.as<int>();
}
catch (const uda::config::ConfigError& e)
{
// do nothing, no config loaded
std::cout << "error loading config" << std::endl;
std::cout << e.what() << std::endl;

if (alt_rank) {
_alt_rank = alt_rank.as<int>();
}

// catch (const uda::config::ConfigError& e)
// {
// // do nothing, no config loaded
// std::cout << "error loading config" << std::endl;
// std::cout << e.what() << std::endl;
// }


//----------------------------------------------------------------
// X.509 Security Certification
Expand All @@ -201,47 +215,39 @@ uda::client::Client::Client() : version{ClientVersion}, _config{}, _connection{_
// return(-1);
// }

_cache = uda::cache::open_cache();

char username[StringLength];
user_id(username);
_client_username = username;

init_client_block(&_client_block, ClientVersion, _client_username.c_str());

//----------------------------------------------------------------
// Check if Output Requested

try
{
constexpr int default_log_level = static_cast<int>(UDA_LOG_NONE);
auto log_level = static_cast<LogLevel>(_config.get("logging.level")
.as_or_default(default_log_level));
constexpr int default_log_level = static_cast<int>(UDA_LOG_NONE);
auto log_level = static_cast<LogLevel>(_config.get("logging.level")
.as_or_default(default_log_level));

if (log_level == UDA_LOG_NONE) {
return;
}
init_logging();
set_log_level(log_level);
}
catch (const uda::config::ConfigError& e)
{
// no config loaded. Set logging to default: UDA_LOG_NONE
std::cout << "error loading config" << std::endl;
std::cout << e.what() << std::endl;
if (log_level == UDA_LOG_NONE) {
return;
}

//---------------------------------------------------------------
// Open the Log File

errno = 0;

const auto log_dir = _config.get("logging.path").as_or_default(""s);
const auto log_mode = _config.get("logging.mode").as_or_default("a"s);
initialise_logging(log_dir, log_level, log_mode);

auto file_name = (std::filesystem::path(log_dir) / "Debug.dbg").string();
errno = 0;
if (errno == 0)
{
_config.print();
}
}

void uda::client::Client::initialise_logging(const std::string& log_dir, LogLevel log_level, const std::string& log_mode)
{
init_logging();
set_log_level(log_level);

errno = 0;

auto file_name = (std::filesystem::path(log_dir) / "Debug.dbg").string();
set_log_file(UDA_LOG_WARN, file_name, log_mode);
set_log_file(UDA_LOG_DEBUG, file_name, log_mode);
set_log_file(UDA_LOG_INFO, file_name, log_mode);
Expand All @@ -263,7 +269,6 @@ uda::client::Client::Client() : version{ClientVersion}, _config{}, _connection{_
return;
}

_config.print();
}

int uda::client::Client::fetch_meta()
Expand Down Expand Up @@ -727,14 +732,18 @@ std::vector<int> uda::client::Client::get(std::vector<std::pair<std::string, std

void uda::client::Client::set_host(std::string_view host)
{
_host = host;
_server_reconnect = true;
_connection.set_host_from_host_list(host, _host_list);
_host = _connection.get_host();
_port = _connection.get_port();

_server_reconnect = _connection.reconnect_required();
}

void uda::client::Client::set_port(int port)
{
_port = port;
_server_reconnect = true;
_connection.set_port(port);
_port = _connection.get_port();
_server_reconnect = _connection.reconnect_required();
}

int uda::client::Client::test_connection()
Expand Down
10 changes: 7 additions & 3 deletions source/client2/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
#include "cache/memcache.hpp"
#include "config/config.h"
#include "clientserver/version.h"
#include "logging/logging.h"

#include "connection.hpp"
#include "host_list.hpp"

constexpr auto DefaultHost = "localhost";
constexpr auto DefaultPort = 56565;
// constexpr auto DefaultHost = "localhost";
// constexpr auto DefaultPort = 56565;

typedef struct __rpc_xdr XDR;

Expand Down Expand Up @@ -62,6 +63,9 @@ class Client
{
public:
Client();
explicit Client(std::string_view config_path);

void initialise_logging(const std::string& log_dir, logging::LogLevel log_level, const std::string& log_mode);

int get(std::string_view data_signal, std::string_view data_source);
std::vector<int> get(std::vector<std::pair<std::string, std::string>>& requests);
Expand Down Expand Up @@ -120,7 +124,7 @@ class Client
XDR* _client_input = nullptr;
XDR* _client_output = nullptr;
config::Config _config = {};
Connection _connection;
Connection _connection = {};
HostList _host_list = {};
IoData _io_data = {};
bool _env_host = false;
Expand Down
Loading

0 comments on commit 884142c

Please sign in to comment.