diff --git a/kenaito-conf-c-client-demo/main.c b/kenaito-conf-c-client-demo/main.c index 1be23bd..3effc9a 100644 --- a/kenaito-conf-c-client-demo/main.c +++ b/kenaito-conf-c-client-demo/main.c @@ -1,17 +1,10 @@ /** * Kenaito Config Center C Client Demo * - * 演示如何使用 C 客户端 SDK: - * - 初始化客户端 - * - 注册配置变更回调 - * - 启动/停止客户端 - * - 读取配置 + * 演示如何使用 C 客户端 SDK * * 编译: - * gcc -o demo main.c -I../kenaito-conf-c-client -L../kenaito-conf-c-client -lkenaito_conf -lcurl -lcjson -lpthread - * - * 或直接编译所有源文件: - * gcc -o demo main.c ../kenaito-conf-c-client/main.c -lcurl -lcjson -lpthread + * gcc -o demo main.c ../kenaito-conf-c-client/main.c -I/usr/local/opt/cjson/include -L/usr/local/opt/cjson/lib -lcurl -lcjson -lpthread * * 运行: * ./demo @@ -21,10 +14,39 @@ #include #include #include -#include "../kenaito-conf-c-client/main.c" +#include +#include + +/* 声明 SDK 类型和函数 */ +typedef struct { + char baseUrl[512]; + char appName[128]; + char env[32]; + char version[32]; + char token[256]; + int syncInterval; + int ignoreGlobal; + char cacheDir[256]; +} Config; + +typedef struct { + Config config; + void *configs; + long lastTime; + void (*onChange)(const char *key, const char *value); + void *curl; + int running; + void *syncThread; +} ConfigClient; + +extern void config_client_init(ConfigClient *client, Config *config); +extern const char *config_client_get(ConfigClient *client, const char *key); +extern void config_client_start(ConfigClient *client); +extern void config_client_stop(ConfigClient *client); /* 全局客户端实例,用于信号处理 */ static ConfigClient *g_demo_client = NULL; +static int g_running = 1; /** * 配置变更回调示例 @@ -32,12 +54,10 @@ static ConfigClient *g_demo_client = NULL; void on_config_change(const char *key, const char *value) { printf("\n[Callback] Config changed: %s = %s\n", key, value); - /* 示例:根据配置键执行不同操作 */ if (strcmp(key, "log.level") == 0) { printf(" -> Log level changed to: %s\n", value); } else if (strcmp(key, "database.host") == 0) { printf(" -> Database host changed to: %s\n", value); - /* 实际应用中可在此重新初始化数据库连接 */ } else if (strcmp(key, "database.port") == 0) { printf(" -> Database port changed to: %s\n", value); } else if (strcmp(key, "feature.enabled") == 0) { @@ -50,6 +70,7 @@ void on_config_change(const char *key, const char *value) { */ static void signal_handler(int sig) { printf("\n[Signal] Received signal %d, shutting down...\n", sig); + g_running = 0; if (g_demo_client) { g_demo_client->running = 0; } @@ -64,9 +85,6 @@ int main() { signal(SIGINT, signal_handler); signal(SIGTERM, signal_handler); - /* 初始化 CURL */ - curl_global_init(CURL_GLOBAL_DEFAULT); - /* 配置客户端 */ Config config = { .baseUrl = "http://localhost:8080", @@ -92,7 +110,7 @@ int main() { printf("\n[Demo] Running... Press Ctrl+C to stop\n\n"); int iterations = 0; - while (client.running && iterations < 10) { + while (g_running && iterations < 10) { sleep(5); /* 演示:定期读取并打印配置 */ @@ -106,7 +124,7 @@ int main() { printf(" database.port = %s\n", dbPort ? dbPort : "(not set)"); printf(" log.level = %s\n", logLevel ? logLevel : "(not set)"); printf(" feature.enabled = %s\n", featureEnabled ? featureEnabled : "(not set)"); - printf(" Total configs: %d\n\n", client.configs.count); + printf("\n"); iterations++; } diff --git a/kenaito-conf-c-client/main.c b/kenaito-conf-c-client/main.c index 9147152..568371d 100644 --- a/kenaito-conf-c-client/main.c +++ b/kenaito-conf-c-client/main.c @@ -14,13 +14,16 @@ * - pthread * * 编译: - * gcc -c main.c -o kenaito_conf.o -lcurl -lcjson + * gcc -c main.c -o kenaito_conf.o -I/usr/local/opt/cjson/include -lcurl * * @author odboy * @version 1.0.0 * @date 2026-10-16 */ +#ifndef KENAITO_CONF_CLIENT_IMPL +#define KENAITO_CONF_CLIENT_IMPL + #include #include #include @@ -440,3 +443,5 @@ void config_client_stop(ConfigClient *client) { curl_global_cleanup(); printf("[Client] Stopped\n"); } + +#endif /* KENAITO_CONF_CLIENT_IMPL */