稳定,不易扩展
static struct
{
PGconn *db_connection; /* 连接存储 */
char *hostname; /* 数据库服务器地址 */
char *dbname; /* 数据库实例名 */
int port; /* 数据库端口 */
char *user; /* 数据库用户 */
char *password; /* 数据库密码 */
int timeout; /* 连接超时时间 */
int status; /* 连接状态 */
} db_globals_t ;
static struct
{
REDIS redis; /* 连接存储 */
char *hostname; /* redis务器地址 */
int port; /* redis端口 */
char *password; /* redis密码 */
int timeout; /* 连接超时时间 */
int status; /* 连接状态 */
} redis_globals_t ;
static switch_xml_config_item_t instructions[] = {
/* parameter name type reloadable pointer default value options structure */
SWITCH_CONFIG_ITEM_STRING_STRDUP("db-hostname", CONFIG_RELOAD, &db_globals_t.hostname, NULL, "localhost", "Hostname for db server"), /* 字符串用这个 */
SWITCH_CONFIG_ITEM_STRING_STRDUP("db-name", CONFIG_RELOAD, &db_globals_t.dbname, NULL, "xcroute", "dbname for db server"),
SWITCH_CONFIG_ITEM_STRING_STRDUP("db-user", CONFIG_RELOAD, &db_globals_t.user, NULL, "postgres", "user for db server"),
SWITCH_CONFIG_ITEM_STRING_STRDUP("db-password", CONFIG_RELOAD, &db_globals_t.password, NULL, "abc123", "password for db server"),
SWITCH_CONFIG_ITEM("db-port", SWITCH_CONFIG_INT, CONFIG_RELOADABLE, &db_globals_t.port, (void *) 5432, NULL,NULL, NULL), /* 数字用这个 */
SWITCH_CONFIG_ITEM("db-timeout", SWITCH_CONFIG_INT, CONFIG_RELOADABLE, &db_globals_t.timeout, (void *) 10, NULL,NULL, NULL),
SWITCH_CONFIG_ITEM_STRING_STRDUP("redis-hostname", CONFIG_RELOAD, &redis_globals_t.hostname, NULL, "localhost", "Hostname for redis server"), /* 字符串用这个 */
SWITCH_CONFIG_ITEM_STRING_STRDUP("redis-password", CONFIG_RELOAD, &redis_globals_t.password, NULL, "abc123", "password for redis server"), /* 字符串用这个 */
SWITCH_CONFIG_ITEM("redis-port", SWITCH_CONFIG_INT, CONFIG_RELOADABLE, &redis_globals_t.port, (void *) 6379, NULL,NULL, NULL),
SWITCH_CONFIG_ITEM("redis-timeout", SWITCH_CONFIG_INT, CONFIG_RELOADABLE, &redis_globals_t.timeout, (void *) 10, NULL,NULL, NULL),
SWITCH_CONFIG_ITEM_END()
};
//load模块里写下
if (switch_xml_config_parse_module_settings("stpbx.conf", SWITCH_FALSE, instructions) != SWITCH_STATUS_SUCCESS) {
return SWITCH_STATUS_FALSE;
}
自定义读取配置文件,可扩展性高,容易崩溃
static switch_status_t load_config( )
{
char *CONF_NAME = "stpbx.conf";
switch_xml_t xml = NULL, x_lists = NULL, x_list = NULL, cfg = NULL;
if ( (xml = switch_xml_open_cfg( CONF_NAME, &cfg, NULL ) ) ) //reload 老是报错崩溃,还没研究原因
{
if ( (x_lists = switch_xml_child( cfg, "settings" ) ) )
{
for ( x_list = switch_xml_child( x_lists, "param" ); x_list; x_list = x_list->next )
{
const char *name = switch_xml_attr( x_list, "name" ); /* This needs to be const */
const char *value = switch_xml_attr( x_list, "value" );
/* Ignore empty/missing attributes */
if ( switch_strlen_zero( name ) )
{
continue;
}
if ( switch_strlen_zero( value ) )
{
continue;
}
if ( !strcmp( name, "odbc-dsn" ) )
{
set_global_odbc_dsn( value );
globals.shutdown_int = 1; /* 检测连接设置为启动 */
} else if ( !strcmp( name, "debug" ) )
{
globals.debug = atoi( value );
} else if ( !strcmp( name, "esldebug" ) )
{
globals.esldebug = atoi( value );
}else {
switch_log_printf( SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unknown attribute %s\n", name );
}
}
}
switch_xml_free( xml );
} else {
switch_log_printf( SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Configuration file %s not found!\n", CONF_NAME );
switch_log_printf( SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Please try again reload!\n" );
switch_xml_free( xml );
return(SWITCH_STATUS_FALSE);
}
return(SWITCH_STATUS_SUCCESS);
}