Ejemplo n.º 1
0
celix_status_t wiringTopologyManager_waRemoved(void * handle, service_reference_pt reference, void * service) {
    celix_status_t status = CELIX_SUCCESS;
    wiring_topology_manager_pt manager = handle;
    wiring_admin_service_pt wiringAdminService = (wiring_admin_service_pt) service;

    /* check whether one of the exported Wires can be exported here via the newly available wiringAdmin*/
    celixThreadMutex_lock(&manager->exportedWiringEndpointsLock);
    hash_map_iterator_pt iter = hashMapIterator_create(manager->exportedWiringEndpoints);
    while (hashMapIterator_hasNext(iter)) {
        hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);
        hash_map_pt wiringAdminMap = hashMapEntry_getValue(entry);

        if (hashMap_containsKey(wiringAdminMap, wiringAdminService)) {
            wiring_endpoint_description_pt wEndpoint = (wiring_endpoint_description_pt) hashMap_remove(wiringAdminMap, wiringAdminService);

            status = wiringTopologyManager_notifyListenersWiringEndpointRemoved(manager, wEndpoint);

            if (status == CELIX_SUCCESS) {
                status = wiringAdminService->removeExportedWiringEndpoint(wiringAdminService->admin, wEndpoint);
            } else {
                printf("WTM: failed while removing WiringAdmin.\n");
            }
        }
    }

    hashMapIterator_destroy(iter);

    celixThreadMutex_unlock(&manager->exportedWiringEndpointsLock);

    /* Check if the added WA can match one of the imported WiringEndpoints */
    celixThreadMutex_lock(&manager->importedWiringEndpointsLock);
    iter = hashMapIterator_create(manager->importedWiringEndpoints);
    while (hashMapIterator_hasNext(iter)) {
        hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);
        wiring_endpoint_description_pt importedWiringEndpointDesc = hashMapEntry_getKey(entry);
        array_list_pt wiringAdminList = hashMapEntry_getValue(entry);

        if (arrayList_contains(wiringAdminList, wiringAdminService)) {
            status = wiringAdminService->removeImportedWiringEndpoint(wiringAdminService->admin, importedWiringEndpointDesc);
            arrayList_removeElement(wiringAdminList, wiringAdminService);
        }

        if (status == CELIX_SUCCESS) {
            arrayList_add(wiringAdminList, wiringAdminService);
        }

    }
    hashMapIterator_destroy(iter);
    celixThreadMutex_unlock(&manager->importedWiringEndpointsLock);

    celixThreadMutex_lock(&manager->waListLock);
    arrayList_removeElement(manager->waList, wiringAdminService);
    celixThreadMutex_unlock(&manager->waListLock);

    printf("WTM: Removed WA\n");

    return status;
}
Ejemplo n.º 2
0
int producer_getUtilizationStatsValue(producer_pt producer, double* statVal) {

    double total_average = 0;

    pthread_rwlock_rdlock(&producer->queueLock);

    hash_map_iterator_pt iter = hashMapIterator_create(producer->queueServices);

    while (hashMapIterator_hasNext(iter)) {

        hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);

        if (entry != NULL) {
            producer_thread_data_pt value = (producer_thread_data_pt) hashMapEntry_getValue(entry);
            if (value != NULL) {
                total_average += (double) (((double) (value->single_throughput + value->burst_throughput)) / ((double) 2.0f));
            }
        }
    }

    hashMapIterator_destroy(iter);

    if (hashMap_size(producer->queueServices) > 0) {
        total_average /= hashMap_size(producer->queueServices);
    }

    pthread_rwlock_unlock(&producer->queueLock);

    (*statVal) = total_average;
    return (int) CELIX_SUCCESS;
}
Ejemplo n.º 3
0
array_list_pt serviceRegistry_getUsingBundles(service_registry_pt registry, service_reference_pt reference) {
	array_list_pt bundles = NULL;
	hash_map_iterator_pt iter;
	arrayList_create(&bundles);

	celixThreadMutex_lock(&registry->mutex);
	iter = hashMapIterator_create(registry->inUseMap);
	while (hashMapIterator_hasNext(iter)) {
		hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);
		bundle_pt bundle = hashMapEntry_getKey(entry);
		array_list_pt usages = hashMapEntry_getValue(entry);
		unsigned int i;
		for (i = 0; i < arrayList_size(usages); i++) {
			usage_count_pt usage = arrayList_get(usages, i);
			bool equals = false;
			serviceReference_equals(usage->reference, reference, &equals);
			if (equals) {
				arrayList_add(bundles, bundle);
			}
		}
	}
	hashMapIterator_destroy(iter);
	celixThreadMutex_unlock(&registry->mutex);
	return bundles;
}
Ejemplo n.º 4
0
static void *endpointDiscoveryPoller_performPeriodicPoll(void *data) {
	endpoint_discovery_poller_pt poller = (endpoint_discovery_poller_pt) data;

	useconds_t interval = (useconds_t) (poller->poll_interval * 1000000L);

	while (poller->running) {
		usleep(interval);
		celix_status_t status = celixThreadMutex_lock(&poller->pollerLock);

		if (status != CELIX_SUCCESS) {
			logHelper_log(*poller->loghelper, OSGI_LOGSERVICE_WARNING, "ENDPOINT_POLLER: failed to obtain lock; retrying...");
		} else {
			hash_map_iterator_pt iterator = hashMapIterator_create(poller->entries);

			while (hashMapIterator_hasNext(iterator)) {
				hash_map_entry_pt entry = hashMapIterator_nextEntry(iterator);

				char *url = hashMapEntry_getKey(entry);
				array_list_pt currentEndpoints = hashMapEntry_getValue(entry);

				endpointDiscoveryPoller_poll(poller, url, currentEndpoints);
			}

			hashMapIterator_destroy(iterator);
		}

		status = celixThreadMutex_unlock(&poller->pollerLock);
		if (status != CELIX_SUCCESS) {
			logHelper_log(*poller->loghelper, OSGI_LOGSERVICE_WARNING, "ENDPOINT_POLLER: failed to release lock; retrying...");
		}
	}

	return NULL;
}
Ejemplo n.º 5
0
    /// \TEST_CASE_ID{2}
    /// \TEST_CASE_TITLE{Test scope initialisation}
    /// \TEST_CASE_REQ{REQ-2}
    /// \TEST_CASE_DESC Checks if scopes can be added, but not twice
    static void testScope(void) {
    	int nr_exported;
    	int nr_imported;
    	array_list_pt epList;

    	printf("\nBegin: %s\n", __func__);
        scopeInit("scope.json", &nr_exported, &nr_imported);
        CHECK_EQUAL(1, nr_exported);
        CHECK_EQUAL(0, nr_imported);

        discMock->getEPDescriptors(discMock->handle, &epList);
        // We export two services: Calculator and Calculator2, but only 1 has DFI bundle info
        CHECK_EQUAL(1, arrayList_size(epList));
        for (unsigned int i = 0; i < arrayList_size(epList); i++) {
        	endpoint_description_pt ep = (endpoint_description_pt) arrayList_get(epList, i);
        	properties_pt props = ep->properties;
        	hash_map_entry_pt entry = hashMap_getEntry(props, (void*)"key2");
        	char* value = (char*) hashMapEntry_getValue(entry);
        	STRCMP_EQUAL("inaetics", value);
        	/*
        	printf("Service: %s ", ep->service);
        	hash_map_iterator_pt iter = hashMapIterator_create(props);
        	while (hashMapIterator_hasNext(iter)) {
        		hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);
        		printf("%s - %s\n", (char*)hashMapEntry_getKey(entry),
        						   (char*)hashMapEntry_getValue(entry));
        	}
        	printf("\n");
        	hashMapIterator_destroy(iter);
        	*/
        }
        printf("End: %s\n", __func__);
    }
Ejemplo n.º 6
0
celix_status_t pubsubAdmin_closeAllSubscriptions(pubsub_admin_pt admin,char* scope,char* topic){
	celix_status_t status = CELIX_SUCCESS;

	printf("PSA_ZMQ: Closing all subscriptions\n");

	celixThreadMutex_lock(&admin->subscriptionsLock);
	char *scope_topic = createScopeTopicKey(scope, topic);
	hash_map_entry_pt sub_entry = (hash_map_entry_pt)hashMap_getEntry(admin->subscriptions,scope_topic);
	if(sub_entry!=NULL){
		char* topic = (char*)hashMapEntry_getKey(sub_entry);

		topic_subscription_pt ts = (topic_subscription_pt)hashMapEntry_getValue(sub_entry);
		status += pubsub_topicSubscriptionStop(ts);
		disconnectTopicPubSubFromSerializer(admin, ts, false);
		status += pubsub_topicSubscriptionDestroy(ts);
		hashMap_remove(admin->subscriptions,scope_topic);
		free(topic);

	}
	free(scope_topic);
	celixThreadMutex_unlock(&admin->subscriptionsLock);

	return status;

}
Ejemplo n.º 7
0
celix_status_t pubsubAdmin_closeAllPublications(pubsub_admin_pt admin, char *scope, char* topic){
	celix_status_t status = CELIX_SUCCESS;

	printf("PSA_ZMQ: Closing all publications\n");

	celixThreadMutex_lock(&admin->localPublicationsLock);
	char *scope_topic = createScopeTopicKey(scope, topic);
	hash_map_entry_pt pubsvc_entry = (hash_map_entry_pt)hashMap_getEntry(admin->localPublications,scope_topic);
	if(pubsvc_entry!=NULL){
		char* key = (char*)hashMapEntry_getKey(pubsvc_entry);
		service_factory_pt factory= (service_factory_pt)hashMapEntry_getValue(pubsvc_entry);
		topic_publication_pt pub = (topic_publication_pt)factory->handle;
		status += pubsub_topicPublicationStop(pub);
		disconnectTopicPubSubFromSerializer(admin, pub, true);
		status += pubsub_topicPublicationDestroy(pub);
		hashMap_remove(admin->localPublications,scope_topic);
		free(key);
		free(factory);
	}
	free(scope_topic);
	celixThreadMutex_unlock(&admin->localPublicationsLock);

	return status;

}
Ejemplo n.º 8
0
celix_status_t pubsub_discovery_uninterestedInTopic(void *handle, const char* scope, const char* topic) {
    pubsub_discovery_pt pubsub_discovery = (pubsub_discovery_pt) handle;

    char *scope_topic_key = createScopeTopicKey(scope, topic);
    celixThreadMutex_lock(&pubsub_discovery->watchersMutex);

    hash_map_entry_pt entry =  hashMap_getEntry(pubsub_discovery->watchers, scope_topic_key);
    if(entry) {
        struct watcher_info * wi = hashMapEntry_getValue(entry);
        wi->nr_references--;
        if(wi->nr_references == 0) {
            char *key = hashMapEntry_getKey(entry);
            hashMap_remove(pubsub_discovery->watchers, scope_topic_key);
            free(key);
            free(scope_topic_key);
            etcdWatcher_stop(wi->watcher);
            etcdWatcher_destroy(wi->watcher);
            free(wi);
        }
    } else {
        fprintf(stderr, "[DISC] Inconsistency error: Removing unknown topic %s\n", topic);
    }
    celixThreadMutex_unlock(&pubsub_discovery->watchersMutex);
    return CELIX_SUCCESS;
}
Ejemplo n.º 9
0
void properties_destroy(properties_pt properties) {
	hash_map_iterator_pt iter = hashMapIterator_create(properties);
	while (hashMapIterator_hasNext(iter)) {
		hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);
		free(hashMapEntry_getKey(entry));
		free(hashMapEntry_getValue(entry));
	}
	hashMapIterator_destroy(iter);
	hashMap_destroy(properties, false, false);
}
Ejemplo n.º 10
0
static void process_msg(topic_subscription_pt sub,pubsub_udp_msg_t *msg){

	celixThreadMutex_lock(&sub->ts_lock);
	hash_map_iterator_pt iter = hashMapIterator_create(sub->servicesMap);
	while (hashMapIterator_hasNext(iter)) {
		hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);
		pubsub_subscriber_pt subsvc = hashMapEntry_getKey(entry);
		hash_map_pt msgTypes = hashMapEntry_getValue(entry);

		pubsub_msg_serializer_t *msgSer = hashMap_get(msgTypes,(void*)(uintptr_t )msg->header.type);
		if (msgSer == NULL) {
			printf("PSA_UDP_MC_TS: Serializer not available for message %d.\n",msg->header.type);
		}
		else{
			void *msgInst = NULL;
			bool validVersion = checkVersion(msgSer->msgVersion,&msg->header);

			if(validVersion){

				celix_status_t status = msgSer->deserialize(msgSer, (const void *) msg->payload, 0, &msgInst);

				if (status == CELIX_SUCCESS) {
					bool release = true;
					pubsub_multipart_callbacks_t mp_callbacks;
					mp_callbacks.handle = sub;
					mp_callbacks.localMsgTypeIdForMsgType = pubsub_localMsgTypeIdForMsgType;
					mp_callbacks.getMultipart = NULL;

					subsvc->receive(subsvc->handle, msgSer->msgName, msg->header.type, msgInst, &mp_callbacks, &release);

					if(release){
						msgSer->freeMsg(msgSer,msgInst);
					}
				}
				else{
					printf("PSA_UDP_MC_TS: Cannot deserialize msgType %s.\n",msgSer->msgName);
				}

			}
			else{
				int major=0,minor=0;
				version_getMajor(msgSer->msgVersion,&major);
				version_getMinor(msgSer->msgVersion,&minor);
				printf("PSA_UDP_MC_TS: Version mismatch for primary message '%s' (have %d.%d, received %u.%u). NOT sending any part of the whole message.\n",
						msgSer->msgName,major,minor,msg->header.major,msg->header.minor);
			}

		}
	}
	hashMapIterator_destroy(iter);
	celixThreadMutex_unlock(&sub->ts_lock);
}
Ejemplo n.º 11
0
static void importRegistration_clearProxies(import_registration_pt import) {
    if (import != NULL) {
        pthread_mutex_lock(&import->proxiesMutex);
        if (import->proxies != NULL) {
            hash_map_iterator_pt iter = hashMapIterator_create(import->proxies);
            while (hashMapIterator_hasNext(iter)) {
                hash_map_entry_pt  entry = hashMapIterator_nextEntry(iter);
                struct service_proxy *proxy = hashMapEntry_getValue(entry);
                importRegistration_destroyProxy(proxy);
            }
            hashMapIterator_destroy(iter);
        }
        pthread_mutex_unlock(&import->proxiesMutex);
    }
}
static celix_status_t remoteProxyFactory_registerProxyService(remote_proxy_factory_pt remote_proxy_factory_ptr, endpoint_description_pt endpointDescription, remote_service_admin_pt rsa, sendToHandle sendToCallback) {
	celix_status_t status = CELIX_SUCCESS;
	proxy_instance_pt proxy_instance_ptr = NULL;

	if (!remote_proxy_factory_ptr || !remote_proxy_factory_ptr->create_proxy_service_ptr) {
		status = CELIX_ILLEGAL_ARGUMENT;
	}

	if (status == CELIX_SUCCESS) {
		proxy_instance_ptr = calloc(1, sizeof(*proxy_instance_ptr));
		if (!proxy_instance_ptr) {
			status = CELIX_ENOMEM;
		}
	}

	if (status == CELIX_SUCCESS) {
		proxy_instance_ptr->properties = properties_create();
		if (!proxy_instance_ptr->properties) {
			status = CELIX_BUNDLE_EXCEPTION;
		}
	}

	if (status == CELIX_SUCCESS) {
		status = remote_proxy_factory_ptr->create_proxy_service_ptr(remote_proxy_factory_ptr->handle, endpointDescription, rsa, sendToCallback, proxy_instance_ptr->properties, &proxy_instance_ptr->service);
	}

	if (status == CELIX_SUCCESS) {
		properties_set(proxy_instance_ptr->properties, "proxy.interface", remote_proxy_factory_ptr->service);

		hash_map_iterator_pt iter = hashMapIterator_create(endpointDescription->properties);
		while (hashMapIterator_hasNext(iter)) {
			hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);
			char *key = hashMapEntry_getKey(entry);
			char *value = hashMapEntry_getValue(entry);
			properties_set(proxy_instance_ptr->properties, key, value);
		}
	}

	if (status == CELIX_SUCCESS) {
		status = bundleContext_registerService(remote_proxy_factory_ptr->context_ptr, remote_proxy_factory_ptr->service, proxy_instance_ptr->service, proxy_instance_ptr->properties, &proxy_instance_ptr->registration_ptr);
	}

	if (status == CELIX_SUCCESS) {
		hashMap_put(remote_proxy_factory_ptr->proxy_instances, endpointDescription, proxy_instance_ptr);
	}

	return status;
}
Ejemplo n.º 13
0
 /// \TEST_CASE_ID{4}
 /// \TEST_CASE_TITLE{Test scope initialisation}
 /// \TEST_CASE_REQ{REQ-4}
 /// \TEST_CASE_DESC Checks if scopes can be added, but not twice
 static void testScope3(void) {
 	int nr_exported;
 	int nr_imported;
 	array_list_pt epList;
 	printf("\nBegin: %s\n", __func__);
     scopeInit("scope3.json", &nr_exported, &nr_imported);
     CHECK_EQUAL(2, nr_exported);
     discMock->getEPDescriptors(discMock->handle, &epList);
     // We export two services: Calculator and Calculator2, but only 1 has DFI bundle info
     CHECK_EQUAL(1, arrayList_size(epList));
     for (unsigned int i = 0; i < arrayList_size(epList); i++) {
     	endpoint_description_pt ep = (endpoint_description_pt) arrayList_get(epList, i);
     	properties_pt props = ep->properties;
     	hash_map_entry_pt entry = hashMap_getEntry(props, (void *)"key2");
     	char* value = (char*) hashMapEntry_getValue(entry);
     	STRCMP_EQUAL("inaetics", value);
     }
     printf("End: %s\n", __func__);
 }
Ejemplo n.º 14
0
celix_status_t statistic_tracker_statServiceRemoved(void *handle, service_reference_pt reference, void *service) {

	celix_status_t status = CELIX_BUNDLE_EXCEPTION;
	statistic_tracker_pt statTracker = (statistic_tracker_pt) handle;

	pthread_t* thread_pt = NULL;

	pthread_rwlock_wrlock(&statTracker->statLock);

	hash_map_iterator_pt iter = hashMapIterator_create(statTracker->statServices);

	while (hashMapIterator_hasNext(iter) && thread_pt == NULL) {
		hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);

		if (service == hashMapEntry_getValue(entry)) {
			struct stats_provider_service* statService = (struct stats_provider_service*) service;
			char* name = NULL;

			statService->getName(statService->statsProvider, &name);

			thread_pt = hashMapEntry_getKey(entry);

			msg(1, "STAT_TRACKER: Service %s Removed. ", name);
			hashMap_remove(statTracker->statServices, thread_pt);
		}
	}

	hashMapIterator_destroy(iter);

	pthread_rwlock_unlock(&statTracker->statLock);

	if (thread_pt != NULL) {
		pthread_join(*thread_pt, NULL);
		msg(1, "STAT_TRACKER: Service Removed. Thread %lu stopped.", (unsigned long) *thread_pt);
		free(thread_pt);
		status = CELIX_SUCCESS;
	}

	return status;
}
Ejemplo n.º 15
0
celix_status_t deploymentPackage_processEntries(deployment_package_pt package) {
	celix_status_t status = CELIX_SUCCESS;

	hash_map_pt entries = NULL;
	manifest_getEntries(package->manifest, &entries);
	hash_map_iterator_pt iter = hashMapIterator_create(entries);
	while (hashMapIterator_hasNext(iter)) {
		hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);
		char *name = hashMapEntry_getKey(entry);
		properties_pt values = hashMapEntry_getValue(entry);

		bool isBundleResource;
		deploymentPackage_isBundleResource(values, &isBundleResource);
		if (isBundleResource) {
			bundle_info_pt info = apr_palloc(package->pool, sizeof(*info));
			info->path = name;
			info->attributes = values;
			info->symbolicName = properties_get(values, (char *) OSGI_FRAMEWORK_BUNDLE_SYMBOLICNAME);
			char *version = properties_get(values, (char *) OSGI_FRAMEWORK_BUNDLE_VERSION);
			info->version = NULL;
			status = version_createVersionFromString(version, &info->version);
			char *customizer = properties_get(values, (char *) DEPLOYMENTPACKAGE_CUSTOMIZER);
			deploymentPackage_parseBooleanHeader(customizer, &info->customizer);

			arrayList_add(package->bundleInfos, info);
		} else {
			resource_info_pt info = apr_palloc(package->pool, sizeof(*info));
			info->path = name;
			info->attributes = values;
			info->resourceProcessor = properties_get(values, (char *) RESOURCE_PROCESSOR);

			arrayList_add(package->resourceInfos, info);
		}
	}
	hashMapIterator_destroy(iter);

	return status;
}
Ejemplo n.º 16
0
/**
 * Header is ignored for now, cannot handle comments yet
 */
void properties_store(properties_pt properties, char * filename, char * header) {
	FILE *file = fopen ( filename, "w+" );
	int i;
	char *str;

	if (file != NULL) {
		if (hashMap_size(properties) > 0) {
			hash_map_iterator_pt iterator = hashMapIterator_create(properties);
			while (hashMapIterator_hasNext(iterator)) {
				hash_map_entry_pt entry = hashMapIterator_nextEntry(iterator);
				str = hashMapEntry_getKey(entry);
				for (int i = 0; i < strlen(str); i += 1) {
					if (str[i] == '#' || str[i] == '!' || str[i] == '=' || str[i] == ':') {
						fputc('\\', file);
					}
					fputc(str[i], file);
				}

				fputc('=', file);

				str = hashMapEntry_getValue(entry);
				for (int i = 0; i < strlen(str); i += 1) {
					if (str[i] == '#' || str[i] == '!' || str[i] == '=' || str[i] == ':') {
						fputc('\\', file);
					}
					fputc(str[i], file);
				}

				fputc('\n', file);

			}
			hashMapIterator_destroy(iterator);
		}
		fclose(file);
	} else {
		perror("File is null");
	}
}
Ejemplo n.º 17
0
static celix_status_t endpointDescriptorWriter_writeEndpoint(endpoint_descriptor_writer_pt writer, endpoint_description_pt endpoint) {
    celix_status_t status = CELIX_SUCCESS;

    if (endpoint == NULL || writer == NULL) {
        status = CELIX_ILLEGAL_ARGUMENT;
    } else {
        xmlTextWriterStartElement(writer->writer, ENDPOINT_DESCRIPTION);

        hash_map_iterator_pt iter = hashMapIterator_create(endpoint->properties);
        while (hashMapIterator_hasNext(iter)) {
            hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);

            void* propertyName = hashMapEntry_getKey(entry);
			const xmlChar* propertyValue = (const xmlChar*) hashMapEntry_getValue(entry);

            xmlTextWriterStartElement(writer->writer, PROPERTY);
            xmlTextWriterWriteAttribute(writer->writer, NAME, propertyName);

            if (strcmp(OSGI_FRAMEWORK_OBJECTCLASS, (char*) propertyName) == 0) {
            	// objectClass *must* be represented as array of string values...
            	endpointDescriptorWriter_writeArrayValue(writer->writer, propertyValue);
            } else if (strcmp(OSGI_RSA_ENDPOINT_SERVICE_ID, (char*) propertyName) == 0) {
            	// endpoint.service.id *must* be represented as long value...
            	endpointDescriptorWriter_writeTypedValue(writer->writer, VALUE_TYPE_LONG, propertyValue);
            } else {
            	// represent all other values as plain string values...
            	endpointDescriptorWriter_writeUntypedValue(writer->writer, propertyValue);
            }

            xmlTextWriterEndElement(writer->writer);
        }
        hashMapIterator_destroy(iter);

        xmlTextWriterEndElement(writer->writer);
    }

    return status;
}
Ejemplo n.º 18
0
celix_status_t discovery_stop(discovery_pt discovery) {
	celix_status_t status;

	status = etcdWatcher_destroy(discovery->watcher);
	if (status != CELIX_SUCCESS) {
		return CELIX_BUNDLE_EXCEPTION;
	}

	status = endpointDiscoveryServer_destroy(discovery->server);
	if (status != CELIX_SUCCESS) {
		return CELIX_BUNDLE_EXCEPTION;
	}

	status = endpointDiscoveryPoller_destroy(discovery->poller);
	if (status != CELIX_SUCCESS) {
		return CELIX_BUNDLE_EXCEPTION;
	}

	hash_map_iterator_pt iter;

	celixThreadMutex_lock(&discovery->discoveredServicesMutex);

	iter = hashMapIterator_create(discovery->discoveredServices);
	while (hashMapIterator_hasNext(iter)) {
		hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);
		endpoint_description_pt endpoint = hashMapEntry_getValue(entry);

		discovery_informEndpointListeners(discovery, endpoint, false);
	}
	hashMapIterator_destroy(iter);

	celixThreadMutex_unlock(&discovery->discoveredServicesMutex);


	logHelper_stop(discovery->loghelper);

	return status;
}
Ejemplo n.º 19
0
celix_status_t pubsub_topicSubscriptionStop(topic_subscription_pt ts){
	celix_status_t status = CELIX_SUCCESS;
	struct epoll_event ev;
	memset(&ev, 0, sizeof(ev));

	ts->running = false;

	pthread_kill(ts->recv_thread.thread,SIGUSR1);

	celixThread_join(ts->recv_thread,NULL);

	status = serviceTracker_close(ts->tracker);

	celixThreadMutex_lock(&ts->socketMap_lock);
	hash_map_iterator_pt it = hashMapIterator_create(ts->socketMap);
	while(hashMapIterator_hasNext(it)) {
		hash_map_entry_pt entry = hashMapIterator_nextEntry(it);
		char *url = hashMapEntry_getKey(entry);
		int *s = hashMapEntry_getValue(entry);
		memset(&ev, 0, sizeof(ev));
		if(epoll_ctl(ts->topicEpollFd, EPOLL_CTL_DEL, *s, &ev) == -1) {
			printf("in if error()\n");
			perror("epoll_ctl() EPOLL_CTL_DEL");
			status += CELIX_SERVICE_EXCEPTION;
		}
		free(s);
		free(url);
		//hashMapIterator_remove(it);
	}
	hashMapIterator_destroy(it);
	hashMap_clear(ts->socketMap, false, false);
	celixThreadMutex_unlock(&ts->socketMap_lock);


	return status;
}
Ejemplo n.º 20
0
celix_status_t pubsubAdmin_destroy(pubsub_admin_pt admin)
{
	celix_status_t status = CELIX_SUCCESS;

	free(admin->ipAddress);

	celixThreadMutex_lock(&admin->pendingSubscriptionsLock);
	hash_map_iterator_pt iter = hashMapIterator_create(admin->pendingSubscriptions);
	while(hashMapIterator_hasNext(iter)){
		hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);
		free((char*)hashMapEntry_getKey(entry));
		arrayList_destroy((array_list_pt)hashMapEntry_getValue(entry));
	}
	hashMapIterator_destroy(iter);
	hashMap_destroy(admin->pendingSubscriptions,false,false);
	celixThreadMutex_unlock(&admin->pendingSubscriptionsLock);

	celixThreadMutex_lock(&admin->subscriptionsLock);
	hashMap_destroy(admin->subscriptions,false,false);
	celixThreadMutex_unlock(&admin->subscriptionsLock);

	celixThreadMutex_lock(&admin->localPublicationsLock);
	hashMap_destroy(admin->localPublications,true,false);
	celixThreadMutex_unlock(&admin->localPublicationsLock);

	celixThreadMutex_lock(&admin->externalPublicationsLock);
	iter = hashMapIterator_create(admin->externalPublications);
	while(hashMapIterator_hasNext(iter)){
		hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);
		free((char*)hashMapEntry_getKey(entry));
		arrayList_destroy((array_list_pt)hashMapEntry_getValue(entry));
	}
	hashMapIterator_destroy(iter);
	hashMap_destroy(admin->externalPublications,false,false);
	celixThreadMutex_unlock(&admin->externalPublicationsLock);

	celixThreadMutex_lock(&admin->serializerListLock);
	arrayList_destroy(admin->serializerList);
	celixThreadMutex_unlock(&admin->serializerListLock);

	celixThreadMutex_lock(&admin->noSerializerPendingsLock);
	arrayList_destroy(admin->noSerializerSubscriptions);
	arrayList_destroy(admin->noSerializerPublications);
	celixThreadMutex_unlock(&admin->noSerializerPendingsLock);


	celixThreadMutex_lock(&admin->usedSerializersLock);

	iter = hashMapIterator_create(admin->topicSubscriptionsPerSerializer);
	while(hashMapIterator_hasNext(iter)){
		arrayList_destroy((array_list_pt)hashMapIterator_nextValue(iter));
	}
	hashMapIterator_destroy(iter);
	hashMap_destroy(admin->topicSubscriptionsPerSerializer,false,false);

	iter = hashMapIterator_create(admin->topicPublicationsPerSerializer);
	while(hashMapIterator_hasNext(iter)){
		arrayList_destroy((array_list_pt)hashMapIterator_nextValue(iter));
	}
	hashMapIterator_destroy(iter);
	hashMap_destroy(admin->topicPublicationsPerSerializer,false,false);

	celixThreadMutex_unlock(&admin->usedSerializersLock);

	celixThreadMutex_destroy(&admin->usedSerializersLock);
	celixThreadMutex_destroy(&admin->serializerListLock);
	celixThreadMutex_destroy(&admin->pendingSubscriptionsLock);

	celixThreadMutexAttr_destroy(&admin->noSerializerPendingsAttr);
	celixThreadMutex_destroy(&admin->noSerializerPendingsLock);

	celixThreadMutexAttr_destroy(&admin->pendingSubscriptionsAttr);
	celixThreadMutex_destroy(&admin->subscriptionsLock);

	celixThreadMutex_destroy(&admin->localPublicationsLock);
	celixThreadMutex_destroy(&admin->externalPublicationsLock);

	logHelper_stop(admin->loghelper);

	logHelper_destroy(&admin->loghelper);

#ifdef BUILD_WITH_ZMQ_SECURITY
	if (admin->zmq_auth != NULL){
		zactor_destroy(&(admin->zmq_auth));
	}
#endif

	free(admin);

	return status;
}
Ejemplo n.º 21
0
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;

}
Ejemplo n.º 22
0
celix_status_t pubsubAdmin_serializerRemoved(void * handle, service_reference_pt reference, void * service){

	pubsub_admin_pt admin = (pubsub_admin_pt)handle;
	int i=0, j=0;
	const char *serType = NULL;

	serviceReference_getProperty(reference, PUBSUB_SERIALIZER_TYPE_KEY,&serType);
	if(serType == NULL){
		printf("Serializer serviceReference %p has no pubsub_serializer.type property specified\n",reference);
		return CELIX_SERVICE_EXCEPTION;
	}

	celixThreadMutex_lock(&admin->serializerListLock);
	/* Remove the serializer from the list */
	arrayList_removeElement(admin->serializerList, reference);
	celixThreadMutex_unlock(&admin->serializerListLock);


	celixThreadMutex_lock(&admin->usedSerializersLock);
	array_list_pt topicPubList = (array_list_pt)hashMap_remove(admin->topicPublicationsPerSerializer, service);
	array_list_pt topicSubList = (array_list_pt)hashMap_remove(admin->topicSubscriptionsPerSerializer, service);
	celixThreadMutex_unlock(&admin->usedSerializersLock);

	/* Now destroy the topicPublications, but first put back the pubsub_endpoints back to the noSerializer pending list */
	if(topicPubList!=NULL){
		for(i=0;i<arrayList_size(topicPubList);i++){
			topic_publication_pt topicPub = (topic_publication_pt)arrayList_get(topicPubList,i);
			/* Stop the topic publication */
			pubsub_topicPublicationStop(topicPub);
			/* Get the endpoints that are going to be orphan */
			array_list_pt pubList = pubsub_topicPublicationGetPublisherList(topicPub);
			for(j=0;j<arrayList_size(pubList);j++){
				pubsub_endpoint_pt pubEP = (pubsub_endpoint_pt)arrayList_get(pubList,j);
				/* Remove the publication */
				pubsubAdmin_removePublication(admin, pubEP);
				/* Reset the endpoint field, so that will be recreated from scratch when a new serializer will be found */
				if(pubEP->endpoint!=NULL){
					free(pubEP->endpoint);
					pubEP->endpoint = NULL;
				}
				/* Add the orphan endpoint to the noSerializer pending list */
				celixThreadMutex_lock(&admin->noSerializerPendingsLock);
				arrayList_add(admin->noSerializerPublications,pubEP);
				celixThreadMutex_unlock(&admin->noSerializerPendingsLock);
			}
			arrayList_destroy(pubList);

			/* Cleanup also the localPublications hashmap*/
			celixThreadMutex_lock(&admin->localPublicationsLock);
			hash_map_iterator_pt iter = hashMapIterator_create(admin->localPublications);
			char *key = NULL;
			service_factory_pt factory = NULL;
			while(hashMapIterator_hasNext(iter)){
				hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);
				factory = (service_factory_pt)hashMapEntry_getValue(entry);
				topic_publication_pt pub = (topic_publication_pt)factory->handle;
				if(pub==topicPub){
					key = (char*)hashMapEntry_getKey(entry);
					break;
				}
			}
			hashMapIterator_destroy(iter);
			if(key!=NULL){
				hashMap_remove(admin->localPublications, key);
				free(factory);
				free(key);
			}
			celixThreadMutex_unlock(&admin->localPublicationsLock);

			/* Finally destroy the topicPublication */
			pubsub_topicPublicationDestroy(topicPub);
		}
		arrayList_destroy(topicPubList);
	}

	/* Now destroy the topicSubscriptions, but first put back the pubsub_endpoints back to the noSerializer pending list */
	if(topicSubList!=NULL){
		for(i=0;i<arrayList_size(topicSubList);i++){
			topic_subscription_pt topicSub = (topic_subscription_pt)arrayList_get(topicSubList,i);
			/* Stop the topic subscription */
			pubsub_topicSubscriptionStop(topicSub);
			/* Get the endpoints that are going to be orphan */
			array_list_pt subList = pubsub_topicSubscriptionGetSubscribersList(topicSub);
			for(j=0;j<arrayList_size(subList);j++){
				pubsub_endpoint_pt subEP = (pubsub_endpoint_pt)arrayList_get(subList,j);
				/* Remove the subscription */
				pubsubAdmin_removeSubscription(admin, subEP);
				/* Reset the endpoint field, so that will be recreated from scratch when a new serializer will be found */
				if(subEP->endpoint!=NULL){
					free(subEP->endpoint);
					subEP->endpoint = NULL;
				}
				/* Add the orphan endpoint to the noSerializer pending list */
				celixThreadMutex_lock(&admin->noSerializerPendingsLock);
				arrayList_add(admin->noSerializerSubscriptions,subEP);
				celixThreadMutex_unlock(&admin->noSerializerPendingsLock);
			}

			/* Cleanup also the subscriptions hashmap*/
			celixThreadMutex_lock(&admin->subscriptionsLock);
			hash_map_iterator_pt iter = hashMapIterator_create(admin->subscriptions);
			char *key = NULL;
			while(hashMapIterator_hasNext(iter)){
				hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);
				topic_subscription_pt sub = (topic_subscription_pt)hashMapEntry_getValue(entry);
				if(sub==topicSub){
					key = (char*)hashMapEntry_getKey(entry);
					break;
				}
			}
			hashMapIterator_destroy(iter);
			if(key!=NULL){
				hashMap_remove(admin->subscriptions, key);
				free(key);
			}
			celixThreadMutex_unlock(&admin->subscriptionsLock);

			/* Finally destroy the topicSubscription */
			pubsub_topicSubscriptionDestroy(topicSub);
		}
		arrayList_destroy(topicSubList);
	}



	printf("PSA_ZMQ: %s serializer removed\n",serType);


	return CELIX_SUCCESS;
}
Ejemplo n.º 23
0
celix_status_t inspectCommand_printImportedServices(command_pt command, array_list_pt ids, void (*out)(char *), void (*err)(char *)) {
    celix_status_t status = CELIX_SUCCESS;
    array_list_pt bundles = NULL;

    if (arrayList_isEmpty(ids)) {
        celix_status_t status = bundleContext_getBundles(command->bundleContext, &bundles);
    } else {
        unsigned int i;

        arrayList_create(&bundles);
        for (i = 0; i < arrayList_size(ids); i++) {
            char *idStr = (char *) arrayList_get(ids, i);
            long id = atol(idStr);
            bundle_pt b = NULL;
            celix_status_t st = bundleContext_getBundleById(command->bundleContext, id, &b);
            if (st == CELIX_SUCCESS) {
                arrayList_add(bundles, b);
            } else {
                char line[256];
                sprintf(line, "INSPECT: Invalid bundle ID: %ld\n", id);
                out(line);
            }
        }
    }

    if (status == CELIX_SUCCESS) {
        unsigned int i = 0;
        for (i = 0; i < arrayList_size(bundles); i++) {
            bundle_pt bundle = (bundle_pt) arrayList_get(bundles, i);

            if (i > 0) {
                out("\n");
            }

            if (bundle != NULL) {
                array_list_pt refs = NULL;

                if (bundle_getServicesInUse(bundle, &refs) == CELIX_SUCCESS) {
                    char line[256];
                    module_pt module = NULL;
                    char * name = NULL;
                    status = bundle_getCurrentModule(bundle, &module);
                    if (status == CELIX_SUCCESS) {
                        status = module_getSymbolicName(module, &name);
                        if (status == CELIX_SUCCESS) {
                            sprintf(line, "%s requires services:\n", name);
                            out(line);
                            out("==============\n");

                            if (refs == NULL || arrayList_size(refs) == 0) {
                                out("Nothing\n");
                            } else {
                                unsigned int j = 0;
                                for (j = 0; j < arrayList_size(refs); j++) {
                                    service_reference_pt ref = (service_reference_pt) arrayList_get(refs, j);
                                    service_registration_pt reg = NULL;
                                    properties_pt props = NULL;
                                    char line[256];
                                    bundle_pt usedBundle = NULL;
                                    module_pt usedModule = NULL;
                                    char *usedSymbolicName = NULL;
                                    long usedBundleId;

                                    serviceReference_getBundle(ref, &usedBundle);
                                    bundle_getBundleId(usedBundle, &usedBundleId);
                                    bundle_getCurrentModule(usedBundle, &usedModule);
                                    module_getSymbolicName(usedModule, &usedSymbolicName);

                                    sprintf(line, "%s [%ld]\n", usedSymbolicName, usedBundleId);
                                    out(line);

                                    serviceReference_getServiceRegistration(ref, &reg);
                                    serviceRegistration_getProperties(reg, &props);
                                    hash_map_iterator_pt iter = hashMapIterator_create(props);
                                    while (hashMapIterator_hasNext(iter)) {
                                        hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);
                                        sprintf(line, "%s = %s\n", hashMapEntry_getKey(entry), hashMapEntry_getValue(entry));
                                        out(line);
                                    }
									hashMapIterator_destroy(iter);

//                                  objectClass = properties_get(props, (char *) OSGI_FRAMEWORK_OBJECTCLASS);
//                                  sprintf(line, "ObjectClass = %s\n", objectClass);
                                    if ((j + 1) < arrayList_size(refs)) {
                                        out("----\n");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }


    return status;
}
Ejemplo n.º 24
0
static int remoteServiceAdmin_callback(struct mg_connection *conn) {
    int result = 1; // zero means: let civetweb handle it further, any non-zero value means it is handled by us...

    const struct mg_request_info *request_info = mg_get_request_info(conn);
    if (request_info->uri != NULL) {
        remote_service_admin_pt rsa = request_info->user_data;


        if (strncmp(request_info->uri, "/service/", 9) == 0 && strcmp("POST", request_info->request_method) == 0) {

            // uri = /services/myservice/call
            const char *uri = request_info->uri;
            // rest = myservice/call

            const char *rest = uri+9;
            char *interfaceStart = strchr(rest, '/');
            int pos = interfaceStart - rest;
            char service[pos+1];
            strncpy(service, rest, pos);
            service[pos] = '\0';
            long serviceId = atol(service);

            celixThreadMutex_lock(&rsa->exportedServicesLock);

            //find endpoint
            export_registration_pt export = NULL;
            hash_map_iterator_pt iter = hashMapIterator_create(rsa->exportedServices);
            while (hashMapIterator_hasNext(iter)) {
                hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);
                array_list_pt exports = hashMapEntry_getValue(entry);
                int expIt = 0;
                for (expIt = 0; expIt < arrayList_size(exports); expIt++) {
                    export_registration_pt check = arrayList_get(exports, expIt);
                    export_reference_pt  ref = NULL;
                    exportRegistration_getExportReference(check, &ref);
                    endpoint_description_pt  checkEndpoint = NULL;
                    exportReference_getExportedEndpoint(ref, &checkEndpoint);
                    if (serviceId == checkEndpoint->serviceId) {
                        export = check;
                        free(ref);
                        break;
                    }
                    free(ref);
                }
            }
            hashMapIterator_destroy(iter);

            if (export != NULL) {

                uint64_t datalength = request_info->content_length;
                char* data = malloc(datalength + 1);
                mg_read(conn, data, datalength);
                data[datalength] = '\0';

                char *response = NULL;
                int responceLength = 0;
                int rc = exportRegistration_call(export, data, -1, &response, &responceLength);
                if (rc != CELIX_SUCCESS) {
                    RSA_LOG_ERROR(rsa, "Error trying to invoke remove service, got error %i\n", rc);
                }

                if (rc == CELIX_SUCCESS && response != NULL) {
                    mg_write(conn, data_response_headers, strlen(data_response_headers));
                    mg_write(conn, response, strlen(response));
                    free(response);
                } else {
                    mg_write(conn, no_content_response_headers, strlen(no_content_response_headers));
                }
                result = 1;

                free(data);
            } else {
Ejemplo n.º 25
0
celix_status_t pubsubAdmin_addPublication(pubsub_admin_pt admin, pubsub_endpoint_pt pubEP) {
	celix_status_t status = CELIX_SUCCESS;

	printf("PSA_ZMQ: Received publication [FWUUID=%s bundleID=%ld scope=%s, topic=%s]\n", pubEP->frameworkUUID, pubEP->serviceID, pubEP->scope, 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) && (pubEP->endpoint == NULL)) {

		celixThreadMutex_lock(&admin->localPublicationsLock);

		service_factory_pt factory = (service_factory_pt) hashMap_get(admin->localPublications, scope_topic);

		if (factory == NULL) {
			topic_publication_pt pub = NULL;
			pubsub_serializer_service_t *best_serializer = NULL;
			if( (status=pubsubAdmin_getBestSerializer(admin, pubEP, &best_serializer)) == CELIX_SUCCESS){
				status = pubsub_topicPublicationCreate(admin->bundle_context, pubEP, best_serializer, admin->ipAddress, admin->basePort, admin->maxPort, &pub);
			}
			else{
				printf("PSA_ZMQ: Cannot find a serializer for publishing topic %s. Adding it to pending list.\n", pubEP->topic);
				celixThreadMutex_lock(&admin->noSerializerPendingsLock);
				arrayList_add(admin->noSerializerPublications,pubEP);
				celixThreadMutex_unlock(&admin->noSerializerPendingsLock);
			}

			if (status == CELIX_SUCCESS) {
				status = pubsub_topicPublicationStart(admin->bundle_context, pub, &factory);
				if (status == CELIX_SUCCESS && factory != NULL) {
					hashMap_put(admin->localPublications, strdup(scope_topic), factory);
					connectTopicPubSubToSerializer(admin, best_serializer, pub, true);
				}
			} else {
				printf("PSA_ZMQ: Cannot create a topicPublication for scope=%s, topic=%s (bundle %ld).\n", pubEP->scope, pubEP->topic, pubEP->serviceID);
			}
		} else {
			//just add the new EP to the list
			topic_publication_pt pub = (topic_publication_pt) factory->handle;
			pubsub_topicPublicationAddPublisherEP(pub, pubEP);
		}

		celixThreadMutex_unlock(&admin->localPublicationsLock);
	}
	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) {
			arrayList_create(&ext_pub_list);
			hashMap_put(admin->externalPublications, strdup(scope_topic), ext_pub_list);
		}

		arrayList_add(ext_pub_list, pubEP);

		celixThreadMutex_unlock(&admin->externalPublicationsLock);
	}

	/* Re-evaluate the pending subscriptions */
	celixThreadMutex_lock(&admin->pendingSubscriptionsLock);

	hash_map_entry_pt pendingSub = hashMap_getEntry(admin->pendingSubscriptions, scope_topic);
	if (pendingSub != NULL) { //There were pending subscription for the just published topic. Let's connect them.
		char* topic = (char*) hashMapEntry_getKey(pendingSub);
		array_list_pt pendingSubList = (array_list_pt) hashMapEntry_getValue(pendingSub);
		int i;
		for (i = 0; i < arrayList_size(pendingSubList); i++) {
			pubsub_endpoint_pt subEP = (pubsub_endpoint_pt) arrayList_get(pendingSubList, i);
			pubsubAdmin_addSubscription(admin, subEP);
		}
		hashMap_remove(admin->pendingSubscriptions, scope_topic);
		arrayList_clear(pendingSubList);
		arrayList_destroy(pendingSubList);
		free(topic);
	}

	celixThreadMutex_unlock(&admin->pendingSubscriptionsLock);

	/* Connect the new publisher to the subscription for his topic, if there is any */
	celixThreadMutex_lock(&admin->subscriptionsLock);

	topic_subscription_pt sub = (topic_subscription_pt) hashMap_get(admin->subscriptions, scope_topic);
	if (sub != NULL && pubEP->endpoint != NULL) {
		pubsub_topicSubscriptionAddConnectPublisherToPendingList(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) {
		pubsub_topicSubscriptionAddConnectPublisherToPendingList(any_sub, pubEP->endpoint);
	}

	free(scope_topic);

	celixThreadMutex_unlock(&admin->subscriptionsLock);

	return status;

}
Ejemplo n.º 26
0
celix_status_t wiringTopologyManager_waAdded(void * handle, service_reference_pt reference, void * service) {
    celix_status_t status = CELIX_SUCCESS;
    wiring_topology_manager_pt manager = handle;
    wiring_admin_service_pt wiringAdminService = (wiring_admin_service_pt) service;

    celixThreadMutex_lock(&manager->waListLock);
    arrayList_add(manager->waList, wiringAdminService);
    celixThreadMutex_unlock(&manager->waListLock);

    /* check whether one of the exported Wires can be exported via the newly available wiringAdmin */
    celixThreadMutex_lock(&manager->exportedWiringEndpointsLock);
    hash_map_iterator_pt iter = hashMapIterator_create(manager->exportedWiringEndpoints);

    // is the properties_match missing here>
    while (hashMapIterator_hasNext(iter)) {
        hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);
        properties_pt exportedWireProperties = hashMapEntry_getKey(entry);
        hash_map_pt wiringAdminList = hashMapEntry_getValue(entry);
        wiring_endpoint_description_pt wEndpoint = NULL;

        char* serviceId = properties_get(exportedWireProperties, "service.id");

        printf("WTM: wiringTopologyManager_waAdded export Wire for %s \n", serviceId);

        status = wiringTopologyManager_WiringAdminServiceExportWiringEndpoint(manager, wiringAdminService, exportedWireProperties, &wEndpoint);

        if (status == CELIX_SUCCESS) {
            hashMap_put(wiringAdminList, wiringAdminService, wEndpoint);
        } else {
            printf("WTM: Could not export wire with new WA\n");
        }
    }
    hashMapIterator_destroy(iter);

    /* check whether the waiting exports can be exported */

    array_list_iterator_pt waitList = arrayListIterator_create(manager->waitingForExport);

    while (arrayListIterator_hasNext(waitList)) {
        properties_pt srvcProperties = arrayListIterator_next(waitList);
        char* serviceId = properties_get(srvcProperties, "service.id");

        printf("WTM: wiringTopologyManager_waAdded export Wire for %s \n", serviceId);
        wiring_endpoint_description_pt wEndpoint = NULL;

        status = wiringTopologyManager_WiringAdminServiceExportWiringEndpoint(manager, wiringAdminService, srvcProperties, &wEndpoint);

        if (status == CELIX_SUCCESS) {
            arrayListIterator_remove(waitList);

            arrayListIterator_destroy(waitList);
            waitList = arrayListIterator_create(manager->waitingForExport);

            hash_map_pt wiringAdminList = hashMap_create(NULL, NULL, NULL, NULL);
            hashMap_put(wiringAdminList, wiringAdminService, wEndpoint);
            hashMap_put(manager->exportedWiringEndpoints, srvcProperties, wiringAdminList);
        } else {
            printf("WTM: Could not export wire with new WA\n");
        }
    }

    arrayListIterator_destroy(waitList);

    celixThreadMutex_unlock(&manager->exportedWiringEndpointsLock);

    /* Check if the added WA can match one of the imported WiringEndpoints */
    celixThreadMutex_lock(&manager->importedWiringEndpointsLock);
    iter = hashMapIterator_create(manager->importedWiringEndpoints);
    while (hashMapIterator_hasNext(iter)) {
        hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);
        wiring_endpoint_description_pt wEndpoint = hashMapEntry_getKey(entry);
        array_list_pt wiringAdminList = hashMapEntry_getValue(entry);

        status = wiringTopologyManager_checkWiringAdminForImportWiringEndpoint(manager, wiringAdminService, wEndpoint);

        if (status == CELIX_SUCCESS) {
            arrayList_add(wiringAdminList, wiringAdminService);
        }

    }
    hashMapIterator_destroy(iter);


    /* check wether waiting service can be exported */
    wiringTopologyManager_checkWaitingForImportServices(manager);

    celixThreadMutex_unlock(&manager->importedWiringEndpointsLock);

    printf("WTM: Added WA\n");

    return status;
}