Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/api/cpp/GravityConfigParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,23 @@ int GravityConfigParser::CONFIG_REQUEST_TIMEOUT = 4000;

bool GravityConfigParser::hasKey(std::string key) { return key_value_map.count(StringCopyToLowerCase(key)) > 0; }

// may be useless...
void GravityConfigParser::setDirectory(std::string dir) { config_dir = dir; }

void GravityConfigParser::setComponentID(std::string componentID) { this->componentID = componentID; }

GravityConfigParser::GravityConfigParser(std::string componentID) { this->componentID = componentID; }

void GravityConfigParser::ParseConfigFile(const char *config_filename)
// filename includes full file path
void GravityConfigParser::parseConfigFile(std::string config_filename)
{
std::vector<const char *> sections;

sections.push_back(componentID.c_str());
sections.push_back("general");
sections.push_back(NULL);

KeyValueConfigParser parser(config_filename, sections);
KeyValueConfigParser parser(config_filename.c_str(), sections);

std::vector<std::string> keys = parser.GetKeys();

Expand All @@ -57,10 +63,17 @@ void GravityConfigParser::ParseConfigFile(const char *config_filename)
std::string key_lower = StringCopyToLowerCase(*i);
key_value_map[key_lower] = value;
}
return;
}

void GravityConfigParser::ParseConfigService(GravityNode &gn)
void GravityConfigParser::parseComponentConfigFile()
{
if (this->componentID != "")
{
parseConfigFile(this->componentID + ".ini");
}
}

void GravityConfigParser::parseConfigService(GravityNode &gn)
{
//Prepare request
GravityDataProduct dataproduct("ConfigRequestPB");
Expand Down
9 changes: 7 additions & 2 deletions src/api/cpp/GravityConfigParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,21 @@ class GravityConfigParser
GravityConfigParser(std::string componentID);

bool hasKey(std::string key);
void setDirectory(std::string dir);
void setComponentID(std::string componentID);

/** Update configuration based on .ini file */
void ParseConfigFile(const char* config_filename);
void parseConfigFile(std::string config_filename);
/** Update configuration based on <componentID>.ini file, if componentID is non-empty */
void parseComponentConfigFile();
/** Update configuration based on Config Service */
void ParseConfigService(GravityNode& gn);
void parseConfigService(GravityNode& gn);

std::string getString(std::string key, std::string default_value = "");

private:
std::string componentID;
std::string config_dir;
std::map<std::string, std::string> key_value_map;

static int CONFIG_REQUEST_TIMEOUT;
Expand Down
107 changes: 39 additions & 68 deletions src/api/cpp/GravityNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,6 @@ GravityNode::GravityNode()
logInitialized = false;
listenerEnabled = false;
heartbeatStarted = false;

parser = NULL;
}

GravityNode::GravityNode(std::string componentID)
Expand All @@ -454,8 +452,6 @@ GravityNode::GravityNode(std::string componentID)
logInitialized = false;
heartbeatStarted = false;

parser = NULL;

init(componentID);
}

Expand Down Expand Up @@ -552,11 +548,6 @@ GravityNode::~GravityNode()
zmq_term(context);
}

if (parser)
{
delete parser;
}

//Do not destroy object until sub manager thread is joined
if (subscriptionManagerThread.joinable())
{
Expand Down Expand Up @@ -663,48 +654,57 @@ void GravityNode::configSpdLoggers()
}
}

GravityReturnCode GravityNode::init()
GravityReturnCode GravityNode::init(std::string componentID, std::string configFilepath)
{
////////////////////////////////////////////////////////
//get gravity configuration.
parser = new GravityConfigParser("");
static const std::string fallback_component_id = "GravityNode";
parser = std::unique_ptr<GravityConfigParser>(new GravityConfigParser(componentID));

parser->ParseConfigFile("Gravity.ini");
this->componentID = componentID; // this may change further down if it's an empty string

std::string id = parser->getString("GravityComponentID", "");

if (id != "")
// we always parse the default config file first (Gravity.ini)
if (configFilepath.empty()) // default configurations
{
parser->parseConfigFile("Gravity.ini");
std::string config_file_name = componentID + ".ini";
if (gravity::IsValidFilename(config_file_name))
{
parser->parseConfigFile(config_file_name.c_str());
}
}
else // user specified a specific file to parse
{
return init(id);
parser->parseConfigFile(configFilepath);
}
else


// empty componentID: discover from config file
if (componentID == "")
{
componentID = "GravityNode";
//Setup Logging if enabled.
if (!logInitialized)
std::string id = parser->getString("GravityComponentID", "");
if (id != "")
{
Log::LogLevel local_log_level = Log::LogStringToLevel(getStringParam("LocalLogLevel", "none").c_str());
if (local_log_level != Log::NONE)
Log::initAndAddFileLogger(getStringParam("LogDirectory", "").c_str(), componentID.c_str(),
local_log_level, getBoolParam("CloseLogFileAfterWrite", false));
// we found a non-empty component ID
this->componentID = id;
}
else
{
this->componentID = fallback_component_id;
}
// update the parser's componentID
parser->setComponentID(this->componentID);
}

Log::LogLevel console_log_level = Log::LogStringToLevel(getStringParam("ConsoleLogLevel", "none").c_str());
if (console_log_level != Log::NONE) Log::initAndAddConsoleLogger(componentID.c_str(), console_log_level);
parser->parseComponentConfigFile();

//log an error indicating the componentID was missing
logger->error(
"Field 'GravityComponentID' missing from Gravity.ini, using GravityComponentID='GravityNode'");
configSpdLoggers();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be called after acquiring the initLock. We should also add a note on that method that it needs to be called within a lock.


logInitialized = true;
}
return init(componentID);
if (this->componentID == fallback_component_id)
{
logger->error("Field 'GravityComponentID' missing from Gravity.ini, using GravityComponentID='{}'",
fallback_component_id);
}
}

GravityReturnCode GravityNode::init(std::string componentID)
{
GravityReturnCode ret = GravityReturnCodes::SUCCESS;
this->componentID = componentID;

std::string serviceDirectoryUrl = "";
bool domainTimeout = false;
Expand All @@ -717,34 +717,6 @@ GravityReturnCode GravityNode::init(std::string componentID)
// Setup zmq context
if (!initialized)
{
////////////////////////////////////////////////////////
//Now that Gravity is set up, get gravity configuration.
parser = new GravityConfigParser(componentID);

parser->ParseConfigFile("Gravity.ini");
std::string config_file_name = componentID + ".ini";
if (gravity::IsValidFilename(config_file_name))
{
parser->ParseConfigFile(config_file_name.c_str());
}

// Setup Logging as soon as config parser is available.
if (!logInitialized)
{
Log::LogLevel local_log_level = Log::LogStringToLevel(getStringParam("LocalLogLevel", "none").c_str());
if (local_log_level != Log::NONE)
Log::initAndAddFileLogger(getStringParam("LogDirectory", "").c_str(), componentID.c_str(),
local_log_level, getBoolParam("CloseLogFileAfterWrite", false));

Log::LogLevel console_log_level = Log::LogStringToLevel(getStringParam("ConsoleLogLevel", "none").c_str());
if (console_log_level != Log::NONE) Log::initAndAddConsoleLogger(componentID.c_str(), console_log_level);

// Configure spdlog loggers
configSpdLoggers();

logInitialized = true;
}

context = zmq_init(1);
if (!context)
{
Expand Down Expand Up @@ -1020,10 +992,9 @@ GravityReturnCode GravityNode::init(std::string componentID)

if (componentID != "ConfigServer" && getBoolParam("NoConfigServer", false) != true)
{
parser->ParseConfigService(
parser->parseConfigService(
*this); //Although this is done last, this has the least priority. We just need to do it last so we know where the service directory is located.
}
//parser->ParseCmdLine

configureServiceManager();
configureSubscriptionManager();
Expand Down
25 changes: 12 additions & 13 deletions src/api/cpp/GravityNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ class GRAVITY_API GravityNode
std::string myDomain;
std::string componentID;
std::map<std::string, uint32_t> dataRegistrationTimeMap; // Maps data product id to registration time
GravityConfigParser* parser;
std::unique_ptr<GravityConfigParser> parser;

GravityConfigParamPB configParamPB;
GravityDataProduct settingsGDP = GravityDataProduct(gravity::constants::GRAVITY_SETTINGS_DPID);
Expand Down Expand Up @@ -283,34 +283,33 @@ class GRAVITY_API GravityNode

public:
/**
* Default Constructor
* Default Constructor. Does not call init().
*/
GravityNode();

/**
* Constructor that also initializes
* \param componentID ID of the component to initialize
*/
* Constructor that also calls init(), passing the component ID.
* \param componentID ID of the component to initialize
* \note Setting the GRAVITY_CONFIG_DIR environment variable will cause GravityNode to look in
* a different directory for configuration files than the current directory.
*/
GravityNode(std::string componentID);

/**
* Default Destructor
*/
virtual ~GravityNode();

/**
* Initialize the Gravity infrastructure.
* Reads the ComponentID from the Gravity.ini file.
* \return GravityReturnCode code to identify any errors that occur during initialization
*/
GravityReturnCode init();

/**
* Initialize the Gravity infrastructure.
* \copydetails GravityNode(std::string)
* \return GravityReturnCode code to identify any errors that occur during initialization
* \note If componentID is an empty string (the default), the component ID will be inferred
* from the "GravityComponentID" located in the default configuration file.
* \note Setting the GRAVITY_CONFIG_DIR environment variable will cause GravityNode to look in
* a different directory for configuration files than the current directory.
*/
GravityReturnCode init(std::string componentID);
GravityReturnCode init(std::string componentID = "", std::string configFilepath = "");

/**
* Wait for the GravityNode to exit.
Expand Down
3 changes: 1 addition & 2 deletions src/api/java/src/swig/gravitynode.i
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ public:
GravityNode();
GravityNode(std::string);
~GravityNode();
GravityReturnCode init();
GravityReturnCode init(std::string);
GravityReturnCode init(std::string="", std::string="");
void waitForExit();
GravityReturnCode registerDataProduct(const std::string& dataProductID, const GravityTransportType& transportType);
GravityReturnCode registerDataProduct(const std::string& dataProductID, const GravityTransportType& transportType, bool cacheLastValue);
Expand Down
3 changes: 1 addition & 2 deletions src/api/python/src/swig/gravitynode.i
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ namespace gravity {
GravityNode();
GravityNode(std::string);
~GravityNode();
GravityReturnCode init();
GravityReturnCode init(std::string);
GravityReturnCode init(std::string="", std::string="");
GravityReturnCode registerDataProduct(const std::string& dataProductID, const GravityTransportType& transportType);
GravityReturnCode registerDataProduct(const std::string& dataProductID, const GravityTransportType& transportType, bool cacheLastValue);
GravityReturnCode unregisterDataProduct(const std::string& dataProductID);
Expand Down
38 changes: 33 additions & 5 deletions src/components/cpp/Archiver/FileArchiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,48 @@ using namespace std;

int main(int argc, const char* argv[])
{
gravity::FileArchiver archiver;
archiver.waitForExit();

if (argc > 1)
{
std::string configFilepath = std::string(argv[1]);
if (configFilepath.substr(configFilepath.size() - 4) != ".ini")
{
std::cerr << "Usage: invalid filename. Must be .ini. Using default config path.\n";

gravity::FileArchiver archiver;
archiver.waitForExit();
}
else
{
gravity::FileArchiver archiver(configFilepath);
archiver.waitForExit();
}
}
else
{
gravity::FileArchiver archiver;
archiver.waitForExit();
}

}

namespace gravity
{

const char* FileArchiver::ComponentName = "FileArchiver";

FileArchiver::FileArchiver()
FileArchiver::FileArchiver(std::string configDir)
{
// Initialize Gravity Node
gravityNode.init(FileArchiver::ComponentName);

if (configDir.empty())
{
gravityNode.init(FileArchiver::ComponentName);
}
else
{
gravityNode.init(FileArchiver::ComponentName, configDir);
}

// Get Gravity logger
logger = gravityNode.getGravityLogger();
if (!logger)
Expand Down
2 changes: 1 addition & 1 deletion src/components/cpp/Archiver/FileArchiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class FileArchiver : public GravitySubscriber, GravityServiceProvider
std::shared_ptr<spdlog::logger> logger;

public:
FileArchiver();
FileArchiver(std::string configDir = "");
virtual ~FileArchiver();

virtual void subscriptionFilled(const std::vector<std::shared_ptr<GravityDataProduct> >& dataProducts);
Expand Down
Loading
Loading