From dev-return-1797-apmail-celix-dev-archive=celix.apache.org@celix.apache.org Mon Mar 30 12:04:11 2020 Return-Path: X-Original-To: apmail-celix-dev-archive@www.apache.org Delivered-To: apmail-celix-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by minotaur.apache.org (Postfix) with SMTP id 996B5190D8 for ; Mon, 30 Mar 2020 12:04:11 +0000 (UTC) Received: (qmail 98670 invoked by uid 500); 30 Mar 2020 12:04:11 -0000 Delivered-To: apmail-celix-dev-archive@celix.apache.org Received: (qmail 98618 invoked by uid 500); 30 Mar 2020 12:04:11 -0000 Mailing-List: contact dev-help@celix.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@celix.apache.org Delivered-To: mailing list dev@celix.apache.org Received: (qmail 98542 invoked by uid 99); 30 Mar 2020 12:04:10 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 30 Mar 2020 12:04:10 +0000 From: GitBox To: dev@celix.apache.org Subject: [GitHub] [celix] pnoltes commented on a change in pull request #174: Feature/pubsub inteceptors Message-ID: <158556985073.14398.11816166341168737069.gitbox@gitbox.apache.org> References: In-Reply-To: Date: Mon, 30 Mar 2020 12:04:10 -0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit pnoltes commented on a change in pull request #174: Feature/pubsub inteceptors URL: https://github.com/apache/celix/pull/174#discussion_r400134468 ########## File path: bundles/pubsub/pubsub_spi/src/pubsub_interceptors_handler.c ########## @@ -0,0 +1,174 @@ +/* + * 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 "celix_bundle_context.h" +#include "celix_constants.h" +#include "utils.h" + +#include "pubsub_interceptors_handler.h" + +typedef struct entry { + const celix_properties_t *properties; + pubsub_interceptor_t *interceptor; +} entry_t; + +struct pubsub_interceptors_handler { + pubsub_interceptor_properties_t properties; + + celix_array_list_t *interceptors; + + long interceptorsTrackerId; + + celix_bundle_context_t *ctx; + + celix_thread_mutex_t mutex; +}; + +static int referenceCompare(const void *a, const void *b); + +static void pubsubInterceptorsHandler_addInterceptor(void *handle, void *svc, const celix_properties_t *props); +static void pubsubInterceptorsHandler_removeInterceptor(void *handle, void *svc, const celix_properties_t *props); + +celix_status_t pubsubInterceptorsHandler_create(celix_bundle_context_t *ctx, const char *scope, const char *topic, pubsub_interceptors_handler_t **handler) { + celix_status_t status = CELIX_SUCCESS; + + *handler = calloc(1, sizeof(**handler)); + if (!*handler) { + status = CELIX_ENOMEM; + } else { + (*handler)->ctx = ctx; + + (*handler)->properties.scope = scope; + (*handler)->properties.topic = topic; + + (*handler)->interceptors = celix_arrayList_create(); + + celixThreadMutex_create(&(*handler)->mutex, NULL); + + // Create service tracker here, and not in the activator + celix_service_tracking_options_t opts = CELIX_EMPTY_SERVICE_TRACKING_OPTIONS; + opts.filter.serviceName = PUBSUB_INTERCEPTOR_SERVICE_NAME; + opts.filter.ignoreServiceLanguage = true; + opts.callbackHandle = *handler; + opts.addWithProperties = pubsubInterceptorsHandler_addInterceptor; + opts.removeWithProperties = pubsubInterceptorsHandler_removeInterceptor; + (*handler)->interceptorsTrackerId = celix_bundleContext_trackServicesWithOptions(ctx, &opts); + } + + return status; +} + +celix_status_t pubsubInterceptorsHandler_destroy(pubsub_interceptors_handler_t *handler) { + celix_bundleContext_stopTracker(handler->ctx, handler->interceptorsTrackerId); + + free(handler->interceptors); + free(handler); + + return CELIX_SUCCESS; +} + +void pubsubInterceptorsHandler_addInterceptor(void *handle, void *svc, const celix_properties_t *props) { + pubsub_interceptors_handler_t *handler = handle; + celixThreadMutex_lock(&handler->mutex); + + bool exists = false; + for (int i = 0; i < arrayList_size(handler->interceptors); i++) { + entry_t *entry = arrayList_get(handler->interceptors, i); + if (entry->interceptor == svc) { + exists = true; + } + } + if (!exists) { + entry_t *entry = calloc(1, sizeof(*entry)); + entry->properties = props; + entry->interceptor = svc; + celix_arrayList_add(handler->interceptors, entry); + + celix_arrayList_sort(handler->interceptors, referenceCompare); + } + + celixThreadMutex_unlock(&handler->mutex); +} + +void pubsubInterceptorsHandler_removeInterceptor(void *handle, void *svc, __attribute__((unused)) const celix_properties_t *props) { + pubsub_interceptors_handler_t *handler = handle; + for (int i = 0; i < arrayList_size(handler->interceptors); i++) { + entry_t *entry = arrayList_get(handler->interceptors, i); + if (entry->interceptor == svc) { + arrayList_remove(handler->interceptors, i); + break; + } + } +} + +bool pubsubInterceptorHandler_invokePreSend(pubsub_interceptors_handler_t *handler, const char *messageType, const uint32_t messageId, const void *message, celix_properties_t *metadata) { + bool cont = true; + for (int i = arrayList_size(handler->interceptors) - 1; i >= 0; i--) { + entry_t *entry = arrayList_get(handler->interceptors, i); + + cont = entry->interceptor->preSend(entry->interceptor->handle, handler->properties, messageType, messageId, message, metadata); + if (!cont) { + break; + } + } + + return cont; +} + +void pubsubInterceptorHandler_invokePostSend(pubsub_interceptors_handler_t *handler, const char *messageType, const uint32_t messageId, const void *message, celix_properties_t *metadata) { + for (int i = arrayList_size(handler->interceptors) - 1; i >= 0; i--) { + entry_t *entry = arrayList_get(handler->interceptors, i); + + entry->interceptor->postSend(entry->interceptor->handle, handler->properties, messageType, messageId, message, metadata); + } +} + +bool pubsubInterceptorHandler_invokePreReceive(pubsub_interceptors_handler_t *handler, const char *messageType, const uint32_t messageId, const void *message, celix_properties_t *metadata) { + bool cont = true; Review comment: Are we sure that the metadata is not NULL? If so, I expected this to be an optimalization in the wire protocol that is there is no metadata (or a configuration is added to skip the metadata) that no celix_properties will be created. And then in the interceptor handler only create a empty metadata if there a interceptors registered. This way we can keep the overhead to a minimum if there is no metadata / interceptors see als invokePreSend ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: users@infra.apache.org With regards, Apache Git Services