xuzhenbao commented on code in PR #495: URL: https://github.com/apache/celix/pull/495#discussion_r1146327599
########## bundles/remote_services/discovery_zeroconf/src/discovery_zeroconf_announcer.c: ########## @@ -0,0 +1,547 @@ +/* + * 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 = (discovery_zeroconf_announcer_t *)calloc(1, sizeof(*announcer)); + if (announcer == NULL) { + celix_logHelper_fatal(logHelper, "Announcer: Failed to alloc announcer."); + status = CELIX_ENOMEM; + goto announcer_err; + } + 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"); Review Comment: Thanks for your suggestion. I prefer to improve this with a new PR. -- 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