base
This commit is contained in:
@@ -0,0 +1,224 @@
|
||||
#include "rcdu.h"
|
||||
#include "parammanager.h"
|
||||
#include "Config.h"
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
CParamManager::CParamManager( )
|
||||
:m_mode(0),
|
||||
m_depth(0)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
CParamManager::~CParamManager( )
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
int CParamManager::inputparamAnalysis( int argc, char * const argv[] )
|
||||
{
|
||||
int o;
|
||||
string cofigpath = DEFAULT_CONFIG_FILE;
|
||||
// parse command line arguments
|
||||
while ((o = getopt(argc, argv, "vhc:m:i:d:")) >= 0)
|
||||
{
|
||||
switch (o)
|
||||
{
|
||||
case 'h': // help
|
||||
cerr << "usage: " << PROG_NAME << " [-vhc] [-m mode] [-i service id] [-d depth] " << endl;
|
||||
cerr << " " << PROG_NAME << " -m reg [-i service id] [-d depth] " << endl;
|
||||
cerr << " " << PROG_NAME << " -m sync [-i service id]" << endl;
|
||||
cerr << "options are: " << endl;
|
||||
cerr << " -h help" << endl;
|
||||
cerr << " -v print current version" << endl;
|
||||
cerr << " -c <path> config path" << endl;
|
||||
cerr << " -m <mode> job mode [reg|sync]" << endl;
|
||||
cerr << " default sync" << endl;
|
||||
cerr << " -i <service id> service id" << endl;
|
||||
cerr << " If you do not enter all the services registered" << endl;
|
||||
cerr << " -d <depth> to calculate the depth of the path" << endl;
|
||||
return 1;
|
||||
break;
|
||||
case 'v': // version
|
||||
cerr << "[info] " << PROG_NAME << " Version :" << PROG_VERSION << endl;
|
||||
return 1;
|
||||
break;
|
||||
case 'c': // config file
|
||||
cofigpath = optarg;
|
||||
break;
|
||||
case 'm': // mode
|
||||
if( strcmp(optarg, "reg") == 0 )
|
||||
m_mode = 1;
|
||||
else if ( strcmp(optarg, "sync") == 0 )
|
||||
m_mode = 0;
|
||||
else
|
||||
m_mode = -1;
|
||||
break;
|
||||
case 'i': // service id
|
||||
m_serviceid = optarg;
|
||||
break;
|
||||
case 'd': // depth
|
||||
m_depth = atoi(optarg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _DEBUG
|
||||
cout << "mode = "<< m_mode <<", service id = " << m_serviceid << ", depth = " << m_depth << endl;
|
||||
#endif // _DEBUG
|
||||
|
||||
// ÀԷ°ª üũ
|
||||
if( !correctinput() )
|
||||
return 1;
|
||||
|
||||
// load config file
|
||||
if( !loadconfig( cofigpath.c_str() ) )
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool CParamManager::correctinput( void )
|
||||
{
|
||||
if( m_mode < 0 )
|
||||
{
|
||||
cerr << "[error] Wrong mode." << endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (m_mode)
|
||||
{
|
||||
case 1: // reg
|
||||
if(m_depth < 0)
|
||||
{
|
||||
cerr << "[error] Wrong Parameter(depth)." << endl;
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 0: // sync
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool CheckValue(std::string& szValue, Config& conf, const std::string& defsection, const std::string& defkey)
|
||||
{
|
||||
if( szValue.size() > 0 )
|
||||
return true;
|
||||
|
||||
if( defsection.size() <= 0 || defkey.size() <= 0 )
|
||||
return false;
|
||||
|
||||
conf.GetConfig( defsection, defkey, szValue );
|
||||
|
||||
if( szValue.size() > 0 )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CParamManager::loadconfig( const char * path )
|
||||
{
|
||||
Config conf;
|
||||
|
||||
// Config File open
|
||||
if( conf.Open( path) == false )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
string szValue;
|
||||
// log path
|
||||
if( conf.GetConfig( PROG_NAME, "DEFAULT_LOG_DIR", szValue ) == false )
|
||||
{
|
||||
if( !CheckValue(szValue, conf, "COMMON", "DEFAULT_LOG_DIR") )
|
||||
{
|
||||
cerr << "[error] Config info get failed. [" << PROG_NAME << "]->LOG_DIR" <<endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
m_logpath = szValue;
|
||||
|
||||
// log level
|
||||
szValue.clear();
|
||||
if( conf.GetConfig( PROG_NAME, "LOG_LEVEL", szValue ) == false )
|
||||
{
|
||||
if( !CheckValue(szValue, conf, "COMMON", "LOG_LEVEL") )
|
||||
{
|
||||
cerr << "[error] Config info get failed. [" << PROG_NAME << "]->LOG_LEVEL" <<endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
m_loglevel = atoi(szValue.c_str());
|
||||
|
||||
// RCDB Information
|
||||
// db host
|
||||
szValue.clear();
|
||||
if( conf.GetConfig( PROG_NAME, "RCDB_IP", szValue ) == false )
|
||||
{
|
||||
if( !CheckValue(szValue, conf, "COMMON", "RCDB_IP") )
|
||||
{
|
||||
cerr << "[error] Config info get failed. [" << PROG_NAME << "]->RCDB_IP" <<endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
m_dbhost = szValue;
|
||||
|
||||
// db port
|
||||
szValue.clear();
|
||||
if( conf.GetConfig( PROG_NAME, "RCDB_PORT", szValue ) == false )
|
||||
{
|
||||
if( !CheckValue(szValue, conf, "COMMON", "RCDB_PORT") )
|
||||
{
|
||||
cerr << "[error] Config info get failed. [" << PROG_NAME << "]->RCDB_PORT" <<endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
m_dbport = atoi(szValue.c_str());
|
||||
|
||||
// db database name
|
||||
szValue.clear();
|
||||
if( conf.GetConfig( PROG_NAME, "RCDB_DB_NAME", szValue ) == false )
|
||||
{
|
||||
if( !CheckValue(szValue, conf, "COMMON", "RCDB_DB_NAME") )
|
||||
{
|
||||
cerr << "[error] Config info get failed. [" << PROG_NAME << "]->RCDB_DB_NAME" <<endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
m_dbnmae = szValue;
|
||||
|
||||
// db user
|
||||
szValue.clear();
|
||||
if( conf.GetConfig( PROG_NAME, "RCDB_ACCT", szValue ) == false )
|
||||
{
|
||||
if( !CheckValue(szValue, conf, "COMMON", "RCDB_ACCT") )
|
||||
{
|
||||
cerr << "[error] Config info get failed. [" << PROG_NAME << "]->RCDB_DB_NAME" <<endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
m_dbuser = szValue;
|
||||
|
||||
// db password
|
||||
szValue.clear();
|
||||
if( conf.GetConfig( PROG_NAME, "RCDB_ACCT_PW", szValue ) == false )
|
||||
{
|
||||
if( !CheckValue(szValue, conf, "COMMON", "RCDB_ACCT_PW") )
|
||||
{
|
||||
cerr << "[error] Config info get failed. [" << PROG_NAME << "]->RCDB_DB_NAME" <<endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
m_dbpasswd = szValue;
|
||||
|
||||
#ifdef _DEBUG
|
||||
cout << "config load : " << m_logpath << "," << m_loglevel << "," << m_dbhost << ",";
|
||||
cout << m_dbport << "," << m_dbnmae << "," << m_dbuser << "," << m_dbpasswd <<endl;
|
||||
#endif // _DEBUG.
|
||||
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user