pnoltes commented on a change in pull request #313:
URL: https://github.com/apache/celix/pull/313#discussion_r568608432



##########
File path: libs/framework/src/dm_component_impl.c
##########
@@ -889,402 +689,158 @@ static celix_status_t 
component_calculateNewState(celix_dm_component_t *componen
     return status;
 }
 
-static celix_status_t component_performTransition(celix_dm_component_t 
*component, celix_dm_component_state_t oldState, celix_dm_component_state_t 
newState, bool *transition) {
-    celix_status_t status = CELIX_SUCCESS;
-    //printf("performing transition for %s in thread %i from %i to %i\n", 
component->name, (int) pthread_self(), oldState, newState);
-
+/**
+ * perform state transition. This function should be called with the 
component->mutex locked.
+ */
+static celix_status_t celix_dmComponent_performTransition(celix_dm_component_t 
*component, celix_dm_component_state_t oldState, celix_dm_component_state_t 
newState, bool *transition) {
     if (oldState == newState) {
         *transition = false;
-    } else if (oldState == DM_CMP_STATE_INACTIVE && newState == 
DM_CMP_STATE_WAITING_FOR_REQUIRED) {
-        component_startDependencies(component, component->dependencies);
+        return CELIX_SUCCESS;
+    }
+
+    celix_bundleContext_log(component->context,
+           CELIX_LOG_LEVEL_TRACE,
+           "performing transition for component '%s' (uuid=%s) from state %s 
to state %s",
+           component->name,
+           component->uuid,
+           celix_dmComponent_stateToString(oldState),
+           celix_dmComponent_stateToString(newState));
+
+    celix_status_t status = CELIX_SUCCESS;
+    if (oldState == DM_CMP_STATE_INACTIVE && newState == 
DM_CMP_STATE_WAITING_FOR_REQUIRED) {
+        celix_dmComponent_enableDependencies(component);
         *transition = true;
     } else if (oldState == DM_CMP_STATE_WAITING_FOR_REQUIRED && newState == 
DM_CMP_STATE_INSTANTIATED_AND_WAITING_FOR_REQUIRED) {
-        component_invokeAddRequiredDependencies(component);
-        component_invokeAutoConfigDependencies(component);
         if (component->callbackInit) {
                status = component->callbackInit(component->implementation);
         }
         *transition = true;
     } else if (oldState == DM_CMP_STATE_INSTANTIATED_AND_WAITING_FOR_REQUIRED 
&& newState == DM_CMP_STATE_TRACKING_OPTIONAL) {
-        component_invokeAddRequiredInstanceBoundDependencies(component);
-        component_invokeAutoConfigInstanceBoundDependencies(component);
-               component_invokeAddOptionalDependencies(component);
         if (component->callbackStart) {
                status = component->callbackStart(component->implementation);
         }
-        component_registerServices(component);
+        celix_dmComponent_registerServices(component, false);
+        component->nrOfTimesStarted += 1;
         *transition = true;
     } else if (oldState == DM_CMP_STATE_TRACKING_OPTIONAL && newState == 
DM_CMP_STATE_INSTANTIATED_AND_WAITING_FOR_REQUIRED) {
-        component_unregisterServices(component);
+        celix_dmComponent_unregisterServices(component, false);
         if (component->callbackStop) {
                status = component->callbackStop(component->implementation);
         }
-               component_invokeRemoveOptionalDependencies(component);
-        component_invokeRemoveInstanceBoundDependencies(component);
         *transition = true;
     } else if (oldState == DM_CMP_STATE_INSTANTIATED_AND_WAITING_FOR_REQUIRED 
&& newState == DM_CMP_STATE_WAITING_FOR_REQUIRED) {
-       if (component->callbackDeinit) {
-               status = component->callbackDeinit(component->implementation);
-       }
-        component_invokeRemoveRequiredDependencies(component);
+        if (component->callbackDeinit) {
+            status = component->callbackDeinit(component->implementation);
+        }
         *transition = true;
     } else if (oldState == DM_CMP_STATE_WAITING_FOR_REQUIRED && newState == 
DM_CMP_STATE_INACTIVE) {
-        component_stopDependencies(component);
+        celix_dmComponent_disableDependencies(component);
         *transition = true;
     }
 
     return status;
 }
 
-static celix_status_t component_allRequiredAvailable(celix_dm_component_t 
*component, bool *available) {
-    celix_status_t status = CELIX_SUCCESS;
-
-    pthread_mutex_lock(&component->mutex);
-    *available = true;
-    for (unsigned int i = 0; i < arrayList_size(component->dependencies); i++) 
{
-        celix_dm_service_dependency_t *dependency = 
arrayList_get(component->dependencies, i);
-        bool required = false;
-        bool instanceBound = false;
-
-        serviceDependency_isRequired(dependency, &required);
-        serviceDependency_isInstanceBound(dependency, &instanceBound);
-
-        if (required && !instanceBound) {
-            bool isAvailable = false;
-            serviceDependency_isAvailable(dependency, &isAvailable);
-            if (!isAvailable) {
-                *available = false;
-                break;
-            }
-        }
-    }
-    pthread_mutex_unlock(&component->mutex);
-
-    return status;
-}
-
-static celix_status_t component_allInstanceBoundAvailable(celix_dm_component_t 
*component, bool *available) {
-    celix_status_t status = CELIX_SUCCESS;
-
-    pthread_mutex_lock(&component->mutex);
-    *available = true;
-    for (unsigned int i = 0; i < arrayList_size(component->dependencies); i++) 
{
-        celix_dm_service_dependency_t *dependency = 
arrayList_get(component->dependencies, i);
-        bool required = false;
-        bool instanceBound = false;
-
-        serviceDependency_isRequired(dependency, &required);
-        serviceDependency_isInstanceBound(dependency, &instanceBound);
-
-        if (required && instanceBound) {
-            bool isAvailable = false;
-            serviceDependency_isAvailable(dependency, &isAvailable);
-            if (!isAvailable) {
-                *available = false;
-                break;
-            }
-        }
-    }
-    pthread_mutex_unlock(&component->mutex);
-
-    return status;
-}
-
-static celix_status_t 
component_invokeAddRequiredDependencies(celix_dm_component_t *component) {
-    celix_status_t status = CELIX_SUCCESS;
-
-    pthread_mutex_lock(&component->mutex);
-    for (unsigned int i = 0; i < arrayList_size(component->dependencies); i++) 
{
-        celix_dm_service_dependency_t *dependency = 
arrayList_get(component->dependencies, i);
-
-        bool required = false;
-        bool instanceBound = false;
-
-        serviceDependency_isRequired(dependency, &required);
-        serviceDependency_isInstanceBound(dependency, &instanceBound);
-
-        if (required && !instanceBound) {
-            array_list_pt events = hashMap_get(component->dependencyEvents, 
dependency);
-            if (events) {
-                               for (unsigned int j = 0; j < 
arrayList_size(events); j++) {
-                                       dm_event_pt event = 
arrayList_get(events, j);
-                                       serviceDependency_invokeAdd(dependency, 
event);
-                               }
-            }
-        }
-    }
-    pthread_mutex_unlock(&component->mutex);
-
-    return status;
-}
-
-static celix_status_t 
component_invokeAutoConfigDependencies(celix_dm_component_t *component) {
-    celix_status_t status = CELIX_SUCCESS;
-
-    pthread_mutex_lock(&component->mutex);
-    for (unsigned int i = 0; i < arrayList_size(component->dependencies); i++) 
{
-        celix_dm_service_dependency_t *dependency = 
arrayList_get(component->dependencies, i);
-
-        bool autoConfig = false;
-        bool instanceBound = false;
-
-        serviceDependency_isAutoConfig(dependency, &autoConfig);
-        serviceDependency_isInstanceBound(dependency, &instanceBound);
-
-        if (autoConfig && !instanceBound) {
-            component_configureImplementation(component, dependency);
+/**
+ * Check if all required dependencies are resolved. This function should be 
called with the component->mutex locked.
+ */
+static bool 
celix_dmComponent_areAllRequiredServiceDependenciesResolved(celix_dm_component_t
 *component) {
+    bool allResolved = true;
+    for (int i = 0; i < celix_arrayList_size(component->dependencies); i++) {
+        celix_dm_service_dependency_t *dependency = 
celix_arrayList_get(component->dependencies, i);
+        bool started = celix_dmServiceDependency_isTrackerOpen(dependency);
+        bool required = celix_dmServiceDependency_isRequired(dependency);
+        bool available = celix_dmServiceDependency_isAvailable(dependency);
+        if (!started) {
+            allResolved = false;
+            break;
         }
-    }
-    pthread_mutex_unlock(&component->mutex);
-
-    return status;
-}
-
-static celix_status_t 
component_invokeAutoConfigInstanceBoundDependencies(celix_dm_component_t 
*component) {
-    celix_status_t status = CELIX_SUCCESS;
-
-    pthread_mutex_lock(&component->mutex);
-    for (unsigned int i = 0; i < arrayList_size(component->dependencies); i++) 
{
-        celix_dm_service_dependency_t *dependency = 
arrayList_get(component->dependencies, i);
-
-        bool autoConfig = false;
-        bool instanceBound = false;
-
-        serviceDependency_isAutoConfig(dependency, &autoConfig);
-        serviceDependency_isInstanceBound(dependency, &instanceBound);
-
-        if (autoConfig && instanceBound) {
-            component_configureImplementation(component, dependency);
+        if (required && !available) {
+            allResolved = false;
+            break;
         }
     }
-    pthread_mutex_unlock(&component->mutex);
-
-    return status;
+    return allResolved;
 }
 
-static celix_status_t 
component_invokeAddRequiredInstanceBoundDependencies(celix_dm_component_t 
*component) {
-    celix_status_t status = CELIX_SUCCESS;
-
-    pthread_mutex_lock(&component->mutex);
-    for (unsigned int i = 0; i < arrayList_size(component->dependencies); i++) 
{
-        celix_dm_service_dependency_t *dependency = 
arrayList_get(component->dependencies, i);
-
-        bool required = false;
-        bool instanceBound = false;
-
-        serviceDependency_isRequired(dependency, &required);
-        serviceDependency_isInstanceBound(dependency, &instanceBound);
-
-        if (instanceBound && required) {
-            array_list_pt events = hashMap_get(component->dependencyEvents, 
dependency);
-            if (events) {
-                               for (unsigned int j = 0; j < 
arrayList_size(events); j++) {
-                                       dm_event_pt event = 
arrayList_get(events, j);
-                                       serviceDependency_invokeAdd(dependency, 
event);
-                               }
-            }
-        }
+/**
+ * Register component services (if not already registered).
+ * If needLock is false, this function should be called with the 
component->mutex locked.
+ */
+static celix_status_t celix_dmComponent_registerServices(celix_dm_component_t 
*component, bool needLock) {
+    if (needLock) {
+        celixThreadMutex_lock(&component->mutex);
     }
-    pthread_mutex_unlock(&component->mutex);
-
-    return status;
-}
-
-static celix_status_t 
component_invokeAddOptionalDependencies(celix_dm_component_t *component) {
-    celix_status_t status = CELIX_SUCCESS;
-
-    pthread_mutex_lock(&component->mutex);
-    for (unsigned int i = 0; i < arrayList_size(component->dependencies); i++) 
{
-        celix_dm_service_dependency_t *dependency = 
arrayList_get(component->dependencies, i);
-
-        bool required = false;
-
-        serviceDependency_isRequired(dependency, &required);
-
-        if (!required) {
-            array_list_pt events = hashMap_get(component->dependencyEvents, 
dependency);
-            if (events) {
-                               for (unsigned int j = 0; j < 
arrayList_size(events); j++) {
-                                       dm_event_pt event = 
arrayList_get(events, j);
-                                       serviceDependency_invokeAdd(dependency, 
event);
-                               }
-            }
-        }
+    for (int i = 0; i < celix_arrayList_size(component->providedInterfaces); 
i++) {
+        dm_interface_t *interface = 
arrayList_get(component->providedInterfaces, i);
+        if (interface->svcId == -1L) {
+            celix_properties_t *regProps = 
celix_properties_copy(interface->properties);
+            celix_service_registration_options_t opts = 
CELIX_EMPTY_SERVICE_REGISTRATION_OPTIONS;
+            opts.properties = regProps;
+            opts.svc = (void*)interface->service;
+            opts.serviceName = interface->serviceName;
+            opts.serviceLanguage = celix_properties_get(regProps, 
CELIX_FRAMEWORK_SERVICE_LANGUAGE, NULL);
+            celix_bundleContext_log(component->context, CELIX_LOG_LEVEL_TRACE,
+                   "Async registering service %s for component %s (uuid=%s)",
+                   interface->serviceName,
+                   component->name,
+                   component->uuid);
+            long svcId = 
celix_bundleContext_registerServiceWithOptionsAsync(component->context, &opts);
+            interface->svcId = svcId;
+        }
+    }
+    if (needLock) {
+        celixThreadMutex_unlock(&component->mutex);
     }
-    pthread_mutex_unlock(&component->mutex);
 
-    return status;
+    return CELIX_SUCCESS;
 }
 
-static celix_status_t 
component_invokeRemoveOptionalDependencies(celix_dm_component_t *component) {
+/**
+ * Unregister component services. This function should be called with the 
component->mutex locked.

Review comment:
       removed




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to