http://git-wip-us.apache.org/repos/asf/celix/blob/1b25a0a8/examples/services_example_c/foo2/private/src/foo2.c ---------------------------------------------------------------------- diff --git a/examples/services_example_c/foo2/private/src/foo2.c b/examples/services_example_c/foo2/private/src/foo2.c new file mode 100644 index 0000000..932d42d --- /dev/null +++ b/examples/services_example_c/foo2/private/src/foo2.c @@ -0,0 +1,109 @@ +/** + *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 "foo2.h" + +#include "array_list.h" + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <signal.h> +#include <stdbool.h> +#include <pthread.h> +#include <assert.h> + + +#define OK 0 +#define ERROR 1 + +static void* foo2_thread(void*); + +struct foo2_struct { + array_list_pt examples; + pthread_t thread; + bool running; +}; + +foo2_t* foo2_create(void) { + foo2_t *self = calloc(1, sizeof(*self)); + if (self != NULL) { + self->examples = NULL; + arrayList_create(&self->examples); + self->running = false; + } else { + //log error + } + return self; +}; + +void foo2_destroy(foo2_t *self) { + assert(!self->running); + arrayList_destroy(self->examples); + free(self); +} + +int foo2_start(foo2_t *self) { + self->running = true; + pthread_create(&self->thread, NULL, foo2_thread, self); + return OK; +} + +int foo2_stop(foo2_t *self) { + self->running = false; + pthread_kill(self->thread, SIGUSR1); + pthread_join(self->thread, NULL); + return OK; +} + +int foo2_addExample(foo2_t *self, const example_t *example) { + //NOTE foo2 is suspended -> thread is not running -> safe to update + int status = OK; + status = arrayList_add(self->examples, (void *)example); + return status; +} + +int foo2_removeExample(foo2_t *self, const example_t *example) { + //NOTE foo2 is suspended -> thread is not running -> safe to update + int status = OK; + status = arrayList_removeElement(self->examples, (void*)example); + return status; +} + +static void* foo2_thread(void *userdata) { + foo2_t *self = userdata; + double result; + int rc; + while (self->running) { + unsigned int size = arrayList_size(self->examples); + int i; + for (i = 0; i < size; i += 1) { + const example_t* example = arrayList_get(self->examples, i); + rc = example->method(example->handle, 1, 2.0, &result); + if (rc == 0) { + printf("Result is %f\n", result); + } else { + printf("Error invoking method for example\n"); + } + } + usleep(10000000); + } + return NULL; +} \ No newline at end of file
http://git-wip-us.apache.org/repos/asf/celix/blob/1b25a0a8/examples/services_example_c/foo2/private/src/foo2_activator.c ---------------------------------------------------------------------- diff --git a/examples/services_example_c/foo2/private/src/foo2_activator.c b/examples/services_example_c/foo2/private/src/foo2_activator.c new file mode 100644 index 0000000..0f61e9a --- /dev/null +++ b/examples/services_example_c/foo2/private/src/foo2_activator.c @@ -0,0 +1,88 @@ +/** + *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 "dm_activator.h" +#include "foo2.h" + +#include <stdlib.h> + +struct activator { + foo2_t *foo; +}; + +celix_status_t dm_create(bundle_context_pt context, void **userData) { + celix_status_t status = CELIX_SUCCESS; + struct activator *act = calloc(1, sizeof(*act)); + if (act != NULL) { + act->foo = foo2_create(); + if (act->foo != NULL) { + *userData = act; + } else { + free(act); + } + } else { + status = CELIX_ENOMEM; + } + return status; +} + +celix_status_t dm_init(void *userData, bundle_context_pt context, dm_dependency_manager_pt manager) { + celix_status_t status = CELIX_SUCCESS; + struct activator *activator = userData; + + dm_component_pt cmp = NULL; + component_create(context, "FOO2", &cmp); + component_setImplementation(cmp, activator->foo); + + /* + With the component_setCallbacksSafe we register callbacks when a component is started / stopped using a component + with type foo1_t* + */ + component_setCallbacksSafe(cmp, foo2_t*, NULL, foo2_start, foo2_stop, NULL); + + dm_service_dependency_pt dep = NULL; + serviceDependency_create(&dep); + serviceDependency_setRequired(dep, false); + serviceDependency_setService(dep, EXAMPLE_NAME, EXAMPLE_CONSUMER_RANGE, NULL); + serviceDependency_setStrategy(dep, DM_SERVICE_DEPENDENCY_STRATEGY_SUSPEND); + + /* + With the serviceDependency_setCallbacksSafe we register callbacks when a service + is added and about to be removed for the component type foo1_t* and service type example_t*. + + We should protect the usage of the + service because after removal of the service the memory location of that service + could be freed + */ + serviceDependency_setCallbacksSafe(dep, foo2_t*, const example_t*, NULL, foo2_addExample, foo2_removeExample, NULL, NULL); + component_addServiceDependency(cmp, dep); + + dependencyManager_add(manager, cmp); + + return status; +} + +celix_status_t dm_destroy(void *userData, bundle_context_pt context, dm_dependency_manager_pt manager) { + celix_status_t status = CELIX_SUCCESS; + struct activator *activator = userData; + foo2_destroy(activator->foo); + free(activator); + return status; +}; + http://git-wip-us.apache.org/repos/asf/celix/blob/1b25a0a8/examples/services_example_cxx/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/examples/services_example_cxx/CMakeLists.txt b/examples/services_example_cxx/CMakeLists.txt new file mode 100644 index 0000000..b74c454 --- /dev/null +++ b/examples/services_example_cxx/CMakeLists.txt @@ -0,0 +1,46 @@ +# 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. +if (BUILD_DEPENDENCY_MANAGER_CXX) + + set(CMAKE_CXX_FLAGS "-Wall -Werror -fno-rtti -fno-exceptions ${CMAKE_CXX_FLAGS}") + + include_directories( + ${PROJECT_SOURCE_DIR}/dependency_manager/public/include + ${PROJECT_SOURCE_DIR}/dependency_manager_cxx/include + ${PROJECT_SOURCE_DIR}/utils/public/include + api + ) + + add_subdirectory(bar) + add_subdirectory(foo) + add_subdirectory(baz) + + add_deploy(services_example_cxx + GROUP services_example + COPY + BUNDLES + shell + shell_tui + dm_shell + bar_cxx + foo_cxx + baz_cxx + PROPERTIES + example=value + ) + +endif () http://git-wip-us.apache.org/repos/asf/celix/blob/1b25a0a8/examples/services_example_cxx/api/IAnotherExample.h ---------------------------------------------------------------------- diff --git a/examples/services_example_cxx/api/IAnotherExample.h b/examples/services_example_cxx/api/IAnotherExample.h new file mode 100644 index 0000000..dfddb23 --- /dev/null +++ b/examples/services_example_cxx/api/IAnotherExample.h @@ -0,0 +1,33 @@ +/** + * 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. + */ + +#ifndef IANOTHER_EXAMPLE_H +#define IANOTHER_EXAMPLE_H + +#define IANOTHER_EXAMPLE_VERSION "1.0.0" +#define IANOTHER_EXAMPLE_CONSUMER_RANGE "[1.0.0,2.0.0)" + +class IAnotherExample { +protected: + IAnotherExample() = default; +public: + virtual double method(int arg1, double arg2) = 0; +}; + +#endif //IANOTHER_EXAMPLE_H http://git-wip-us.apache.org/repos/asf/celix/blob/1b25a0a8/examples/services_example_cxx/api/example.h ---------------------------------------------------------------------- diff --git a/examples/services_example_cxx/api/example.h b/examples/services_example_cxx/api/example.h new file mode 100644 index 0000000..68ce0e3 --- /dev/null +++ b/examples/services_example_cxx/api/example.h @@ -0,0 +1,43 @@ +/** + *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. + */ + +#ifndef EXAMPLE_H_ +#define EXAMPLE_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#define EXAMPLE_NAME "org.example" +#define EXAMPLE_VERSION "1.0.0" +#define EXAMPLE_CONSUMER_RANGE "[1.0.0,2.0.0)" + + +struct example_struct { + void *handle; + int (*method)(void *handle, int arg1, double arg2, double *result); +} ; + +typedef struct example_struct example_t; + +#ifdef __cplusplus +} +#endif + +#endif /* EXAMPLE_H_ */ http://git-wip-us.apache.org/repos/asf/celix/blob/1b25a0a8/examples/services_example_cxx/bar/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/examples/services_example_cxx/bar/CMakeLists.txt b/examples/services_example_cxx/bar/CMakeLists.txt new file mode 100644 index 0000000..d43b1b5 --- /dev/null +++ b/examples/services_example_cxx/bar/CMakeLists.txt @@ -0,0 +1,39 @@ +# 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_directories( + private/include +) + +add_bundle(bar_cxx + SYMBOLIC_NAME Bar + VERSION 1.0.0 + SOURCES + private/src/Bar.cc + private/src/BarActivator.cc +) + +IF(APPLE) + target_link_libraries(bar_cxx celix_framework -Wl,-all_load dependency_manager_cxx_static) +else() + if(ENABLE_ADDRESS_SANITIZER) + #With asan there can be undefined symbols + target_link_libraries(bar_cxx -Wl,--whole-archive dependency_manager_cxx_static -Wl,--no-whole-archive celix_framework) + else() + target_link_libraries(bar_cxx -Wl,--no-undefined -Wl,--whole-archive dependency_manager_cxx_static -Wl,--no-whole-archive celix_framework) + endif() +endif() \ No newline at end of file http://git-wip-us.apache.org/repos/asf/celix/blob/1b25a0a8/examples/services_example_cxx/bar/private/include/Bar.h ---------------------------------------------------------------------- diff --git a/examples/services_example_cxx/bar/private/include/Bar.h b/examples/services_example_cxx/bar/private/include/Bar.h new file mode 100644 index 0000000..c7b0955 --- /dev/null +++ b/examples/services_example_cxx/bar/private/include/Bar.h @@ -0,0 +1,40 @@ +/** + * 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. + */ + +#ifndef BAR_H +#define BAR_H + +#include "IAnotherExample.h" + +class Bar : public IAnotherExample { + const double seed = 42; +public: + Bar() = default; + virtual ~Bar() = default; + + void init(); + void start(); + void stop(); + void deinit(); + + virtual double method(int arg1, double arg2); //implementation of IAnotherExample::method + int cMethod(int arg1, double arg2, double *out); //implementation of example_t->method; +}; + +#endif //BAR_H http://git-wip-us.apache.org/repos/asf/celix/blob/1b25a0a8/examples/services_example_cxx/bar/private/include/BarActivator.h ---------------------------------------------------------------------- diff --git a/examples/services_example_cxx/bar/private/include/BarActivator.h b/examples/services_example_cxx/bar/private/include/BarActivator.h new file mode 100644 index 0000000..1710f10 --- /dev/null +++ b/examples/services_example_cxx/bar/private/include/BarActivator.h @@ -0,0 +1,36 @@ +/** + * 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. + */ + +#ifndef BAR_ACTIVATOR_H +#define BAR_ACTIVATOR_H + +#include "celix/dm/DmActivator.h" +#include "example.h" + +using namespace celix::dm; + +class BarActivator : public DmActivator { +private: + example_t cExample; +public: + BarActivator(DependencyManager& mng) : DmActivator(mng) {} + virtual void init(); +}; + +#endif //BAR_ACTIVATOR_H http://git-wip-us.apache.org/repos/asf/celix/blob/1b25a0a8/examples/services_example_cxx/bar/private/src/Bar.cc ---------------------------------------------------------------------- diff --git a/examples/services_example_cxx/bar/private/src/Bar.cc b/examples/services_example_cxx/bar/private/src/Bar.cc new file mode 100644 index 0000000..7490005 --- /dev/null +++ b/examples/services_example_cxx/bar/private/src/Bar.cc @@ -0,0 +1,48 @@ +/** + * 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 "Bar.h" +#include <iostream> + +void Bar::init() { + std::cout << "init Bar\n"; +} + +void Bar::start() { + std::cout << "start Bar\n"; +} + +void Bar::stop() { + std::cout << "stop Bar\n"; +} + +void Bar::deinit() { + std::cout << "deinit Bar\n"; +} + +double Bar::method(int arg1, double arg2) { + double update = (this->seed + arg1) * arg2; + return update; +} + +int Bar::cMethod(int arg1, double arg2, double *out) { + double r = this->method(arg1, arg2); + *out = r; + return 0; +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/celix/blob/1b25a0a8/examples/services_example_cxx/bar/private/src/BarActivator.cc ---------------------------------------------------------------------- diff --git a/examples/services_example_cxx/bar/private/src/BarActivator.cc b/examples/services_example_cxx/bar/private/src/BarActivator.cc new file mode 100644 index 0000000..93b72be --- /dev/null +++ b/examples/services_example_cxx/bar/private/src/BarActivator.cc @@ -0,0 +1,49 @@ +/** + * 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 "Bar.h" +#include "BarActivator.h" + +using namespace celix::dm; + +DmActivator* DmActivator::create(DependencyManager& mng) { + return new BarActivator(mng); +} + +void BarActivator::init() { + std::shared_ptr<Bar> bar = std::shared_ptr<Bar>{new Bar{}}; + std::cout << "bar pointer is " << bar.get() << "\n"; + + Properties props; + props["meta.info.key"] = "meta.info.value"; + + Properties cProps; + cProps["also.meta.info.key"] = "also.meta.info.value"; + + this->cExample.handle = bar.get(); + this->cExample.method = [](void *handle, int arg1, double arg2, double *out) { + Bar* bar = static_cast<Bar*>(handle); + return bar->cMethod(arg1, arg2, out); + }; + + createComponent(bar) //using a pointer a instance. Also supported is lazy initialization (default constructor needed) or a rvalue reference (move) + .addInterface<IAnotherExample>(IANOTHER_EXAMPLE_VERSION, props) + .addCInterface(&this->cExample, EXAMPLE_NAME, EXAMPLE_VERSION, cProps) + .setCallbacks(&Bar::init, &Bar::start, &Bar::stop, &Bar::deinit); +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/celix/blob/1b25a0a8/examples/services_example_cxx/baz/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/examples/services_example_cxx/baz/CMakeLists.txt b/examples/services_example_cxx/baz/CMakeLists.txt new file mode 100644 index 0000000..23ae7e4 --- /dev/null +++ b/examples/services_example_cxx/baz/CMakeLists.txt @@ -0,0 +1,39 @@ +# 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_directories( + private/include +) + +add_bundle(baz_cxx + SYMBOLIC_NAME Baz + VERSION 1.0.0 + SOURCES + private/src/Baz.cc + private/src/BazActivator.cc +) + +IF(APPLE) + target_link_libraries(baz_cxx celix_framework -Wl,-all_load dependency_manager_cxx_static) +else() + if(ENABLE_ADDRESS_SANITIZER) + #With asan there can be undefined symbols + target_link_libraries(baz_cxx -Wl,--whole-archive dependency_manager_cxx_static -Wl,--no-whole-archive celix_framework) + else() + target_link_libraries(baz_cxx -Wl,--no-undefined -Wl,--whole-archive dependency_manager_cxx_static -Wl,--no-whole-archive celix_framework) + endif() +endif() http://git-wip-us.apache.org/repos/asf/celix/blob/1b25a0a8/examples/services_example_cxx/baz/private/include/Baz.h ---------------------------------------------------------------------- diff --git a/examples/services_example_cxx/baz/private/include/Baz.h b/examples/services_example_cxx/baz/private/include/Baz.h new file mode 100644 index 0000000..d881627 --- /dev/null +++ b/examples/services_example_cxx/baz/private/include/Baz.h @@ -0,0 +1,54 @@ +/** + * 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. + */ + +#ifndef BAZ_H +#define BAZ_H + +#include "example.h" +#include "IAnotherExample.h" +#include <thread> +#include <list> +#include <mutex> + +class Baz { + std::list<IAnotherExample*> examples {}; + std::mutex lock_for_examples {}; + + std::list<const example_t*> cExamples {}; + std::mutex lock_for_cExamples {}; + + std::thread pollThread {}; + bool running = false; +public: + Baz() = default; + virtual ~Baz() = default; + + void start(); + void stop(); + + void addAnotherExample(IAnotherExample* e); + void removeAnotherExample(IAnotherExample* e); + + void addExample(const example_t* e); + void removeExample(const example_t* e); + + void poll(); +}; + +#endif //BAZ_H http://git-wip-us.apache.org/repos/asf/celix/blob/1b25a0a8/examples/services_example_cxx/baz/private/include/BazActivator.h ---------------------------------------------------------------------- diff --git a/examples/services_example_cxx/baz/private/include/BazActivator.h b/examples/services_example_cxx/baz/private/include/BazActivator.h new file mode 100644 index 0000000..eaac934 --- /dev/null +++ b/examples/services_example_cxx/baz/private/include/BazActivator.h @@ -0,0 +1,34 @@ +/** + * 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. + */ + +#ifndef BAZ_ACTIVATOR_H +#define BAZ_ACTIVATOR_H + +#include "celix/dm/DmActivator.h" + +using namespace celix::dm; + +class BazActivator : public DmActivator { +private: +public: + BazActivator(DependencyManager& mng) : DmActivator(mng) {} + virtual void init(); +}; + +#endif //BAZ_ACTIVATOR_H http://git-wip-us.apache.org/repos/asf/celix/blob/1b25a0a8/examples/services_example_cxx/baz/private/src/Baz.cc ---------------------------------------------------------------------- diff --git a/examples/services_example_cxx/baz/private/src/Baz.cc b/examples/services_example_cxx/baz/private/src/Baz.cc new file mode 100644 index 0000000..1b5f39e --- /dev/null +++ b/examples/services_example_cxx/baz/private/src/Baz.cc @@ -0,0 +1,84 @@ +/** + * 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 "Baz.h" +#include <iostream> + +void Baz::start() { + std::cout << "start Baz\n"; + this->running = true; + pollThread = std::thread {&Baz::poll, this}; +} + +void Baz::stop() { + std::cout << "stop Baz\n"; + this->running = false; + this->pollThread.join(); +} + +void Baz::addAnotherExample(IAnotherExample *e) { + std::lock_guard<std::mutex> lock(this->lock_for_examples); + this->examples.push_back(e); +} + +void Baz::removeAnotherExample(IAnotherExample *e) { + std::lock_guard<std::mutex> lock(this->lock_for_examples); + this->examples.remove(e); +} + +void Baz::addExample(const example_t *e) { + std::lock_guard<std::mutex> lock(this->lock_for_cExamples); + this->cExamples.push_back(e); +} + +void Baz::removeExample(const example_t *e) { + std::lock_guard<std::mutex> lock(this->lock_for_cExamples); + this->cExamples.remove(e); +} + +void Baz::poll() { + double r1 = 1.0; + double r2 = 1.0; + while (this->running) { + //c++ service required -> if component started always available + + { + int index = 0; + std::lock_guard<std::mutex> lock(this->lock_for_examples); + for (IAnotherExample *e : this->examples) { + r1 = e->method(3, r1); + std::cout << "Result IAnotherExample " << index++ << " is " << r1 << "\n"; + } + } + + + { + int index = 0; + std::lock_guard<std::mutex> lock(this->lock_for_cExamples); + for (const example_t *e : this->cExamples) { + double out; + e->method(e->handle, 4, r2, &out); + r2 = out; + std::cout << "Result example_t " << index++ << " is " << r2 << "\n"; + } + } + + std::this_thread::sleep_for(std::chrono::milliseconds(4000)); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/celix/blob/1b25a0a8/examples/services_example_cxx/baz/private/src/BazActivator.cc ---------------------------------------------------------------------- diff --git a/examples/services_example_cxx/baz/private/src/BazActivator.cc b/examples/services_example_cxx/baz/private/src/BazActivator.cc new file mode 100644 index 0000000..f4f43c3 --- /dev/null +++ b/examples/services_example_cxx/baz/private/src/BazActivator.cc @@ -0,0 +1,45 @@ +/** + * 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 "Baz.h" +#include "BazActivator.h" + +using namespace celix::dm; + +DmActivator* DmActivator::create(DependencyManager& mng) { + return new BazActivator(mng); +} + +void BazActivator::init() { + + Component<Baz>& cmp = createComponent<Baz>() + .setCallbacks(nullptr, &Baz::start, &Baz::stop, nullptr); + + cmp.createServiceDependency<IAnotherExample>() + .setRequired(true) + .setStrategy(DependencyUpdateStrategy::locking) + .setVersionRange(IANOTHER_EXAMPLE_CONSUMER_RANGE) + .setCallbacks(&Baz::addAnotherExample, &Baz::removeAnotherExample); + + cmp.createCServiceDependency<example_t>(EXAMPLE_NAME) + .setRequired(false) + .setStrategy(DependencyUpdateStrategy::locking) + .setVersionRange(EXAMPLE_CONSUMER_RANGE) + .setCallbacks(&Baz::addExample, &Baz::removeExample); +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/celix/blob/1b25a0a8/examples/services_example_cxx/foo/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/examples/services_example_cxx/foo/CMakeLists.txt b/examples/services_example_cxx/foo/CMakeLists.txt new file mode 100644 index 0000000..6c36936 --- /dev/null +++ b/examples/services_example_cxx/foo/CMakeLists.txt @@ -0,0 +1,39 @@ +# 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_directories( + private/include +) + +add_bundle(foo_cxx + SYMBOLIC_NAME Foo + VERSION 1.0.0 + SOURCES + private/src/Foo.cc + private/src/FooActivator.cc +) + +IF(APPLE) + target_link_libraries(foo_cxx celix_framework -Wl,-all_load dependency_manager_cxx_static) +else() + if(ENABLE_ADDRESS_SANITIZER) + #With asan there can be undefined symbols + target_link_libraries(foo_cxx -Wl,--whole-archive dependency_manager_cxx_static -Wl,--no-whole-archive celix_framework) + else() + target_link_libraries(foo_cxx -Wl,--no-undefined -Wl,--whole-archive dependency_manager_cxx_static -Wl,--no-whole-archive celix_framework) + endif() +endif() \ No newline at end of file http://git-wip-us.apache.org/repos/asf/celix/blob/1b25a0a8/examples/services_example_cxx/foo/private/include/Foo.h ---------------------------------------------------------------------- diff --git a/examples/services_example_cxx/foo/private/include/Foo.h b/examples/services_example_cxx/foo/private/include/Foo.h new file mode 100644 index 0000000..a07ddba --- /dev/null +++ b/examples/services_example_cxx/foo/private/include/Foo.h @@ -0,0 +1,45 @@ +/** + * 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. + */ + +#ifndef FOO_H +#define FOO_H + +#include "example.h" +#include "IAnotherExample.h" +#include <thread> + +class Foo { + IAnotherExample* example {nullptr}; + const example_t* cExample {nullptr}; + std::thread pollThread {}; + bool running = false; +public: + Foo() = default; + virtual ~Foo() = default; + + void start(); + void stop(); + + void setAnotherExample(IAnotherExample* e); + void setExample(const example_t* e); + + void poll(); +}; + +#endif //FOO_H http://git-wip-us.apache.org/repos/asf/celix/blob/1b25a0a8/examples/services_example_cxx/foo/private/include/FooActivator.h ---------------------------------------------------------------------- diff --git a/examples/services_example_cxx/foo/private/include/FooActivator.h b/examples/services_example_cxx/foo/private/include/FooActivator.h new file mode 100644 index 0000000..fc35953 --- /dev/null +++ b/examples/services_example_cxx/foo/private/include/FooActivator.h @@ -0,0 +1,34 @@ +/** + * 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. + */ + +#ifndef BAR_ACTIVATOR_H +#define BAR_ACTIVATOR_H + +#include "celix/dm/DmActivator.h" + +using namespace celix::dm; + +class FooActivator : public DmActivator { +private: +public: + FooActivator(DependencyManager& mng) : DmActivator(mng) {} + virtual void init(); +}; + +#endif //BAR_ACTIVATOR_H http://git-wip-us.apache.org/repos/asf/celix/blob/1b25a0a8/examples/services_example_cxx/foo/private/src/Foo.cc ---------------------------------------------------------------------- diff --git a/examples/services_example_cxx/foo/private/src/Foo.cc b/examples/services_example_cxx/foo/private/src/Foo.cc new file mode 100644 index 0000000..241513c --- /dev/null +++ b/examples/services_example_cxx/foo/private/src/Foo.cc @@ -0,0 +1,60 @@ +/** + * 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 "Foo.h" +#include <iostream> + +void Foo::start() { + std::cout << "start Foo\n"; + this->running = true; + pollThread = std::thread {&Foo::poll, this}; +} + +void Foo::stop() { + std::cout << "stop Foo\n"; + this->running = false; + this->pollThread.join(); +} + +void Foo::setAnotherExample(IAnotherExample *e) { + this->example = e; +} + +void Foo::setExample(const example_t *e) { + this->cExample = e; +} + +void Foo::poll() { + double r1 = 1.0; + double r2 = 1.0; + while (this->running) { + //c++ service required -> if component started always available + r1 = this->example->method(3, r1); + std::cout << "Result IAnotherExample is " << r1 << "\n"; + + //c service is optional, can be nullptr + if (this->cExample != nullptr) { + double out; + this->cExample->method(this->cExample->handle, 4, r2, &out); + r2 = out; + std::cout << "Result example_t is " << r2 << "\n"; + } + std::this_thread::sleep_for(std::chrono::milliseconds(5000)); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/celix/blob/1b25a0a8/examples/services_example_cxx/foo/private/src/FooActivator.cc ---------------------------------------------------------------------- diff --git a/examples/services_example_cxx/foo/private/src/FooActivator.cc b/examples/services_example_cxx/foo/private/src/FooActivator.cc new file mode 100644 index 0000000..57d702b --- /dev/null +++ b/examples/services_example_cxx/foo/private/src/FooActivator.cc @@ -0,0 +1,43 @@ +/** + * 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 "Foo.h" +#include "FooActivator.h" + +using namespace celix::dm; + +DmActivator* DmActivator::create(DependencyManager& mng) { + return new FooActivator(mng); +} + +void FooActivator::init() { + + Component<Foo>& cmp = createComponent<Foo>() + .setCallbacks(nullptr, &Foo::start, &Foo::stop, nullptr); + + cmp.createServiceDependency<IAnotherExample>() + .setRequired(true) + .setVersionRange(IANOTHER_EXAMPLE_CONSUMER_RANGE) + .setCallbacks(&Foo::setAnotherExample); + + cmp.createCServiceDependency<example_t>(EXAMPLE_NAME) + .setRequired(false) + .setVersionRange(EXAMPLE_CONSUMER_RANGE) + .setCallbacks(&Foo::setExample); +} \ No newline at end of file
