Beispiel #1
0
celix_status_t pubsub_discovery_destroy(pubsub_discovery_pt ps_discovery) {
	celix_status_t status = CELIX_SUCCESS;

	celixThreadMutex_lock(&ps_discovery->discoveredPubsMutex);

	hash_map_iterator_pt iter = hashMapIterator_create(ps_discovery->discoveredPubs);

	while (hashMapIterator_hasNext(iter)) {
		array_list_pt pubEP_list = (array_list_pt) hashMapIterator_nextValue(iter);

		for(int i=0; i < arrayList_size(pubEP_list); i++) {
			pubsubEndpoint_destroy(((pubsub_endpoint_pt)arrayList_get(pubEP_list,i)));
		}
		arrayList_destroy(pubEP_list);
	}

	hashMapIterator_destroy(iter);

	hashMap_destroy(ps_discovery->discoveredPubs, true, false);
	ps_discovery->discoveredPubs = NULL;

	celixThreadMutex_unlock(&ps_discovery->discoveredPubsMutex);

	celixThreadMutex_destroy(&ps_discovery->discoveredPubsMutex);


	celixThreadMutex_lock(&ps_discovery->listenerReferencesMutex);

	hashMap_destroy(ps_discovery->listenerReferences, false, false);
	ps_discovery->listenerReferences = NULL;

	celixThreadMutex_unlock(&ps_discovery->listenerReferencesMutex);

	celixThreadMutex_destroy(&ps_discovery->listenerReferencesMutex);

	free(ps_discovery);

	return status;
}
Beispiel #2
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;
}
Beispiel #3
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");
	}
}
Beispiel #4
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;
}
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;
}
Beispiel #6
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;
}
Beispiel #7
0
static celix_status_t deviceManager_attachAlgorithm(device_manager_pt manager, service_reference_pt ref, void *service) {
	celix_status_t status = CELIX_SUCCESS;

	driver_loader_pt loader = NULL;
	status = driverLoader_create(manager->context, &loader);
	if (status == CELIX_SUCCESS) {
		array_list_pt included = NULL;
		array_list_pt excluded = NULL;

		array_list_pt driverIds = NULL;

		hashMap_put(manager->devices, ref, service);

		status = arrayList_create(&included);
		if (status == CELIX_SUCCESS) {
			status = arrayList_create(&excluded);
			if (status == CELIX_SUCCESS) {
				properties_pt properties = properties_create();

				unsigned int size = 0;
				char **keys;

				serviceReference_getPropertyKeys(ref, &keys, &size);
				for (int i = 0; i < size; i++) {
					char* key = keys[i];
					const char* value = NULL;
					serviceReference_getProperty(ref, key, &value);
					properties_set(properties, key, value);
				}

				status = driverLoader_findDrivers(loader, manager->locators, properties, &driverIds);
				if (status == CELIX_SUCCESS) {
					hash_map_iterator_pt iter = hashMapIterator_create(manager->drivers);
					while (hashMapIterator_hasNext(iter)) {
						driver_attributes_pt driverAttributes = hashMapIterator_nextValue(iter);
						arrayList_add(included, driverAttributes);

						// Each driver that already is installed can be removed from the list
						char *id = NULL;
						celix_status_t substatus = driverAttributes_getDriverId(driverAttributes, &id);
						if (substatus == CELIX_SUCCESS) {
							// arrayList_removeElement(driverIds, id);
							array_list_iterator_pt idsIter = arrayListIterator_create(driverIds);
							while (arrayListIterator_hasNext(idsIter)) {
								char *value = arrayListIterator_next(idsIter);
								if (strcmp(value, id) == 0) {
									arrayListIterator_remove(idsIter);
								}
							}
							arrayListIterator_destroy(idsIter);
						}
						if(id != NULL){
							free(id);
						}
					}
					hashMapIterator_destroy(iter);

					status = deviceManager_matchAttachDriver(manager, loader, driverIds, included, excluded, service, ref);

				}
				arrayList_destroy(driverIds);
				properties_destroy(properties);
				arrayList_destroy(excluded);
			}
			arrayList_destroy(included);
		}

	}

	driverLoader_destroy(&loader);
	return status;
}
Beispiel #8
0
celix_status_t deviceManager_driverRemoved(void * handle, service_reference_pt ref, void * service) {
	celix_status_t status = CELIX_SUCCESS;

	printf("DEVICE_MANAGER: Remove driver\n");
	device_manager_pt manager = handle;
	hashMap_remove(manager->drivers, ref);

	apr_pool_t *idleCheckPool;
	apr_status_t aprStatus = apr_pool_create(&idleCheckPool, manager->pool);
	if (aprStatus != APR_SUCCESS) {
		status = CELIX_ILLEGAL_ARGUMENT;
	} else {
		array_list_pt idleDevices = NULL;
		status = deviceManager_getIdleDevices(manager, idleCheckPool, &idleDevices);
		if (status == CELIX_SUCCESS) {
			int i;
			for (i = 0; i < arrayList_size(idleDevices); i++) {
				celix_status_t forStatus = CELIX_SUCCESS;
				service_reference_pt ref = arrayList_get(idleDevices, i);
				char *bsn = NULL;
				bundle_pt bundle = NULL;
				forStatus = serviceReference_getBundle(ref, &bundle);
				if (forStatus == CELIX_SUCCESS) {
					module_pt module = NULL;
					forStatus = bundle_getCurrentModule(bundle, &module);
					if (forStatus == CELIX_SUCCESS) {
						forStatus = module_getSymbolicName(module, &bsn);
						if (forStatus == CELIX_SUCCESS) {
							printf("DEVICE_MANAGER: IDLE: %s\n", bsn);
							// #TODO attachDriver (idle device)
							// #TODO this can result in a loop?
							//		Locate and install a driver
							//		Let the match fail, the device is idle
							//		The driver is removed, idle check is performed
							//		Attach is tried again
							//		.. loop ..
							void *device = hashMap_get(manager->devices, ref);
							forStatus = deviceManager_attachAlgorithm(manager, ref, device);
						}
					}
				}

				if (forStatus != CELIX_SUCCESS) {
					break; //Got error, stop loop and return status
				}
			}


			hash_map_iterator_pt iter = hashMapIterator_create(manager->drivers);
			while (hashMapIterator_hasNext(iter)) {
				driver_attributes_pt da = hashMapIterator_nextValue(iter);
				//driverAttributes_tryUninstall(da);
			}
			hashMapIterator_destroy(iter);
		}

		if (idleDevices != NULL) {
			arrayList_destroy(idleDevices);
		}
	}

	return status;
}
Beispiel #9
0
static celix_status_t deviceManager_attachAlgorithm(device_manager_pt manager, service_reference_pt ref, void *service) {
	celix_status_t status = CELIX_SUCCESS;

	apr_pool_t *attachPool = NULL;
	apr_status_t aprStatus = apr_pool_create(&attachPool, manager->pool);
	if (aprStatus != APR_SUCCESS) {
		status = CELIX_ILLEGAL_STATE;
	} else {
		driver_loader_pt loader = NULL;
		status = driverLoader_create(attachPool, manager->context, &loader);
		if (status == CELIX_SUCCESS) {
			array_list_pt included = NULL;
			array_list_pt excluded = NULL;

			array_list_pt driverIds = NULL;

			hashMap_put(manager->devices, ref, service);

			status = arrayList_create(&included);
			if (status == CELIX_SUCCESS) {
				status = arrayList_create(&excluded);
				if (status == CELIX_SUCCESS) {
					service_registration_pt registration = NULL;
					status = serviceReference_getServiceRegistration(ref, &registration);
					if (status == CELIX_SUCCESS) {
						properties_pt properties = NULL;
						status = serviceRegistration_getProperties(registration, &properties);
						if (status == CELIX_SUCCESS) {
							status = driverLoader_findDrivers(loader, attachPool, manager->locators, properties, &driverIds);
							if (status == CELIX_SUCCESS) {
								hash_map_iterator_pt iter = hashMapIterator_create(manager->drivers);
								while (hashMapIterator_hasNext(iter)) {
									driver_attributes_pt driverAttributes = hashMapIterator_nextValue(iter);
									arrayList_add(included, driverAttributes);

									// Each driver that already is installed can be removed from the list
									char *id = NULL;
									celix_status_t substatus = driverAttributes_getDriverId(driverAttributes, &id);
									if (substatus == CELIX_SUCCESS) {
										// arrayList_removeElement(driverIds, id);
										array_list_iterator_pt idsIter = arrayListIterator_create(driverIds);
										while (arrayListIterator_hasNext(idsIter)) {
											char *value = arrayListIterator_next(idsIter);
											if (strcmp(value, id) == 0) {
												arrayListIterator_remove(idsIter);
											}
										}
										arrayListIterator_destroy(idsIter);
									} else {
										// Ignore
									}
								}
								hashMapIterator_destroy(iter);

								status = deviceManager_matchAttachDriver(manager, attachPool, loader, driverIds, included, excluded, service, ref);
								arrayList_destroy(driverIds);
							}
						}
					}
					arrayList_destroy(excluded);
				}
				arrayList_destroy(included);
			}

		}
		apr_pool_destroy(attachPool);
	}
	return status;
}
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 {
static int wiringAdmin_callback(struct mg_connection *conn) {
    int result = 0; // 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) {
        wiring_admin_pt admin = request_info->user_data;

        if (hashMap_size(admin->wiringReceiveServices) == 0) {
            printf("%s: No wiringReceiveServices available\n", TAG);
        }

        if (strcmp("POST", request_info->request_method) == 0) {

            celixThreadMutex_lock(&admin->exportedWiringEndpointLock);

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

            char *response = NULL;

            hash_map_iterator_pt iter = hashMapIterator_create(admin->wiringReceiveServices);
            while (hashMapIterator_hasNext(iter)) {
                array_list_pt wiringReceiveServiceList = hashMapIterator_nextValue(iter);

                if (arrayList_size(wiringReceiveServiceList) > 0) {
                    //		printf("WIRING_ADMIN: size of wiringReceiveServiceList is %d\n", arrayList_size(wiringReceiveServiceList));
                    // TODO: we do not support mulitple wiringReceivers?
                    wiring_receive_service_pt wiringReceiveService = (wiring_receive_service_pt) arrayList_get(wiringReceiveServiceList, 0);
                    if (wiringReceiveService->receive(wiringReceiveService->handle, data, &response) != CELIX_SUCCESS) {
                        response = NULL;
                    }

                    break;
                } else {
                    printf("%s: wiringReceiveServiceList is empty\n", TAG);
                }
            }
            hashMapIterator_destroy(iter);

            if (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 {
            printf("%s: Received HTTP Request, but no RSA_Inaetics callback is installed. Discarding request.\n", TAG);
        }

        celixThreadMutex_unlock(&admin->exportedWiringEndpointLock);
    } else {
        printf("%s: Received URI is NULL\n", TAG);
    }

    return result;
}
Beispiel #12
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;
}
Beispiel #13
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;
}
Beispiel #14
0
static celix_status_t pubsubAdmin_addAnySubscription(pubsub_admin_pt admin,pubsub_endpoint_pt subEP){
	celix_status_t status = CELIX_SUCCESS;

	celixThreadMutex_lock(&admin->subscriptionsLock);

	topic_subscription_pt any_sub = hashMap_get(admin->subscriptions,PUBSUB_ANY_SUB_TOPIC);

	if(any_sub==NULL){

		int i;
		pubsub_serializer_service_t *best_serializer = NULL;
		if( (status=pubsubAdmin_getBestSerializer(admin, subEP, &best_serializer)) == CELIX_SUCCESS){
			status = pubsub_topicSubscriptionCreate(admin->bundle_context, PUBSUB_SUBSCRIBER_SCOPE_DEFAULT, PUBSUB_ANY_SUB_TOPIC, best_serializer, &any_sub);
		}
		else{
			printf("PSA_ZMQ: Cannot find a serializer for subscribing topic %s. Adding it to pending list.\n",subEP->topic);
			celixThreadMutex_lock(&admin->noSerializerPendingsLock);
			arrayList_add(admin->noSerializerSubscriptions,subEP);
			celixThreadMutex_unlock(&admin->noSerializerPendingsLock);
		}

		if (status == CELIX_SUCCESS){

			/* Connect all internal publishers */
			celixThreadMutex_lock(&admin->localPublicationsLock);
			hash_map_iterator_pt lp_iter =hashMapIterator_create(admin->localPublications);
			while(hashMapIterator_hasNext(lp_iter)){
				service_factory_pt factory = (service_factory_pt)hashMapIterator_nextValue(lp_iter);
				topic_publication_pt topic_pubs = (topic_publication_pt)factory->handle;
				array_list_pt topic_publishers = pubsub_topicPublicationGetPublisherList(topic_pubs);

				if(topic_publishers!=NULL){
					for(i=0;i<arrayList_size(topic_publishers);i++){
						pubsub_endpoint_pt pubEP = (pubsub_endpoint_pt)arrayList_get(topic_publishers,i);
						if(pubEP->endpoint !=NULL){
							status += pubsub_topicSubscriptionConnectPublisher(any_sub,pubEP->endpoint);
						}
					}
					arrayList_destroy(topic_publishers);
				}
			}
			hashMapIterator_destroy(lp_iter);
			celixThreadMutex_unlock(&admin->localPublicationsLock);

			/* Connect also all external publishers */
			celixThreadMutex_lock(&admin->externalPublicationsLock);
			hash_map_iterator_pt extp_iter =hashMapIterator_create(admin->externalPublications);
			while(hashMapIterator_hasNext(extp_iter)){
				array_list_pt ext_pub_list = (array_list_pt)hashMapIterator_nextValue(extp_iter);
				if(ext_pub_list!=NULL){
					for(i=0;i<arrayList_size(ext_pub_list);i++){
						pubsub_endpoint_pt pubEP = (pubsub_endpoint_pt)arrayList_get(ext_pub_list,i);
						if(pubEP->endpoint !=NULL){
							status += pubsub_topicSubscriptionConnectPublisher(any_sub,pubEP->endpoint);
						}
					}
				}
			}
			hashMapIterator_destroy(extp_iter);
			celixThreadMutex_unlock(&admin->externalPublicationsLock);


			pubsub_topicSubscriptionAddSubscriber(any_sub,subEP);

			status += pubsub_topicSubscriptionStart(any_sub);

		}

		if (status == CELIX_SUCCESS){
			hashMap_put(admin->subscriptions,strdup(PUBSUB_ANY_SUB_TOPIC),any_sub);
			connectTopicPubSubToSerializer(admin, best_serializer, any_sub, false);
		}

	}

	celixThreadMutex_unlock(&admin->subscriptionsLock);

	return status;
}
Beispiel #15
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;
}
Beispiel #16
0
celix_status_t driverMatcher_getBestMatchInternal(driver_matcher_pt matcher, match_pt *match) {
    celix_status_t status = CELIX_SUCCESS;

    if (!hashMap_isEmpty(matcher->attributes)) {
        match_key_t matchKey = NULL;
        hash_map_iterator_pt iter = hashMapIterator_create(matcher->attributes);
        while (hashMapIterator_hasNext(iter)) {
            hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);
            match_key_t key = hashMapEntry_getKey(entry);
            if (matchKey == NULL || matchKey->matchValue < key->matchValue) {
                matchKey = key;
            }
        }
        hashMapIterator_destroy(iter);

        array_list_pt das = hashMap_get(matcher->attributes, matchKey);
        service_reference_pt best = NULL;
        int i;
        for (i = 0; i < arrayList_size(das); i++) {
            driver_attributes_pt attributes = arrayList_get(das, i);
            service_reference_pt reference = NULL;

            celix_status_t substatus = driverAttributes_getReference(attributes, &reference);
            if (substatus == CELIX_SUCCESS) {
                if (best != NULL) {
                    char *rank1Str, *rank2Str;
                    int rank1, rank2;

                    rank1Str = "0";
                    rank2Str = "0";

                    logHelper_log(matcher->loghelper, OSGI_LOGSERVICE_DEBUG, "DRIVER_MATCHER: Compare ranking");

                    serviceReference_getProperty(reference, (char *) OSGI_FRAMEWORK_SERVICE_RANKING, &rank1Str);
                    serviceReference_getProperty(reference, (char *) OSGI_FRAMEWORK_SERVICE_RANKING, &rank2Str);

                    rank1 = atoi(rank1Str);
                    rank2 = atoi(rank2Str);

                    if (rank1 != rank2) {
                        if (rank1 > rank2) {
                            best = reference;
                        }
                    } else {
                        char *id1Str, *id2Str;
                        long id1, id2;

                        id1Str = NULL;
                        id2Str = NULL;

                        logHelper_log(matcher->loghelper, OSGI_LOGSERVICE_DEBUG, "DRIVER_MATCHER: Compare id's");

                        serviceReference_getProperty(reference, (char *) OSGI_FRAMEWORK_SERVICE_ID, &id1Str);
                        serviceReference_getProperty(reference, (char *) OSGI_FRAMEWORK_SERVICE_ID, &id2Str);

                        id1 = atol(id1Str);
                        id2 = atol(id2Str);

                        if (id1 < id2) {
                            best = reference;
                        }
                    }
                } else {
                    best = reference;
                }
            }

        }

        *match = calloc(1, sizeof(**match));
        if (!*match) {
            status = CELIX_ENOMEM;
        } else {
            (*match)->matchValue = matchKey->matchValue;
            (*match)->reference = best;
        }
    }

    return status;
}
Beispiel #17
0
celix_status_t driverMatcher_getBestMatchInternal(driver_matcher_pt matcher, apr_pool_t *pool, match_pt *match) {
	celix_status_t status = CELIX_SUCCESS;

	if (!hashMap_isEmpty(matcher->attributes)) {
		match_key_t matchKey = NULL;
		hash_map_iterator_pt iter = hashMapIterator_create(matcher->attributes);
		while (hashMapIterator_hasNext(iter)) {
			hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);
			match_key_t key = hashMapEntry_getKey(entry);
			if (matchKey == NULL || matchKey->matchValue < key->matchValue) {
				matchKey = key;
			}
		}
		hashMapIterator_destroy(iter);

		array_list_pt das = hashMap_get(matcher->attributes, matchKey);
		service_reference_pt best = NULL;
		int i;
		for (i = 0; i < arrayList_size(das); i++) {
			driver_attributes_pt attributes = arrayList_get(das, i);
			service_reference_pt reference = NULL;

			celix_status_t substatus = driverAttributes_getReference(attributes, &reference);
			if (substatus == CELIX_SUCCESS) {
				if (best != NULL) {
					printf("DRIVER_MATCHER: Compare ranking\n");
					char *rank1Str, *rank2Str;
					int rank1, rank2;
					service_registration_pt registration = NULL;
					substatus = serviceReference_getServiceRegistration(reference, &registration);
					if (substatus == CELIX_SUCCESS) {
						properties_pt properties = NULL;
						status = serviceRegistration_getProperties(registration, &properties);
						if (status == CELIX_SUCCESS) {

							rank1Str = properties_getWithDefault(properties, (char *) OSGI_FRAMEWORK_SERVICE_RANKING, "0");
							rank2Str = properties_getWithDefault(properties, (char *) OSGI_FRAMEWORK_SERVICE_RANKING, "0");

							rank1 = atoi(rank1Str);
							rank2 = atoi(rank2Str);

							if (rank1 != rank2) {
								if (rank1 > rank2) {
									best = reference;
								}
							} else {
								printf("DRIVER_MATCHER: Compare id's\n");
								char *id1Str, *id2Str;
								long id1, id2;

								id1Str = properties_get(properties, (char *) OSGI_FRAMEWORK_SERVICE_ID);
								id2Str = properties_get(properties, (char *) OSGI_FRAMEWORK_SERVICE_ID);

								id1 = atol(id1Str);
								id2 = atol(id2Str);

								if (id1 < id2) {
									best = reference;
								}
							}
						}
					}
				} else {
					best = reference;
				}
			}

		}

		*match = apr_palloc(pool, sizeof(**match));
		if (!*match) {
			status = CELIX_ENOMEM;
		} else {
			(*match)->matchValue = matchKey->matchValue;
			(*match)->reference = best;
		}
	}

	return status;
}
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;
}