-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathdm_component_impl.c
More file actions
1228 lines (1081 loc) · 52 KB
/
dm_component_impl.c
File metadata and controls
1228 lines (1081 loc) · 52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include <stdlib.h>
#include <string.h>
#include <uuid/uuid.h>
#include <assert.h>
#include "celix_utils.h"
#include "celix_bundle.h"
#include "celix_stdlib_cleanup.h"
#include "celix_constants.h"
#include "celix_filter.h"
#include "dm_component_impl.h"
#include "celix_framework.h"
#include "hash_map.h"
static const char * const CELIX_DM_PRINT_OK_COLOR = "\033[92m";
static const char * const CELIX_DM_PRINT_WARNING_COLOR = "\033[93m";
static const char * const CELIX_DM_PRINT_NOK_COLOR = "\033[91m";
static const char * const CELIX_DM_PRINT_END_COLOR = "\033[m";
struct celix_dm_component_struct {
char uuid[DM_COMPONENT_MAX_ID_LENGTH];
char name[DM_COMPONENT_MAX_NAME_LENGTH];
celix_bundle_context_t* context;
void* implementation;
celix_dm_cmp_impl_destroy_fpt implementationDestroyFn;
celix_dm_cmp_lifecycle_fpt callbackInit;
celix_dm_cmp_lifecycle_fpt callbackStart;
celix_dm_cmp_lifecycle_fpt callbackStop;
celix_dm_cmp_lifecycle_fpt callbackDeinit;
celix_dm_component_state_t state; //atomic
celix_thread_mutex_t mutex; //protects below
celix_array_list_t* providedInterfaces; //type = dm_interface_t*
celix_array_list_t* dependencies; //type = celix_dm_service_dependency_t*
/**
* Removed dependencies, which are potential still being stopped async
*/
celix_array_list_t* removedDependencies; //type = celix_dm_service_dependency_t*
size_t nrOfTimesStarted;
size_t nrOfTimesResumed;
bool isEnabled;
/**
* Whether the component is an a transition (active performTransition call).
* Should only be used inside the Celix event Thread -> no locking needed.
*/
bool inTransition;
};
typedef struct dm_interface_struct {
char* serviceName;
const void* service;
celix_properties_t *properties;
long svcId;
} dm_interface_t;
static celix_status_t celix_dmComponent_registerServices(celix_dm_component_t *component, bool needLock);
static celix_status_t celix_dmComponent_unregisterServices(celix_dm_component_t *component, bool needLock);
static bool celix_dmComponent_areAllRequiredServiceDependenciesResolved(celix_dm_component_t *component);
static bool celix_dmComponent_performTransition(celix_dm_component_t *component, celix_dm_component_state_t oldState, celix_dm_component_state_t newState);
static celix_status_t celix_dmComponent_calculateNewState(celix_dm_component_t *component, celix_dm_component_state_t currentState, celix_dm_component_state_t *newState);
static celix_status_t celix_dmComponent_handleChange(celix_dm_component_t *component);
static celix_status_t celix_dmComponent_handleAdd(celix_dm_component_t *component, const celix_dm_event_t* event);
static celix_status_t celix_dmComponent_handleRemove(celix_dm_component_t *component, const celix_dm_event_t* event);
static celix_status_t celix_dmComponent_handleSet(celix_dm_component_t *component, const celix_dm_event_t* event);
static celix_status_t celix_dmComponent_enableDependencies(celix_dm_component_t *component);
static celix_status_t celix_dmComponent_suspend(celix_dm_component_t *component, celix_dm_service_dependency_t *dependency);
static celix_status_t celix_dmComponent_resume(celix_dm_component_t *component, celix_dm_service_dependency_t *dependency);
static celix_status_t celix_dmComponent_disable(celix_dm_component_t *component);
static void celix_dmComponent_disableDirectly(celix_dm_component_t *component);
static celix_status_t celix_dmComponent_disableDependencies(celix_dm_component_t *component);
static bool celix_dmComponent_isDisabled(celix_dm_component_t *component);
static void celix_dmComponent_cleanupRemovedDependencies(celix_dm_component_t* component);
static bool celix_dmComponent_isActiveInternal(celix_dm_component_t *component);
static void celix_dmComponent_setCurrentState(celix_dm_component_t* cmp, celix_dm_component_state_t s);
static void celix_dmComponent_logTransition(celix_dm_component_t* cmp, celix_dm_component_state_t currentState, celix_dm_component_state_t desiredState);
celix_dm_component_t* celix_dmComponent_create(bundle_context_t *context, const char* name) {
return celix_dmComponent_createWithUUID(context, name, NULL);
}
celix_dm_component_t* celix_dmComponent_createWithUUID(bundle_context_t *context, const char* name, const char *uuidIn) {
celix_dm_component_t *component = calloc(1, sizeof(*component));
char uuidStr[DM_COMPONENT_MAX_ID_LENGTH];
bool genRandomUUID = true;
if (uuidIn != NULL) {
uuid_t uuid;
int rc = uuid_parse(uuidIn, uuid);
if (rc == 0) {
uuid_unparse(uuid, uuidStr);
genRandomUUID = false;
} else {
//parsing went wrong
celix_bundleContext_log(component->context, CELIX_LOG_LEVEL_WARNING,
"Cannot parse provided uuid '%s'. Not a valid UUID?. UUID will be generated", uuidIn);
}
}
if (genRandomUUID) {
//gen uuid
uuid_t uuid;
uuid_generate(uuid);
uuid_unparse(uuid, uuidStr);
}
snprintf(component->uuid, DM_COMPONENT_MAX_ID_LENGTH, "%s", uuidStr);
snprintf(component->name, DM_COMPONENT_MAX_NAME_LENGTH, "%s", name == NULL ? "n/a" : name);
component->context = context;
component->implementation = NULL;
component->callbackInit = NULL;
component->callbackStart = NULL;
component->callbackStop = NULL;
component->callbackDeinit = NULL;
component->implementation= NULL;
component->implementationDestroyFn = NULL;
component->state = CELIX_DM_CMP_STATE_INACTIVE;
component->providedInterfaces = celix_arrayList_create();
component->dependencies = celix_arrayList_create();
component->removedDependencies = celix_arrayList_create();
celixThreadMutex_create(&component->mutex, NULL);
component->isEnabled = false;
component->inTransition = false;
return component;
}
celix_status_t component_create(bundle_context_pt context, const char *name, celix_dm_component_t **out) {
celix_status_t status = CELIX_SUCCESS;
celix_dm_component_t *cmp = celix_dmComponent_create(context, name);
if (cmp == NULL) {
status = CELIX_BUNDLE_EXCEPTION;
} else {
*out = cmp;
}
return status;
}
void component_destroy(celix_dm_component_t *component) {
celix_dmComponent_destroy(component);
}
void celix_dmComponent_destroy(celix_dm_component_t *component) {
if (component != NULL) {
if (celix_framework_isCurrentThreadTheEventLoop(celix_bundleContext_getFramework(component->context))) {
celix_bundleContext_log(component->context, CELIX_LOG_LEVEL_ERROR,
"Cannot synchronized destroy dm component on Celix event thread. Use celix_dmComponent_destroyAsync instead!");
} else {
celix_bundle_context_t* context = component->context;
celix_dmComponent_destroyAsync(component, NULL, NULL);
celix_bundleContext_waitForEvents(context);
}
}
}
struct celix_dm_component_destroy_data {
celix_dm_component_t* cmp;
void* doneData;
void (*doneCallback)(void*);
};
static void celix_dmComponent_destroyCallback(void *voidData) {
struct celix_dm_component_destroy_data *data = voidData;
celix_dm_component_t *component = data->cmp;
celix_dmComponent_disable(component); //all service unregistered // all svc tracker stopped
if (celix_dmComponent_isDisabled(component)) {
if (component->implementationDestroyFn) {
if (component->implementation == NULL) {
celix_bundleContext_log(component->context, CELIX_LOG_LEVEL_ERROR,
"Component `%s` [uuid=%s] has a configured destroy component "
"implementation callback, but the implementation pointer is NULL. "
"Destroy callback will not be called!",
component->name,
component->uuid);
} else {
component->implementationDestroyFn(component->implementation);
}
}
for (int i = 0; i < celix_arrayList_size(component->providedInterfaces); ++i) {
dm_interface_t *interface = celix_arrayList_get(component->providedInterfaces, i);
if (interface->properties != NULL) {
celix_properties_destroy(interface->properties);
}
free(interface->serviceName);
free(interface);
}
celix_arrayList_destroy(component->providedInterfaces);
for (int i = 0; i < celix_arrayList_size(component->dependencies); ++i) {
celix_dm_service_dependency_t *dep = celix_arrayList_get(component->dependencies, i);
celix_dmServiceDependency_destroy(dep);
}
celix_arrayList_destroy(component->dependencies);
for (int i = 0; i < celix_arrayList_size(component->removedDependencies); ++i) {
celix_dm_service_dependency_t *dep = celix_arrayList_get(component->removedDependencies, i);
celix_dmServiceDependency_destroy(dep);
}
celix_arrayList_destroy(component->removedDependencies);
celixThreadMutex_destroy(&component->mutex);
free(component);
if (data->doneCallback) {
data->doneCallback(data->doneData);
}
free(data);
} else {
//not yet disabled, adding a new event on the event queue
celix_bundle_t* bnd = celix_bundleContext_getBundle(component->context);
celix_framework_t* fw = celix_bundleContext_getFramework(component->context);
celix_framework_fireGenericEvent(
fw, -1, celix_bundle_getId(bnd),
"destroy dm component",
data,
celix_dmComponent_destroyCallback,
NULL,
NULL);
}
}
void celix_dmComponent_destroyAsync(celix_dm_component_t *component, void *doneData, void (*doneCallback)(void*)) {
if (component != NULL) {
struct celix_dm_component_destroy_data* data = malloc(sizeof(*data));
data->cmp = component;
data->doneData = doneData;
data->doneCallback = doneCallback;
celix_bundle_t* bnd = celix_bundleContext_getBundle(component->context);
celix_framework_t* fw = celix_bundleContext_getFramework(component->context);
celix_framework_fireGenericEvent(
fw, -1, celix_bundle_getId(bnd),
"destroy dm component",
data,
celix_dmComponent_destroyCallback,
NULL,
NULL);
}
}
const char* celix_dmComponent_getUUID(celix_dm_component_t* cmp) {
return cmp->uuid;
}
celix_status_t component_addServiceDependency(celix_dm_component_t *component, celix_dm_service_dependency_t *dep) {
return celix_dmComponent_addServiceDependency(component, dep);
}
celix_status_t celix_dmComponent_addServiceDependency(celix_dm_component_t *component, celix_dm_service_dependency_t *dep) {
celix_status_t status = CELIX_SUCCESS;
celix_dmServiceDependency_setComponent(dep, component);
celixThreadMutex_lock(&component->mutex);
celix_arrayList_add(component->dependencies, dep);
bool startDep = celix_dmComponent_currentState(component) != CELIX_DM_CMP_STATE_INACTIVE;
if (startDep) {
celix_dmServiceDependency_enable(dep);
}
celix_dmComponent_cleanupRemovedDependencies(component);
celixThreadMutex_unlock(&component->mutex);
celix_dmComponent_handleChange(component);
return status;
}
celix_status_t celix_dmComponent_removeServiceDependency(celix_dm_component_t *component, celix_dm_service_dependency_t *dep) {
celixThreadMutex_lock(&component->mutex);
celix_arrayList_remove(component->dependencies, dep);
bool disableDependency = celix_dmComponent_currentState(component) != CELIX_DM_CMP_STATE_INACTIVE;
if (disableDependency) {
celix_dmServiceDependency_disable(dep);
}
celix_arrayList_add(component->removedDependencies, dep);
celix_dmComponent_cleanupRemovedDependencies(component);
celixThreadMutex_unlock(&component->mutex);
celix_dmComponent_handleChange(component);
return CELIX_SUCCESS;
}
celix_dm_component_state_t component_currentState(celix_dm_component_t *cmp) {
return celix_dmComponent_currentState(cmp);
}
static void celix_dmComponent_setCurrentState(celix_dm_component_t* cmp, celix_dm_component_state_t s) {
__atomic_store_n(&cmp->state, s, __ATOMIC_RELEASE);
}
celix_dm_component_state_t celix_dmComponent_currentState(celix_dm_component_t *cmp) {
celix_dm_component_state_t state = __atomic_load_n(&cmp->state, __ATOMIC_ACQUIRE);
return state;
}
void* component_getImplementation(celix_dm_component_t *cmp) {
return celix_dmComponent_getImplementation(cmp);
}
void* celix_dmComponent_getImplementation(celix_dm_component_t *cmp) {
return cmp->implementation;
}
const char* component_getName(celix_dm_component_t *cmp) {
return celix_dmComponent_getName(cmp);
}
const char * celix_dmComponent_getName(celix_dm_component_t *cmp) {
return cmp->name;
}
celix_status_t component_removeServiceDependency(celix_dm_component_t *component, celix_dm_service_dependency_t *dependency) {
return celix_dmComponent_removeServiceDependency(component, dependency);
}
celix_status_t celix_private_dmComponent_enable(celix_dm_component_t *component) {
celixThreadMutex_lock(&component->mutex);
bool changed = false;
if (!component->isEnabled) {
changed = !component->isEnabled;
component->isEnabled = true;
}
celixThreadMutex_unlock(&component->mutex);
if (changed) {
celix_dmComponent_handleChange(component);
}
return CELIX_SUCCESS;
}
/**
* @brief disables component and effectualize this through normal component transitions.
*/
static celix_status_t celix_dmComponent_disable(celix_dm_component_t *component) {
celixThreadMutex_lock(&component->mutex);
bool changed = false;
if (component->isEnabled) {
changed = component->isEnabled;
component->isEnabled = false;
}
celixThreadMutex_unlock(&component->mutex);
if (changed) {
celix_dmComponent_handleChange(component);
}
return CELIX_SUCCESS;
}
/**
* Disables component directly, this is needed for an error flow.
*
* Should be called with component mutex locked.
*/
static void celix_dmComponent_disableDirectly(celix_dm_component_t *component) {
component->isEnabled = false;
celix_dmComponent_setCurrentState(component, CELIX_DM_CMP_STATE_INACTIVE);
celix_dmComponent_unregisterServices(component, false);
celix_dmComponent_disableDependencies(component);
}
static void celix_dmComponent_cleanupRemovedDependencies(celix_dm_component_t* component) {
//note should be called with lock
bool removedDep = true;
while (removedDep) {
removedDep = false;
for (int i = 0 ; i < celix_arrayList_size(component->removedDependencies); ++i) {
celix_dm_service_dependency_t* dep = celix_arrayList_get(component->removedDependencies, i);
if (celix_dmServiceDependency_isDisabled(dep)) {
celix_arrayList_remove(component->removedDependencies, dep);
celix_dmServiceDependency_destroy(dep);
removedDep = true;
break;
}
}
}
}
static bool celix_dmComponent_areAllDependenciesDisabled(celix_dm_component_t* component) {
//note should be called with lock
bool allDisabled = true;
for (int i = 0 ; allDisabled && i < celix_arrayList_size(component->dependencies); ++i) {
celix_dm_service_dependency_t* dep = celix_arrayList_get(component->dependencies, i);
if (!celix_dmServiceDependency_isDisabled(dep)) {
allDisabled = false;
}
}
for (int i = 0 ; allDisabled && i < celix_arrayList_size(component->removedDependencies); ++i) {
celix_dm_service_dependency_t* dep = celix_arrayList_get(component->removedDependencies, i);
if (!celix_dmServiceDependency_isDisabled(dep)) {
allDisabled = false;
}
}
return allDisabled;
}
static bool celix_dmComponent_isDisabled(celix_dm_component_t *component) {
bool isStopped;
celixThreadMutex_lock(&component->mutex);
isStopped =
!component->isEnabled &&
celix_dmComponent_currentState(component) == CELIX_DM_CMP_STATE_INACTIVE &&
celix_dmComponent_areAllDependenciesDisabled(component);
celixThreadMutex_unlock(&component->mutex);
return isStopped;
}
celix_status_t component_setCLanguageProperty(celix_dm_component_t *component, bool setCLangProp) {
return celix_dmComponent_setCLanguageProperty(component, setCLangProp);
}
celix_status_t celix_dmComponent_setCLanguageProperty(celix_dm_component_t *component, bool setCLangProp) {
//nop
return CELIX_SUCCESS;
}
celix_status_t component_addInterface(celix_dm_component_t *component, const char* serviceName, const char* serviceVersion, const void* service, celix_properties_t* properties) {
return celix_dmComponent_addInterface(component, serviceName, serviceVersion, service, properties);
}
celix_status_t celix_dmComponent_addInterface(celix_dm_component_t* component,
const char* serviceName,
const char* serviceVersion,
const void* service,
celix_properties_t* properties) {
if (serviceName == NULL || celix_utils_stringEquals(serviceName, "")) {
celix_bundleContext_log(
component->context, CELIX_LOG_LEVEL_ERROR, "Cannot add interface with a NULL or empty serviceName");
return CELIX_ILLEGAL_ARGUMENT;
}
celix_autofree dm_interface_t* interface = calloc(1, sizeof(*interface));
celix_autofree char* name = celix_utils_strdup(serviceName);
if (properties == NULL) {
properties = celix_properties_create();
}
if (serviceVersion != NULL) {
celix_autoptr(celix_version_t) version = celix_version_createVersionFromString(serviceVersion);
if (!version) {
celix_bundleContext_log(
component->context, CELIX_LOG_LEVEL_ERROR, "Cannot add interface with an invalid serviceVersion");
celix_properties_destroy(properties);
return CELIX_ILLEGAL_ARGUMENT;
}
celix_status_t rc =
celix_properties_assignVersion(properties, CELIX_FRAMEWORK_SERVICE_VERSION, celix_steal_ptr(version));
if (rc != CELIX_SUCCESS) {
celix_bundleContext_log(
component->context, CELIX_LOG_LEVEL_ERROR, "Cannot add interface with an invalid serviceVersion");
celix_properties_destroy(properties);
return CELIX_ILLEGAL_ARGUMENT;
}
}
celix_properties_set(properties, CELIX_DM_COMPONENT_UUID, (char*)component->uuid);
celixThreadMutex_lock(&component->mutex);
interface->serviceName = celix_steal_ptr(name);
interface->service = service;
interface->properties = properties;
interface->svcId = -1L;
celix_arrayList_add(component->providedInterfaces, celix_steal_ptr(interface));
if (celix_dmComponent_currentState(component) == CELIX_DM_CMP_STATE_TRACKING_OPTIONAL) {
celix_dmComponent_registerServices(component, false);
}
celixThreadMutex_unlock(&component->mutex);
return CELIX_SUCCESS;
}
celix_status_t component_removeInterface(celix_dm_component_t *component, const void* service) {
return celix_dmComponent_removeInterface(component, service);
}
celix_status_t celix_dmComponent_removeInterface(celix_dm_component_t *component, const void* service) {
celix_status_t status = CELIX_ILLEGAL_ARGUMENT;
celixThreadMutex_lock(&component->mutex);
int nof_interfaces = celix_arrayList_size(component->providedInterfaces);
dm_interface_t* removedInterface = NULL;
for (int i = 0; i < nof_interfaces; ++i) {
dm_interface_t *interface = celix_arrayList_get(component->providedInterfaces, i);
if (interface->service == service) {
celix_arrayList_removeAt(component->providedInterfaces, i);
removedInterface = interface;
break;
}
}
celixThreadMutex_unlock(&component->mutex);
if (removedInterface != NULL) {
celix_bundleContext_unregisterService(component->context, removedInterface->svcId);
celix_properties_destroy(removedInterface->properties);
free(removedInterface->serviceName);
free(removedInterface);
}
return status;
}
celix_status_t component_getInterfaces(celix_dm_component_t *component, celix_array_list_t **out) {
return celix_dmComponent_getInterfaces(component, out);
}
celix_status_t celix_dmComponent_getInterfaces(celix_dm_component_t *component, celix_array_list_t **out) {
celix_array_list_t* names = celix_arrayList_create();
celixThreadMutex_lock(&component->mutex);
int size = celix_arrayList_size(component->providedInterfaces);
for (int i = 0; i < size; i += 1) {
dm_interface_info_t* info = calloc(1, sizeof(*info));
dm_interface_t *interface = celix_arrayList_get(component->providedInterfaces, i);
info->name = celix_utils_strdup(interface->serviceName);
info->properties = celix_properties_copy(interface->properties);
celix_arrayList_add(names, info);
}
celixThreadMutex_unlock(&component->mutex);
*out = names;
return CELIX_SUCCESS;
}
celix_status_t celix_private_dmComponent_handleEvent(celix_dm_component_t *component, const celix_dm_event_t* event) {
celix_status_t status = CELIX_SUCCESS;
switch (event->eventType) {
case CELIX_DM_EVENT_SVC_ADD:
celix_dmComponent_handleAdd(component, event);
break;
case CELIX_DM_EVENT_SVC_REM:
celix_dmComponent_handleRemove(component, event);
break;
case CELIX_DM_EVENT_SVC_SET:
celix_dmComponent_handleSet(component, event);
break;
default:
break;
}
return status;
}
static celix_status_t celix_dmComponent_suspend(celix_dm_component_t *component, celix_dm_service_dependency_t *dependency) {
celix_status_t status = CELIX_SUCCESS;
celixThreadMutex_lock(&component->mutex);
celix_dmComponent_logTransition(component, celix_dmComponent_currentState(component),
CELIX_DM_CMP_STATE_SUSPENDING);
celix_dmComponent_setCurrentState(component, CELIX_DM_CMP_STATE_SUSPENDING);
celix_dmComponent_unregisterServices(component, false);
if (component->callbackStop != NULL ) {
status = component->callbackStop(component->implementation);
}
if (status == CELIX_SUCCESS) {
celix_dmComponent_logTransition(component, celix_dmComponent_currentState(component),
CELIX_DM_CMP_STATE_SUSPENDED);
celix_dmComponent_setCurrentState(component, CELIX_DM_CMP_STATE_SUSPENDED);
} else {
celix_bundleContext_log(component->context, CELIX_LOG_LEVEL_ERROR,
"Error stopping component %s (uuid=%s) using the stop callback. Disabling component.",
component->name,
component->uuid);
celix_dmComponent_disableDirectly(component);
}
celixThreadMutex_unlock(&component->mutex);
return status;
}
static celix_status_t celix_dmComponent_resume(celix_dm_component_t *component, celix_dm_service_dependency_t *dependency) {
celix_status_t status = CELIX_SUCCESS;
if (celix_dmComponent_currentState(component) == CELIX_DM_CMP_STATE_SUSPENDED) {
celixThreadMutex_lock(&component->mutex);
celix_dmComponent_logTransition(component, celix_dmComponent_currentState(component),
CELIX_DM_CMP_STATE_RESUMING);
celix_dmComponent_setCurrentState(component, CELIX_DM_CMP_STATE_RESUMING);
if (component->callbackStart != NULL) {
status = component->callbackStart(component->implementation);
}
if (status == CELIX_SUCCESS) {
celix_dmComponent_registerServices(component, false);
component->nrOfTimesResumed += 1;
celix_dmComponent_logTransition(component, celix_dmComponent_currentState(component),
CELIX_DM_CMP_STATE_TRACKING_OPTIONAL);
celix_dmComponent_setCurrentState(component, CELIX_DM_CMP_STATE_TRACKING_OPTIONAL);
} else {
celix_bundleContext_log(component->context, CELIX_LOG_LEVEL_ERROR,
"Error starting component %s (uuid=%s) using the start callback. Disabling component.",
component->name,
component->uuid);
celix_dmComponent_disableDirectly(component);
}
celixThreadMutex_unlock(&component->mutex);
}
return status;
}
static bool celix_dmComponent_needsSuspend(celix_dm_component_t *component, const celix_dm_event_t* event) {
if (celix_dmComponent_currentState(component) == CELIX_DM_CMP_STATE_TRACKING_OPTIONAL) {
bool callbackConfigured = event->eventType == CELIX_DM_EVENT_SVC_SET ?
celix_dmServiceDependency_isSetCallbackConfigured(event->dep) :
/*add or rem*/ celix_dmServiceDependency_isAddRemCallbacksConfigured(event->dep);
dm_service_dependency_strategy_t strategy = celix_dmServiceDependency_getStrategy(event->dep);
bool suspendStrategy = strategy == DM_SERVICE_DEPENDENCY_STRATEGY_SUSPEND;
return suspendStrategy && callbackConfigured;
}
return false;
}
static celix_status_t celix_dmComponent_handleEvent(celix_dm_component_t *component, const celix_dm_event_t* event, celix_status_t (*setAddOrRemFp)(celix_dm_service_dependency_t *dependency, void* svc, const celix_properties_t* props), const char *invokeName) {
celix_bundleContext_log(component->context, CELIX_LOG_LEVEL_TRACE,
"Calling %s service for component %s (uuid=%s) on service dependency with type %s",
invokeName,
component->name,
component->uuid,
event->dep->serviceName);
if (component->inTransition) {
/* Note if the component is already in transition (stopping, starting, etc) then only remove the svc
* function pointers. This can happen when with dependency loops or if a component also depends on a
* services it provides. (e.g. during stopping the component a handle event will be created to remove the
* service dependency).
*/
setAddOrRemFp(event->dep, event->svc, event->props);
return CELIX_SUCCESS;
}
bool eventHandled = false;
if (event->eventType == CELIX_DM_EVENT_SVC_ADD || (event->eventType == CELIX_DM_EVENT_SVC_SET && event->svc != NULL)) {
//note adding service or setting new service, so cmp will not be stopped in handleChange -> use suspend / resume now
eventHandled = true;
bool needSuspend = celix_dmComponent_needsSuspend(component, event);
if (needSuspend) {
celix_dmComponent_suspend(component, event->dep);
}
setAddOrRemFp(event->dep, event->svc, event->props);
if (needSuspend) {
celix_dmComponent_resume(component, event->dep);
}
};
celix_dmComponent_handleChange(component);
if (!eventHandled /*remove or set null*/) {
//removing svc or set svc to null -> if still active check if suspend is needed before invoking
bool needSuspend = celix_dmComponent_needsSuspend(component, event);
if (needSuspend) {
celix_dmComponent_suspend(component, event->dep);
}
setAddOrRemFp(event->dep, event->svc, event->props);
if (needSuspend) {
celix_dmComponent_resume(component, event->dep);
}
}
return CELIX_SUCCESS;
}
static celix_status_t celix_dmComponent_handleAdd(celix_dm_component_t *component, const celix_dm_event_t* event) {
return celix_dmComponent_handleEvent(component, event, celix_dmServiceDependency_invokeAdd, "add");
}
static celix_status_t celix_dmComponent_handleRemove(celix_dm_component_t *component, const celix_dm_event_t* event) {
return celix_dmComponent_handleEvent(component, event, celix_dmServiceDependency_invokeRemove, "remove");
}
static celix_status_t celix_dmComponent_handleSet(celix_dm_component_t *component, const celix_dm_event_t* event) {
return celix_dmComponent_handleEvent(component, event, celix_dmServiceDependency_invokeSet, "set");
}
/**
* perform state transition. This function should be called with the component->mutex locked.
*/
static celix_status_t celix_dmComponent_enableDependencies(celix_dm_component_t *component) {
for (int i = 0; i < celix_arrayList_size(component->dependencies); i++) {
celix_dm_service_dependency_t *dependency = celix_arrayList_get(component->dependencies, i);
celix_dmServiceDependency_enable(dependency);
}
return CELIX_SUCCESS;
}
/**
* perform state transition. This function should be called with the component->mutex locked.
*/
static celix_status_t celix_dmComponent_disableDependencies(celix_dm_component_t *component) {
for (int i = 0; i < celix_arrayList_size(component->dependencies); i++) {
celix_dm_service_dependency_t *dependency = celix_arrayList_get(component->dependencies, i);
celix_dmServiceDependency_disable(dependency);
}
return CELIX_SUCCESS;
}
/**
* Calculate and handle state change.
*/
static void celix_dmComponent_handleChangeOnEventThread(void *data) {
celix_dm_component_t* component = data;
assert(celix_framework_isCurrentThreadTheEventLoop(celix_bundleContext_getFramework(component->context)));
celixThreadMutex_lock(&component->mutex);
celix_dm_component_state_t oldState;
celix_dm_component_state_t newState;
bool transition = false;
do {
oldState = celix_dmComponent_currentState(component);
celix_status_t status = celix_dmComponent_calculateNewState(component, oldState, &newState);
if (status == CELIX_SUCCESS) {
transition = celix_dmComponent_performTransition(component, oldState, newState);
if (transition) {
celix_dmComponent_setCurrentState(component, newState);
}
}
if (status != CELIX_SUCCESS) {
break;
}
} while (transition);
celixThreadMutex_unlock(&component->mutex);
}
static celix_status_t celix_dmComponent_handleChange(celix_dm_component_t *component) {
celix_framework_t* fw = celix_bundleContext_getFramework(component->context);
if (celix_framework_isCurrentThreadTheEventLoop(fw)) {
celix_dmComponent_handleChangeOnEventThread(component);
} else {
celix_framework_fireGenericEvent(
fw,
-1,
celix_bundleContext_getBundleId(component->context),
"dm component handle change",
component,
celix_dmComponent_handleChangeOnEventThread,
NULL,
NULL);
}
return CELIX_SUCCESS;
}
/**
* Calculate possible state change. This function should be called with the component->mutex locked.
*/
static celix_status_t celix_dmComponent_calculateNewState(celix_dm_component_t *component, celix_dm_component_state_t currentState, celix_dm_component_state_t *newState) {
celix_status_t status = CELIX_SUCCESS;
bool allResolved = celix_dmComponent_areAllRequiredServiceDependenciesResolved(component);
if (currentState == CELIX_DM_CMP_STATE_INACTIVE) {
if (component->isEnabled) {
*newState = CELIX_DM_CMP_STATE_WAITING_FOR_REQUIRED;
} else {
*newState = currentState;
}
} else if (currentState == CELIX_DM_CMP_STATE_WAITING_FOR_REQUIRED) {
if (!component->isEnabled) {
*newState = CELIX_DM_CMP_STATE_INACTIVE;
} else {
if (allResolved) {
*newState = CELIX_DM_CMP_STATE_INITIALIZING;
} else {
*newState = currentState;
}
}
} else if (currentState == CELIX_DM_CMP_STATE_INITIALIZING) {
*newState = CELIX_DM_CMP_STATE_INITIALIZED_AND_WAITING_FOR_REQUIRED;
} else if (currentState == CELIX_DM_CMP_STATE_DEINITIALIZING) {
*newState = CELIX_DM_CMP_STATE_INACTIVE;
} else if (currentState == CELIX_DM_CMP_STATE_INITIALIZED_AND_WAITING_FOR_REQUIRED) {
if (!component->isEnabled) {
*newState = CELIX_DM_CMP_STATE_DEINITIALIZING;
} else {
if (allResolved) {
*newState = CELIX_DM_CMP_STATE_STARTING;
} else {
*newState = currentState;
}
}
} else if (currentState == CELIX_DM_CMP_STATE_STARTING) {
*newState = CELIX_DM_CMP_STATE_TRACKING_OPTIONAL;
} else if (currentState == CELIX_DM_CMP_STATE_STOPPING) {
*newState = CELIX_DM_CMP_STATE_INITIALIZED_AND_WAITING_FOR_REQUIRED;
} else if (currentState == CELIX_DM_CMP_STATE_TRACKING_OPTIONAL) {
if (component->isEnabled && allResolved) {
*newState = currentState;
} else {
*newState = CELIX_DM_CMP_STATE_STOPPING;
}
} else {
//should not reach
*newState = CELIX_DM_CMP_STATE_INACTIVE;
status = CELIX_BUNDLE_EXCEPTION;
}
return status;
}
/**
* perform state transition. This function should be called with the component->mutex locked.
*
* Function can also indicate if the component needs to be disabled. This can be needed if a stop or deinit fails,
* because the component cannot be destroyed but apparently also not cleanly stopped
*/
static bool celix_dmComponent_performTransition(celix_dm_component_t *component, celix_dm_component_state_t currentState, celix_dm_component_state_t desiredState) {
if (currentState == desiredState) {
return false;
}
component->inTransition = true;
celix_dmComponent_logTransition(component, currentState, desiredState);
celix_status_t status = CELIX_SUCCESS;
if (currentState == CELIX_DM_CMP_STATE_INACTIVE && desiredState == CELIX_DM_CMP_STATE_WAITING_FOR_REQUIRED) {
celix_dmComponent_enableDependencies(component);
} else if (currentState == CELIX_DM_CMP_STATE_WAITING_FOR_REQUIRED && desiredState == CELIX_DM_CMP_STATE_INITIALIZING) {
//nop
} else if (currentState == CELIX_DM_CMP_STATE_INITIALIZING && desiredState == CELIX_DM_CMP_STATE_INITIALIZED_AND_WAITING_FOR_REQUIRED) {
if (component->callbackInit) {
status = component->callbackInit(component->implementation);
}
} else if (currentState == CELIX_DM_CMP_STATE_INITIALIZED_AND_WAITING_FOR_REQUIRED && desiredState == CELIX_DM_CMP_STATE_DEINITIALIZING) {
//nop
} else if (currentState == CELIX_DM_CMP_STATE_DEINITIALIZING && desiredState == CELIX_DM_CMP_STATE_INACTIVE) {
if (component->callbackDeinit) {
status = component->callbackDeinit(component->implementation);
}
celix_dmComponent_disableDependencies(component);
} else if (currentState == CELIX_DM_CMP_STATE_INITIALIZED_AND_WAITING_FOR_REQUIRED && desiredState == CELIX_DM_CMP_STATE_STARTING) {
//nop
} else if (currentState == CELIX_DM_CMP_STATE_STARTING && desiredState == CELIX_DM_CMP_STATE_TRACKING_OPTIONAL) {
if (component->callbackStart) {
status = component->callbackStart(component->implementation);
}
if (status == CELIX_SUCCESS) {
celix_dmComponent_registerServices(component, false);
component->nrOfTimesStarted += 1;
}
} else if (currentState == CELIX_DM_CMP_STATE_TRACKING_OPTIONAL && desiredState == CELIX_DM_CMP_STATE_STOPPING) {
//nop
} else if (currentState == CELIX_DM_CMP_STATE_STOPPING && desiredState == CELIX_DM_CMP_STATE_INITIALIZED_AND_WAITING_FOR_REQUIRED) {
celix_dmComponent_unregisterServices(component, false);
if (component->callbackStop) {
status = component->callbackStop(component->implementation);
}
} else if (currentState == CELIX_DM_CMP_STATE_WAITING_FOR_REQUIRED && desiredState == CELIX_DM_CMP_STATE_INACTIVE) {
celix_dmComponent_disableDependencies(component);
} else {
assert(false); //should not be reached.
}
bool transition = true;
if (status != CELIX_SUCCESS) {
celix_bundleContext_log(component->context, CELIX_LOG_LEVEL_ERROR,
"Error in component %s (uuid=%s) transition from %s to %s. Disabling component.",
component->name,
component->uuid,
celix_dmComponent_stateToString(currentState),
celix_dmComponent_stateToString(desiredState));
//If a lifecycle method fails, the component will disable itself (but no higher level transition is needed anymore)
transition = false;
celix_dmComponent_disableDirectly(component);
}
component->inTransition = false;
return transition;
}
/**
* 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;
}
if (required && !available) {
allResolved = false;
break;
}
}
return allResolved;
}
/**
* Register component services (if not already registered).
*/
static celix_status_t celix_dmComponent_registerServices(celix_dm_component_t *component, bool needLock) {
if (needLock) {
celixThreadMutex_lock(&component->mutex);
}
for (int i = 0; i < celix_arrayList_size(component->providedInterfaces); i++) {
dm_interface_t *interface = celix_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;
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);
}
return CELIX_SUCCESS;
}
/**
* Unregister component services. This function should be called with the component->mutex locked.
* If needLock is false, this function should be called with the component->mutex locked.
*/
static celix_status_t celix_dmComponent_unregisterServices(celix_dm_component_t *component, bool needLock) {
celix_status_t status = CELIX_SUCCESS;
if (needLock) {
celixThreadMutex_lock(&component->mutex);
}
celix_array_list_t* ids = NULL;
for (int i = 0; i < celix_arrayList_size(component->providedInterfaces); ++i) {
dm_interface_t *interface = celix_arrayList_get(component->providedInterfaces, i);
if (interface->svcId == -1) {
continue;
}
if (ids == NULL) {
ids = celix_arrayList_create();
}
celix_arrayList_addLong(ids, interface->svcId);
interface->svcId = -1L;
celix_bundleContext_log(component->context, CELIX_LOG_LEVEL_TRACE,
"Unregistering service %s for component %s (uuid=%s)",
interface->serviceName,
component->name,
component->uuid);
}
if (needLock) {
celixThreadMutex_unlock(&component->mutex);
}
if (ids != NULL) {
if (!needLock /*already locked when calling*/) {
celixThreadMutex_unlock(&component->mutex);
}
for (int i = 0; i < celix_arrayList_size(ids); ++i) {
long svcId = celix_arrayList_getLong(ids, i);
celix_bundleContext_unregisterService(component->context, svcId);
}
celix_arrayList_destroy(ids);
if (!needLock) {
celixThreadMutex_lock(&component->mutex);
}
}
return status;
}
celix_status_t component_setCallbacks(celix_dm_component_t *component, celix_dm_cmp_lifecycle_fpt init, celix_dm_cmp_lifecycle_fpt start, celix_dm_cmp_lifecycle_fpt stop, celix_dm_cmp_lifecycle_fpt deinit) {
return celix_dmComponent_setCallbacks(component, init, start, stop, deinit);
}