Repository: celix Updated Branches: refs/heads/develop c396aeed1 -> 1010f7126
CELIX-252: Aligned memory allocation Project: http://git-wip-us.apache.org/repos/asf/celix/repo Commit: http://git-wip-us.apache.org/repos/asf/celix/commit/1010f712 Tree: http://git-wip-us.apache.org/repos/asf/celix/tree/1010f712 Diff: http://git-wip-us.apache.org/repos/asf/celix/diff/1010f712 Branch: refs/heads/develop Commit: 1010f71262e1438d08c30b179bb35c0393df9e71 Parents: c396aee Author: Bjoern Petri <[email protected]> Authored: Tue Aug 25 23:05:28 2015 +0200 Committer: Bjoern Petri <[email protected]> Committed: Tue Aug 25 23:05:28 2015 +0200 ---------------------------------------------------------------------- .../private/src/endpoint_discovery_poller.c | 72 +++++++++++--------- .../discovery_etcd/private/src/etcd_watcher.c | 9 ++- .../discovery_shm/private/src/shm_watcher.c | 2 +- 3 files changed, 46 insertions(+), 37 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/celix/blob/1010f712/remote_services/discovery/private/src/endpoint_discovery_poller.c ---------------------------------------------------------------------- diff --git a/remote_services/discovery/private/src/endpoint_discovery_poller.c b/remote_services/discovery/private/src/endpoint_discovery_poller.c index 569d7a9..ae269cf 100644 --- a/remote_services/discovery/private/src/endpoint_discovery_poller.c +++ b/remote_services/discovery/private/src/endpoint_discovery_poller.c @@ -172,60 +172,66 @@ celix_status_t endpointDiscoveryPoller_getDiscoveryEndpoints(endpoint_discovery_ * Adds a new endpoint URL to the list of polled endpoints. */ celix_status_t endpointDiscoveryPoller_addDiscoveryEndpoint(endpoint_discovery_poller_pt poller, char *url) { - celix_status_t status; + celix_status_t status; - status = celixThreadMutex_lock(&(poller)->pollerLock); - if (status != CELIX_SUCCESS) { - return CELIX_BUNDLE_EXCEPTION; - } + status = celixThreadMutex_lock(&(poller)->pollerLock); + if (status != CELIX_SUCCESS) { + return CELIX_BUNDLE_EXCEPTION; + } // Avoid memory leaks when adding an already existing URL... array_list_pt endpoints = hashMap_get(poller->entries, url); - if (endpoints == NULL) { + if (endpoints == NULL) { status = arrayList_createWithEquals(endpointDiscoveryPoller_endpointDescriptionEquals, &endpoints); if (status == CELIX_SUCCESS) { - hashMap_put(poller->entries, url, endpoints); + logHelper_log(*poller->loghelper, OSGI_LOGSERVICE_DEBUG, "ENDPOINT_POLLER: add new discovery endpoint with url %s", url); + hashMap_put(poller->entries, strdup(url), endpoints); } - } + } status = celixThreadMutex_unlock(&poller->pollerLock); - return status; + return status; } /** * Removes an endpoint URL from the list of polled endpoints. */ celix_status_t endpointDiscoveryPoller_removeDiscoveryEndpoint(endpoint_discovery_poller_pt poller, char *url) { - celix_status_t status; + celix_status_t status = CELIX_SUCCESS; - status = celixThreadMutex_lock(&poller->pollerLock); - if (status != CELIX_SUCCESS) { - return CELIX_BUNDLE_EXCEPTION; - } + if (celixThreadMutex_lock(&poller->pollerLock) != CELIX_SUCCESS) { + status = CELIX_BUNDLE_EXCEPTION; + } else { + hash_map_entry_pt entry = hashMap_getEntry(poller->entries, url); - hash_map_entry_pt entry = hashMap_getEntry(poller->entries, url); - char* origKey = hashMapEntry_getKey(entry); + if (entry == NULL) { + logHelper_log(*poller->loghelper, OSGI_LOGSERVICE_DEBUG, "ENDPOINT_POLLER: There was no entry found belonging to url %s - maybe already removed?", url); + } else { + char* origKey = hashMapEntry_getKey(entry); - array_list_pt entries = hashMap_remove(poller->entries, url); + logHelper_log(*poller->loghelper, OSGI_LOGSERVICE_DEBUG, "ENDPOINT_POLLER: remove discovery endpoint with url %s", url); - for (unsigned int i = arrayList_size(entries); i > 0 ; i--) { - endpoint_description_pt endpoint = arrayList_get(entries, i-1); - discovery_removeDiscoveredEndpoint(poller->discovery, endpoint); - arrayList_remove(entries, i-1); - endpointDescription_destroy(endpoint); - } + array_list_pt entries = hashMap_remove(poller->entries, url); - if (entries != NULL) { - arrayList_destroy(entries); - } + for (unsigned int i = arrayList_size(entries); i > 0; i--) { + endpoint_description_pt endpoint = arrayList_get(entries, i - 1); + discovery_removeDiscoveredEndpoint(poller->discovery, endpoint); + arrayList_remove(entries, i - 1); + endpointDescription_destroy(endpoint); + } + if (entries != NULL) { + arrayList_destroy(entries); + } - free(origKey); - status = celixThreadMutex_unlock(&poller->pollerLock); + free(origKey); + } + status = celixThreadMutex_unlock(&poller->pollerLock); + } - return status; + return status; } static void *endpointDiscoveryPoller_poll(void *data) { @@ -243,6 +249,7 @@ static void *endpointDiscoveryPoller_poll(void *data) { } hash_map_iterator_pt iterator = hashMapIterator_create(poller->entries); + while (hashMapIterator_hasNext(iterator)) { hash_map_entry_pt entry = hashMapIterator_nextEntry(iterator); @@ -251,9 +258,9 @@ static void *endpointDiscoveryPoller_poll(void *data) { array_list_pt updatedEndpoints = NULL; // create an arraylist with a custom equality test to ensure we can find endpoints properly... - status = arrayList_createWithEquals(endpointDiscoveryPoller_endpointDescriptionEquals, &updatedEndpoints); - + arrayList_createWithEquals(endpointDiscoveryPoller_endpointDescriptionEquals, &updatedEndpoints); status = endpointDiscoveryPoller_getEndpoints(poller, url, &updatedEndpoints); + if (status != CELIX_SUCCESS) { status = celixThreadMutex_unlock(&poller->pollerLock); continue; @@ -262,6 +269,7 @@ static void *endpointDiscoveryPoller_poll(void *data) { if (updatedEndpoints) { for (unsigned int i = arrayList_size(currentEndpoints); i > 0 ; i--) { endpoint_description_pt endpoint = arrayList_get(currentEndpoints, i-1); + if (!arrayList_contains(updatedEndpoints, endpoint)) { status = discovery_removeDiscoveredEndpoint(poller->discovery, endpoint); arrayList_remove(currentEndpoints, i-1); @@ -339,6 +347,8 @@ static celix_status_t endpointDiscoveryPoller_getEndpoints(endpoint_discovery_po curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, endpointDiscoveryPoller_writeMemory); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk); + curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 5L); + curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L); res = curl_easy_perform(curl); curl_easy_cleanup(curl); } http://git-wip-us.apache.org/repos/asf/celix/blob/1010f712/remote_services/discovery_etcd/private/src/etcd_watcher.c ---------------------------------------------------------------------- diff --git a/remote_services/discovery_etcd/private/src/etcd_watcher.c b/remote_services/discovery_etcd/private/src/etcd_watcher.c index 89be84e..c54fcdc 100644 --- a/remote_services/discovery_etcd/private/src/etcd_watcher.c +++ b/remote_services/discovery_etcd/private/src/etcd_watcher.c @@ -131,7 +131,7 @@ static celix_status_t etcdWatcher_addAlreadyExistingWatchpoints(discovery_pt dis 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, strdup(&value[0])); + endpointDiscoveryPoller_addDiscoveryEndpoint(discovery->poller, &value[0]); if (modIndex > *highestModified) { *highestModified = modIndex; @@ -206,23 +206,22 @@ static celix_status_t etcdWatcher_addOwnFramework(etcd_watcher_pt watcher) static celix_status_t etcdWatcher_addEntry(etcd_watcher_pt watcher, char* key, char* value) { - celix_status_t status = CELIX_BUNDLE_EXCEPTION; + celix_status_t status = CELIX_BUNDLE_EXCEPTION; endpoint_discovery_poller_pt poller = watcher->discovery->poller; if (!hashMap_containsKey(watcher->entries, key)) { status = endpointDiscoveryPoller_addDiscoveryEndpoint(poller, value); if (status == CELIX_SUCCESS) { - hashMap_put(watcher->entries, key, value); + hashMap_put(watcher->entries, strdup(key), strdup(value)); } } return status; } - static celix_status_t etcdWatcher_removeEntry(etcd_watcher_pt watcher, char* key, char* value) { - celix_status_t status = CELIX_BUNDLE_EXCEPTION; + celix_status_t status = CELIX_BUNDLE_EXCEPTION; endpoint_discovery_poller_pt poller = watcher->discovery->poller; if (hashMap_containsKey(watcher->entries, key)) { http://git-wip-us.apache.org/repos/asf/celix/blob/1010f712/remote_services/discovery_shm/private/src/shm_watcher.c ---------------------------------------------------------------------- diff --git a/remote_services/discovery_shm/private/src/shm_watcher.c b/remote_services/discovery_shm/private/src/shm_watcher.c index 212af55..2152345 100644 --- a/remote_services/discovery_shm/private/src/shm_watcher.c +++ b/remote_services/discovery_shm/private/src/shm_watcher.c @@ -111,7 +111,7 @@ static celix_status_t shmWatcher_syncEndpoints(shm_watcher_pt watcher) { } if (elementFound == false) { - endpointDiscoveryPoller_addDiscoveryEndpoint(watcher->poller, strdup(url)); + endpointDiscoveryPoller_addDiscoveryEndpoint(watcher->poller, url); } } }
