Example #1
0
int main(int argc, char *argv[]) {
    char *h;
    etcd_response response;
    const struct etcd_data *val;
    const char *key = "/key1", *value = "value1";

    if (argc <= 1) {
        hostname = DEFAULT_HOSTNAME;
        port = DEFAULT_PORT;
    } else {
        /* Input in format 'host:port' */
        hostname = h = strdup(argv[1]);
        h = strchr(hostname, ':');
        *h++ = '\0';
        port = atoi(h);
    }


    response = etcd_set(key, value, 0);
    assert(response == ETCD_SUCCESS);

    val = etcd_get(key);
    assert(val->response == ETCD_SUCCESS);
    assert(strcmp(val->value, value) == 0);
    free((struct etcd_data *)val);

    response = etcd_delete(key);
    assert(response == ETCD_SUCCESS);

    val = etcd_get(key);
    assert(val->response == ETCD_FAILURE);
    free((struct etcd_data *)val);

    response = etcd_set(key, value, 5);
    assert(response == ETCD_SUCCESS);

    response = etcd_test_and_set(key, "value2", value, 0);
    assert(response == ETCD_SUCCESS);

    response = etcd_test_and_set(key, "value2", value, 0);
    assert(response == ETCD_FAILURE);

    return 0;
}
Example #2
0
static celix_status_t etcdWatcher_addAlreadyExistingNodes(node_discovery_pt node_discovery, int* highestModified) {
    celix_status_t status = CELIX_SUCCESS;
    char** endpointArray = calloc(MAX_NODES, sizeof(*endpointArray));
    char rootPath[MAX_ROOTNODE_LENGTH];
    int i, size;

    *highestModified = -1;

    if (!endpointArray) {
        status = CELIX_ENOMEM;
    } else {

        for (i = 0; i < MAX_NODES; i++) {
            endpointArray[i] = calloc(MAX_KEY_LENGTH, sizeof(*endpointArray[i]));
        }

        // we need to go though all nodes and get the highest modifiedIndex
        if (((status = etcdWatcher_getRootPath(node_discovery->context, &rootPath[0])) == CELIX_SUCCESS) && (etcd_getEndpoints(rootPath, endpointArray, &size) == true)) {
            for (i = 0; i < size; i++) {
                node_description_pt nodeDescription = NULL;

                char* key = endpointArray[i];
                int modIndex = 0;

                // check for key ..

                char action[MAX_ACTION_LENGTH];
                char etcdValue[MAX_VALUE_LENGTH];

                if (etcd_get(key, &etcdValue[0], &action[0], &modIndex) != true) {
                    printf("NODE_DISCOVERY: Could not retrieve value for %s\n", key);
                    status = CELIX_ILLEGAL_STATE;
                }
                else  {
                    status = etcdWatcher_getWiringEndpointFromKey(node_discovery, key, &etcdValue[0], &nodeDescription);

                    node_discovery_addNode(node_discovery, nodeDescription);
                }

                if (modIndex > *highestModified) {
                    *highestModified = modIndex;
                }
            }
        }

        for (i = 0; i < MAX_NODES; i++) {
            free(endpointArray[i]);
        }
    }

    free(endpointArray);

    return status;
}
Example #3
0
static celix_status_t etcdWatcher_addOwnFramework(etcd_watcher_pt watcher)
{
    celix_status_t status = CELIX_BUNDLE_EXCEPTION;
    char localNodePath[MAX_LOCALNODE_LENGTH];
    char value[MAX_VALUE_LENGTH];
    char action[MAX_VALUE_LENGTH];
    char url[MAX_VALUE_LENGTH];
    int modIndex;
    char* endpoints = NULL;
    char* ttlStr = NULL;
    int ttl;

    bundle_context_pt context = watcher->discovery->context;
    endpoint_discovery_server_pt server = watcher->discovery->server;

    // register own framework
    if ((status = etcdWatcher_getLocalNodePath(context, &localNodePath[0])) != CELIX_SUCCESS) {
        return status;
    }

    if (endpointDiscoveryServer_getUrl(server, &url[0]) != CELIX_SUCCESS) {
        snprintf(url, MAX_VALUE_LENGTH, "http://%s:%s/%s", DEFAULT_SERVER_IP, DEFAULT_SERVER_PORT, DEFAULT_SERVER_PATH);
    }

    endpoints = &url[0];

    if ((bundleContext_getProperty(context, CFG_ETCD_TTL, &ttlStr) != CELIX_SUCCESS) || !ttlStr) {
        ttl = DEFAULT_ETCD_TTL;
    }
    else
    {
        char* endptr = ttlStr;
        errno = 0;
        ttl =  strtol(ttlStr, &endptr, 10);
        if (*endptr || errno != 0) {
            ttl = DEFAULT_ETCD_TTL;
        }
    }

    if (etcd_get(localNodePath, &value[0], &action[0], &modIndex) != true) {
        etcd_set(localNodePath, endpoints, ttl, false);
    }
    else if (etcd_set(localNodePath, endpoints, ttl, true) == false)  {
        logHelper_log(*watcher->loghelper, OSGI_LOGSERVICE_WARNING, "Cannot register local discovery");
    }
    else {
        status = CELIX_SUCCESS;
    }

    return status;
}
Example #4
0
enum nss_status nss_etcd_ip_address(const char *name, uint32_t *ip_address){

    char *key;
    char *value = NULL;
    char *servers = "localhost";
    etcd_session    etcd_session = NULL;
    enum nss_status return_code;

    // covert the domain to a key
    key = nss_etcd_key_from_name(name);
    if(!key){
        return_code = NSS_STATUS_UNAVAIL;
        goto cleanup;
    }

    // create the session
    etcd_session = etcd_open_str(servers);
    if(!etcd_session){
        return_code = NSS_STATUS_UNAVAIL;
        goto cleanup;
    }

    // get the value
    value = etcd_get(etcd_session,key);
    if(!value){
        return_code = NSS_STATUS_NOTFOUND;
        goto cleanup;
    }

    // get the value in decimal form
    if(!inet_pton(AF_INET, value, ip_address)){
        return_code = NSS_STATUS_NOTFOUND;
        goto cleanup;
    }

    // We found the record!
    return_code = NSS_STATUS_SUCCESS;
    goto cleanup;

    cleanup:
    free(key);
    if(value != NULL) {
        free(value);
    }
    if(etcd_session != NULL) {
        etcd_close_str(etcd_session);
    }
    return return_code;

}
Example #5
0
/*
 * retrieves all already existing discovery endpoints
 * from etcd and adds them to the poller.
 *
 * returns the modifiedIndex of the last modified
 * discovery endpoint (see etcd documentation).
 */
static celix_status_t etcdWatcher_addAlreadyExistingWatchpoints(discovery_pt discovery, int* highestModified) {
    celix_status_t status = CELIX_SUCCESS;
    char** nodeArr = calloc(MAX_NODES, sizeof(*nodeArr));
    char rootPath[MAX_ROOTNODE_LENGTH];
    int i, size;

    *highestModified = -1;

    for (i = 0; i < MAX_NODES; i++) {
        nodeArr[i] = calloc(MAX_KEY_LENGTH, sizeof(*nodeArr[i]));
    }

    // we need to go though all nodes and get the highest modifiedIndex
    if (((status = etcdWatcher_getRootPath(discovery->context, &rootPath[0])) == CELIX_SUCCESS) &&
            (etcd_getNodes(rootPath, nodeArr, &size) == true)) {
        for (i = 0; i < size; i++) {
            char* key = nodeArr[i];
            char value[MAX_VALUE_LENGTH];
            char action[MAX_VALUE_LENGTH];
            int modIndex;

            if (etcd_get(key, &value[0], &action[0], &modIndex) == true) {
                // TODO: check that this is not equals to the local endpoint
                endpointDiscoveryPoller_addDiscoveryEndpoint(discovery->poller, &value[0]);

                if (modIndex > *highestModified) {
                    *highestModified = modIndex;
                }
            }
        }
    }

    for (i = 0; i < MAX_NODES; i++) {
        free(nodeArr[i]);
    }

    free(nodeArr);

    return status;
}