/* * test main for conf module, usage: a.out conffile */ int main(int argc, char *argv[]) { struct opts *opts; err_init(argv[0]); setbuf(stdout, NULL); opts_init(Opttable, Opttable_cnt); opts = opts_parse(NULL, NULL, 0); if (argc != 2) err(EF_RAW, "usage: %s conffile\n", argv[0]); conf_open(argv[1], argv[1], opts); printf("conffile <%s>:\n", argv[1]); conf_print(stdout, NULL); conf_close(opts); err_done(0); /* NOTREACHED */ return (0); }
//解析配置信息,并进行有效性判断 struct conf * conf_create(char *filename) { rstatus_t status; struct conf *cf; //打开配置文件,创建conf结构,并初始化 cf = conf_open(filename); if (cf == NULL) { return NULL; } /* validate configuration file before parsing */ status = conf_pre_validate(cf); //检查配置格式是否符合yml标准 if (status != NC_OK) { goto error; } /* parse the configuration file */ status = conf_parse(cf); //配置解析 if (status != NC_OK) { goto error; } /* validate parsed configuration */ status = conf_post_validate(cf); //重复性判断 if (status != NC_OK) { goto error; } conf_dump(cf); fclose(cf->fh); cf->fh = NULL; return cf; error: log_stderr("nutcracker: configuration file '%s' syntax is invalid", filename); fclose(cf->fh); cf->fh = NULL; conf_destroy(cf); return NULL; }
rmt_conf * conf_create(char *filename) { int ret; rmt_conf *cf; cf = conf_open(filename); if (cf == NULL) { return NULL; } /* validate configuration file before parsing */ ret = conf_pre_validate(cf); if (ret != RMT_OK) { goto error; } conf_organizations_dump(cf); /* parse the configuration file */ ret = conf_parse(cf); if (ret != RMT_OK) { goto error; } /* validate parsed configuration */ ret = conf_post_validate(cf); if (ret != RMT_OK) { goto error; } conf_dump(cf); fclose(cf->fh); cf->fh = NULL; return cf; error: fclose(cf->fh); cf->fh = NULL; conf_destroy(cf); return NULL; }
struct conf * conf_create(char *filename) { rstatus_t status; struct conf *cf; cf = conf_open(filename); if (cf == NULL) { return NULL; } /* validate configuration file before parsing */ status = conf_pre_validate(cf); if (status != NC_OK) { goto error; } /* parse the configuration file */ status = conf_parse(cf); if (status != NC_OK) { goto error; } /* validate parsed configuration */ status = conf_post_validate(cf); if (status != NC_OK) { goto error; } conf_dump(cf); fclose(cf->fh); cf->fh = NULL; return cf; error: fclose(cf->fh); cf->fh = NULL; conf_destroy(cf); return NULL; }
/* * test main for conf module, usage: a.out conffile */ int main(int argc, char *argv[]) { err_init(argv[0]); setbuf(stdout, NULL); if (argc != 2) err(EF_RAW, "usage: %s conffile\n", argv[0]); conf_open(argv[1], 1); printf("conffile <%s>:\n", argv[1]); conf_print(stdout); conf_close(opts_parse(NULL, 0)); err_done(0); /* NOTREACHED */ return (0); }
int main(int argc, char** argv) { char path[50] = { 0x00 }; if (2 <= argc) { strncpy(path, argv[1], strlen(argv[1])); } else { strncpy(path, "server.cfg", 10); } //读取配置文件 TinyConf* config; char* run; for(;;) { config = conf_open(path, '='); if(NULL == config) { sleep(5); continue; } run = conf_get(config, "run"); if(0 >= strlen(run)) { sleep(5); continue; } if(0 != strcmp(run, "on")) { sleep(5); continue; } break; //一切ok,启动正常处理 } //初始化mysql配置 char* mysql_host = conf_get(config, "mysql_host"); char* mysql_port_str = conf_get(config, "mysql_port"); char* mysql_user = conf_get(config, "mysql_user"); char* mysql_password = conf_get(config, "mysql_password"); char* mysql_database = conf_get(config, "mysql_database"); //将数据库设置存入到map中 TinyMap* store_map = map_create(); map_add(store_map, "host", mysql_host, NULL); map_add(store_map, "port", mysql_port_str, NULL); map_add(store_map, "user", mysql_user, NULL); map_add(store_map, "password", mysql_password, NULL); map_add(store_map, "database", mysql_database, NULL); //初始化tcpserver的相关代码 int tcp_port = 7000; char* tcp_port_str = conf_get(config, "port"); if(0 < strlen(tcp_port_str)) { tcp_port = atoi(tcp_port_str); } TCPServer* tcp_server = tcp_server_create(tcp_port); //生成tcp服务器实例 if(NULL == tcp_server) { fprintf(stderr, "Create server falied.\n"); } tcp_server_set_proc_callback(tcp_server, store_map, analyze_proc); //注册数据处理的回调函数 tcp_server_set_timeout_callback(tcp_server, store_map, analyze_timeout); //注册客户端连接超时的回调函数 int ret = tcp_server_start(tcp_server); //启动tcp服务器 if(-1 == ret) { fprintf(stderr, "Start tcpserver falied.\n"); return 0; } //创建存储实例 Store* store = store_create(mysql_host, atoi(mysql_port_str), mysql_user, mysql_password, mysql_database); //创建网络检测线程 pthread_t thread_id; int p_ret = pthread_create(&thread_id, NULL, (void*)check_networks, store); if(0 != p_ret) { fprintf(stderr, "%s\n", "create networks check thread err."); } for(;;) { //阻塞 sleep(1000); } pthread_cancel(thread_id); //退出线程 store_destroy(store); //销毁存储实例 tcp_server_stop(tcp_server); //停止tcp服务器实例 tcp_server_destroy(tcp_server); //销毁tcp服务器实例 map_destroy(store_map); //销毁store_map实例 conf_close(config); //关闭配置信息读取实例 return 0; }