test_reloader.cpp 3.8 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
/***************************************************************************
 * 
 * Copyright (c) 2008 Baidu.com, Inc. All Rights Reserved
 * $Id: test_reloader.cpp,v 1.2 2010/02/09 01:58:01 scmpf Exp $ 
 * 
 **************************************************************************/
 
 
 
/**
 * @file test_conf.cpp
 * @author yingxiang(com@baidu.com)
 * @date 2008/12/24 18:47:10
 * @version $Revision: 1.2 $ 
 * @brief 
 *  
 **/

#include "ul_conf.h"
#include "Configure.h"
#include <ConfigReloader.h>
#include <pthread.h>
#include "stdio.h"
#include <string>

using namespace comcfg;

static int g_run = 0;

static void * change_threadfun(void *param)
{
	ConfigReloader * prd = (ConfigReloader *)param;
	while (g_run) {
		printf("g_run:%d, monitor: %d\n",g_run, prd->monitor());
		sleep(10);
	}
	return NULL;
}

int int_key_change(const ConfigUnit &keyold, const ConfigUnit &keynew, void * ) {
	printf("old:%d, new:%d\n", keyold.to_int32(), keynew.to_int32());
	if (g_run) {
		if (++g_run >3) {
			g_run = 0;
		}
	}
	return 0;
}

int int_string_change(const ConfigUnit &keyold, const ConfigUnit &keynew, void * ) {
	printf("old:%s, new:%s\n", keyold.to_bsl_string().c_str(), keynew.to_bsl_string().c_str());
	if (g_run) {
		if (++g_run >3) {
			g_run = 0;
		}
	}
	return 0;
}

int uint64_key_change(const ConfigUnit &keyold, const ConfigUnit &keynew, void * ) {
	printf("[%s]old:%llu, new:%llu\n",keynew.get_key_name().c_str(), keyold.to_uint64(), keynew.to_uint64());
	if (g_run) {
		if (++g_run >3) {
			g_run = 0;
		}
	}
	return 0;
}

int char_key_change(const ConfigUnit &keyold, const ConfigUnit &keynew, void * ) {
	printf("[%s]old:%c, new:%c\n",keynew.get_key_name().c_str(), keyold.to_char(), keynew.to_char());
	if (g_run) {
		if (++g_run >3) {
			g_run = 0;
		}
	}
	return 0;
}

int float_key_change(const ConfigUnit &keyold, const ConfigUnit &keynew, void * ) {
	printf("[%s]old:%f, new:%f\n",keynew.get_key_name().c_str(), keyold.to_double(), keynew.to_double());
	if (g_run) {
		if (++g_run >3) {
			g_run = 0;
		}
	}
	return 0;
}

int group_change(const ConfigUnit &, const ConfigUnit &keynew, void * ) {
//	printf("[%s]\n",keynew.print());
	keynew.print();
	if (g_run) {
		if (++g_run >3) {
			g_run = 0;
		}
	}
	return 0;
}

int file_change(const ConfigUnit &, const ConfigUnit &keynew, void * ) {
	printf("file[ivar]:\n");
	keynew["ivar"].print();
	if (g_run) {
		if (++g_run >3) {
			g_run = 0;
		}
	}
	return 0;
}

int main(){
	ConfigReloader rd;
	if (0 != rd.init("./", "reload.conf","reload.range")) {
		printf("init failed\n");
		return 1;
	}
	rd.add_key_char("ivar.b", char_key_change, NULL);
	rd.add_key_char("ivar.b", int_key_change, NULL);
	rd.add_key_char("ivar.b", char_key_change, NULL);
	printf("add char:%c\n", rd.get_config()->deepGet("ivar.a").to_char());
	rd.add_key_int("ivar.a", char_key_change, NULL);
	rd.add_key_int("ivar.a", int_key_change, NULL);
	printf("add int:%d\n", rd.get_config()->deepGet("ivar.a").to_int32());
	rd.add_key_uint64("ivar.c", uint64_key_change, NULL);
	printf("add int:%llu\n", rd.get_config()->deepGet("ivar.c").to_uint64());
	rd.add_key_float("ivar.d", float_key_change, NULL);
	printf("add char:%c\n", rd.get_config()->deepGet("ivar.a").to_char());
	rd.add_key_string("ubclient.proto[4].method[5].name", int_string_change, NULL);
	printf("add string:%s\n", rd.get_config()->deepGet("ubclient.proto[4].method[5].name").to_bsl_string().c_str());

	rd.add_file_monitor(file_change, NULL);
	rd.add_file_monitor(file_change, NULL);
	printf("add file\n");

	rd.add_group_monitor("ivar.g", group_change, NULL);
	rd.add_group_monitor("ivar", group_change, NULL);
	printf("\ngroup:\n");rd.get_config()->deepGet("ivar").print();
	g_run = 1;
	pthread_t tid;
	pthread_create(&tid, NULL, change_threadfun, &rd);
	pthread_join(tid, NULL);
	return 0;
}

















/* vim: set ts=4 sw=4 sts=4 tw=100 */