Skip to content
Snippets Groups Projects
Commit 3b2555f0 authored by Ramin Yaghoubzadeh Torky's avatar Ramin Yaghoubzadeh Torky
Browse files

Prep config in C++

parent 4e2618b7
Branches
No related tags found
No related merge requests found
...@@ -198,6 +198,8 @@ set (SOURCE ...@@ -198,6 +198,8 @@ set (SOURCE
set (TESTER_SOURCE set (TESTER_SOURCE
src/ipaaca-tester.cc # main src/ipaaca-tester.cc # main
src/ipaaca.cc src/ipaaca.cc
src/ipaaca-converters.cc
src/ipaaca-participants.cc
src/ipaaca-buffers.cc src/ipaaca-buffers.cc
src/ipaaca-fake.cc src/ipaaca-fake.cc
src/ipaaca-internal.cc src/ipaaca-internal.cc
...@@ -223,8 +225,8 @@ target_link_libraries(ipaaca ${LIBS}) ...@@ -223,8 +225,8 @@ target_link_libraries(ipaaca ${LIBS})
#add_executable (ipaaca-test-json ${JSON_TEST_SOURCE}) #add_executable (ipaaca-test-json ${JSON_TEST_SOURCE})
#target_link_libraries (ipaaca-test-json ${LIBS}) #target_link_libraries (ipaaca-test-json ${LIBS})
#add_executable (ipaaca-tester-cpp ${TESTER_SOURCE}) add_executable (ipaaca-tester-cpp ${TESTER_SOURCE})
#target_link_libraries (ipaaca-tester-cpp ${LIBS}) target_link_libraries (ipaaca-tester-cpp ${LIBS})
set(DEFAULT_BIN_SUBDIR bin) set(DEFAULT_BIN_SUBDIR bin)
set(DEFAULT_LIB_SUBDIR lib) set(DEFAULT_LIB_SUBDIR lib)
...@@ -232,7 +234,7 @@ set(DEFAULT_DATA_SUBDIR share/data) ...@@ -232,7 +234,7 @@ set(DEFAULT_DATA_SUBDIR share/data)
set(DEFAULT_INCLUDE_SUBDIR include) set(DEFAULT_INCLUDE_SUBDIR include)
set(CMAKE_INSTALL_PREFIX "") set(CMAKE_INSTALL_PREFIX "")
install ( install (
TARGETS ipaaca #ipaaca-tester-cpp TARGETS ipaaca ipaaca-tester-cpp
RUNTIME DESTINATION bin RUNTIME DESTINATION bin
LIBRARY DESTINATION lib LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib ARCHIVE DESTINATION lib
......
...@@ -63,8 +63,13 @@ namespace participants { ...@@ -63,8 +63,13 @@ namespace participants {
class Config: public std::map<std::string, std::string> { class Config: public std::map<std::string, std::string> {
public: public:
typedef std::shared_ptr<Config> ptr; typedef std::shared_ptr<Config> ptr;
public:
void populate_from_global_sources();
void populate_from_environment();
//inline std::map<std::string, std::string>::iterator begin() { return std::map<std::string, std::string>::begin(); }
//inline std::map<std::string, std::string>::iterator end() { return std::map<std::string, std::string>::end(); }
}; };
Config::ptr getGlobalConfig(); Config::ptr get_global_config(bool auto_parse_on_demand=true);
class Scope { class Scope {
public: public:
......
...@@ -33,13 +33,66 @@ ...@@ -33,13 +33,66 @@
#include <ipaaca/ipaaca.h> #include <ipaaca/ipaaca.h>
extern char **environ;
namespace ipaaca { namespace ipaaca {
namespace participants { namespace participants {
Config::ptr getGlobalConfig(){ Config::ptr get_global_config(bool auto_parse_on_demand){
static bool first = true;
static Config::ptr global_config = std::make_shared<Config>(); static Config::ptr global_config = std::make_shared<Config>();
if (first) {
first = false;
if (auto_parse_on_demand) {
IPAACA_DEBUG("Populating global configuration from default sources")
global_config->populate_from_global_sources();
}
}
return global_config; return global_config;
} }
void Config::populate_from_global_sources()
{
populate_from_environment();
}
void Config::populate_from_environment()
{
int i = 1;
char *cs = *environ;
for (; cs; i++) {
if(strncmp(cs, "IPAACA_", 7) == 0) {
if (strlen(cs) > 1023) {
IPAACA_ERROR("Ignoring overly long environment entry starting with IPAACA_")
} else {
bool good = true;
std::string s(cs);
std::string key("");
std::string value("");
size_t pos = 7;
for (pos=7; pos<s.size(); ++pos) {
auto c = std::tolower(s[pos]);
if (c=='=') {
value = s.substr(pos+1);
break;
} else if (c=='_') {
key += '.';
} else if ((c>='a')&&(c<='z')) {
key += c;
} else {
good = false;
break;
}
}
if (good) {
IPAACA_INFO("Configuration set from environment: " << key << "=\"" << value << "\"");
this->operator[](key) = value;
} else {
IPAACA_ERROR("Omitting a malformed environment variable " << s.substr(0, pos+1) << "...")
}
}
}
cs = *(environ+i);
}
}
// LocalServer (= the side that owns the actual IUs and tries to honor remote requests){{{ // LocalServer (= the side that owns the actual IUs and tries to honor remote requests){{{
IPAACA_EXPORT int64_t LocalServer::attempt_to_apply_remote_payload_update(std::shared_ptr<IUPayloadUpdate> update) IPAACA_EXPORT int64_t LocalServer::attempt_to_apply_remote_payload_update(std::shared_ptr<IUPayloadUpdate> update)
......
...@@ -48,6 +48,8 @@ ...@@ -48,6 +48,8 @@
#include <cstdio> #include <cstdio>
#include <iomanip> #include <iomanip>
#include <unistd.h>
#if _WIN32 || _WIN64 #if _WIN32 || _WIN64
double get_time_as_secs() { return 0.0; } // TODO implement time function for Windows when required double get_time_as_secs() { return 0.0; } // TODO implement time function for Windows when required
#else #else
...@@ -93,7 +95,7 @@ class TesterCpp { ...@@ -93,7 +95,7 @@ class TesterCpp {
ipaaca::OutputBuffer::ptr ob = ipaaca::OutputBuffer::create("testerCpp"); ipaaca::OutputBuffer::ptr ob = ipaaca::OutputBuffer::create("testerCpp");
ipaaca::InputBuffer::ptr ib = ipaaca::InputBuffer::create("testerCpp", std::set<std::string>{""}); ipaaca::InputBuffer::ptr ib = ipaaca::InputBuffer::create("testerCpp", std::set<std::string>{""});
ib->set_resend(true); ib->set_resend(true);
ib->register_handler(std::bind(&TesterCpp::handle_iu_inbuf, this, _1, _2, _3)); ib->register_handler(std::bind(&TesterCpp::handle_iu_inbuf, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
std::cout << "Listening for all IU events ..." << std::endl; std::cout << "Listening for all IU events ..." << std::endl;
while(true) { while(true) {
sleep(5); sleep(5);
...@@ -105,8 +107,15 @@ class TesterCpp { ...@@ -105,8 +107,15 @@ class TesterCpp {
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
auto config = ipaaca::participants::get_global_config(true);
for (auto it = config->begin(); it!=config->end(); ++it ) {
std::cout << it->first << "=" << it->second << std::endl;
}
exit(1);
ipaaca::__ipaaca_static_option_log_level = IPAACA_LOG_LEVEL_DEBUG; ipaaca::__ipaaca_static_option_log_level = IPAACA_LOG_LEVEL_DEBUG;
TesterCpp tester; TesterCpp tester;
tester.run(); tester.run();
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment