xuzhenbao commented on code in PR #495:
URL: https://github.com/apache/celix/pull/495#discussion_r1132654747


##########
bundles/remote_services/discovery_zeroconf/src/discovery_zeroconf_announcer.c:
##########
@@ -0,0 +1,533 @@
+/*
+ * 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 <discovery_zeroconf_announcer.h>
+#include <discovery_zeroconf_constants.h>
+#include <dns_sd.h>
+#include <endpoint_listener.h>
+#include <remote_constants.h>
+#include <celix_utils.h>
+#include <celix_properties.h>
+#include <celix_constants.h>
+#include <celix_threads.h>
+#include <celix_bundle_context.h>
+#include <celix_string_hash_map.h>
+#include <celix_array_list.h>
+#include <celix_log_helper.h>
+#include <celix_types.h>
+#include <celix_errno.h>
+#include <celix_build_assert.h>
+#include <netinet/in.h>
+#include <net/if.h>
+#include <sys/eventfd.h>
+#include <sys/select.h>
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stddef.h>
+#include <string.h>
+#include <errno.h>
+
+
+#define DZC_MAX_CONFLICT_CNT 256
+
+//According to rfc6763, Using TXT records larger than 1300 bytes is NOT 
RECOMMENDED
+#define DZC_MAX_TXT_RECORD_SIZE 1300
+
+struct discovery_zeroconf_announcer {
+    celix_bundle_context_t *ctx;
+    celix_log_helper_t *logHelper;
+    char fwUuid[64];
+    endpoint_listener_t epListener;
+    long epListenerSvcId;
+    DNSServiceRef sharedRef;
+    int eventFd;
+    celix_thread_t refreshEPThread;
+    celix_thread_mutex_t mutex;//projects below
+    bool running;
+    celix_string_hash_map_t *endpoints;//key:endpoint id, 
val:announce_endpoint_entry_t*
+    celix_array_list_t *revokedEndpoints;
+};
+
+typedef struct announce_endpoint_entry {
+    celix_properties_t *properties;
+    DNSServiceRef registerRef;
+    unsigned int uid;
+    int ifIndex;
+    const char *serviceName;
+    char serviceType[64];
+    bool announced;
+}announce_endpoint_entry_t;
+
+
+static void 
discoveryZeroconfAnnouncer_eventNotify(discovery_zeroconf_announcer_t 
*announcer);
+static  celix_status_t discoveryZeroconfAnnouncer_endpointAdded(void *handle, 
endpoint_description_t *endpoint, char *matchedFilter);
+static celix_status_t discoveryZeroconfAnnouncer_endpointRemoved(void *handle, 
endpoint_description_t *endpoint, char *matchedFilter);
+static void *discoveryZeroconfAnnouncer_refreshEndpointThread(void *data);
+
+
+celix_status_t discoveryZeroconfAnnouncer_create(celix_bundle_context_t *ctx, 
celix_log_helper_t *logHelper, discovery_zeroconf_announcer_t **announcerOut) {
+    celix_status_t status = CELIX_SUCCESS;
+    discovery_zeroconf_announcer_t *announcer = calloc(1, sizeof(*announcer));
+    assert(announcer != NULL);
+    announcer->ctx = ctx;
+    announcer->logHelper = logHelper;
+    announcer->sharedRef = NULL;
+
+    announcer->eventFd = eventfd(0, 0);
+    if (announcer->eventFd < 0) {
+        status = CELIX_ERROR_MAKE(CELIX_FACILITY_CERRNO, errno);
+        celix_logHelper_fatal(logHelper, "Announcer: Failed to open event fd, 
%d.", errno);
+        goto eventfd_err;
+    }
+
+    status = celixThreadMutex_create(&announcer->mutex, NULL);
+    if (status != CELIX_SUCCESS) {
+        celix_logHelper_fatal(logHelper, "Announcer: Failed to create mutex, 
%d.", status);
+        goto mutex_err;
+    }
+
+    announcer->endpoints = celix_stringHashMap_create();
+    assert(announcer->endpoints != NULL);
+    announcer->revokedEndpoints = celix_arrayList_create();
+    assert(announcer->revokedEndpoints != NULL);
+
+    const char *fwUuid = celix_bundleContext_getProperty(ctx, 
OSGI_FRAMEWORK_FRAMEWORK_UUID, NULL);
+    if (fwUuid == NULL || strlen(fwUuid) >= sizeof(announcer->fwUuid)) {
+        status = CELIX_BUNDLE_EXCEPTION;
+        celix_logHelper_fatal(logHelper, "Announcer: Failed to get framework 
uuid.");
+        goto fw_uuid_err;
+    }
+    strcpy(announcer->fwUuid, fwUuid);
+
+    announcer->epListener.handle = announcer;
+    announcer->epListener.endpointAdded = 
discoveryZeroconfAnnouncer_endpointAdded;
+    announcer->epListener.endpointRemoved = 
discoveryZeroconfAnnouncer_endpointRemoved;
+    char scope[256] = {0};
+    (void)snprintf(scope, sizeof(scope), "(&(%s=*)(%s=%s))", 
OSGI_FRAMEWORK_OBJECTCLASS, OSGI_RSA_ENDPOINT_FRAMEWORK_UUID, fwUuid);
+
+    celix_properties_t *props = celix_properties_create();
+    assert(props != NULL);
+    celix_properties_set(props, "DISCOVERY", "true");
+    celix_properties_set(props, (char *) OSGI_ENDPOINT_LISTENER_SCOPE, scope);
+    celix_service_registration_options_t opt = 
CELIX_EMPTY_SERVICE_REGISTRATION_OPTIONS;
+    opt.serviceName = OSGI_ENDPOINT_LISTENER_SERVICE;
+    opt.properties = props;
+    opt.svc = &announcer->epListener;
+    announcer->epListenerSvcId = 
celix_bundleContext_registerServiceWithOptionsAsync(ctx, &opt);
+    if (announcer->epListenerSvcId < 0) {
+        status = CELIX_BUNDLE_EXCEPTION;
+        celix_logHelper_fatal(logHelper, "Announcer: Failed to register 
endpoint listener.");
+        goto ep_listener_err;
+    }
+
+    announcer->running = true;
+    status = celixThread_create(&announcer->refreshEPThread, NULL, 
discoveryZeroconfAnnouncer_refreshEndpointThread, announcer);
+    if (status != CELIX_SUCCESS) {
+        celix_logHelper_fatal(logHelper, "Announcer: Failed to create 
refreshing endpoint thread, %d.", status);
+        goto announce_ep_thread_err;
+    }
+    celixThread_setName(&announcer->refreshEPThread, "DiscAnnouncer");
+
+    *announcerOut = announcer;
+    return CELIX_SUCCESS;
+announce_ep_thread_err:
+    celix_bundleContext_unregisterServiceAsync(ctx, 
announcer->epListenerSvcId, NULL, NULL);
+    celix_bundleContext_waitForAsyncUnregistration(ctx, 
announcer->epListenerSvcId);
+ep_listener_err:
+fw_uuid_err:
+    celix_arrayList_destroy(announcer->revokedEndpoints);
+    celix_stringHashMap_destroy(announcer->endpoints);
+    celixThreadMutex_destroy(&announcer->mutex);
+mutex_err:
+    close(announcer->eventFd);
+eventfd_err:
+    free(announcer);
+    return status;
+}
+
+void discoveryZeroconfAnnouncer_destroy(discovery_zeroconf_announcer_t 
*announcer) {
+    celixThreadMutex_lock(&announcer->mutex);
+    announcer->running= false;
+    celixThreadMutex_unlock(&announcer->mutex);
+    discoveryZeroconfAnnouncer_eventNotify(announcer);
+    celixThread_join(announcer->refreshEPThread, NULL);
+    celix_bundleContext_unregisterServiceAsync(announcer->ctx, 
announcer->epListenerSvcId, NULL, NULL);
+    celix_bundleContext_waitForAsyncUnregistration(announcer->ctx, 
announcer->epListenerSvcId);
+
+    announce_endpoint_entry_t *entry = NULL;
+    int size = celix_arrayList_size(announcer->revokedEndpoints);
+    for (int i = 0; i < size; ++i) {
+        entry = (announce_endpoint_entry_t 
*)celix_arrayList_get(announcer->revokedEndpoints, i);
+        celix_properties_destroy(entry->properties);
+        free(entry);
+    }
+    celix_arrayList_destroy(announcer->revokedEndpoints);
+
+    CELIX_STRING_HASH_MAP_ITERATE(announcer->endpoints,iter) {
+        entry = (announce_endpoint_entry_t *) iter.value.ptrValue;
+        celix_properties_destroy(entry->properties);
+        free(entry);
+    }
+    celix_stringHashMap_destroy(announcer->endpoints);
+
+    celixThreadMutex_destroy(&announcer->mutex);
+    close(announcer->eventFd);
+    free(announcer);
+    return;
+}
+
+static void 
discoveryZeroconfAnnouncer_eventNotify(discovery_zeroconf_announcer_t 
*announcer) {
+    eventfd_t val = 1;
+    eventfd_write(announcer->eventFd, val);
+    return;
+}
+
+static bool isLoopBackNetInterface(int ifIndex) {
+    if (ifIndex <= 0) {
+        return false;
+    }
+    bool loopBack = false;
+    int fd = socket(AF_INET, SOCK_DGRAM, 0);
+    if (fd >= 0) {
+        struct ifreq ifr;
+        memset(&ifr, 0, sizeof(ifr));
+        if (if_indextoname((unsigned int)ifIndex, ifr.ifr_name) != NULL) {
+            if (ioctl(fd, SIOCGIFFLAGS, &ifr) == 0) {
+                loopBack = !!(ifr.ifr_ifru.ifru_flags & IFF_LOOPBACK);
+            }
+        }
+        close(fd);
+    }
+    return loopBack;
+}
+
+static  celix_status_t discoveryZeroconfAnnouncer_endpointAdded(void *handle, 
endpoint_description_t *endpoint, char *matchedFilter) {
+    (void)matchedFilter;//unused
+    celix_status_t status = CELIX_SUCCESS;
+    discovery_zeroconf_announcer_t *announcer = 
(discovery_zeroconf_announcer_t *)handle;
+    assert(announcer != NULL);
+    if (endpointDescription_isInvalid(endpoint)) {
+        celix_logHelper_error(announcer->logHelper, "Announcer: Endpoint is 
invalid.");
+        return CELIX_ILLEGAL_ARGUMENT;
+    }
+
+    celix_logHelper_info(announcer->logHelper, "Announcer: Add endpoint for 
%s(%s).", endpoint->serviceName, endpoint->id);
+
+    announce_endpoint_entry_t *entry = calloc(1, sizeof(*entry));
+    assert(entry != NULL);
+    entry->registerRef = NULL;
+    entry->announced = false;
+    entry->uid = celix_utils_stringHash(endpoint->id);

Review Comment:
   To avoid mDNS service instance name conflicts on localonly interface, in the 
code, the instance name consists of the service name and the UID. But the 
maximum size of an mDNS instance name is 64 bytes, it is not enough to hold the 
service name and  "endpoint->id". Therefore, we use the hash of "endpoint->id".
   
   We can also just set the  "endpoint->id" to the mDNS service instance, but 
it may affect the readability of the mDNS service instance.



-- 
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.

To unsubscribe, e-mail: dev-unsubscr...@celix.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to