From 813bd77127ccd03400b6c328863c26bac1ca8e48 Mon Sep 17 00:00:00 2001 From: PengZheng Date: Sat, 14 Oct 2023 16:56:07 +0800 Subject: [PATCH] Remove broken discovery_shm implementation and remove rsa_shm from documentation. --- .github/workflows/coverity-scan.yml | 1 - .github/workflows/ubuntu.yml | 1 - bundles/remote_services/CMakeLists.txt | 5 - bundles/remote_services/README.md | 18 -- .../discovery_shm/CMakeLists.txt | 46 --- .../discovery_shm/src/discovery_impl.c | 164 ---------- .../discovery_shm/src/discovery_impl.h | 53 ---- .../discovery_shm/src/discovery_shm.c | 281 ------------------ .../discovery_shm/src/discovery_shm.h | 53 ---- .../discovery_shm/src/discovery_shmWatcher.c | 255 ---------------- .../discovery_shm/src/discovery_shmWatcher.h | 40 --- 11 files changed, 917 deletions(-) delete mode 100644 bundles/remote_services/discovery_shm/CMakeLists.txt delete mode 100644 bundles/remote_services/discovery_shm/src/discovery_impl.c delete mode 100644 bundles/remote_services/discovery_shm/src/discovery_impl.h delete mode 100644 bundles/remote_services/discovery_shm/src/discovery_shm.c delete mode 100644 bundles/remote_services/discovery_shm/src/discovery_shm.h delete mode 100644 bundles/remote_services/discovery_shm/src/discovery_shmWatcher.c delete mode 100644 bundles/remote_services/discovery_shm/src/discovery_shmWatcher.h diff --git a/.github/workflows/coverity-scan.yml b/.github/workflows/coverity-scan.yml index eef249268..6baceb7b5 100644 --- a/.github/workflows/coverity-scan.yml +++ b/.github/workflows/coverity-scan.yml @@ -46,7 +46,6 @@ jobs: -DBUILD_EXPERIMENTAL=ON -DENABLE_TESTING=ON -DRSA_JSON_RPC=ON - -DRSA_SHM=ON -DRSA_REMOTE_SERVICE_ADMIN_SHM_V2=ON -DSHELL_BONJOUR=ON run: | diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index 694c595bf..a42f0774c 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -144,7 +144,6 @@ jobs: -DBUILD_EXPERIMENTAL=ON -DENABLE_TESTING=ON -DRSA_JSON_RPC=ON - -DRSA_SHM=ON -DRSA_REMOTE_SERVICE_ADMIN_SHM_V2=ON -DENABLE_TESTING_ON_CI=ON -DCMAKE_BUILD_TYPE=${{ matrix.type }} diff --git a/bundles/remote_services/CMakeLists.txt b/bundles/remote_services/CMakeLists.txt index 250abc9c8..1d069192f 100644 --- a/bundles/remote_services/CMakeLists.txt +++ b/bundles/remote_services/CMakeLists.txt @@ -31,11 +31,6 @@ if (REMOTE_SERVICE_ADMIN) add_subdirectory(rsa_rpc_json) add_subdirectory(remote_service_admin_shm_v2) - celix_subproject(RSA_SHM "Option to enable building the Discovery (SHM) bundle" OFF) - if (RSA_SHM) - add_subdirectory(discovery_shm) - endif () - if (BUILD_RSA_DISCOVERY_ETCD AND BUILD_RSA_REMOTE_SERVICE_ADMIN_DFI AND BUILD_SHELL AND BUILD_SHELL_TUI AND BUILD_LOG_SERVICE AND BUILD_LAUNCHER) add_celix_container(remote-services-dfi LAUNCHER Celix::launcher diff --git a/bundles/remote_services/README.md b/bundles/remote_services/README.md index f09869b64..eaf7bddc4 100644 --- a/bundles/remote_services/README.md +++ b/bundles/remote_services/README.md @@ -54,26 +54,10 @@ Provides a RSA implementation that uses JSON to marshal requests and HTTP as tra | **Configuration** | `RSA_PORT`: defines the port on which the HTTP server should listen for incoming requests. Defaults to port `8888`; | | | `ENDPOINTS`: defines the location in which service endpoints and/or proxies can be found. Defaults to `endpoints` in the current working directory | -#### Shared memory (SHM) - -Provides a RSA implementation that uses shared memory for its remote method invocation. Note that this only works when all remote services are located on the same machine. - -| **Bundle** | `remote_service_admin_shm.zip` | -|--|--| -| **Configuration** | `ENDPOINTS`: defines the location in which service endpoints and/or proxies can be found. Defaults to `endpoints` in the current working directory | - ### Discovery Actively discovers the presence of remote exported services and provides information about local exported services, as given by the Topology Manager, to others. -#### Shared memory (SHM) based discovery - -Provides service discovery for the RSA SHM implementation. - -| **Bundle** | `discovery_shm.zip` | -|--|--| -| **Configuration** | *None* | - #### Configured discovery Provides a service discovery with preconfigured discovery endpoints, allowing a static mesh of nodes for remote service invocation to be created. The configured discovery bundle in Celix is compatible with the configured discovery implementation provided by [Amdatu Remote](https://amdatu.atlassian.net/wiki/display/AMDATUDEV/Amdatu+Remote). @@ -162,11 +146,9 @@ Also the following libraries are required for building and/or using the Remote S ## RSA Bundles * [Remote Service Admin DFI](remote_service_admin_dfi) - A Dynamic Function Interface (DFI) implementation of the RSA. -* [Remote Service Admin SHM](remote_service_admin_shm) - A shared memory implementation of the RSA. * [Topology Manager](topology_manager) - A (scoped) RSA Topology Manager implementation. * [Discovery Configured](discovery_configured) - A RSA Discovery implementation using static configuration (xml). * [Discovery Etcd](discovery_etcd/README.md) - A RSA Discovery implementation using etcd. -* [Discovery SHM](discovery_shm) - A RSA Discovery implementation using shared memory. ## Notes diff --git a/bundles/remote_services/discovery_shm/CMakeLists.txt b/bundles/remote_services/discovery_shm/CMakeLists.txt deleted file mode 100644 index 1f860cb71..000000000 --- a/bundles/remote_services/discovery_shm/CMakeLists.txt +++ /dev/null @@ -1,46 +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. - -find_package(CURL REQUIRED) -find_package(LibXml2 REQUIRED) -find_package(civetweb REQUIRED) - -add_celix_bundle(rsa_discovery_shm - VERSION 0.0.1 - SYMBOLIC_NAME "apache_celix_rsa_discovery_shm" - NAME "Apache Celix RSA Discovery SHM" - GROUP "Celix/RSA" - SOURCES - src/discovery_shm.c - src/discovery_shmWatcher.c - src/discovery_impl.c -) -target_include_directories(rsa_discovery_shm PRIVATE - src - ${LIBXML2_INCLUDE_DIR} - ${CURL_INCLUDE_DIR} -) -target_link_libraries(rsa_discovery_shm PRIVATE - Celix::rsa_discovery_common Celix::framework CURL::libcurl ${LIBXML2_LIBRARIES} Celix::log_helper - rsa_common civetweb::civetweb) -celix_deprecated_framework_headers(rsa_discovery_shm) -celix_deprecated_utils_headers(rsa_discovery_shm) - -install_celix_bundle(rsa_discovery_shm EXPORT celix COMPONENT rsa) - -#Setup target aliases to match external usage -add_library(Celix::rsa_discovery_shm ALIAS rsa_discovery_shm) diff --git a/bundles/remote_services/discovery_shm/src/discovery_impl.c b/bundles/remote_services/discovery_shm/src/discovery_impl.c deleted file mode 100644 index 71824e158..000000000 --- a/bundles/remote_services/discovery_shm/src/discovery_impl.c +++ /dev/null @@ -1,164 +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. - */ -/** - * discovery_impl.c - * - * \date Aug 8, 2014 - * \author Apache Celix Project Team - * \copyright Apache License, Version 2.0 - */ - -#include -#include -#include -#include -#include -#include - -#include "celix_constants.h" -#include "celix_threads.h" -#include "bundle_context.h" -#include "array_list.h" -#include "utils.h" -#include "celix_errno.h" -#include "filter.h" -#include "service_reference.h" -#include "service_registration.h" -#include "remote_constants.h" - - -#include "discovery.h" -#include "discovery_impl.h" -#include "discovery_shmWatcher.h" -#include "endpoint_discovery_poller.h" -#include "endpoint_discovery_server.h" - -celix_status_t discovery_create(celix_bundle_context_t *context, discovery_t** out) { - celix_status_t status = CELIX_SUCCESS; - - discovery_t* discovery = calloc(1, sizeof(*discovery)); - discovery_impl_t* pImpl = calloc(1, sizeof(*pImpl)); - if (discovery != NULL && pImpl != NULL) { - discovery->pImpl = pImpl; - discovery->context = context; - discovery->poller = NULL; - discovery->server = NULL; - discovery->stopped = false; - - celixThreadMutex_create(&discovery->mutex, NULL); - - discovery->listenerReferences = hashMap_create(serviceReference_hashCode, NULL, serviceReference_equals2, NULL); - discovery->discoveredServices = hashMap_create(utils_stringHash, NULL, utils_stringEquals, NULL); - - discovery->loghelper = celix_logHelper_create(context, "rsa_discovery"); - } else { - status = CELIX_ENOMEM; - free(discovery); - free(pImpl); - } - - if (status == CELIX_SUCCESS) { - *out = discovery; - } - - return status; -} - - - -celix_status_t discovery_destroy(discovery_t *discovery) { - celix_status_t status = CELIX_SUCCESS; - - discovery->context = NULL; - discovery->poller = NULL; - discovery->server = NULL; - - celixThreadMutex_lock(&discovery->mutex); - - hashMap_destroy(discovery->discoveredServices, false, false); - discovery->discoveredServices = NULL; - - hashMap_destroy(discovery->listenerReferences, false, false); - discovery->listenerReferences = NULL; - - celixThreadMutex_unlock(&discovery->mutex); - - celixThreadMutex_destroy(&discovery->mutex); - - - free(discovery->pImpl); - free(discovery); - - return status; -} - -celix_status_t discovery_start(discovery_t *discovery) { - celix_status_t status; - - status = endpointDiscoveryPoller_create(discovery, discovery->context, DEFAULT_POLL_ENDPOINTS, &discovery->poller); - if (status == CELIX_SUCCESS) { - status = endpointDiscoveryServer_create(discovery, discovery->context, DEFAULT_SERVER_PATH, DEFAULT_SERVER_PORT, DEFAULT_SERVER_IP, &discovery->server); - } - - if (status == CELIX_SUCCESS) { - status = discoveryShmWatcher_create(discovery); - } - - return status; -} - -celix_status_t discovery_stop(discovery_t *discovery) { - celix_status_t status; - - celixThreadMutex_lock(&discovery->mutex); - discovery->stopped = true; - celixThreadMutex_unlock(&discovery->mutex); - - status = discoveryShmWatcher_destroy(discovery); - - if (status == CELIX_SUCCESS) { - status = endpointDiscoveryServer_destroy(discovery->server); - } - - endpointDiscoveryPoller_destroy(discovery->poller); - - if (status == CELIX_SUCCESS) { - hash_map_iterator_pt iter; - - celixThreadMutex_lock(&discovery->mutex); - - iter = hashMapIterator_create(discovery->discoveredServices); - while (hashMapIterator_hasNext(iter)) { - hash_map_entry_pt entry = hashMapIterator_nextEntry(iter); - endpoint_description_t *endpoint = hashMapEntry_getValue(entry); - - discovery_informEndpointListeners(discovery, endpoint, false); - } - hashMapIterator_destroy(iter); - - celixThreadMutex_unlock(&discovery->mutex); - - celix_logHelper_destroy(discovery->loghelper); - } - - return status; -} - - - diff --git a/bundles/remote_services/discovery_shm/src/discovery_impl.h b/bundles/remote_services/discovery_shm/src/discovery_impl.h deleted file mode 100644 index 11781f9e9..000000000 --- a/bundles/remote_services/discovery_shm/src/discovery_impl.h +++ /dev/null @@ -1,53 +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. - */ -/** - * discovery_impl.h - * - * \date Oct 01, 2014 - * \author Apache Celix Project Team - * \copyright Apache License, Version 2.0 - */ - -#ifndef DISCOVERY_IMPL_H_ -#define DISCOVERY_IMPL_H_ - -#include "bundle_context.h" -#include "service_reference.h" - -#include "endpoint_description.h" -#include "endpoint_listener.h" - -#include "endpoint_discovery_poller.h" -#include "endpoint_discovery_server.h" -#include "discovery_shmWatcher.h" - -#define DEFAULT_SERVER_IP "127.0.0.1" -#define DEFAULT_SERVER_PORT "9999" -#define DEFAULT_SERVER_PATH "/org.apache.celix.discovery.shm" -#define DEFAULT_POLL_ENDPOINTS "http://localhost:9999/org.apache.celix.discovery.shm" - -#define MAX_ROOTNODE_LENGTH 64 -#define MAX_LOCALNODE_LENGTH 256 - - -struct discovery_impl { - shm_watcher_t* watcher; -}; - -#endif /* DISCOVERY_H_ */ diff --git a/bundles/remote_services/discovery_shm/src/discovery_shm.c b/bundles/remote_services/discovery_shm/src/discovery_shm.c deleted file mode 100644 index c71d61f9c..000000000 --- a/bundles/remote_services/discovery_shm/src/discovery_shm.c +++ /dev/null @@ -1,281 +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. - */ -/** - * discovery_shm.c - * - * \date 26 Jul 2014 - * \author Apache Celix Project Team - * \copyright Apache License, Version 2.0 - */ - -#include -#include -#include -#include -#include - -#include -#include -#include - -#include -#include - -#include "discovery_shm.h" - -#define DISCOVERY_SHM_MEMSIZE 262144 -#define DISCOVERY_SHM_FILENAME "/dev/null" -#define DISCOVERY_SHM_FTOK_ID 50 -#define DISCOVERY_SEM_FILENAME "/dev/null" -#define DISCOVERY_SEM_FTOK_ID 54 - -struct shmEntry { - char key[SHM_ENTRY_MAX_KEY_LENGTH]; - char value[SHM_ENTRY_MAX_VALUE_LENGTH]; - - time_t expires; -}; - -typedef struct shmEntry shmEntry; - -struct shmData { - shmEntry entries[SHM_DATA_MAX_ENTRIES]; - int numOfEntries; - int shmId; - - celix_thread_mutex_t globalLock; -}; - -void* shmAddress; - -static celix_status_t discoveryShm_removeWithIndex(shmData_t *data, int index); - -/* returns the ftok key to identify shared memory*/ -static key_t discoveryShm_getKey() { - return ftok(DISCOVERY_SHM_FILENAME, DISCOVERY_SHM_FTOK_ID); -} - -/* creates a new shared memory block */ -celix_status_t discoveryShm_create(shmData_t **data) { - celix_status_t status; - - shmData_t *shmData = calloc(1, sizeof(*shmData)); - key_t shmKey = discoveryShm_getKey(); - - if (!shmData) { - status = CELIX_ENOMEM; - } else if ((shmData->shmId = shmget(shmKey, DISCOVERY_SHM_MEMSIZE, IPC_CREAT | 0666)) < 0) { - status = CELIX_BUNDLE_EXCEPTION; - } else if ((shmAddress = shmat(shmData->shmId, 0, 0)) == (char*) -1) { - status = CELIX_BUNDLE_EXCEPTION; - } else { - celix_thread_mutexattr_t threadAttr; - - shmData->numOfEntries = 0; - - status = celixThreadMutexAttr_create(&threadAttr); - -#ifdef LINUX - if (status == CELIX_SUCCESS) { - // This is Linux specific - status = pthread_mutexattr_setrobust(&threadAttr, PTHREAD_MUTEX_ROBUST); - } -#endif - - if (status == CELIX_SUCCESS) { - status = celixThreadMutex_create(&shmData->globalLock, &threadAttr); - } - - if (status == CELIX_SUCCESS) { - memcpy(shmAddress, shmData, sizeof(struct shmData)); - (*data) = shmAddress; - } - } - - free(shmData); - - return status; -} - -celix_status_t discoveryShm_attach(shmData_t **data) { - celix_status_t status = CELIX_SUCCESS; - key_t shmKey = ftok(DISCOVERY_SHM_FILENAME, DISCOVERY_SHM_FTOK_ID); - int shmId = -1; - - if ((shmId = shmget(shmKey, DISCOVERY_SHM_MEMSIZE, 0666)) < 0) { - status = CELIX_BUNDLE_EXCEPTION; - } - - /* shmat has a curious return value of (void*)-1 in case of error */ - void *mem=shmat(shmId, 0, 0); - if(mem==((void*)-1)){ - status = CELIX_BUNDLE_EXCEPTION; - } - else{ - (*data)=mem; - } - - return status; -} - -static celix_status_t discoveryShm_getwithIndex(shmData_t *data, char* key, char* value, int* index) { - celix_status_t status = CELIX_BUNDLE_EXCEPTION; - time_t currentTime = time(NULL); - unsigned int i; - - for (i = 0; i < data->numOfEntries && status != CELIX_SUCCESS; i++) { - shmEntry entry = data->entries[i]; - // check if entry is still valid - if (data->entries[i].expires < currentTime) { - discoveryShm_removeWithIndex(data, i); - } else if (strcmp(entry.key, key) == 0) { - if (value) { - strcpy(value, entry.value); - } - if (index) { - (*index) = i; - } - status = CELIX_SUCCESS; - } - } - - return status; -} - -celix_status_t discoveryShm_getKeys(shmData_t *data, char** keys, int* size) { - celix_status_t status; - - status = celixThreadMutex_lock(&data->globalLock); - - if (status == CELIX_SUCCESS) { - unsigned int i = 0; - for (i = 0; i < data->numOfEntries; i++) { - shmEntry entry = data->entries[i]; - - if (strlen(entry.key)>0) { - snprintf(keys[i], SHM_ENTRY_MAX_KEY_LENGTH, "%s", entry.key); - } - } - - (*size) = i; - - celixThreadMutex_unlock(&data->globalLock); - } - - return status; -} - -celix_status_t discoveryShm_set(shmData_t *data, char *key, char* value) { - celix_status_t status; - int index = -1; - - if (data->numOfEntries >= SHM_DATA_MAX_ENTRIES) { - status = CELIX_ILLEGAL_STATE; - } else { - status = celixThreadMutex_lock(&data->globalLock); - - if (status == CELIX_SUCCESS) { - // check if key already there - status = discoveryShm_getwithIndex(data, key, NULL, &index); - if (status != CELIX_SUCCESS) { - index = data->numOfEntries; - - snprintf(data->entries[index].key, SHM_ENTRY_MAX_KEY_LENGTH, "%s", key); - data->numOfEntries++; - - status = CELIX_SUCCESS; - } - - snprintf(data->entries[index].value, SHM_ENTRY_MAX_VALUE_LENGTH, "%s", value); - data->entries[index].expires = (time(NULL) + SHM_ENTRY_DEFAULT_TTL); - - celixThreadMutex_unlock(&data->globalLock); - } - } - - return status; -} - -celix_status_t discoveryShm_get(shmData_t *data, char* key, char* value) { - celix_status_t status; - - status = celixThreadMutex_lock(&data->globalLock); - - if (status == CELIX_SUCCESS) { - status = discoveryShm_getwithIndex(data, key, value, NULL); - - celixThreadMutex_unlock(&data->globalLock); - } - - return status; -} - -static celix_status_t discoveryShm_removeWithIndex(shmData_t *data, int index) { - celix_status_t status = CELIX_SUCCESS; - - data->numOfEntries--; - if (index < data->numOfEntries) { - memcpy((void*) &data->entries[index], (void*) &data->entries[index + 1], ((data->numOfEntries - index) * sizeof(struct shmEntry))); - } - - return status; -} - -celix_status_t discoveryShm_remove(shmData_t *data, char* key) { - celix_status_t status; - int index = -1; - - status = celixThreadMutex_lock(&data->globalLock); - - if (status == CELIX_SUCCESS) { - status = discoveryShm_getwithIndex(data, key, NULL, &index); - - if (status == CELIX_SUCCESS) { - status = discoveryShm_removeWithIndex(data, index); - } - - celixThreadMutex_unlock(&data->globalLock); - } - - return status; -} - -celix_status_t discoveryShm_detach(shmData_t *data) { - celix_status_t status = CELIX_BUNDLE_EXCEPTION; - - if (data->numOfEntries == 0) { - status = discoveryShm_destroy(data); - } - else if (shmdt(shmAddress) == 0) { - status = CELIX_SUCCESS; - } - - return status; -} - -celix_status_t discoveryShm_destroy(shmData_t *data) { - celix_status_t status = CELIX_BUNDLE_EXCEPTION; - - if (shmctl(data->shmId, IPC_RMID, 0) == 0) { - status = CELIX_SUCCESS; - } - - return status; - -} diff --git a/bundles/remote_services/discovery_shm/src/discovery_shm.h b/bundles/remote_services/discovery_shm/src/discovery_shm.h deleted file mode 100644 index e5d825274..000000000 --- a/bundles/remote_services/discovery_shm/src/discovery_shm.h +++ /dev/null @@ -1,53 +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. - */ -/** - * shm.h - * - * \date 26 Jul 2014 - * \author Apache Celix Project Team - * \copyright Apache License, Version 2.0 - */ - -#ifndef _DISCOVERY_SHM_H_ -#define _DISCOVERY_SHM_H_ - -#include - -#define SHM_ENTRY_MAX_KEY_LENGTH 256 -#define SHM_ENTRY_MAX_VALUE_LENGTH 256 - -// defines the time-to-live in seconds -#define SHM_ENTRY_DEFAULT_TTL 60 - -// we currently support 64 separate discovery instances -#define SHM_DATA_MAX_ENTRIES 64 - -typedef struct shmData shmData_t; - -/* creates a new shared memory block */ -celix_status_t discoveryShm_create(shmData_t **data); -celix_status_t discoveryShm_attach(shmData_t **data); -celix_status_t discoveryShm_set(shmData_t *data, char *key, char* value); -celix_status_t discoveryShm_get(shmData_t *data, char* key, char* value); -celix_status_t discoveryShm_getKeys(shmData_t *data, char** keys, int* size); -celix_status_t discoveryShm_remove(shmData_t *data, char* key); -celix_status_t discoveryShm_detach(shmData_t *data); -celix_status_t discoveryShm_destroy(shmData_t *data); - -#endif diff --git a/bundles/remote_services/discovery_shm/src/discovery_shmWatcher.c b/bundles/remote_services/discovery_shm/src/discovery_shmWatcher.c deleted file mode 100644 index d9571dc07..000000000 --- a/bundles/remote_services/discovery_shm/src/discovery_shmWatcher.c +++ /dev/null @@ -1,255 +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. - */ -/** - * discovery_shmWatcher.c - * - * \date 16 Sep 2014 - * \author Apache Celix Project Team - * \copyright Apache License, Version 2.0 - */ - -#include -#include -#include -#include -#include - - -#include "celix_log.h" -#include "celix_constants.h" -#include "discovery_impl.h" - -#include "discovery_shm.h" -#include "discovery_shmWatcher.h" - -#include "endpoint_discovery_poller.h" - -#define DEFAULT_SERVER_IP "127.0.0.1" -#define DEFAULT_SERVER_PORT "9999" -#define DEFAULT_SERVER_PATH "/org.apache.celix.discovery.shm" -#define DEFAULT_POLL_ENDPOINTS "http://localhost:9999/org.apache.celix.discovery.shm" - -#define MAX_ROOTNODE_LENGTH 64 -#define MAX_LOCALNODE_LENGTH 256 - - -struct shm_watcher { - shmData_t *shmData; - celix_thread_t watcherThread; - celix_thread_mutex_t watcherLock; - - volatile bool running; -}; - -// note that the rootNode shouldn't have a leading slash -static celix_status_t discoveryShmWatcher_getRootPath(char* rootNode) { - celix_status_t status = CELIX_SUCCESS; - - strcpy(rootNode, "discovery"); - - return status; -} - -static celix_status_t discoveryShmWatcher_getLocalNodePath(celix_bundle_context_t *context, char* localNodePath) { - celix_status_t status; - char rootPath[MAX_ROOTNODE_LENGTH]; - const char* uuid = NULL; - - status = discoveryShmWatcher_getRootPath(&rootPath[0]); - - if (status == CELIX_SUCCESS) { - status = bundleContext_getProperty(context, OSGI_FRAMEWORK_FRAMEWORK_UUID, &uuid); - } - - if (status == CELIX_SUCCESS) { - if (rootPath[strlen(&rootPath[0]) - 1] == '/') { - snprintf(localNodePath, MAX_LOCALNODE_LENGTH, "%s%s", &rootPath[0], uuid); - } else { - snprintf(localNodePath, MAX_LOCALNODE_LENGTH, "%s/%s", &rootPath[0], uuid); - } - } - - return status; -} - -/* retrieves all endpoints from shm and syncs them with the ones already available */ -static celix_status_t discoveryShmWatcher_syncEndpoints(discovery_t *discovery) { - celix_status_t status = CELIX_SUCCESS; - shm_watcher_t *watcher = discovery->pImpl->watcher; - char** shmKeyArr = calloc(SHM_DATA_MAX_ENTRIES, sizeof(*shmKeyArr)); - array_list_pt registeredKeyArr = NULL; - - int i, j, shmSize; - - for (i = 0; i < SHM_DATA_MAX_ENTRIES; i++) { - shmKeyArr[i] = calloc(SHM_ENTRY_MAX_KEY_LENGTH, sizeof(*shmKeyArr[i])); - } - - arrayList_create(®isteredKeyArr); - - // get all urls available in shm - discoveryShm_getKeys(watcher->shmData, shmKeyArr, &shmSize); - - // get all locally registered endpoints - endpointDiscoveryPoller_getDiscoveryEndpoints(discovery->poller, registeredKeyArr); - - // add discovery points which are in shm, but not local yet - for (i = 0; i < shmSize; i++) { - char url[SHM_ENTRY_MAX_VALUE_LENGTH]; - - if (discoveryShm_get(watcher->shmData, shmKeyArr[i], &url[0]) == CELIX_SUCCESS) { - bool elementFound = false; - - for (j = 0; j < arrayList_size(registeredKeyArr) && elementFound == false; j++) { - - if (strcmp(url, (char*) arrayList_get(registeredKeyArr, j)) == 0) { - free(arrayList_remove(registeredKeyArr, j)); - elementFound = true; - } - } - - if (elementFound == false) { - endpointDiscoveryPoller_addDiscoveryEndpoint(discovery->poller, url); - } - } - } - - // remove those which are not in shm - for (i = 0; i < arrayList_size(registeredKeyArr); i++) { - char* regUrl = arrayList_get(registeredKeyArr, i); - - if (regUrl != NULL) { - endpointDiscoveryPoller_removeDiscoveryEndpoint(discovery->poller, regUrl); - } - } - - for (i = 0; i < SHM_DATA_MAX_ENTRIES; i++) { - free(shmKeyArr[i]); - } - - free(shmKeyArr); - - for (j = 0; j < arrayList_size(registeredKeyArr); j++) { - free(arrayList_get(registeredKeyArr, j)); - } - - arrayList_destroy(registeredKeyArr); - - return status; -} - -static void* discoveryShmWatcher_run(void* data) { - discovery_t *discovery = (discovery_t *) data; - shm_watcher_t *watcher = discovery->pImpl->watcher; - char localNodePath[MAX_LOCALNODE_LENGTH]; - char url[MAX_LOCALNODE_LENGTH]; - - if (discoveryShmWatcher_getLocalNodePath(discovery->context, &localNodePath[0]) != CELIX_SUCCESS) { - celix_logHelper_log(discovery->loghelper, CELIX_LOG_LEVEL_WARNING, "Cannot retrieve local discovery path."); - } - - if (endpointDiscoveryServer_getUrl(discovery->server, &url[0], MAX_LOCALNODE_LENGTH) != CELIX_SUCCESS) { - snprintf(url, MAX_LOCALNODE_LENGTH, "http://%s:%s/%s", DEFAULT_SERVER_IP, DEFAULT_SERVER_PORT, DEFAULT_SERVER_PATH); - } - - while (watcher->running) { - // register own framework - if (discoveryShm_set(watcher->shmData, localNodePath, url) != CELIX_SUCCESS) { - celix_logHelper_log(discovery->loghelper, CELIX_LOG_LEVEL_WARNING, "Cannot set local discovery registration."); - } - - discoveryShmWatcher_syncEndpoints(discovery); - sleep(5); - } - - return NULL; -} - -celix_status_t discoveryShmWatcher_create(discovery_t *discovery) { - celix_status_t status = CELIX_SUCCESS; - shm_watcher_t *watcher = NULL; - - watcher = calloc(1, sizeof(*watcher)); - - if (!watcher) { - status = CELIX_ENOMEM; - } else { - status = discoveryShm_attach(&(watcher->shmData)); - - if (status != CELIX_SUCCESS) { - celix_logHelper_log(discovery->loghelper, CELIX_LOG_LEVEL_DEBUG, "Attaching to Shared Memory Failed. Trying to create."); - - status = discoveryShm_create(&(watcher->shmData)); - - if (status != CELIX_SUCCESS) { - celix_logHelper_log(discovery->loghelper, CELIX_LOG_LEVEL_ERROR, "Failed to create Shared Memory Segment."); - } - } - - if (status == CELIX_SUCCESS) { - discovery->pImpl->watcher = watcher; - } - else{ - discovery->pImpl->watcher = NULL; - free(watcher); - } - - } - - if (status == CELIX_SUCCESS) { - status += celixThreadMutex_create(&watcher->watcherLock, NULL); - status += celixThreadMutex_lock(&watcher->watcherLock); - watcher->running = true; - status += celixThread_create(&watcher->watcherThread, NULL, discoveryShmWatcher_run, discovery); - status += celixThreadMutex_unlock(&watcher->watcherLock); - } - - return status; -} - -celix_status_t discoveryShmWatcher_destroy(discovery_t *discovery) { - celix_status_t status; - shm_watcher_t *watcher = discovery->pImpl->watcher; - char localNodePath[MAX_LOCALNODE_LENGTH]; - - celixThreadMutex_lock(&watcher->watcherLock); - watcher->running = false; - celixThreadMutex_unlock(&watcher->watcherLock); - - celixThread_join(watcher->watcherThread, NULL); - - // remove own framework - status = discoveryShmWatcher_getLocalNodePath(discovery->context, &localNodePath[0]); - - if (status == CELIX_SUCCESS) { - status = discoveryShm_remove(watcher->shmData, localNodePath); - } - - if (status == CELIX_SUCCESS) { - discoveryShm_detach(watcher->shmData); - free(watcher); - } - else { - celix_logHelper_log(discovery->loghelper, CELIX_LOG_LEVEL_WARNING, "Cannot remove local discovery registration."); - } - - - return status; -} - diff --git a/bundles/remote_services/discovery_shm/src/discovery_shmWatcher.h b/bundles/remote_services/discovery_shm/src/discovery_shmWatcher.h deleted file mode 100644 index 1fac911cd..000000000 --- a/bundles/remote_services/discovery_shm/src/discovery_shmWatcher.h +++ /dev/null @@ -1,40 +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. - */ -/** - * shm_watcher.h - * - * \date 30 Sep 2014 - * \author Apache Celix Project Team - * \copyright Apache License, Version 2.0 - */ - -#ifndef DISCOVERY_SHM_WATCHER_H_ -#define DISCOVERY_SHM_WATCHER_H_ - -#include "celix_errno.h" -#include "discovery.h" -#include "endpoint_discovery_poller.h" - -typedef struct shm_watcher shm_watcher_t; - -celix_status_t discoveryShmWatcher_create(discovery_t *discovery); -celix_status_t discoveryShmWatcher_destroy(discovery_t *discovery); - - -#endif /* DISCOVERY_SHM_WATCHER_H_ */