celix_status_t etcdWatcher_addOwnNode(etcd_watcher_pt watcher) { celix_status_t status = CELIX_BUNDLE_EXCEPTION; char localNodePath[MAX_LOCALNODE_LENGTH]; char* ttlStr = NULL; int ttl; node_discovery_pt node_discovery = watcher->node_discovery; bundle_context_pt context = node_discovery->context; node_description_pt ownNodeDescription = node_discovery->ownNode; // register own framework status = etcdWatcher_getLocalNodePath(context, ownNodeDescription, &localNodePath[0]); // determine ttl 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; } } // register every wiring endpoint array_list_iterator_pt endpointIter = arrayListIterator_create(ownNodeDescription->wiring_ep_descriptions_list); while (status == CELIX_SUCCESS && arrayListIterator_hasNext(endpointIter)) { wiring_endpoint_description_pt wiringEndpointDesc = arrayListIterator_next(endpointIter); if (wiringEndpointDesc == NULL) { status = CELIX_ILLEGAL_ARGUMENT; } else { char etcdKey[MAX_LOCALNODE_LENGTH]; char etcdValue[MAX_LOCALNODE_LENGTH]; char* wireId = properties_get(wiringEndpointDesc->properties, WIRING_ENDPOINT_DESCRIPTION_WIRE_ID_KEY); wiringEndpoint_properties_store(wiringEndpointDesc->properties, &etcdValue[0]); snprintf(etcdKey, MAX_LOCALNODE_LENGTH, "%s/%s", localNodePath, wireId); // TODO : implement update etcd_set(etcdKey, etcdValue, ttl, false); } } arrayListIterator_destroy(endpointIter); return status; }
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; }
celix_status_t etcdWatcher_destroy(etcd_watcher_pt watcher) { celix_status_t status = CELIX_SUCCESS; char localNodePath[MAX_LOCALNODE_LENGTH]; celixThreadMutex_lock(&(watcher->watcherLock)); watcher->running = false; celixThreadMutex_unlock(&(watcher->watcherLock)); watcher->running = false; celixThread_join(watcher->watcherThread, NULL); celixThreadMutex_destroy(&(watcher->watcherLock)); // remove own registration status = etcdWatcher_getLocalNodePath(watcher->node_discovery->context, watcher->node_discovery->ownNode, &localNodePath[0]); if (status != CELIX_SUCCESS || etcd_del(localNodePath) == false) { printf("Cannot remove local discovery registration."); } free(watcher); return status; }
celix_status_t etcdWatcher_destroy(etcd_watcher_pt watcher) { celix_status_t status = CELIX_SUCCESS; char localNodePath[MAX_LOCALNODE_LENGTH]; watcher->running = false; celixThread_join(watcher->watcherThread, NULL); // register own framework status = etcdWatcher_getLocalNodePath(watcher->discovery->context, &localNodePath[0]); if (status != CELIX_SUCCESS || etcd_del(localNodePath) == false) { logHelper_log(*watcher->loghelper, OSGI_LOGSERVICE_WARNING, "Cannot remove local discovery registration."); } watcher->loghelper = NULL; hashMap_destroy(watcher->entries, true, true); free(watcher); return status; }