Apache Celix
2.3.0
An implementation of the OSGi specification adapted to C and C++
|
Go to the documentation of this file.
28 #include "celix/Properties.h"
43 class ServiceRegistration;
70 static std::shared_ptr<ServiceRegistration>
create(std::shared_ptr<celix_bundle_context_t> cCtx,
71 std::shared_ptr<void> svc,
72 std::string_view name,
73 std::string_view version,
99 "celix::ServiceRegistration delete callback",
110 auto reg = std::shared_ptr<ServiceRegistration>{
116 std::move(properties),
119 std::move(onRegisteredCallbacks),
120 std::move(onUnregisteredCallbacks)},
124 reg->registerService();
149 std::lock_guard<std::mutex> lck{mutex};
157 std::lock_guard<std::mutex> lck{mutex};
172 bool needWaitUnregistering =
false;
173 bool needWaitRegistering =
false;
176 std::lock_guard<std::mutex> lck{mutex};
179 needWaitRegistering =
true;
181 needWaitUnregistering =
true;
184 if (needWaitRegistering) {
187 if (needWaitUnregistering) {
199 if (unregisterAsync) {
200 std::lock_guard<std::mutex> lck{mutex};
207 auto reg = static_cast<ServiceRegistration*>(data);
209 std::lock_guard<std::mutex> lck{reg->mutex};
210 reg->state = ServiceRegistrationState::UNREGISTERED;
213 for (
const auto &cb : reg->onUnregisteredCallbacks) {
219 long localSvcId = -1;
221 std::lock_guard<std::mutex> lck{mutex};
228 if (localSvcId >= 0) {
231 std::lock_guard<std::mutex> lck{mutex};
235 for (
const auto& cb: onUnregisteredCallbacks) {
247 std::shared_ptr<ServiceRegistration>
getSelf()
const {
248 std::lock_guard<std::mutex> lck{mutex};
257 std::shared_ptr<celix_bundle_context_t> _cCtx,
258 std::shared_ptr<void> _svc,
259 std::string_view _name,
260 std::string_view _version,
263 bool _unregisterAsync,
266 cCtx{std::move(_cCtx)},
269 properties{std::move(_properties)},
270 registerAsync{_registerAsync},
271 unregisterAsync{_unregisterAsync},
272 onRegisteredCallbacks{std::move(_onRegisteredCallbacks)},
273 onUnregisteredCallbacks{std::move(_onUnregisteredCallbacks)},
274 svc{std::move(_svc)} {}
285 void registerService() {
287 auto* cProps = celix_properties_copy(properties.getCProperties());
289 opts.svc = svc.get();
290 opts.serviceName = name.c_str();
291 opts.properties = cProps;
292 if (!version.empty()) {
293 opts.serviceVersion = version.c_str();
297 opts.asyncData =
static_cast<void*
>(
this);
299 auto *reg =
static_cast<ServiceRegistration *
>(data);
301 std::lock_guard<std::mutex> lck{reg->mutex};
304 for (
const auto &cb : reg->onRegisteredCallbacks) {
308 std::lock_guard<std::mutex> lck{mutex};
315 if (localSvcId < 0) {
319 std::lock_guard<std::mutex> lck{mutex};
323 for (
const auto& cb: onRegisteredCallbacks) {
332 void setSelf(
const std::shared_ptr<ServiceRegistration>& s) {
333 std::lock_guard<std::mutex> lck{mutex};
337 const std::shared_ptr<celix_bundle_context_t> cCtx;
338 const std::string name;
339 const std::string version;
341 const bool registerAsync;
342 const bool unregisterAsync;
343 const std::vector<std::function<void(ServiceRegistration&)>> onRegisteredCallbacks;
344 const std::vector<std::function<void(ServiceRegistration&)>> onUnregisteredCallbacks;
346 mutable std::mutex mutex{};
348 std::shared_ptr<void> svc;
350 std::weak_ptr<ServiceRegistration>
self{};
long celix_bundleContext_registerServiceWithOptionsAsync(celix_bundle_context_t *ctx, const celix_service_registration_options_t *opts)
Register a service to the Celix framework using the provided service registration options.
long celix_framework_fireGenericEvent(celix_framework_t *fw, long eventId, long bndId, const char *eventName, void *processData, void(*processCallback)(void *data), void *doneData, void(*doneCallback)(void *doneData))
Fire a generic event. The event will be added to the event loop and handled on the event loop thread.
std::shared_ptr< ServiceRegistration > getSelf() const
Returns the shared_ptr of for this object.
Definition: ServiceRegistration.h:247
Service Registration Options when registering services to the Celix framework.
Definition: celix_bundle_context.h:119
void wait() const
If the service registration is REGISTERING or UNREGISTERING, wait until state is REGISTERED OR UNREGI...
Definition: ServiceRegistration.h:171
static std::shared_ptr< ServiceRegistration > create(std::shared_ptr< celix_bundle_context_t > cCtx, std::shared_ptr< void > svc, std::string_view name, std::string_view version, celix::Properties properties, bool registerAsync, bool unregisterAsync, std::vector< std::function< void(ServiceRegistration &)>> onRegisteredCallbacks, std::vector< std::function< void(ServiceRegistration &)>> onUnregisteredCallbacks)
Definition: ServiceRegistration.h:70
void celix_bundleContext_waitForAsyncRegistration(celix_bundle_context_t *ctx, long serviceId)
Waits til the async service registration for the provided serviceId is done.
A registered service.
Definition: ServiceRegistration.h:53
long celix_bundleContext_registerServiceWithOptions(celix_bundle_context_t *ctx, const celix_service_registration_options_t *opts)
Register a service to the Celix framework using the provided service registration options.
void celix_bundleContext_unregisterServiceAsync(celix_bundle_context_t *ctx, long serviceId, void *doneData, void(*doneCallback)(void *doneData))
Unregister the service or service factory with service id.
constexpr const char *const SERVICE_RANKING
Service property (named "service.ranking") identifying a service's ranking number (of type long).
Definition: Constants.h:91
ServiceRegistrationState getState() const
The state of the service registration.
Definition: ServiceRegistration.h:148
long getServiceId() const
The service id for this service registration.
Definition: ServiceRegistration.h:156
const std::string & getServiceName() const
The service name for this service registration.
Definition: ServiceRegistration.h:131
void(* asyncCallback)(void *data, long serviceId) CELIX_OPTS_INIT
Async callback.
Definition: celix_bundle_context.h:199
celix_framework_t * celix_bundleContext_getFramework(const celix_bundle_context_t *ctx)
const celix::Properties & getServiceProperties() const
The service properties for this service registration.
Definition: ServiceRegistration.h:143
long getServiceRanking() const
The service ranking for this service registration.
Definition: ServiceRegistration.h:164
void unregister()
Unregister the service from the Celix framework if the state is REGISTERED.
Definition: ServiceRegistration.h:198
ServiceRegistrationState
Definition: ServiceRegistration.h:36
const std::string & getServiceVersion() const
The service version for this service registration.
Definition: ServiceRegistration.h:138
long celix_bundle_getId(const celix_bundle_t *bnd)
Returns the bundle id.
celix::Properties Properties
Definition: Properties.h:25
Celix runtime Exception.
Definition: Exception.h:28
void celix_bundleContext_waitForAsyncUnregistration(celix_bundle_context_t *ctx, long serviceId)
Waits til the async service unregistration for the provided serviceId is done.
celix_bundle_t * celix_bundleContext_getBundle(const celix_bundle_context_t *ctx)
Returns the bundle for this bundle context.
void celix_bundleContext_unregisterService(celix_bundle_context_t *ctx, long serviceId)
Unregister the service or service factory with service id.