1. 程式人生 > >Zookeeper C API應用示例(3)——配置管理(非同步API)

Zookeeper C API應用示例(3)——配置管理(非同步API)

場景描述同:https://blog.csdn.net/qq_41688455/article/details/83780854

服務端程式碼如下:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <zookeeper.h>
#include <zookeeper_log.h>
int loop = 1;
#define ZK_CFG_ZNODE_PATH        "/configure"
extern void zkServer_Read(zhandle_t *zh, const char *path);
extern void zkServer_Get_Children(zhandle_t *zh, const char *path);
static char *type2string(int type)
{
    if (ZOO_CREATED_EVENT == type)
        return "CreateEvent";
    else if (ZOO_DELETED_EVENT == type)
        return "DeleteEvent";
    else if (ZOO_CHANGED_EVENT == type)
        return "DataChangeEvent";
    else if (ZOO_CHILD_EVENT == type)
        return "ChildEvent";
    else if (ZOO_SESSION_EVENT == type)
        return "SessionEvent";
    else
        return "NonEvent";
}
static void free_String_vector(struct String_vector *v)
{
    int i = 0;
    
    if (v->data)
    {
        for (i = 0; i < v->count; i++)
        {
            free(v->data[i]);
        }
        free(v->data);
        v->data = 0;
    }
    
    return;
}
void zkServer_Create_Znode_Completion(int rc, const char *value, const void *data)
{
    switch (rc)
    {
        case ZCONNECTIONLOSS:
        {
            printf("ZK connection lost!\n", value);
            break;
        }
        case ZOK:
        {
            printf("Create znode(%s) successfully!\n", value);
            break;
        }
        case ZNODEEXISTS:
        {
            printf("Node already exist!\n");
            break;
        }
        default:
        {
            printf("Something wrong!\n");
            break;
        }
    }
    
    return;
}
void zkServer_Create_Znode(zhandle_t *zh, char* path)
{
    zoo_acreate(zh, path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, zkServer_Create_Znode_Completion, NULL);
}
void zkServer_Read_Completion(int rc, const char *value, int value_len, const struct Stat *stat, const void *data)
{
    switch (rc)
    {
        case ZCONNECTIONLOSS:
        case ZOPERATIONTIMEOUT:
        {
            printf("Connection lost or timeout!\n");
            break;
        }
        case ZOK:
        {
            printf("value_len: %d\n", value_len);
            if (value_len > 0)
            {
                /* 此處直接列印value可能會出現亂碼或者字串不完整的現象 */
                char tmp[100] = {0};
                strncpy(tmp, value, value_len);
                printf("Data: %s\n", tmp);
                if (!strncmp(tmp, "close", 6))
                {
                    loop = 0;
                }
            }
            break;
        }
        default:
        {
            printf("Something wrong!!\n");
            break;
        }
    }
    
    return;
}
void zkServer_Read_Watch(zhandle_t *zh, int type, int state, const char *path, void *watchCtx)
{
    if (ZOO_CHANGED_EVENT == type)
    {
        printf("There are data changed in znode(%s)!\n", path);
        zkServer_Read(zh, path);
    }
    else
    {
        printf("[%s]Watched Event: %s!\n", __FUNCTION__, type2string(type));
    }
    
    return;
}
void zkServer_Read(zhandle_t *zh, const char *path)
{
    //zoo_awget(zh, path, zkServer_Read_Watch, NULL, zkServer_Read_Completion, path);
    zoo_awget(zh, path, zkServer_Read_Watch, NULL, zkServer_Read_Completion, NULL);
    
    return;
}
void zkServer_Get_Children_Watcher(zhandle_t *zh, int type, int state, const char *path, void *watchCtx)
{
    if (ZOO_CHILD_EVENT == type)
    {
        printf("Children had changed in znode(%s)!\n", path);
        zkServer_Get_Children(zh, path);
    }
    else
    {
        printf("[%s] Watched Event: %s!\n", __FUNCTION__, type2string(type));
    }
    
    return;
}
void zkServer_Get_Children_Completion(int rc, const char *value, const void *data)
{
    switch (rc)
    {
        case ZOK:
        {
            int i = 0;
            struct String_vector *child_v = (struct String_vector *)value;
            printf("There are %d children in znode(%s)!\n", child_v->count, (char*)(data==0 ? "null" : data));
            for (i = 0; i < child_v->count; i++)
            {
                printf("%d: %s\n", i, child_v->data[i]);
            }
            
            free_String_vector(child_v);
            break;
        }
        case ZCONNECTIONLOSS:
        case ZOPERATIONTIMEOUT:
        {
            printf("Connection Lost or Operation Timeout!\n");
            break;
        }
        default:
        {
            printf("Something wrong!!\n");
            break;
        }
    }
    
    return;
}
void zkServer_Get_Children(zhandle_t *zh, const char *path)
{
    zoo_awget_children(zh, path, zkServer_Get_Children_Watcher, NULL, zkServer_Get_Children_Completion, path);
    
    return;
}
void zkServer_Watcher_fn(zhandle_t *zh, int type, int state, const char *path, void *watcherCtx)
{
    if (ZOO_SESSION_EVENT == type)
    {
        if (ZOO_CONNECTED_STATE == state)
        {
            printf("Connect zookeeper successfully!!\n");
        }
        else
        {
            printf("Connect zookeeper failed!!\n");
        }
    }
    
    return;
}
int main(int argc, char** argv)
{
    int res = 0;
    int exists = 0;
    const char* host = "10.43.17.38:2181,10.43.17.39:2181,10.43.17.41:2181";
    
    zhandle_t* zh = zookeeper_init(host, zkServer_Watcher_fn, 30000, 0, "zk_server", 0);
    if (NULL == zh)
    {
        return -1;
    }
    
    zkServer_Create_Znode(zh, ZK_CFG_ZNODE_PATH);
    zkServer_Read(zh, ZK_CFG_ZNODE_PATH);
    zkServer_Get_Children(zh, ZK_CFG_ZNODE_PATH);
    
    while (loop)
    {
        sleep(1);
    }
    
    zookeeper_close(zh);
    return 0;
}

客戶端程式碼如下:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <zookeeper.h>
#include <zookeeper_log.h>
#define ZK_CFG_ZNODE_PATH        "/configure"
void zkClient_CreateChild_Completion(int rc, const char *value, const void *data)
{
    if (ZOK == rc)
        printf("Create child(%s) successfully!\n", value);
    else if (ZNONODE == rc)
        printf("The parent does not exist!\n");
    else if (ZNODEEXISTS == rc)
        printf("The znode(%s) already exist!\n", value);
    else
        printf("Create child failed(%d)!\n", rc);
    
    return;
}
void zkClient_CreateChild(zhandle_t *zh, const char *path)
{
    zoo_acreate(zh, path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, zkClient_CreateChild_Completion, NULL);
    
    return;
}
void zkClient_DeleteZnode_Completion(int rc, const void *data)
{
    if (ZOK == rc)
        printf("Delete znode(%s) successfully!\n", (char*)(data==0 ? "null" : data));
    else if (ZNONODE == rc)
        printf("The znode does not exist already!\n");
    else if (ZNOTEMPTY == rc)
        printf("There are children in znode(%s)!\n", (char*)(data==0 ? "null" : data));
    else
        printf("Delete znode failed(%d)!\n", rc);
    
    return;
}
void zkClient_DeleteZnode(zhandle_t *zh, char *path)
{
    zoo_adelete(zh, path, -1, zkClient_DeleteZnode_Completion, path);
    
    return;
}
ZOOAPI int zoo_aset(zhandle_t * zh, const char *path,
                    const char *buffer, int buflen, int version,
                    stat_completion_t completion, const void *data);
void zkClient_Write_Completion(int rc, const struct Stat *stat, const void *data)
{
    if (ZOK == rc)
        printf("Write data to znode(%s) successfully!\n", (char*)(data==0 ? "null" : data));
    else
        printf("Write data to znode(%s) failed(%d)!\n", (char*)(data==0 ? "null" : data), rc);
    return;
}
void zkClient_Write(zhandle_t *zh, const char *path, char* data)
{
    zoo_aset(zh, path, data, strlen(data), -1, zkClient_Write_Completion, path);
    
    return;
}
void zkClient_Watcher_fn(zhandle_t *zh, int type, int state, const char *path, void *watcherCtx)
{
    if (ZOO_SESSION_EVENT == type)
    {
        if (ZOO_CONNECTED_STATE == state)
        {
            printf("Connect zookeeper successfully!!\n");
        }
        else
        {
            printf("Connect zookeeper failed!!\n");
        }
    }
    
    return;
}
int main(int argc, char** argv)
{
    int selection = 0;
    const char* host = "10.43.17.38:2181,10.43.17.39:2181,10.43.17.41:2181";
    int loop = 1;
    
    zhandle_t* zh = zookeeper_init(host, zkClient_Watcher_fn, 30000, 0, "zk_client", 0);
    if (NULL == zh)
    {
        printf("zk_client: Connect zookeeper failed!\n");
        return -1;
    }
    
    do
    {
        printf("1: Set znode data\n");
        printf("2: Create child\n");
        printf("3: Delete znode\n");
        printf("Select: ");
        scanf("%d", &selection);
        switch (selection)
        {
            case 1:
            {
                char buffer[100] = {0};
                
                scanf("%s", buffer); /* scanf遇到空格結束, 可以替換為gets */
                printf("buffer: %s\n", buffer);
                zkClient_Write(zh, ZK_CFG_ZNODE_PATH, buffer);
                break;
            }
            case 2:
            {
                char name[10] = {0};
                char path[100] = {0};
                
                scanf("%s", name);
                snprintf(path, 100, "%s/%s", ZK_CFG_ZNODE_PATH, name);
                printf("child: %s\n", path);
                zkClient_CreateChild(zh, path);
                break;
            }
            case 3:
            {
                char path[100] = {0};
                
                scanf("%s", path);
                printf("znode: %s\n", path);
                zkClient_DeleteZnode(zh, path);
                break;
            }
            default:
            {
                loop = 0;
                break;
            }
        }
    } while (loop);
    
    zookeeper_close(zh);
    return 0;
}