celix_status_t topologyManager_rsaAdded()

in bundles/remote_services/topology_manager/src/topology_manager.c [538:626]


celix_status_t topologyManager_rsaAdded(void * handle, service_reference_pt rsaSvcRef, void * service) {
	topology_manager_pt manager = (topology_manager_pt) handle;
	celix_properties_t *serviceProperties = NULL;
	remote_service_admin_service_t *rsa = (remote_service_admin_service_t *) service;
	celix_logHelper_log(manager->loghelper, CELIX_LOG_LEVEL_INFO, "TOPOLOGY_MANAGER: Added RSA");

    bool dynamicIpSupport = false;
    const char *dynamicIpSupportStr = NULL;
    serviceReference_getProperty(rsaSvcRef, CELIX_RSA_DYNAMIC_IP_SUPPORT, &dynamicIpSupportStr);
    if (dynamicIpSupportStr != NULL && celix_utils_stringEquals(dynamicIpSupportStr, "true")) {
        dynamicIpSupport = true;
    }

    celix_autofree celix_rsa_service_entry_t* rsaSvcEntry = calloc(1, sizeof(*rsaSvcEntry));
    if (rsaSvcEntry == NULL) {
        return CELIX_ENOMEM;
    }
    rsaSvcEntry->dynamicIpSupport = dynamicIpSupport;
    rsaSvcEntry->rsa = rsa;

    celix_auto(celix_mutex_lock_guard_t) lockGuard = celixMutexLockGuard_init(&manager->lock);

    long rsaSvcId = serviceReference_getServiceId(rsaSvcRef);
    celix_status_t ret = celix_longHashMap_put(manager->rsaMap, rsaSvcId, rsaSvcEntry);
    if (ret != CELIX_SUCCESS) {
        celix_logHelper_logTssErrors(manager->loghelper, CELIX_LOG_LEVEL_ERROR);
        celix_logHelper_error(manager->loghelper, "TOPOLOGY_MANAGER: Error adding rsa service entry to map.");
        return ret;
    }
    celix_steal_ptr(rsaSvcEntry);

    // add already imported services to new rsa
    hash_map_iterator_pt importedServicesIterator = hashMapIterator_create(manager->importedServices);

    while (hashMapIterator_hasNext(importedServicesIterator)) {
        hash_map_entry_pt entry = hashMapIterator_nextEntry(importedServicesIterator);
        endpoint_description_t *endpoint = hashMapEntry_getKey(entry);
        if (scope_allowImport(manager->scope, endpoint)) {
            import_registration_t *import = NULL;
            celix_status_t status = rsa->importService(rsa->admin, endpoint, &import);
            if (status == CELIX_SUCCESS) {
                hash_map_pt imports = hashMapEntry_getValue(entry);

                if (imports == NULL) {
                    imports = hashMap_create(NULL, NULL, NULL, NULL);
                    hashMap_put(manager->importedServices, endpoint, imports);
                }

                hashMap_put(imports, service, import);
            }
        }
    }

    hashMapIterator_destroy(importedServicesIterator);

	// add already exported services to new rsa
    CELIX_LONG_HASH_MAP_ITERATE(manager->exportedServices, iter) {
        celix_exported_service_entry_t* svcEntry = iter.value.ptrValue;
        service_reference_pt reference = svcEntry->reference;
        long serviceId = iter.key;
        char serviceIdStr[32];
        (void)snprintf(serviceIdStr, sizeof(serviceIdStr), "%li", serviceId);

        scope_getExportProperties(manager->scope, reference, &serviceProperties);

        celix_autoptr(celix_array_list_t) registrations = NULL;
        celix_status_t status = rsa->exportService(rsa->admin, serviceIdStr, serviceProperties, &registrations);
        if (status != CELIX_SUCCESS) {
            celix_logHelper_error(manager->loghelper, "TOPOLOGY_MANAGER: Error exporting service.");
            continue;
        }
        if (celix_longHashMap_put(svcEntry->registrations, rsaSvcId, registrations) != CELIX_SUCCESS) {
            celix_logHelper_error(manager->loghelper, "TOPOLOGY_MANAGER: Error adding exported endpoints to map.");
            for (int i = 0; i < celix_arrayList_size(registrations); ++i) {
                export_registration_t* export = celix_arrayList_get(registrations, i);
                rsa->exportRegistration_close(rsa->admin, export);
            }
            continue;
        }
        if (dynamicIpSupport) {
            topologyManager_addDynamicIpEndpointsForExportedService(manager, rsaSvcId,serviceId, registrations);
        } else {
            topologyManager_notifyListenersEndpointAdded(manager, rsa, registrations);
        }
        celix_steal_ptr(registrations);
    }

	return CELIX_SUCCESS;
}