CELIX-237: Added callback from export/import registration to rsa to remove the export/import from internal registration
Project: http://git-wip-us.apache.org/repos/asf/celix/repo Commit: http://git-wip-us.apache.org/repos/asf/celix/commit/89968d93 Tree: http://git-wip-us.apache.org/repos/asf/celix/tree/89968d93 Diff: http://git-wip-us.apache.org/repos/asf/celix/diff/89968d93 Branch: refs/heads/develop Commit: 89968d937fd239b93382d419dd6354469f107bbb Parents: 3853a7c Author: Pepijn Noltes <pepijnnol...@gmail.com> Authored: Wed Sep 2 13:42:50 2015 +0200 Committer: Pepijn Noltes <pepijnnol...@gmail.com> Committed: Wed Sep 2 13:42:50 2015 +0200 ---------------------------------------------------------------------- .../private/include/export_registration_dfi.h | 2 +- .../private/include/import_registration_dfi.h | 4 +- .../private/include/remote_service_admin_dfi.h | 56 +++++++++++++++ .../include/remote_service_admin_http_impl.h | 73 ------------------- .../rsa/private/src/export_registration_dfi.c | 75 +++++++++++++++++--- .../rsa/private/src/import_registration_dfi.c | 23 ++++-- .../src/remote_service_admin_activator.c | 4 +- .../rsa/private/src/remote_service_admin_dfi.c | 11 +-- .../rsa_tst/bundle/tst_activator.c | 2 +- 9 files changed, 153 insertions(+), 97 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/celix/blob/89968d93/remote_services/remote_service_admin_dfi/rsa/private/include/export_registration_dfi.h ---------------------------------------------------------------------- diff --git a/remote_services/remote_service_admin_dfi/rsa/private/include/export_registration_dfi.h b/remote_services/remote_service_admin_dfi/rsa/private/include/export_registration_dfi.h index 4356646..100294c 100644 --- a/remote_services/remote_service_admin_dfi/rsa/private/include/export_registration_dfi.h +++ b/remote_services/remote_service_admin_dfi/rsa/private/include/export_registration_dfi.h @@ -9,7 +9,7 @@ #include "log_helper.h" #include "endpoint_description.h" -celix_status_t exportRegistration_create(log_helper_pt helper, service_reference_pt reference, endpoint_description_pt endpoint, bundle_context_pt context, export_registration_pt *registration); +celix_status_t exportRegistration_create(log_helper_pt helper, void (*closedCallback)(void *handle, export_registration_pt reg), void *handle, service_reference_pt reference, endpoint_description_pt endpoint, bundle_context_pt context, export_registration_pt *registration); void exportRegistration_destroy(export_registration_pt registration); celix_status_t exportRegistration_start(export_registration_pt registration); http://git-wip-us.apache.org/repos/asf/celix/blob/89968d93/remote_services/remote_service_admin_dfi/rsa/private/include/import_registration_dfi.h ---------------------------------------------------------------------- diff --git a/remote_services/remote_service_admin_dfi/rsa/private/include/import_registration_dfi.h b/remote_services/remote_service_admin_dfi/rsa/private/include/import_registration_dfi.h index ec885fd..6f2a232 100644 --- a/remote_services/remote_service_admin_dfi/rsa/private/include/import_registration_dfi.h +++ b/remote_services/remote_service_admin_dfi/rsa/private/include/import_registration_dfi.h @@ -10,7 +10,9 @@ typedef void (*send_func_type)(void *handle, endpoint_description_pt endpointDescription, char *request, char **reply, int* replyStatus); -celix_status_t importRegistration_create(bundle_context_pt context, endpoint_description_pt description, const char *classObject, import_registration_pt *import); +celix_status_t importRegistration_create(bundle_context_pt context, void (*rsaCallback)(void *, import_registration_pt), + void *rsaHandle, endpoint_description_pt description, const char *classObject, + import_registration_pt *import); void importRegistration_destroy(import_registration_pt import); celix_status_t importRegistration_setSendFn(import_registration_pt reg, http://git-wip-us.apache.org/repos/asf/celix/blob/89968d93/remote_services/remote_service_admin_dfi/rsa/private/include/remote_service_admin_dfi.h ---------------------------------------------------------------------- diff --git a/remote_services/remote_service_admin_dfi/rsa/private/include/remote_service_admin_dfi.h b/remote_services/remote_service_admin_dfi/rsa/private/include/remote_service_admin_dfi.h new file mode 100644 index 0000000..64e69e4 --- /dev/null +++ b/remote_services/remote_service_admin_dfi/rsa/private/include/remote_service_admin_dfi.h @@ -0,0 +1,56 @@ +/** + *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. + */ +/* + * remote_service_admin_http_impl.h + * + * \date Sep 30, 2011 + * \author <a href="mailto:d...@celix.apache.org">Apache Celix Project Team</a> + * \copyright Apache License, Version 2.0 + */ + +#ifndef REMOTE_SERVICE_ADMIN_HTTP_IMPL_H_ +#define REMOTE_SERVICE_ADMIN_HTTP_IMPL_H_ + +#include "bundle_context.h" +#include "endpoint_description.h" + +typedef struct remote_service_admin *remote_service_admin_pt; + +celix_status_t remoteServiceAdmin_create(bundle_context_pt context, remote_service_admin_pt *admin); +celix_status_t remoteServiceAdmin_destroy(remote_service_admin_pt *admin); + +celix_status_t remoteServiceAdmin_stop(remote_service_admin_pt admin); + +celix_status_t remoteServiceAdmin_exportService(remote_service_admin_pt admin, char *serviceId, properties_pt properties, array_list_pt *registrations); +celix_status_t remoteServiceAdmin_removeExportedService(remote_service_admin_pt admin, export_registration_pt registration); +celix_status_t remoteServiceAdmin_getExportedServices(remote_service_admin_pt admin, array_list_pt *services); +celix_status_t remoteServiceAdmin_getImportedEndpoints(remote_service_admin_pt admin, array_list_pt *services); +celix_status_t remoteServiceAdmin_importService(remote_service_admin_pt admin, endpoint_description_pt endpoint, import_registration_pt *registration); +celix_status_t remoteServiceAdmin_removeImportedService(remote_service_admin_pt admin, import_registration_pt registration); + + +celix_status_t exportReference_getExportedEndpoint(export_reference_pt reference, endpoint_description_pt *endpoint); +celix_status_t exportReference_getExportedService(export_reference_pt reference); + +celix_status_t importReference_getImportedEndpoint(import_reference_pt reference); +celix_status_t importReference_getImportedService(import_reference_pt reference); + +celix_status_t remoteServiceAdmin_destroyEndpointDescription(endpoint_description_pt *description); + +#endif /* REMOTE_SERVICE_ADMIN_HTTP_IMPL_H_ */ http://git-wip-us.apache.org/repos/asf/celix/blob/89968d93/remote_services/remote_service_admin_dfi/rsa/private/include/remote_service_admin_http_impl.h ---------------------------------------------------------------------- diff --git a/remote_services/remote_service_admin_dfi/rsa/private/include/remote_service_admin_http_impl.h b/remote_services/remote_service_admin_dfi/rsa/private/include/remote_service_admin_http_impl.h deleted file mode 100644 index 65ca83b..0000000 --- a/remote_services/remote_service_admin_dfi/rsa/private/include/remote_service_admin_http_impl.h +++ /dev/null @@ -1,73 +0,0 @@ -/** - *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. - */ -/* - * remote_service_admin_http_impl.h - * - * \date Sep 30, 2011 - * \author <a href="mailto:d...@celix.apache.org">Apache Celix Project Team</a> - * \copyright Apache License, Version 2.0 - */ - -#ifndef REMOTE_SERVICE_ADMIN_HTTP_IMPL_H_ -#define REMOTE_SERVICE_ADMIN_HTTP_IMPL_H_ - -#include "remote_service_admin.h" -#include "log_helper.h" -#include "civetweb.h" - -struct remote_service_admin { - bundle_context_pt context; - log_helper_pt loghelper; - - celix_thread_mutex_t exportedServicesLock; - hash_map_pt exportedServices; - - celix_thread_mutex_t importedServicesLock; - hash_map_pt importedServices; - - char *port; - char *ip; - - struct mg_context *ctx; -}; - - -celix_status_t remoteServiceAdmin_create(bundle_context_pt context, remote_service_admin_pt *admin); -celix_status_t remoteServiceAdmin_destroy(remote_service_admin_pt *admin); - -celix_status_t remoteServiceAdmin_stop(remote_service_admin_pt admin); -celix_status_t remoteServiceAdmin_send(remote_service_admin_pt rsa, endpoint_description_pt endpointDescription, char *methodSignature, char **reply, int* replyStatus); - -celix_status_t remoteServiceAdmin_exportService(remote_service_admin_pt admin, char *serviceId, properties_pt properties, array_list_pt *registrations); -celix_status_t remoteServiceAdmin_removeExportedService(export_registration_pt registration); -celix_status_t remoteServiceAdmin_getExportedServices(remote_service_admin_pt admin, array_list_pt *services); -celix_status_t remoteServiceAdmin_getImportedEndpoints(remote_service_admin_pt admin, array_list_pt *services); -celix_status_t remoteServiceAdmin_importService(remote_service_admin_pt admin, endpoint_description_pt endpoint, import_registration_pt *registration); -celix_status_t remoteServiceAdmin_removeImportedService(remote_service_admin_pt admin, import_registration_pt registration); - - -celix_status_t exportReference_getExportedEndpoint(export_reference_pt reference, endpoint_description_pt *endpoint); -celix_status_t exportReference_getExportedService(export_reference_pt reference); - -celix_status_t importReference_getImportedEndpoint(import_reference_pt reference); -celix_status_t importReference_getImportedService(import_reference_pt reference); - -celix_status_t remoteServiceAdmin_destroyEndpointDescription(endpoint_description_pt *description); - -#endif /* REMOTE_SERVICE_ADMIN_HTTP_IMPL_H_ */ http://git-wip-us.apache.org/repos/asf/celix/blob/89968d93/remote_services/remote_service_admin_dfi/rsa/private/src/export_registration_dfi.c ---------------------------------------------------------------------- diff --git a/remote_services/remote_service_admin_dfi/rsa/private/src/export_registration_dfi.c b/remote_services/remote_service_admin_dfi/rsa/private/src/export_registration_dfi.c index 85ecb43..b5f0fec 100644 --- a/remote_services/remote_service_admin_dfi/rsa/private/src/export_registration_dfi.c +++ b/remote_services/remote_service_admin_dfi/rsa/private/src/export_registration_dfi.c @@ -5,7 +5,9 @@ #include <dyn_interface.h> #include <json_serializer.h> #include <remote_constants.h> -#include "export_registration.h" +#include <remote_service_admin.h> +#include <service_tracker_customizer.h> +#include <service_tracker.h> #include "export_registration_dfi.h" struct export_reference { @@ -15,28 +17,50 @@ struct export_reference { struct export_registration { bundle_context_pt context; + void (*rsaCloseExportCallback)(void *handle, export_registration_pt reg); + void *handle; struct export_reference exportReference; - void *service; + char *servId; dyn_interface_type *intf; //owner + service_tracker_pt tracker; + + celix_thread_mutex_t mutex; + void *service; //protected by mutex //TODO add tracker and lock bool closed; }; -celix_status_t exportRegistration_create(log_helper_pt helper, service_reference_pt reference, endpoint_description_pt endpoint, bundle_context_pt context, export_registration_pt *out) { +static void exportRegistration_addServ(export_registration_pt reg, service_reference_pt ref, void *service); +static void exportRegistration_removeServ(export_registration_pt reg, service_reference_pt ref, void *service); + +celix_status_t exportRegistration_create(log_helper_pt helper, void (*closedCallback)(void *handle, export_registration_pt reg), void *handle, service_reference_pt reference, endpoint_description_pt endpoint, bundle_context_pt context, export_registration_pt *out) { celix_status_t status = CELIX_SUCCESS; - export_registration_pt reg = calloc(1, sizeof(*reg)); + char *servId = NULL; + status = serviceReference_getProperty(reference, "service.id", &servId); + if (status != CELIX_SUCCESS) { + logHelper_log(helper, OSGI_LOGSERVICE_WARNING, "Cannot find service.id for ref"); + } - if (reg == NULL) { - status = CELIX_ENOMEM; + export_registration_pt reg = NULL; + if (status == CELIX_SUCCESS) { + reg = calloc(1, sizeof(*reg)); + if (reg == NULL) { + status = CELIX_ENOMEM; + } } + if (status == CELIX_SUCCESS) { reg->context = context; + reg->rsaCloseExportCallback = closedCallback; + reg->handle = handle; reg->exportReference.endpoint = endpoint; reg->exportReference.reference = reference; reg->closed = false; + + celixThreadMutex_create(®->mutex, NULL); } char *exports = NULL; @@ -75,6 +99,17 @@ celix_status_t exportRegistration_create(log_helper_pt helper, service_reference } if (status == CELIX_SUCCESS) { + service_tracker_customizer_pt cust = NULL; + status = serviceTrackerCustomizer_create(reg, NULL, exportRegistration_addServ, NULL, + exportRegistration_removeServ, &cust); + if (status == CELIX_SUCCESS) { + char filter[32]; + snprintf(filter, 32, "(service.id=%s)", servId); + status = serviceTracker_createWithFilter(reg->context, filter, cust, ®->tracker); + } + } + + if (status == CELIX_SUCCESS) { *out = reg; } else { logHelper_log(helper, OSGI_LOGSERVICE_ERROR, "Error creating export registration"); @@ -88,9 +123,9 @@ celix_status_t exportRegistration_call(export_registration_pt export, char *data int status = CELIX_SUCCESS; *responseLength = -1; - //TODO lock service + celixThreadMutex_lock(&export->mutex); status = jsonSerializer_call(export->intf, export->service, data, responseOut); - //TODO unlock service + celixThreadMutex_unlock(&export->mutex); return status; } @@ -108,6 +143,10 @@ void exportRegistration_destroy(export_registration_pt reg) { reg->exportReference.endpoint = NULL; endpointDescription_destroy(ep); } + if (reg->tracker != NULL) { + serviceTracker_destroy(reg->tracker); + } + celixThreadMutex_destroy(®->mutex); free(reg); } @@ -115,10 +154,25 @@ void exportRegistration_destroy(export_registration_pt reg) { celix_status_t exportRegistration_start(export_registration_pt reg) { celix_status_t status = CELIX_SUCCESS; - status = bundleContext_getService(reg->context, reg->exportReference.reference, ®->service); //TODO use tracker + + serviceTracker_open(reg->tracker); return status; } +static void exportRegistration_addServ(export_registration_pt reg, service_reference_pt ref, void *service) { + celixThreadMutex_lock(®->mutex); + reg->service = service; + celixThreadMutex_unlock(®->mutex); +} + +static void exportRegistration_removeServ(export_registration_pt reg, service_reference_pt ref, void *service) { + celixThreadMutex_lock(®->mutex); + if (reg->service == service) { + reg->service == NULL; + } + celixThreadMutex_unlock(®->mutex); +} + celix_status_t exportRegistration_stop(export_registration_pt reg) { celix_status_t status = CELIX_SUCCESS; status = bundleContext_ungetService(reg->context, reg->exportReference.reference, NULL); @@ -128,7 +182,7 @@ celix_status_t exportRegistration_stop(export_registration_pt reg) { celix_status_t exportRegistration_close(export_registration_pt reg) { celix_status_t status = CELIX_SUCCESS; exportRegistration_stop(reg); - //TODO callback to rsa to remove from list + reg->rsaCloseExportCallback(reg->handle, reg); return status; } @@ -146,7 +200,6 @@ celix_status_t exportRegistration_getExportReference(export_registration_pt regi ref->reference = registration->exportReference.reference; } else { status = CELIX_ENOMEM; - //TODO log } if (status == CELIX_SUCCESS) { http://git-wip-us.apache.org/repos/asf/celix/blob/89968d93/remote_services/remote_service_admin_dfi/rsa/private/src/import_registration_dfi.c ---------------------------------------------------------------------- diff --git a/remote_services/remote_service_admin_dfi/rsa/private/src/import_registration_dfi.c b/remote_services/remote_service_admin_dfi/rsa/private/src/import_registration_dfi.c index 09ad25b..9667fa9 100644 --- a/remote_services/remote_service_admin_dfi/rsa/private/src/import_registration_dfi.c +++ b/remote_services/remote_service_admin_dfi/rsa/private/src/import_registration_dfi.c @@ -16,6 +16,9 @@ struct import_registration { service_registration_pt factoryReg; hash_map_pt proxies; //key -> bundle, value -> service_proxy + + void (*rsaCloseImportCallback)(void *, import_registration_pt); + void *rsaHandle; }; struct service_proxy { @@ -29,7 +32,9 @@ static celix_status_t importRegistration_createProxy(import_registration_pt impo static void importRegistration_proxyFunc(void *userData, void *args[], void *returnVal); static void importRegistration_destroyProxy(struct service_proxy *proxy); -celix_status_t importRegistration_create(bundle_context_pt context, endpoint_description_pt endpoint, const char *classObject, import_registration_pt *out) { +celix_status_t importRegistration_create(bundle_context_pt context, void (*rsaCallback)(void *, import_registration_pt), + void *rsaHandle, endpoint_description_pt endpoint, const char *classObject, + import_registration_pt *out) { celix_status_t status = CELIX_SUCCESS; import_registration_pt reg = calloc(1, sizeof(*reg)); @@ -39,6 +44,8 @@ celix_status_t importRegistration_create(bundle_context_pt context, endpoint_des if (reg != NULL && reg->factory != NULL) { reg->context = context; + reg->rsaCloseImportCallback = rsaCallback; + reg->rsaHandle = rsaHandle; reg->endpoint = endpoint; reg->classObject = classObject; reg->proxies = hashMap_create(NULL, NULL, NULL, NULL); @@ -97,7 +104,7 @@ celix_status_t importRegistration_stop(import_registration_pt import) { if (import->factoryReg != NULL) { serviceRegistration_unregister(import->factoryReg); } - //TODO unregister every serv instance? + //TODO unregister every serv instance? Needed for factory? return status; } @@ -273,13 +280,21 @@ celix_status_t importRegistration_ungetService(import_registration_pt import, bu } static void importRegistration_destroyProxy(struct service_proxy *proxy) { - //TODO + if (proxy != NULL) { + if (proxy->intf != NULL) { + dynInterface_destroy(proxy->intf); + } + if (proxy->service != NULL) { + free(proxy->service); + } + free(proxy); + } } celix_status_t importRegistration_close(import_registration_pt registration) { celix_status_t status = CELIX_SUCCESS; - //TODO + registration->rsaCloseImportCallback(registration->rsaHandle, registration); return status; } http://git-wip-us.apache.org/repos/asf/celix/blob/89968d93/remote_services/remote_service_admin_dfi/rsa/private/src/remote_service_admin_activator.c ---------------------------------------------------------------------- diff --git a/remote_services/remote_service_admin_dfi/rsa/private/src/remote_service_admin_activator.c b/remote_services/remote_service_admin_dfi/rsa/private/src/remote_service_admin_activator.c index 9961a9b..40fb288 100644 --- a/remote_services/remote_service_admin_dfi/rsa/private/src/remote_service_admin_activator.c +++ b/remote_services/remote_service_admin_dfi/rsa/private/src/remote_service_admin_activator.c @@ -24,11 +24,13 @@ * \copyright Apache License, Version 2.0 */ #include <stdlib.h> +#include <remote_service_admin.h> + +#include "remote_service_admin_dfi.h" #include "bundle_activator.h" #include "service_registration.h" -#include "remote_service_admin_http_impl.h" #include "export_registration_dfi.h" #include "import_registration_dfi.h" http://git-wip-us.apache.org/repos/asf/celix/blob/89968d93/remote_services/remote_service_admin_dfi/rsa/private/src/remote_service_admin_dfi.c ---------------------------------------------------------------------- diff --git a/remote_services/remote_service_admin_dfi/rsa/private/src/remote_service_admin_dfi.c b/remote_services/remote_service_admin_dfi/rsa/private/src/remote_service_admin_dfi.c index a092a63..00a4b0f 100644 --- a/remote_services/remote_service_admin_dfi/rsa/private/src/remote_service_admin_dfi.c +++ b/remote_services/remote_service_admin_dfi/rsa/private/src/remote_service_admin_dfi.c @@ -34,13 +34,14 @@ #include <curl/curl.h> #include <jansson.h> -#include <json_serializer.h> +#include "json_serializer.h" +#include "remote_service_admin.h" #include "import_registration_dfi.h" #include "export_registration_dfi.h" +#include "remote_service_admin_dfi.h" #include "dyn_interface.h" -#include "remote_service_admin.h" #include "remote_constants.h" #include "constants.h" #include "civetweb.h" @@ -412,7 +413,7 @@ celix_status_t remoteServiceAdmin_exportService(remote_service_admin_pt admin, c remoteServiceAdmin_createEndpointDescription(admin, reference, interface, &endpoint); printf("RSA: Creating export registration with endpoint pointer %p\n", endpoint); //TOOD precheck if descriptor exists - status = exportRegistration_create(admin->loghelper, reference, endpoint, admin->context, ®istration); + status = exportRegistration_create(admin->loghelper, remoteServiceAdmin_removeExportedService, admin, reference, endpoint, admin->context, ®istration); if (status == CELIX_SUCCESS) { status = exportRegistration_start(registration); if (status == CELIX_SUCCESS) { @@ -431,7 +432,7 @@ celix_status_t remoteServiceAdmin_exportService(remote_service_admin_pt admin, c return status; } -celix_status_t remoteServiceAdmin_removeExportedService(export_registration_pt registration) { +celix_status_t remoteServiceAdmin_removeExportedService(remote_service_admin_pt admin, export_registration_pt registration) { celix_status_t status = CELIX_SUCCESS; //TODO /* @@ -580,7 +581,7 @@ celix_status_t remoteServiceAdmin_importService(remote_service_admin_pt admin, e import_registration_pt import = NULL; if (objectClass != NULL) { - status = importRegistration_create(admin->context, endpointDescription, objectClass, &import); + status = importRegistration_create(admin->context, NULL, NULL, endpointDescription, objectClass, &import); } if (status == CELIX_SUCCESS) { importRegistration_setSendFn(import, remoteServiceAdmin_send, admin); http://git-wip-us.apache.org/repos/asf/celix/blob/89968d93/remote_services/remote_service_admin_dfi/rsa_tst/bundle/tst_activator.c ---------------------------------------------------------------------- diff --git a/remote_services/remote_service_admin_dfi/rsa_tst/bundle/tst_activator.c b/remote_services/remote_service_admin_dfi/rsa_tst/bundle/tst_activator.c index 1f72db1..c067abf 100644 --- a/remote_services/remote_service_admin_dfi/rsa_tst/bundle/tst_activator.c +++ b/remote_services/remote_service_admin_dfi/rsa_tst/bundle/tst_activator.c @@ -125,7 +125,7 @@ static int test(void *handle) { int rc; if (act->calc != NULL) { rc = act->calc->sqrt(act->calc->calculator, 4, &result); - printf("calc result is %d\n", result); + printf("calc result is %f\n", result); } else { printf("calc not ready\n"); }