void test_arrayList_remove(void) { char * entry = "entry"; char * entry2 = "entry2"; char * entry3 = "entry3"; char * get; char * removed; arrayList_clear(list); arrayList_add(list, entry); arrayList_add(list, entry2); get = arrayList_get(list, 1); CU_ASSERT_EQUAL(entry2, get); // Remove first entry removed = arrayList_remove(list, 0); CU_ASSERT_EQUAL(entry, removed); // Check the new first element get = arrayList_get(list, 0); CU_ASSERT_EQUAL(entry2, get); // Add a new element arrayList_add(list, entry3); get = arrayList_get(list, 1); CU_ASSERT_EQUAL(entry3, get); }
void filter_destroy(filter_pt filter) { if (filter != NULL) { if(filter->value!=NULL){ if (filter->operand == SUBSTRING) { int size = arrayList_size(filter->value); for (; size > 0; --size) { char* operand = (char*) arrayList_remove(filter->value, 0); free(operand); } arrayList_destroy(filter->value); filter->value = NULL; } else if ( (filter->operand == OR) || (filter->operand == AND) ) { int size = arrayList_size(filter->value); unsigned int i = 0; for (i = 0; i < size; i++) { filter_pt f = arrayList_get(filter->value, i); filter_destroy(f); } arrayList_destroy(filter->value); filter->value = NULL; } else if (filter->operand == NOT) { filter_destroy(filter->value); filter->value = NULL; } else { free(filter->value); filter->value = NULL; } } free(filter->attribute); filter->attribute = NULL; free(filter); filter = NULL; } }
celix_status_t etcdWriter_deletePublisherEndpoint(etcd_writer_pt writer, pubsub_endpoint_pt pubEP) { celix_status_t status = CELIX_SUCCESS; char *key = NULL; const char *rootPath = etcdWriter_getRootPath(writer->pubsub_discovery->context); asprintf(&key, "%s/%s/%s/%s/%ld", rootPath, pubEP->scope, pubEP->topic, pubEP->frameworkUUID, pubEP->serviceID); celixThreadMutex_lock(&writer->localPubsLock); for (unsigned int i = 0; i < arrayList_size(writer->localPubs); i++) { pubsub_endpoint_pt ep = arrayList_get(writer->localPubs, i); if (pubsubEndpoint_equals(ep, pubEP)) { arrayList_remove(writer->localPubs, i); pubsubEndpoint_destroy(ep); break; } } celixThreadMutex_unlock(&writer->localPubsLock); if (etcd_del(key)) { printf("Failed to remove key %s from ETCD\n",key); status = CELIX_ILLEGAL_ARGUMENT; } FREE_MEM(key); 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_SUCCESS; if (celixThreadMutex_lock(&poller->pollerLock) != CELIX_SUCCESS) { status = CELIX_BUNDLE_EXCEPTION; } else { hash_map_entry_pt entry = hashMap_getEntry(poller->entries, url); 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); logHelper_log(*poller->loghelper, OSGI_LOGSERVICE_DEBUG, "ENDPOINT_POLLER: remove discovery endpoint with url %s", url); array_list_pt entries = hashMap_remove(poller->entries, 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); } if (entries != NULL) { arrayList_destroy(entries); } free(origKey); } status = celixThreadMutex_unlock(&poller->pollerLock); } return status; }
int sampleQueue_takeAll(sample_queue_type *sampleQueue, uint32_t min, uint32_t max, struct sample_sequence samples) { celix_status_t status = CELIX_ILLEGAL_STATE; struct timespec ts; unsigned int i = 0; int rc = 0; clock_gettime(CLOCK_REALTIME, &ts); ts.tv_sec += WAIT_TIME_SECONDS; pthread_mutex_lock(&sampleQueue->lock); /* block, till sufficient elements available */ while (sampleQueue->queue!=NULL && arrayList_size(sampleQueue->queue) < min && rc != ETIMEDOUT) { rc = pthread_cond_timedwait(&sampleQueue->listEmpty, &sampleQueue->lock, &ts); } if (rc == 0 && sampleQueue->queue!=NULL) { status = CELIX_SUCCESS; for (i = 0; i < max && arrayList_size(sampleQueue->queue) > 0; i++) { struct sample *tmpSample = arrayList_remove(sampleQueue->queue, 0); memcpy(&samples.buf[i], tmpSample, sizeof(struct sample)); free(tmpSample); } } //*samplesSize = i; sampleQueue->takeCnt += i; sampleQueue->currentQueueSize -= i; pthread_mutex_unlock(&sampleQueue->lock); return (int) status; }
int sampleQueue_take(sample_queue_type *sampleQueue, struct sample **out) { celix_status_t status = CELIX_ILLEGAL_STATE; struct timespec ts; int rc = 0; clock_gettime(CLOCK_REALTIME, &ts); ts.tv_sec += WAIT_TIME_SECONDS; pthread_mutex_lock(&sampleQueue->lock); while (sampleQueue->queue!=NULL && arrayList_size(sampleQueue->queue) == 0 && rc != ETIMEDOUT) { rc = pthread_cond_timedwait(&sampleQueue->listEmpty, &sampleQueue->lock, &ts); } if (rc == 0 && sampleQueue->queue!=NULL && arrayList_size(sampleQueue->queue)>0) { struct sample *sample = arrayList_remove(sampleQueue->queue, 0); sampleQueue->takeCnt++; sampleQueue->currentQueueSize -= 1; *out = sample; status = CELIX_SUCCESS; } else if (arrayList_size(sampleQueue->queue) == 0) { *out = NULL; status = CELIX_SUCCESS; } pthread_mutex_unlock(&sampleQueue->lock); return (int) status; }
celix_status_t pubsub_discovery_removeNode(pubsub_discovery_pt pubsub_discovery, pubsub_endpoint_pt pubEP) { celix_status_t status = CELIX_SUCCESS; pubsub_endpoint_pt p = NULL; bool found = false; celixThreadMutex_lock(&pubsub_discovery->discoveredPubsMutex); char *pubs_key = createScopeTopicKey(pubEP->scope, pubEP->topic); array_list_pt pubEP_list = (array_list_pt) hashMap_get(pubsub_discovery->discoveredPubs, pubs_key); free(pubs_key); if (pubEP_list == NULL) { printf("PSD: Cannot find any registered publisher for topic %s. Something is not consistent.\n", pubEP->topic); status = CELIX_ILLEGAL_STATE; } else { int i; for (i = 0; !found && i < arrayList_size(pubEP_list); i++) { p = arrayList_get(pubEP_list, i); found = pubsubEndpoint_equals(pubEP, p); if (found) { arrayList_remove(pubEP_list, i); pubsubEndpoint_destroy(p); } } } celixThreadMutex_unlock(&pubsub_discovery->discoveredPubsMutex); if (found) { status = pubsub_discovery_informPublishersListeners(pubsub_discovery, pubEP, false); } pubsubEndpoint_destroy(pubEP); return status; }
static void disconnectPendingPublishers(topic_subscription_pt sub) { celixThreadMutex_lock(&sub->pendingDisconnections_lock); while(!arrayList_isEmpty(sub->pendingDisconnections)) { char * pubEP = arrayList_remove(sub->pendingDisconnections, 0); pubsub_topicSubscriptionDisconnectPublisher(sub, pubEP); free(pubEP); } celixThreadMutex_unlock(&sub->pendingDisconnections_lock); }
celix_status_t pubsub_discovery_stop(pubsub_discovery_pt ps_discovery) { celix_status_t status = CELIX_SUCCESS; const char* fwUUID = NULL; bundleContext_getProperty(ps_discovery->context, OSGI_FRAMEWORK_FRAMEWORK_UUID, &fwUUID); if (fwUUID == NULL) { printf("PSD: Cannot retrieve fwUUID.\n"); return CELIX_INVALID_BUNDLE_CONTEXT; } celixThreadMutex_lock(&ps_discovery->watchersMutex); hash_map_iterator_pt iter = hashMapIterator_create(ps_discovery->watchers); while (hashMapIterator_hasNext(iter)) { struct watcher_info * wi = hashMapIterator_nextValue(iter); etcdWatcher_stop(wi->watcher); } hashMapIterator_destroy(iter); celixThreadMutex_lock(&ps_discovery->discoveredPubsMutex); /* Unexport all publishers for the local framework, and also delete from ETCD publisher belonging to the local framework */ iter = hashMapIterator_create(ps_discovery->discoveredPubs); while (hashMapIterator_hasNext(iter)) { array_list_pt pubEP_list = (array_list_pt) hashMapIterator_nextValue(iter); int i; for (i = 0; i < arrayList_size(pubEP_list); i++) { pubsub_endpoint_pt pubEP = (pubsub_endpoint_pt) arrayList_get(pubEP_list, i); if (strcmp(pubEP->frameworkUUID, fwUUID) == 0) { etcdWriter_deletePublisherEndpoint(ps_discovery->writer, pubEP); } else { pubsub_discovery_informPublishersListeners(ps_discovery, pubEP, false); arrayList_remove(pubEP_list, i); pubsubEndpoint_destroy(pubEP); i--; } } } hashMapIterator_destroy(iter); celixThreadMutex_unlock(&ps_discovery->discoveredPubsMutex); etcdWriter_destroy(ps_discovery->writer); iter = hashMapIterator_create(ps_discovery->watchers); while (hashMapIterator_hasNext(iter)) { struct watcher_info * wi = hashMapIterator_nextValue(iter); etcdWatcher_destroy(wi->watcher); } hashMapIterator_destroy(iter); hashMap_destroy(ps_discovery->watchers, true, true); celixThreadMutex_unlock(&ps_discovery->watchersMutex); return status; }
celix_status_t endpointDiscoveryPoller_poll(endpoint_discovery_poller_pt poller, char *url, array_list_pt currentEndpoints) { celix_status_t status; array_list_pt updatedEndpoints = NULL; // create an arraylist with a custom equality test to ensure we can find endpoints properly... arrayList_createWithEquals(endpointDiscoveryPoller_endpointDescriptionEquals, &updatedEndpoints); status = endpointDiscoveryPoller_getEndpoints(poller, url, &updatedEndpoints); if (status != CELIX_SUCCESS) { status = celixThreadMutex_unlock(&poller->pollerLock); } else { 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); endpointDescription_destroy(endpoint); } } for (int i = arrayList_size(updatedEndpoints); i > 0; i--) { endpoint_description_pt endpoint = arrayList_remove(updatedEndpoints, 0); if (!arrayList_contains(currentEndpoints, endpoint)) { arrayList_add(currentEndpoints, endpoint); status = discovery_addDiscoveredEndpoint(poller->discovery, endpoint); } else { endpointDescription_destroy(endpoint); } } } if (updatedEndpoints) { arrayList_destroy(updatedEndpoints); } } return status; }
celix_status_t readerService_getFirstData(database_handler_pt handler, data_pt firstData) { celix_status_t status = CELIX_BUNDLE_EXCEPTION; celixThreadMutex_lock(&handler->lock); if ((firstData = (data_pt) arrayList_remove(handler->data, 0)) != NULL) { handler->dataIndex--; status = CELIX_SUCCESS; } celixThreadMutex_unlock(&handler->lock); return status; }
celix_status_t dependencyManager_removeAllComponents(dm_dependency_manager_pt manager) { celix_status_t status = CELIX_SUCCESS; unsigned int i=0; unsigned int size = arrayList_size(manager->components); for(;i<size;i++){ dm_component_pt cmp = arrayList_get(manager->components, i); printf("Stopping comp %s\n", component_getName(cmp)); component_stop(cmp); } while (!arrayList_isEmpty(manager->components)) { dm_component_pt cmp = arrayList_remove(manager->components, 0); printf("Removing comp %s\n", component_getName(cmp)); component_destroy(cmp); } return status; }
static celix_status_t serviceTracker_untrack(service_tracker_pt tracker, service_reference_pt reference, service_event_pt event) { celix_status_t status = CELIX_SUCCESS; tracked_pt tracked = NULL; unsigned int i; bool result = false; for (i = 0; i < arrayList_size(tracker->tracked); i++) { bool equals; tracked = (tracked_pt) arrayList_get(tracker->tracked, i); serviceReference_equals(reference, tracked->reference, &equals); if (equals) { arrayList_remove(tracker->tracked, i); if (status == CELIX_SUCCESS) { if (tracker->customizer != NULL) { void *handle = NULL; removed_callback_pt function = NULL; serviceTrackerCustomizer_getHandle(tracker->customizer, &handle); serviceTrackerCustomizer_getRemovedFunction(tracker->customizer, &function); if (function != NULL) { status = function(handle, reference, tracked->service); } else { status = bundleContext_ungetService(tracker->context, reference, &result); } } else { status = bundleContext_ungetService(tracker->context, reference, &result); } // ungetServiceReference bundleContext_ungetServiceReference(tracker->context, reference); //break; } free(tracked); } } framework_logIfError(logger, status, NULL, "Cannot untrack reference"); return status; }
celix_status_t pubsubAdmin_removePublication(pubsub_admin_pt admin,pubsub_endpoint_pt pubEP){ celix_status_t status = CELIX_SUCCESS; int count = 0; printf("PSA_ZMQ: Removing publication [FWUUID=%s bundleID=%ld topic=%s]\n",pubEP->frameworkUUID,pubEP->serviceID,pubEP->topic); const char* fwUUID = NULL; bundleContext_getProperty(admin->bundle_context,OSGI_FRAMEWORK_FRAMEWORK_UUID,&fwUUID); if(fwUUID==NULL){ printf("PSA_ZMQ: Cannot retrieve fwUUID.\n"); return CELIX_INVALID_BUNDLE_CONTEXT; } char *scope_topic = createScopeTopicKey(pubEP->scope, pubEP->topic); if(strcmp(pubEP->frameworkUUID,fwUUID)==0){ celixThreadMutex_lock(&admin->localPublicationsLock); service_factory_pt factory = (service_factory_pt)hashMap_get(admin->localPublications,scope_topic); if(factory!=NULL){ topic_publication_pt pub = (topic_publication_pt)factory->handle; pubsub_topicPublicationRemovePublisherEP(pub,pubEP); } celixThreadMutex_unlock(&admin->localPublicationsLock); if(factory==NULL){ /* Maybe the endpoint was pending */ celixThreadMutex_lock(&admin->noSerializerPendingsLock); if(!arrayList_removeElement(admin->noSerializerPublications, pubEP)){ status = CELIX_ILLEGAL_STATE; } celixThreadMutex_unlock(&admin->noSerializerPendingsLock); } } else{ celixThreadMutex_lock(&admin->externalPublicationsLock); array_list_pt ext_pub_list = (array_list_pt)hashMap_get(admin->externalPublications,scope_topic); if(ext_pub_list!=NULL){ int i; bool found = false; for(i=0;!found && i<arrayList_size(ext_pub_list);i++){ pubsub_endpoint_pt p = (pubsub_endpoint_pt)arrayList_get(ext_pub_list,i); found = pubsubEndpoint_equals(pubEP,p); if (found){ arrayList_remove(ext_pub_list,i); } } // Check if there are more publishers on the same endpoint (happens when 1 celix-instance with multiple bundles publish in same topic) for(i=0; i<arrayList_size(ext_pub_list);i++) { pubsub_endpoint_pt p = (pubsub_endpoint_pt)arrayList_get(ext_pub_list,i); if (strcmp(pubEP->endpoint,p->endpoint) == 0) { count++; } } if(arrayList_size(ext_pub_list)==0){ hash_map_entry_pt entry = hashMap_getEntry(admin->externalPublications,scope_topic); char* topic = (char*)hashMapEntry_getKey(entry); array_list_pt list = (array_list_pt)hashMapEntry_getValue(entry); hashMap_remove(admin->externalPublications,topic); arrayList_destroy(list); free(topic); } } celixThreadMutex_unlock(&admin->externalPublicationsLock); } /* Check if this publisher was connected to one of our subscribers*/ celixThreadMutex_lock(&admin->subscriptionsLock); topic_subscription_pt sub = (topic_subscription_pt)hashMap_get(admin->subscriptions,scope_topic); if(sub!=NULL && pubEP->endpoint!=NULL && count == 0){ pubsub_topicSubscriptionAddDisconnectPublisherToPendingList(sub,pubEP->endpoint); } /* And check also for ANY subscription */ topic_subscription_pt any_sub = (topic_subscription_pt)hashMap_get(admin->subscriptions,PUBSUB_ANY_SUB_TOPIC); if(any_sub!=NULL && pubEP->endpoint!=NULL && count == 0){ pubsub_topicSubscriptionAddDisconnectPublisherToPendingList(any_sub,pubEP->endpoint); } free(scope_topic); celixThreadMutex_unlock(&admin->subscriptionsLock); return status; }