Improve:Ipc config.
This commit is contained in:
		
							parent
							
								
									69e69ba86e
								
							
						
					
					
						commit
						8511e2947a
					
				| 
						 | 
				
			
			@ -467,7 +467,7 @@ void IpcConfigImpl::ReadingConfigThread(void)
 | 
			
		|||
    }
 | 
			
		||||
    mThreadRuning = true;
 | 
			
		||||
    DIR *dir = nullptr;
 | 
			
		||||
    LogInfo("Reading config thread is running.dirPath = %s\n", dirPath);
 | 
			
		||||
    LogInfo("Reading config thread is running, dirPath = %s\n", dirPath);
 | 
			
		||||
    while (mThreadRuning) {
 | 
			
		||||
        dir = opendir(dirPath);
 | 
			
		||||
        if (nullptr != dir) {
 | 
			
		||||
| 
						 | 
				
			
			@ -476,7 +476,7 @@ void IpcConfigImpl::ReadingConfigThread(void)
 | 
			
		|||
            mCfg = OpenConfigFile(IPC_CONFIG_FILE_PATH);
 | 
			
		||||
            if (nullptr == mCfg) {
 | 
			
		||||
                LogError("Open config file failed.\n");
 | 
			
		||||
                return;
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
            std::lock_guard<std::mutex> locker(mMutex);
 | 
			
		||||
            InitConfigMap();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -31,7 +31,9 @@ void *OpenConfigFile(const char *fileName)
 | 
			
		|||
{
 | 
			
		||||
    std::shared_ptr<IConfigBase> *configObject = NewConfigBase(fileName);
 | 
			
		||||
    if (nullptr != configObject) {
 | 
			
		||||
        (*configObject)->OpenConfigFile();
 | 
			
		||||
        if ((*configObject)->OpenConfigFile() == false) {
 | 
			
		||||
            return nullptr;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return configObject;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -28,37 +28,38 @@ constexpr int INVALID_RESULT = -1;
 | 
			
		|||
ConfigBaseImpl::ConfigBaseImpl(const std::string &fileName) : mFileName(fileName)
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
void ConfigBaseImpl::OpenConfigFile(void)
 | 
			
		||||
bool ConfigBaseImpl::OpenConfigFile(void)
 | 
			
		||||
{
 | 
			
		||||
    config_init(&cfg);
 | 
			
		||||
    config_set_options(&cfg,
 | 
			
		||||
    config_init(&mCfg);
 | 
			
		||||
    config_set_options(&mCfg,
 | 
			
		||||
                       (CONFIG_OPTION_FSYNC | CONFIG_OPTION_SEMICOLON_SEPARATORS |
 | 
			
		||||
                        CONFIG_OPTION_COLON_ASSIGNMENT_FOR_GROUPS | CONFIG_OPTION_OPEN_BRACE_ON_SEPARATE_LINE));
 | 
			
		||||
    constexpr int FIEL_EXIST = 0;
 | 
			
		||||
    if (FIEL_EXIST == access(mFileName.c_str(), F_OK)) {
 | 
			
		||||
        if (!config_read_file(&cfg, mFileName.c_str())) {
 | 
			
		||||
        if (!config_read_file(&mCfg, mFileName.c_str())) {
 | 
			
		||||
            LogError("Read file failed[%s].\n", mFileName.c_str());
 | 
			
		||||
            fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg));
 | 
			
		||||
            return;
 | 
			
		||||
            fprintf(stderr, "%s:%d - %s\n", config_error_file(&mCfg), config_error_line(&mCfg), config_error_text(&mCfg));
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
        LogInfo("Config file doesn't exist.mFileName = %s\n", mFileName.c_str());
 | 
			
		||||
        /* Write out the new configuration. */
 | 
			
		||||
        if (!config_write_file(&cfg, mFileName.c_str())) {
 | 
			
		||||
        if (!config_write_file(&mCfg, mFileName.c_str())) {
 | 
			
		||||
            fprintf(stderr, "Error while writing file.\n");
 | 
			
		||||
            return;
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
void ConfigBaseImpl::CloseConfigFile(void)
 | 
			
		||||
{
 | 
			
		||||
    config_destroy(&cfg);
 | 
			
		||||
    config_destroy(&mCfg);
 | 
			
		||||
}
 | 
			
		||||
StatusCode ConfigBaseImpl::ConfigSaveFile(void)
 | 
			
		||||
{
 | 
			
		||||
    LogInfo("Save file[%s].\n", mFileName.c_str());
 | 
			
		||||
    if (!config_write_file(&cfg, mFileName.c_str())) {
 | 
			
		||||
    if (!config_write_file(&mCfg, mFileName.c_str())) {
 | 
			
		||||
        LogError("Save config failed.\n");
 | 
			
		||||
        return CreateStatusCode(STATUS_CODE_NOT_OK);
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -67,7 +68,7 @@ StatusCode ConfigBaseImpl::ConfigSaveFile(void)
 | 
			
		|||
StatusCode ConfigBaseImpl::ConfigGetInt(const char *name, int *value)
 | 
			
		||||
{
 | 
			
		||||
    int result = INVALID_RESULT;
 | 
			
		||||
    result = config_lookup_int(&cfg, name, value);
 | 
			
		||||
    result = config_lookup_int(&mCfg, name, value);
 | 
			
		||||
    if (CONFIG_FALSE == result) {
 | 
			
		||||
        return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -77,7 +78,7 @@ StatusCode ConfigBaseImpl::ConfigSetInt(const char *name, const int value)
 | 
			
		|||
{
 | 
			
		||||
    config_setting_t *root = nullptr;
 | 
			
		||||
    config_setting_t *setting = nullptr;
 | 
			
		||||
    root = config_root_setting(&cfg);
 | 
			
		||||
    root = config_root_setting(&mCfg);
 | 
			
		||||
    if (nullptr == root) {
 | 
			
		||||
        LogError("Config function failed.\n");
 | 
			
		||||
        return CreateConfigCode(STATUS_CODE_NOT_OK);
 | 
			
		||||
| 
						 | 
				
			
			@ -97,7 +98,7 @@ StatusCode ConfigBaseImpl::ConfigGetShort(const char *name, short *value)
 | 
			
		|||
{
 | 
			
		||||
    int intValue = 0;
 | 
			
		||||
    int result = 0;
 | 
			
		||||
    result = config_lookup_int(&cfg, name, &intValue);
 | 
			
		||||
    result = config_lookup_int(&mCfg, name, &intValue);
 | 
			
		||||
    if (CONFIG_FALSE == result || CHECK_SHORT_LIMIT(intValue)) {
 | 
			
		||||
        return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -108,7 +109,7 @@ StatusCode ConfigBaseImpl::ConfigSetShort(const char *name, const short value)
 | 
			
		|||
{
 | 
			
		||||
    config_setting_t *root = nullptr;
 | 
			
		||||
    config_setting_t *setting = nullptr;
 | 
			
		||||
    root = config_root_setting(&cfg);
 | 
			
		||||
    root = config_root_setting(&mCfg);
 | 
			
		||||
    if (nullptr == root) {
 | 
			
		||||
        LogError("Config function failed.\n");
 | 
			
		||||
        return CreateConfigCode(STATUS_CODE_NOT_OK);
 | 
			
		||||
| 
						 | 
				
			
			@ -129,7 +130,7 @@ StatusCode ConfigBaseImpl::ConfigGetLong(const char *name, long *value)
 | 
			
		|||
{
 | 
			
		||||
    long long llongValue = 0;
 | 
			
		||||
    int result = 0;
 | 
			
		||||
    result = config_lookup_int64(&cfg, name, &llongValue);
 | 
			
		||||
    result = config_lookup_int64(&mCfg, name, &llongValue);
 | 
			
		||||
    if (CONFIG_FALSE == result || CHECK_LONG_LIMIT(llongValue)) {
 | 
			
		||||
        return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -140,7 +141,7 @@ StatusCode ConfigBaseImpl::ConfigSetLong(const char *name, const long value)
 | 
			
		|||
{
 | 
			
		||||
    config_setting_t *root = nullptr;
 | 
			
		||||
    config_setting_t *setting = nullptr;
 | 
			
		||||
    root = config_root_setting(&cfg);
 | 
			
		||||
    root = config_root_setting(&mCfg);
 | 
			
		||||
    if (nullptr == root) {
 | 
			
		||||
        LogError("Config function failed.\n");
 | 
			
		||||
        return CreateConfigCode(STATUS_CODE_NOT_OK);
 | 
			
		||||
| 
						 | 
				
			
			@ -160,7 +161,7 @@ StatusCode ConfigBaseImpl::ConfigSetLong(const char *name, const long value)
 | 
			
		|||
StatusCode ConfigBaseImpl::ConfigGetLLong(const char *name, long long *value)
 | 
			
		||||
{
 | 
			
		||||
    int result = 0;
 | 
			
		||||
    result = config_lookup_int64(&cfg, name, value);
 | 
			
		||||
    result = config_lookup_int64(&mCfg, name, value);
 | 
			
		||||
    if (CONFIG_FALSE == result) {
 | 
			
		||||
        return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -170,7 +171,7 @@ StatusCode ConfigBaseImpl::ConfigSetLLong(const char *name, const long long valu
 | 
			
		|||
{
 | 
			
		||||
    config_setting_t *root = nullptr;
 | 
			
		||||
    config_setting_t *setting = nullptr;
 | 
			
		||||
    root = config_root_setting(&cfg);
 | 
			
		||||
    root = config_root_setting(&mCfg);
 | 
			
		||||
    if (nullptr == root) {
 | 
			
		||||
        LogError("Config function failed.\n");
 | 
			
		||||
        return CreateConfigCode(STATUS_CODE_NOT_OK);
 | 
			
		||||
| 
						 | 
				
			
			@ -190,7 +191,7 @@ StatusCode ConfigBaseImpl::ConfigGetChar(const char *name, char *value)
 | 
			
		|||
{
 | 
			
		||||
    int charValue = 0;
 | 
			
		||||
    int result = 0;
 | 
			
		||||
    result = config_lookup_int(&cfg, name, &charValue);
 | 
			
		||||
    result = config_lookup_int(&mCfg, name, &charValue);
 | 
			
		||||
    if (CONFIG_FALSE == result && CHECK_CHAR_LIMIT(charValue)) {
 | 
			
		||||
        return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -201,7 +202,7 @@ StatusCode ConfigBaseImpl::ConfigSetChar(const char *name, const char value)
 | 
			
		|||
{
 | 
			
		||||
    config_setting_t *root = nullptr;
 | 
			
		||||
    config_setting_t *setting = nullptr;
 | 
			
		||||
    root = config_root_setting(&cfg);
 | 
			
		||||
    root = config_root_setting(&mCfg);
 | 
			
		||||
    if (nullptr == root) {
 | 
			
		||||
        LogError("Config function failed.\n");
 | 
			
		||||
        return CreateConfigCode(STATUS_CODE_NOT_OK);
 | 
			
		||||
| 
						 | 
				
			
			@ -221,7 +222,7 @@ StatusCode ConfigBaseImpl::ConfigSetChar(const char *name, const char value)
 | 
			
		|||
StatusCode ConfigBaseImpl::ConfigGetBool(const char *name, bool *value)
 | 
			
		||||
{
 | 
			
		||||
    int result = 0;
 | 
			
		||||
    result = config_lookup_bool(&cfg, name, (int *)value);
 | 
			
		||||
    result = config_lookup_bool(&mCfg, name, (int *)value);
 | 
			
		||||
    if (CONFIG_FALSE == result) {
 | 
			
		||||
        return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -231,7 +232,7 @@ StatusCode ConfigBaseImpl::ConfigSetBool(const char *name, const bool value)
 | 
			
		|||
{
 | 
			
		||||
    config_setting_t *root = nullptr;
 | 
			
		||||
    config_setting_t *setting = nullptr;
 | 
			
		||||
    root = config_root_setting(&cfg);
 | 
			
		||||
    root = config_root_setting(&mCfg);
 | 
			
		||||
    if (nullptr == root) {
 | 
			
		||||
        LogError("Config function failed.\n");
 | 
			
		||||
        return CreateConfigCode(STATUS_CODE_NOT_OK);
 | 
			
		||||
| 
						 | 
				
			
			@ -251,7 +252,7 @@ StatusCode ConfigBaseImpl::ConfigGetFloat(const char *name, float *value)
 | 
			
		|||
{
 | 
			
		||||
    double dValue = 0;
 | 
			
		||||
    int result = 0;
 | 
			
		||||
    result = config_lookup_float(&cfg, name, &dValue);
 | 
			
		||||
    result = config_lookup_float(&mCfg, name, &dValue);
 | 
			
		||||
    if (CONFIG_FALSE == result || CHECK_FLOAT_LIMIT(dValue)) {
 | 
			
		||||
        return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -262,7 +263,7 @@ StatusCode ConfigBaseImpl::ConfigSetFloat(const char *name, const float value)
 | 
			
		|||
{
 | 
			
		||||
    config_setting_t *root = nullptr;
 | 
			
		||||
    config_setting_t *setting = nullptr;
 | 
			
		||||
    root = config_root_setting(&cfg);
 | 
			
		||||
    root = config_root_setting(&mCfg);
 | 
			
		||||
    if (nullptr == root) {
 | 
			
		||||
        LogError("Config function failed.\n");
 | 
			
		||||
        return CreateConfigCode(STATUS_CODE_NOT_OK);
 | 
			
		||||
| 
						 | 
				
			
			@ -282,7 +283,7 @@ StatusCode ConfigBaseImpl::ConfigSetFloat(const char *name, const float value)
 | 
			
		|||
StatusCode ConfigBaseImpl::ConfigGetDouble(const char *name, double *value)
 | 
			
		||||
{
 | 
			
		||||
    int result = 0;
 | 
			
		||||
    result = config_lookup_float(&cfg, name, value);
 | 
			
		||||
    result = config_lookup_float(&mCfg, name, value);
 | 
			
		||||
    if (CONFIG_FALSE == result) {
 | 
			
		||||
        return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -292,7 +293,7 @@ StatusCode ConfigBaseImpl::ConfigSetDouble(const char *name, const double value)
 | 
			
		|||
{
 | 
			
		||||
    config_setting_t *root = nullptr;
 | 
			
		||||
    config_setting_t *setting = nullptr;
 | 
			
		||||
    root = config_root_setting(&cfg);
 | 
			
		||||
    root = config_root_setting(&mCfg);
 | 
			
		||||
    if (nullptr == root) {
 | 
			
		||||
        LogError("Config function failed.\n");
 | 
			
		||||
        return CreateConfigCode(STATUS_CODE_NOT_OK);
 | 
			
		||||
| 
						 | 
				
			
			@ -311,7 +312,7 @@ StatusCode ConfigBaseImpl::ConfigSetDouble(const char *name, const double value)
 | 
			
		|||
StatusCode ConfigBaseImpl::ConfigGetString(const char *name, const char **value)
 | 
			
		||||
{
 | 
			
		||||
    int result = 0;
 | 
			
		||||
    result = config_lookup_string(&cfg, name, value);
 | 
			
		||||
    result = config_lookup_string(&mCfg, name, value);
 | 
			
		||||
    if (CONFIG_FALSE == result) {
 | 
			
		||||
        return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -321,7 +322,7 @@ StatusCode ConfigBaseImpl::ConfigSetString(const char *name, const char *value)
 | 
			
		|||
{
 | 
			
		||||
    config_setting_t *root = nullptr;
 | 
			
		||||
    config_setting_t *setting = nullptr;
 | 
			
		||||
    root = config_root_setting(&cfg);
 | 
			
		||||
    root = config_root_setting(&mCfg);
 | 
			
		||||
    if (nullptr == root) {
 | 
			
		||||
        LogError("Config function failed.\n");
 | 
			
		||||
        return CreateConfigCode(STATUS_CODE_NOT_OK);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -21,7 +21,7 @@ class ConfigBaseImpl : public IConfigBase
 | 
			
		|||
public:
 | 
			
		||||
    ConfigBaseImpl(const std::string &fileName);
 | 
			
		||||
    virtual ~ConfigBaseImpl() = default;
 | 
			
		||||
    void OpenConfigFile(void) override;
 | 
			
		||||
    bool OpenConfigFile(void) override;
 | 
			
		||||
    void CloseConfigFile(void) override;
 | 
			
		||||
    StatusCode ConfigSaveFile(void) override;
 | 
			
		||||
    StatusCode ConfigGetInt(const char *name, int *value) override;
 | 
			
		||||
| 
						 | 
				
			
			@ -45,6 +45,6 @@ public:
 | 
			
		|||
 | 
			
		||||
private:
 | 
			
		||||
    const std::string mFileName;
 | 
			
		||||
    config_t cfg;
 | 
			
		||||
    config_t mCfg;
 | 
			
		||||
};
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			@ -16,8 +16,9 @@
 | 
			
		|||
#include "ConfigBaseImpl.h"
 | 
			
		||||
#include "ILog.h"
 | 
			
		||||
#include <cstring>
 | 
			
		||||
void IConfigBase::OpenConfigFile(void)
 | 
			
		||||
bool IConfigBase::OpenConfigFile(void)
 | 
			
		||||
{
 | 
			
		||||
    return false;
 | 
			
		||||
}
 | 
			
		||||
void IConfigBase::CloseConfigFile(void)
 | 
			
		||||
{
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -21,7 +21,7 @@ class IConfigBase
 | 
			
		|||
public:
 | 
			
		||||
    IConfigBase() = default;
 | 
			
		||||
    virtual ~IConfigBase() = default;
 | 
			
		||||
    virtual void OpenConfigFile(void);
 | 
			
		||||
    virtual bool OpenConfigFile(void);
 | 
			
		||||
    virtual void CloseConfigFile(void);
 | 
			
		||||
    virtual StatusCode ConfigSaveFile(void);
 | 
			
		||||
    virtual StatusCode ConfigGetInt(const char *name, int *value);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user