You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@celix.apache.org by pn...@apache.org on 2021/05/17 14:53:42 UTC

[celix] 01/01: Refactor C rsa to use celix_ api. This breaks the top man test.

This is an automated email from the ASF dual-hosted git repository.

pnoltes pushed a commit to branch feature/use_async_api_for_rsa
in repository https://gitbox.apache.org/repos/asf/celix.git

commit 225d413fa0ce2f97ecd8ca68ed31538192c223cb
Author: Pepijn Noltes <pe...@gmail.com>
AuthorDate: Mon May 17 16:53:31 2021 +0200

    Refactor C rsa to use celix_ api. This breaks the top man test.
---
 .../gtest/src/rsa_tests.cc                         |  70 ++++++-----
 .../src/import_registration_dfi.c                  | 128 ++++++++-------------
 .../src/import_registration_dfi.h                  |   3 -
 .../src/remote_service_admin_activator.c           | 110 ++++++------------
 .../topology_manager/tms_tst/tms_tests.cpp         |   7 --
 5 files changed, 115 insertions(+), 203 deletions(-)

diff --git a/bundles/remote_services/remote_service_admin_dfi/gtest/src/rsa_tests.cc b/bundles/remote_services/remote_service_admin_dfi/gtest/src/rsa_tests.cc
index b2fee66..037a4e6 100644
--- a/bundles/remote_services/remote_service_admin_dfi/gtest/src/rsa_tests.cc
+++ b/bundles/remote_services/remote_service_admin_dfi/gtest/src/rsa_tests.cc
@@ -107,43 +107,30 @@ extern "C" {
     }
 
     static void testImportServiceCallback(void *handle __attribute__((unused)), void *svc) {
-        auto *rsa = static_cast<remote_service_admin_service_t *>(svc);
-
-        int rc = 0;
-        import_registration_t *reg = NULL;
-        endpoint_description_t *endpoint = NULL;
-
-        celix_properties_t *props = celix_properties_create();
-        celix_properties_set(props, OSGI_RSA_ENDPOINT_SERVICE_ID, "42");
-        celix_properties_set(props, OSGI_RSA_ENDPOINT_FRAMEWORK_UUID, "eec5404d-51d0-47ef-8d86-c825a8beda42");
-        celix_properties_set(props, OSGI_RSA_ENDPOINT_ID, "eec5404d-51d0-47ef-8d86-c825a8beda42-42");
-        celix_properties_set(props, OSGI_RSA_SERVICE_IMPORTED_CONFIGS, TST_CONFIGURATION_TYPE);
-        celix_properties_set(props, OSGI_FRAMEWORK_OBJECTCLASS, "org.apache.celix.Example");
-
-        rc = endpointDescription_create(props, &endpoint);
-        ASSERT_EQ(CELIX_SUCCESS, rc);
-
-        rc = rsa->importService(rsa->admin, endpoint, &reg);
-        ASSERT_EQ(CELIX_SUCCESS, rc);
-        ASSERT_TRUE(reg != NULL);
-
-        service_reference_pt ref = NULL;
-        rc = bundleContext_getServiceReference(context, (char *)"org.apache.celix.Example", &ref);
-        ASSERT_EQ(CELIX_SUCCESS, rc);
-        ASSERT_TRUE(ref != NULL);
-
-        rc = bundleContext_ungetServiceReference(context, ref);
-        ASSERT_EQ(CELIX_SUCCESS, rc);
-
-        rc = endpointDescription_destroy(endpoint);
-        ASSERT_EQ(CELIX_SUCCESS, rc);
-
-        /* Cannot test. uses requesting bundles descriptor
-        void *service = NULL;
-        rc = bundleContext_getService(context, ref, &service);
-        ASSERT_EQ(CELIX_SUCCESS, rc);
-        ASSERT_TRUE(service != NULL);
-         */
+        thread_local bool init = true;
+        thread_local endpoint_description_t *endpoint = nullptr;
+        if (init) {
+            auto *rsa = static_cast<remote_service_admin_service_t *>(svc);
+            celix_properties_t *props = celix_properties_create();
+            celix_properties_set(props, OSGI_RSA_ENDPOINT_SERVICE_ID, "42");
+            celix_properties_set(props, OSGI_RSA_ENDPOINT_FRAMEWORK_UUID, "eec5404d-51d0-47ef-8d86-c825a8beda42");
+            celix_properties_set(props, OSGI_RSA_ENDPOINT_ID, "eec5404d-51d0-47ef-8d86-c825a8beda42-42");
+            celix_properties_set(props, OSGI_RSA_SERVICE_IMPORTED_CONFIGS, TST_CONFIGURATION_TYPE);
+            celix_properties_set(props, OSGI_FRAMEWORK_OBJECTCLASS, "org.apache.celix.Example");
+
+            int rc = endpointDescription_create(props, &endpoint);
+            ASSERT_EQ(CELIX_SUCCESS, rc);
+
+            import_registration_t* reg = nullptr;
+            rc = rsa->importService(rsa->admin, endpoint, &reg);
+            ASSERT_EQ(CELIX_SUCCESS, rc);
+            ASSERT_TRUE(reg != nullptr);
+
+            init = false;
+        } else {
+            int rc = endpointDescription_destroy(endpoint);
+            ASSERT_EQ(CELIX_SUCCESS, rc);
+        }
     }
 
     static void testImportService(void) {
@@ -152,8 +139,17 @@ extern "C" {
         opts.use = testImportServiceCallback;
         opts.filter.ignoreServiceLanguage = true;
         opts.waitTimeoutInSeconds = 0.25;
+
+        //first call -> init
         bool called = celix_bundleContext_useServiceWithOptions(context, &opts);
         ASSERT_TRUE(called);
+
+        long svcId = celix_bundleContext_findService(context, "org.apache.celix.Example");
+        EXPECT_GE(svcId, 0);
+
+        //second call -> deinit
+        called = celix_bundleContext_useServiceWithOptions(context, &opts);
+        ASSERT_TRUE(called);
     }
 
     static void testBundles(void) {
diff --git a/bundles/remote_services/remote_service_admin_dfi/src/import_registration_dfi.c b/bundles/remote_services/remote_service_admin_dfi/src/import_registration_dfi.c
index 55f0e05..2c4e8dc 100644
--- a/bundles/remote_services/remote_service_admin_dfi/src/import_registration_dfi.c
+++ b/bundles/remote_services/remote_service_admin_dfi/src/import_registration_dfi.c
@@ -38,8 +38,8 @@ struct import_registration {
     send_func_type send;
     void *sendHandle;
 
-    service_factory_pt factory;
-    service_registration_t *factoryReg;
+    celix_service_factory_t factory;
+    long factorySvcId;
 
     hash_map_pt proxies; //key -> bundle, value -> service_proxy
     celix_thread_mutex_t proxiesMutex; //protects proxies
@@ -64,34 +64,28 @@ static void importRegistration_destroyProxy(struct service_proxy *proxy);
 static void importRegistration_clearProxies(import_registration_t *import);
 static const char* importRegistration_getUrl(import_registration_t *reg);
 static const char* importRegistration_getServiceName(import_registration_t *reg);
+static void* importRegistration_getService(void *handle, const celix_bundle_t *requestingBundle, const celix_properties_t *svcProperties);
+void importRegistration_ungetService(void *handle, const celix_bundle_t *requestingBundle, const celix_properties_t *svcProperties);
 
 celix_status_t importRegistration_create(celix_bundle_context_t *context, endpoint_description_t *endpoint, const char *classObject, const char* serviceVersion, FILE *logFile, import_registration_t **out) {
     celix_status_t status = CELIX_SUCCESS;
     import_registration_t *reg = calloc(1, sizeof(*reg));
+    reg->context = context;
+    reg->endpoint = endpoint;
+    reg->classObject = classObject;
+    reg->proxies = hashMap_create(NULL, NULL, NULL, NULL);
 
-    if (reg != NULL) {
-        reg->factory = calloc(1, sizeof(*reg->factory));
-    }
-
-    if (reg != NULL && reg->factory != NULL) {
-        reg->context = context;
-        reg->endpoint = endpoint;
-        reg->classObject = classObject;
-        reg->proxies = hashMap_create(NULL, NULL, NULL, NULL);
+    remoteInterceptorsHandler_create(context, &reg->interceptorsHandler);
 
-        remoteInterceptorsHandler_create(context, &reg->interceptorsHandler);
+    celixThreadMutex_create(&reg->mutex, NULL);
+    celixThreadMutex_create(&reg->proxiesMutex, NULL);
+    status = version_createVersionFromString((char*)serviceVersion,&(reg->version));
 
-        celixThreadMutex_create(&reg->mutex, NULL);
-        celixThreadMutex_create(&reg->proxiesMutex, NULL);
-        status = version_createVersionFromString((char*)serviceVersion,&(reg->version));
-
-        reg->factory->handle = reg;
-        reg->factory->getService = (void *)importRegistration_getService;
-        reg->factory->ungetService = (void *)importRegistration_ungetService;
-        reg->logFile = logFile;
-    } else {
-        status = CELIX_ENOMEM;
-    }
+    reg->factorySvcId = -1;
+    reg->factory.handle = reg;
+    reg->factory.getService = importRegistration_getService;
+    reg->factory.ungetService = importRegistration_ungetService;
+    reg->logFile = logFile;
 
 
     if (status == CELIX_SUCCESS) {
@@ -144,10 +138,6 @@ void importRegistration_destroy(import_registration_t *import) {
         pthread_mutex_destroy(&import->mutex);
         pthread_mutex_destroy(&import->proxiesMutex);
 
-        if (import->factory != NULL) {
-            free(import->factory);
-        }
-
         if(import->version!=NULL){
         	version_destroy(import->version);
         }
@@ -156,32 +146,24 @@ void importRegistration_destroy(import_registration_t *import) {
 }
 
 celix_status_t importRegistration_start(import_registration_t *import) {
-    celix_status_t  status = CELIX_SUCCESS;
-    if (import->factoryReg == NULL && import->factory != NULL) {
-        celix_properties_t *props =  celix_properties_copy(import->endpoint->properties);
-        status = bundleContext_registerServiceFactory(import->context, (char *)import->classObject, import->factory, props, &import->factoryReg);
-    } else {
-        status = CELIX_ILLEGAL_STATE;
-    }
-    return status;
+    celix_properties_t *props =  celix_properties_copy(import->endpoint->properties);
+    import->factorySvcId = celix_bundleContext_registerServiceFactoryAsync(import->context, &import->factory, import->classObject, props);
+    return import->factorySvcId >= 0 ? CELIX_SUCCESS : CELIX_ILLEGAL_STATE;
 }
 
 celix_status_t importRegistration_stop(import_registration_t *import) {
-    celix_status_t status = CELIX_SUCCESS;
-
-    if (import->factoryReg != NULL) {
-        serviceRegistration_unregister(import->factoryReg);
-        import->factoryReg = NULL;
+    if (import != NULL) {
+        celix_bundleContext_unregisterService(import->context, import->factorySvcId);
+        import->factorySvcId = -1;
+        importRegistration_clearProxies(import);
     }
-
-    importRegistration_clearProxies(import);
-
-    return status;
+    return CELIX_SUCCESS;
 }
 
-
-celix_status_t importRegistration_getService(import_registration_t *import, celix_bundle_t *bundle, service_registration_t *registration, void **out) {
+static void* importRegistration_getService(void *handle, const celix_bundle_t *requestingBundle, const celix_properties_t *svcProperties) {
     celix_status_t  status = CELIX_SUCCESS;
+    void* svc = NULL;
+    import_registration_t* import = handle;
 
     /*
     module_pt module = NULL;
@@ -193,21 +175,36 @@ celix_status_t importRegistration_getService(import_registration_t *import, celi
 
 
     pthread_mutex_lock(&import->proxiesMutex);
-    struct service_proxy *proxy = hashMap_get(import->proxies, bundle);
+    struct service_proxy *proxy = hashMap_get(import->proxies, requestingBundle);
     if (proxy == NULL) {
-        status = importRegistration_createProxy(import, bundle, &proxy);
+        status = importRegistration_createProxy(import, (celix_bundle_t*)requestingBundle, &proxy);
         if (status == CELIX_SUCCESS) {
-            hashMap_put(import->proxies, bundle, proxy);
+            hashMap_put(import->proxies, (void*)requestingBundle, proxy);
         }
     }
-
     if (status == CELIX_SUCCESS) {
         proxy->count += 1;
-        *out = proxy->service;
+        svc = proxy->service;
     }
     pthread_mutex_unlock(&import->proxiesMutex);
 
-    return status;
+    return svc;
+}
+
+void importRegistration_ungetService(void *handle, const celix_bundle_t *requestingBundle, const celix_properties_t *svcProperties) {
+    import_registration_t* import = handle;
+    assert(import != NULL);
+    assert(import->proxies != NULL);
+    pthread_mutex_lock(&import->proxiesMutex);
+    struct service_proxy *proxy = hashMap_get(import->proxies, requestingBundle);
+    if (proxy != NULL) {
+        proxy->count -= 1;
+        if (proxy->count == 0) {
+            hashMap_remove(import->proxies, requestingBundle);
+            importRegistration_destroyProxy(proxy);
+        }
+    }
+    pthread_mutex_unlock(&import->proxiesMutex);
 }
 
 static celix_status_t importRegistration_findAndParseInterfaceDescriptor(celix_bundle_context_t * const context, celix_bundle_t * const bundle, char const * const name, dyn_interface_type **out) {
@@ -376,33 +373,6 @@ static void importRegistration_proxyFunc(void *userData, void *args[], void *ret
     }
 }
 
-celix_status_t importRegistration_ungetService(import_registration_t *import, celix_bundle_t *bundle, service_registration_t *registration, void **out) {
-    celix_status_t  status = CELIX_SUCCESS;
-
-    assert(import != NULL);
-    assert(import->proxies != NULL);
-
-    pthread_mutex_lock(&import->proxiesMutex);
-
-    struct service_proxy *proxy = hashMap_get(import->proxies, bundle);
-    if (proxy != NULL) {
-        if (*out == proxy->service) {
-            proxy->count -= 1;
-        } else {
-            status = CELIX_ILLEGAL_ARGUMENT;
-        }
-
-        if (proxy->count == 0) {
-            hashMap_remove(import->proxies, bundle);
-            importRegistration_destroyProxy(proxy);
-        }
-    }
-
-    pthread_mutex_unlock(&import->proxiesMutex);
-
-    return status;
-}
-
 static void importRegistration_destroyProxy(struct service_proxy *proxy) {
     if (proxy != NULL) {
         if (proxy->intf != NULL) {
diff --git a/bundles/remote_services/remote_service_admin_dfi/src/import_registration_dfi.h b/bundles/remote_services/remote_service_admin_dfi/src/import_registration_dfi.h
index c99fcee..7f4c3a1 100644
--- a/bundles/remote_services/remote_service_admin_dfi/src/import_registration_dfi.h
+++ b/bundles/remote_services/remote_service_admin_dfi/src/import_registration_dfi.h
@@ -38,7 +38,4 @@ celix_status_t importRegistration_setSendFn(import_registration_t *reg,
 celix_status_t importRegistration_start(import_registration_t *import);
 celix_status_t importRegistration_stop(import_registration_t *import);
 
-celix_status_t importRegistration_getService(import_registration_t *import, celix_bundle_t *bundle, service_registration_t *registration, void **service);
-celix_status_t importRegistration_ungetService(import_registration_t *import, celix_bundle_t *bundle, service_registration_t *registration, void **service);
-
 #endif //CELIX_IMPORT_REGISTRATION_DFI_H
diff --git a/bundles/remote_services/remote_service_admin_dfi/src/remote_service_admin_activator.c b/bundles/remote_services/remote_service_admin_dfi/src/remote_service_admin_activator.c
index 9ad6f0f..b524b45 100644
--- a/bundles/remote_services/remote_service_admin_dfi/src/remote_service_admin_activator.c
+++ b/bundles/remote_services/remote_service_admin_dfi/src/remote_service_admin_activator.c
@@ -17,102 +17,58 @@
  * under the License.
  */
 
-#include <stdlib.h>
-#include <remote_service_admin.h>
+#include "celix_api.h"
 
 #include "remote_service_admin_dfi.h"
-
-#include "bundle_activator.h"
-#include "service_registration.h"
-
 #include "export_registration_dfi.h"
 #include "import_registration_dfi.h"
 
-struct activator {
-	remote_service_admin_t *admin;
-	remote_service_admin_service_t *adminService;
-	service_registration_t *registration;
-};
-
-celix_status_t bundleActivator_create(celix_bundle_context_t *context, void **userData) {
-	celix_status_t status = CELIX_SUCCESS;
-	struct activator *activator;
-
-	activator = calloc(1, sizeof(*activator));
-	if (!activator) {
-		status = CELIX_ENOMEM;
-	} else {
-		activator->admin = NULL;
-		activator->registration = NULL;
-
-		*userData = activator;
-	}
+typedef struct celix_remote_service_admin_activator {
+    remote_service_admin_t *admin;
+    remote_service_admin_service_t adminService;
+    long svcIdRsa;
+} celix_remote_service_admin_activator_t;
 
-	return status;
-}
-
-celix_status_t bundleActivator_start(void * userData, celix_bundle_context_t *context) {
-	celix_status_t status = CELIX_SUCCESS;
-	struct activator *activator = userData;
-	remote_service_admin_service_t *remoteServiceAdmin = NULL;
+static celix_status_t celix_rsa_start(celix_remote_service_admin_activator_t* activator, celix_bundle_context_t* ctx) {
+    celix_status_t status = CELIX_SUCCESS;
+    activator->svcIdRsa = -1;
 
-	status = remoteServiceAdmin_create(context, &activator->admin);
-	if (status == CELIX_SUCCESS) {
-		remoteServiceAdmin = calloc(1, sizeof(*remoteServiceAdmin));
-		if (!remoteServiceAdmin) {
-			status = CELIX_ENOMEM;
-		} else {
-			remoteServiceAdmin->admin = activator->admin;
-			remoteServiceAdmin->exportService = remoteServiceAdmin_exportService;
+    status = remoteServiceAdmin_create(ctx, &activator->admin);
+    if (status == CELIX_SUCCESS) {
+        activator->adminService.admin = activator->admin;
+        activator->adminService.exportService = remoteServiceAdmin_exportService;
 
-			remoteServiceAdmin->getExportedServices = remoteServiceAdmin_getExportedServices;
-			remoteServiceAdmin->getImportedEndpoints = remoteServiceAdmin_getImportedEndpoints;
-			remoteServiceAdmin->importService = remoteServiceAdmin_importService;
+        activator->adminService.getExportedServices = remoteServiceAdmin_getExportedServices;
+        activator->adminService.getImportedEndpoints = remoteServiceAdmin_getImportedEndpoints;
+        activator->adminService.importService = remoteServiceAdmin_importService;
 
-			remoteServiceAdmin->exportReference_getExportedEndpoint = exportReference_getExportedEndpoint;
-			remoteServiceAdmin->exportReference_getExportedService = exportReference_getExportedService;
+        activator->adminService.exportReference_getExportedEndpoint = exportReference_getExportedEndpoint;
+        activator->adminService.exportReference_getExportedService = exportReference_getExportedService;
 
-			remoteServiceAdmin->exportRegistration_close = remoteServiceAdmin_removeExportedService;
-			remoteServiceAdmin->exportRegistration_getException = exportRegistration_getException;
-			remoteServiceAdmin->exportRegistration_getExportReference = exportRegistration_getExportReference;
+        activator->adminService.exportRegistration_close = remoteServiceAdmin_removeExportedService;
+        activator->adminService.exportRegistration_getException = exportRegistration_getException;
+        activator->adminService.exportRegistration_getExportReference = exportRegistration_getExportReference;
 
-			remoteServiceAdmin->importReference_getImportedEndpoint = importReference_getImportedEndpoint;
-			remoteServiceAdmin->importReference_getImportedService = importReference_getImportedService;
+        activator->adminService.importReference_getImportedEndpoint = importReference_getImportedEndpoint;
+        activator->adminService.importReference_getImportedService = importReference_getImportedService;
 
-			remoteServiceAdmin->importRegistration_close = remoteServiceAdmin_removeImportedService;
-			remoteServiceAdmin->importRegistration_getException = importRegistration_getException;
-			remoteServiceAdmin->importRegistration_getImportReference = importRegistration_getImportReference;
+        activator->adminService.importRegistration_close = remoteServiceAdmin_removeImportedService;
+        activator->adminService.importRegistration_getException = importRegistration_getException;
+        activator->adminService.importRegistration_getImportReference = importRegistration_getImportReference;
 
-			status = bundleContext_registerService(context, OSGI_RSA_REMOTE_SERVICE_ADMIN, remoteServiceAdmin, NULL, &activator->registration);
-			activator->adminService = remoteServiceAdmin;
-		}
-	}
+        activator->svcIdRsa = celix_bundleContext_registerService(ctx, &activator->adminService, OSGI_RSA_REMOTE_SERVICE_ADMIN, NULL);
+    }
 
-	return status;
+    return status;
 }
 
-celix_status_t bundleActivator_stop(void * userData, celix_bundle_context_t *context) {
-    celix_status_t status = CELIX_SUCCESS;
-    struct activator *activator = userData;
-
-    serviceRegistration_unregister(activator->registration);
-    activator->registration = NULL;
-
+static celix_status_t celix_rsa_stop(celix_remote_service_admin_activator_t* activator, celix_bundle_context_t* ctx) {
+    celix_bundleContext_unregisterService(ctx, activator->svcIdRsa);
     remoteServiceAdmin_stop(activator->admin);
     remoteServiceAdmin_destroy(&activator->admin);
-
-    free(activator->adminService);
-
-    return status;
+    return CELIX_SUCCESS;
 }
 
-celix_status_t bundleActivator_destroy(void * userData, celix_bundle_context_t *context) {
-	celix_status_t status = CELIX_SUCCESS;
-	struct activator *activator = userData;
-
-	free(activator);
-
-	return status;
-}
+CELIX_GEN_BUNDLE_ACTIVATOR(celix_remote_service_admin_activator_t, celix_rsa_start, celix_rsa_stop)
 
 
diff --git a/bundles/remote_services/topology_manager/tms_tst/tms_tests.cpp b/bundles/remote_services/topology_manager/tms_tst/tms_tests.cpp
index e21c3cc..5de0a74 100644
--- a/bundles/remote_services/topology_manager/tms_tst/tms_tests.cpp
+++ b/bundles/remote_services/topology_manager/tms_tst/tms_tests.cpp
@@ -16,13 +16,6 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-/**
- * topology_manager_scoped_test.cpp
- *
- *  \date       Feb 11, 2013
- *  \author     <a href="mailto:dev@celix.apache.org">Apache Celix Project Team</a>
- *  \copyright  Apache License, Version 2.0
- */
 
 #include <stdlib.h>
 #include <stdio.h>