diff --git a/services/tunnelbroker/src/Tools/ConfigManager.cpp b/services/tunnelbroker/src/Tools/ConfigManager.cpp index afb729ac9..b38840459 100644 --- a/services/tunnelbroker/src/Tools/ConfigManager.cpp +++ b/services/tunnelbroker/src/Tools/ConfigManager.cpp @@ -1,102 +1,102 @@ #include "ConfigManager.h" #include "Constants.h" #include namespace comm { namespace network { namespace config { const std::string ConfigManager::OPTION_TUNNELBROKER_ID = "tunnelbroker.instance-id"; const std::string ConfigManager::OPTION_DEFAULT_KEYSERVER_ID = "keyserver.default_keyserver_id"; const std::string ConfigManager::OPTION_AMQP_URI = "amqp.uri"; const std::string ConfigManager::OPTION_AMQP_FANOUT_EXCHANGE = "amqp.fanout_exchange_name"; const std::string ConfigManager::OPTION_DYNAMODB_SESSIONS_TABLE = "dynamodb.sessions_table_name"; const std::string ConfigManager::OPTION_DYNAMODB_SESSIONS_VERIFICATION_TABLE = "dynamodb.sessions_verification_table_name"; const std::string ConfigManager::OPTION_DYNAMODB_SESSIONS_PUBLIC_KEY_TABLE = "dynamodb.sessions_public_key_table_name"; const std::string ConfigManager::OPTION_DYNAMODB_MESSAGES_TABLE = "dynamodb.messages_table_name"; ConfigManager &ConfigManager::getInstance() { static ConfigManager instance; return instance; } -void ConfigManager::load() { +void ConfigManager::load(const std::string configFilePath) { try { std::ifstream fileStream; - fileStream.open(CONFIG_FILE_PATH.c_str(), std::ifstream::in); + fileStream.open(configFilePath.c_str(), std::ifstream::in); if (!fileStream.is_open()) { - throw std::runtime_error("Error: can not open file " + CONFIG_FILE_PATH); + throw std::runtime_error("Error: can not open file " + configFilePath); } boost::program_options::options_description description{ "Tunnelbroker options"}; description.add_options()( this->OPTION_TUNNELBROKER_ID.c_str(), boost::program_options::value()->required(), "Tunnelbroker unique identification"); description.add_options()( this->OPTION_DEFAULT_KEYSERVER_ID.c_str(), boost::program_options::value()->required(), "Default and only allowed keyserver deviceID"); description.add_options()( this->OPTION_AMQP_URI.c_str(), boost::program_options::value()->required(), "AMQP URI connection string"); description.add_options()( this->OPTION_AMQP_FANOUT_EXCHANGE.c_str(), boost::program_options::value()->default_value( AMQP_FANOUT_EXCHANGE_NAME), "AMQP Fanout exchange name"); description.add_options()( this->OPTION_DYNAMODB_SESSIONS_TABLE.c_str(), boost::program_options::value()->default_value( DEVICE_SESSIONS_TABLE_NAME), "DynamoDB table name for sessions"); description.add_options()( this->OPTION_DYNAMODB_SESSIONS_VERIFICATION_TABLE.c_str(), boost::program_options::value()->default_value( DEVICE_SESSIONS_VERIFICATION_MESSAGES_TABLE_NAME), "DynamoDB table name for sessions verification messages"); description.add_options()( this->OPTION_DYNAMODB_SESSIONS_PUBLIC_KEY_TABLE.c_str(), boost::program_options::value()->default_value( DEVICE_PUBLIC_KEY_TABLE_NAME), "DynamoDB table name for public keys"); description.add_options()( this->OPTION_DYNAMODB_MESSAGES_TABLE.c_str(), boost::program_options::value()->default_value( MESSAGES_TABLE_NAME), "DynamoDB table name for messages"); boost::program_options::parsed_options parsedDescription = boost::program_options::parse_config_file( fileStream, description, true); boost::program_options::store(parsedDescription, this->variablesMap); boost::program_options::notify(this->variablesMap); fileStream.close(); } catch (const std::exception &e) { throw std::runtime_error( "Got an exception at ConfigManager: " + std::string(e.what())); } } std::string ConfigManager::getParameter(std::string param) { if (!this->variablesMap.count(param) && !this->variablesMap[param].defaulted()) { throw std::runtime_error( "ConfigManager Error: config parameter " + param + " is not set."); } return this->variablesMap[param].as(); } } // namespace config } // namespace network } // namespace comm diff --git a/services/tunnelbroker/src/Tools/ConfigManager.h b/services/tunnelbroker/src/Tools/ConfigManager.h index 3132cedac..93fff090d 100644 --- a/services/tunnelbroker/src/Tools/ConfigManager.h +++ b/services/tunnelbroker/src/Tools/ConfigManager.h @@ -1,33 +1,33 @@ #pragma once #include #include #include namespace comm { namespace network { namespace config { class ConfigManager { private: boost::program_options::variables_map variablesMap; public: static const std::string OPTION_TUNNELBROKER_ID; static const std::string OPTION_DEFAULT_KEYSERVER_ID; static const std::string OPTION_AMQP_URI; static const std::string OPTION_AMQP_FANOUT_EXCHANGE; static const std::string OPTION_DYNAMODB_SESSIONS_TABLE; static const std::string OPTION_DYNAMODB_SESSIONS_VERIFICATION_TABLE; static const std::string OPTION_DYNAMODB_SESSIONS_PUBLIC_KEY_TABLE; static const std::string OPTION_DYNAMODB_MESSAGES_TABLE; static ConfigManager &getInstance(); - void load(); + void load(const std::string configFilePath); std::string getParameter(std::string param); }; } // namespace config } // namespace network } // namespace comm diff --git a/services/tunnelbroker/src/server.cpp b/services/tunnelbroker/src/server.cpp index 7862865c9..46bee5ac0 100644 --- a/services/tunnelbroker/src/server.cpp +++ b/services/tunnelbroker/src/server.cpp @@ -1,48 +1,49 @@ #include "AmqpManager.h" #include "ConfigManager.h" #include "Constants.h" #include "TunnelbrokerServiceImpl.h" #include #include #include #include #include namespace comm { namespace network { void RunServer() { TunnelBrokerServiceImpl service; grpc::EnableDefaultHealthCheckService(true); grpc::ServerBuilder builder; // Listen on the given address without any authentication mechanism. builder.AddListeningPort( SERVER_LISTEN_ADDRESS, grpc::InsecureServerCredentials()); // Register "service" as the instance through which we'll communicate with // clients. In this case it corresponds to an *synchronous* service. builder.RegisterService(&service); std::unique_ptr server(builder.BuildAndStart()); LOG(INFO) << "gRPC Server listening at :" << SERVER_LISTEN_ADDRESS; // Wait for the server to shutdown. Note that some other thread must be // responsible for shutting down the server for this call to ever return. server->Wait(); } void RunAmqpClient() { AmqpManager::getInstance().connect(); } } // namespace network } // namespace comm int main(int argc, char **argv) { google::InitGoogleLogging(argv[0]); - comm::network::config::ConfigManager::getInstance().load(); + comm::network::config::ConfigManager::getInstance().load( + comm::network::CONFIG_FILE_PATH); std::thread amqpThread(comm::network::RunAmqpClient); std::thread grpcThread(comm::network::RunServer); amqpThread.join(); grpcThread.join(); return 0; }