コード例 #1
0
ファイル: etcd_watcher.c プロジェクト: ErjanAltena/celix
/*
 * performs (blocking) etcd_watch calls to check for
 * changing discovery endpoint information within etcd.
 */
static void* etcdWatcher_run(void* data) {
    etcd_watcher_pt watcher = (etcd_watcher_pt) data;
    time_t timeBeforeWatch = time(NULL);
    static char rootPath[MAX_ROOTNODE_LENGTH];
    int highestModified = 0;

    bundle_context_pt context = watcher->discovery->context;

    etcdWatcher_addAlreadyExistingWatchpoints(watcher->discovery, &highestModified);
    etcdWatcher_getRootPath(context, &rootPath[0]);

    while (watcher->running) {

        char rkey[MAX_KEY_LENGTH];
        char value[MAX_VALUE_LENGTH];
        char preValue[MAX_VALUE_LENGTH];
        char action[MAX_ACTION_LENGTH];
        int modIndex;

        if (etcd_watch(rootPath, highestModified + 1, &action[0], &preValue[0], &value[0], &rkey[0], &modIndex) == true) {
            if (strcmp(action, "set") == 0) {
                etcdWatcher_addEntry(watcher, &rkey[0], &value[0]);
            } else if (strcmp(action, "delete") == 0) {
                etcdWatcher_removeEntry(watcher, &rkey[0], &value[0]);
            } else if (strcmp(action, "expire") == 0) {
                etcdWatcher_removeEntry(watcher, &rkey[0], &value[0]);
            } else if (strcmp(action, "update") == 0) {
                etcdWatcher_addEntry(watcher, &rkey[0], &value[0]);
            } else {
                logHelper_log(*watcher->loghelper, OSGI_LOGSERVICE_INFO, "Unexpected action: %s", action);
            }

            highestModified = modIndex;
        }

        // update own framework uuid
        if (time(NULL) - timeBeforeWatch > (DEFAULT_ETCD_TTL/2)) {
            etcdWatcher_addOwnFramework(watcher);
            timeBeforeWatch = time(NULL);
        }
    }

    return NULL;
}
コード例 #2
0
ファイル: etcd_watcher.c プロジェクト: INAETICS/node-wiring-c
/*
 * performs (blocking) etcd_watch calls to check for
 * changing discovery endpoint information within etcd.
 */
static void* etcdWatcher_run(void* data) {
    etcd_watcher_pt watcher = (etcd_watcher_pt) data;
    time_t timeBeforeWatch = time(NULL);
    char rootPath[MAX_ROOTNODE_LENGTH];
    int highestModified = 0;

    node_discovery_pt node_discovery = watcher->node_discovery;
    bundle_context_pt context = node_discovery->context;

    memset(rootPath, 0, MAX_ROOTNODE_LENGTH);

    etcdWatcher_addAlreadyExistingNodes(node_discovery, &highestModified);
    etcdWatcher_getRootPath(context, rootPath);

    while ((celixThreadMutex_lock(&watcher->watcherLock) == CELIX_SUCCESS) && watcher->running) {

        char rkey[MAX_KEY_LENGTH];
        char value[MAX_VALUE_LENGTH];
        char preValue[MAX_VALUE_LENGTH];
        char action[MAX_ACTION_LENGTH];
        int modIndex;

        celixThreadMutex_unlock(&watcher->watcherLock);

        if (etcd_watch(rootPath, highestModified + 1, &action[0], &preValue[0], &value[0], &rkey[0], &modIndex) == true) {
            if ((strcmp(action, "set") == 0) || (strcmp(action, "create") == 0)) {
                node_description_pt nodeDescription = NULL;
                celix_status_t status = etcdWatcher_getWiringEndpointFromKey(node_discovery, &rkey[0], &value[0], &nodeDescription);

                if (status == CELIX_SUCCESS) {
                    node_discovery_addNode(node_discovery, nodeDescription);
                }
            } else if (strcmp(action, "delete") == 0) {
                node_description_pt nodeDescription = NULL;
                celix_status_t status = etcdWatcher_getWiringEndpointFromKey(node_discovery, &rkey[0], NULL, &nodeDescription);
                if (status == CELIX_SUCCESS) {
                    node_discovery_removeNode(node_discovery, nodeDescription);
                }
            } else if (strcmp(action, "expire") == 0) {
                node_description_pt nodeDescription = NULL;
                celix_status_t status = etcdWatcher_getWiringEndpointFromKey(node_discovery, &rkey[0], NULL, &nodeDescription);
                if (status == CELIX_SUCCESS) {
                    node_discovery_removeNode(node_discovery, nodeDescription);
                }
            } else if (strcmp(action, "update") == 0) {
                node_description_pt nodeDescription = NULL;
                celix_status_t status = etcdWatcher_getWiringEndpointFromKey(node_discovery, &rkey[0], &value[0], &nodeDescription);

                if (status == CELIX_SUCCESS) {
                    node_discovery_addNode(node_discovery, nodeDescription);
                }
            } else {
                fw_log(logger, OSGI_FRAMEWORK_LOG_INFO, "Unexpected action: %s", action);
            }
            highestModified = modIndex;
        }
        /* prevent busy waiting, in case etcd_watch returns false */
        else if (time(NULL) - timeBeforeWatch <= (DEFAULT_ETCD_TTL / 4)) {
            sleep(DEFAULT_ETCD_TTL / 4);
        }

        // update own framework uuid
        if (time(NULL) - timeBeforeWatch > (DEFAULT_ETCD_TTL / 4)) {
            etcdWatcher_addOwnNode(watcher);

            // perform additional full-sync
            etcdWatcher_addAlreadyExistingNodes(node_discovery, &highestModified);
            timeBeforeWatch = time(NULL);
        }
    }

    if (watcher->running == false) {
        celixThreadMutex_unlock(&watcher->watcherLock);
    }

    return NULL;
}