freeswitch 内核模块 读取配置文件的两种方式

稳定,不易扩展

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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄宝良

你的鼓励是我不懈努力的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值