http://git-wip-us.apache.org/repos/asf/celix/blob/2a670f26/remote_services/discovery_configured/private/include/discovery_impl.h ---------------------------------------------------------------------- diff --git a/remote_services/discovery_configured/private/include/discovery_impl.h b/remote_services/discovery_configured/private/include/discovery_impl.h deleted file mode 100644 index 0414eac..0000000 --- a/remote_services/discovery_configured/private/include/discovery_impl.h +++ /dev/null @@ -1,62 +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 Sep 29, 2011 - * \author <a href="mailto:[email protected]">Apache Celix Project Team</a> - * \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 "log_helper.h" - -#define DEFAULT_SERVER_IP "127.0.0.1" -#define DEFAULT_SERVER_PORT "9999" -#define DEFAULT_SERVER_PATH "/org.apache.celix.discovery.configured" -#define DEFAULT_POLL_ENDPOINTS "http://localhost:9999/org.apache.celix.discovery.configured" - - -struct discovery { - bundle_context_pt context; - - celix_thread_mutex_t listenerReferencesMutex; - celix_thread_mutex_t discoveredServicesMutex; - - hash_map_pt listenerReferences; //key=serviceReference, value=nop - hash_map_pt discoveredServices; //key=endpointId (string), value=endpoint_description_pt - - endpoint_discovery_poller_pt poller; - endpoint_discovery_server_pt server; - - log_helper_pt loghelper; -}; - -#endif /* DISCOVERY_IMPL_H_ */
http://git-wip-us.apache.org/repos/asf/celix/blob/2a670f26/remote_services/discovery_configured/private/src/desc.xml ---------------------------------------------------------------------- diff --git a/remote_services/discovery_configured/private/src/desc.xml b/remote_services/discovery_configured/private/src/desc.xml deleted file mode 100644 index 5998992..0000000 --- a/remote_services/discovery_configured/private/src/desc.xml +++ /dev/null @@ -1,41 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- - *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. - --> -<endpoint-descriptions xmlns="http://www.osgi.org/xmlns/rsa/v1.0.0"> - <endpoint-description> - <property name="service.intents"> - <list> - <value>SOAP</value> - <value>HTTP</value> - </list> - </property> - <property name="endpoint.id" value="http://ws.acme.com:9000/hello" /> - <property name="objectClass" value="com.acme.Foo" /> - <property name="endpoint.package.version.com.acme" value="4.2" /> - <property name="service.imported.configs" value="com.acme" /> - <property name="com.acme.ws.xml"> - <xml> - <config xmlns="http://acme.com/defs"> - <port>1029</port> - <host>www.acme.com</host> - </config> - </xml> - </property> - </endpoint-description> -</endpoint-descriptions> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/celix/blob/2a670f26/remote_services/discovery_configured/private/src/discovery_impl.c ---------------------------------------------------------------------- diff --git a/remote_services/discovery_configured/private/src/discovery_impl.c b/remote_services/discovery_configured/private/src/discovery_impl.c deleted file mode 100644 index 35a0c71..0000000 --- a/remote_services/discovery_configured/private/src/discovery_impl.c +++ /dev/null @@ -1,123 +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 <a href="mailto:[email protected]">Apache Celix Project Team</a> - * \copyright Apache License, Version 2.0 - */ -#include <stdio.h> -#include <stdlib.h> -#include <stdbool.h> -#include <netdb.h> - -#include "celix_threads.h" -#include "bundle_context.h" -#include "utils.h" -#include "log_helper.h" - -#include "discovery.h" -#include "discovery_impl.h" - - -celix_status_t discovery_create(bundle_context_pt context, discovery_pt *discovery) { - celix_status_t status; - - *discovery = malloc(sizeof(struct discovery)); - if (!*discovery) { - status = CELIX_ENOMEM; - } - else { - (*discovery)->context = context; - (*discovery)->poller = NULL; - (*discovery)->server = NULL; - - (*discovery)->listenerReferences = hashMap_create(serviceReference_hashCode, NULL, serviceReference_equals2, NULL); - (*discovery)->discoveredServices = hashMap_create(utils_stringHash, NULL, utils_stringEquals, NULL); - - status = celixThreadMutex_create(&(*discovery)->listenerReferencesMutex, NULL); - status = celixThreadMutex_create(&(*discovery)->discoveredServicesMutex, NULL); - - logHelper_create(context, &(*discovery)->loghelper); - } - - return status; -} - -celix_status_t discovery_start(discovery_pt discovery) { - celix_status_t status; - - logHelper_start(discovery->loghelper); - - status = endpointDiscoveryPoller_create(discovery, discovery->context, &discovery->poller); - if (status != CELIX_SUCCESS) { - return CELIX_BUNDLE_EXCEPTION; - } - - status = endpointDiscoveryServer_create(discovery, discovery->context, &discovery->server); - if (status != CELIX_SUCCESS) { - return CELIX_BUNDLE_EXCEPTION; - } - - return status; -} - -celix_status_t discovery_stop(discovery_pt discovery) { - celix_status_t status; - - status = endpointDiscoveryServer_destroy(discovery->server); - status = endpointDiscoveryPoller_destroy(discovery->poller); - - logHelper_stop(discovery->loghelper); - - return status; -} - -celix_status_t discovery_destroy(discovery_pt discovery) { - celix_status_t status = CELIX_SUCCESS; - - discovery->context = NULL; - discovery->poller = NULL; - discovery->server = NULL; - - celixThreadMutex_lock(&discovery->discoveredServicesMutex); - - hashMap_destroy(discovery->discoveredServices, false, false); - discovery->discoveredServices = NULL; - - celixThreadMutex_unlock(&discovery->discoveredServicesMutex); - - celixThreadMutex_destroy(&discovery->discoveredServicesMutex); - - celixThreadMutex_lock(&discovery->listenerReferencesMutex); - - hashMap_destroy(discovery->listenerReferences, false, false); - discovery->listenerReferences = NULL; - - celixThreadMutex_unlock(&discovery->listenerReferencesMutex); - - celixThreadMutex_destroy(&discovery->listenerReferencesMutex); - - logHelper_destroy(&discovery->loghelper); - - free(discovery); - - return status; -} http://git-wip-us.apache.org/repos/asf/celix/blob/2a670f26/remote_services/discovery_configured/src/desc.xml ---------------------------------------------------------------------- diff --git a/remote_services/discovery_configured/src/desc.xml b/remote_services/discovery_configured/src/desc.xml new file mode 100644 index 0000000..5998992 --- /dev/null +++ b/remote_services/discovery_configured/src/desc.xml @@ -0,0 +1,41 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + *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. + --> +<endpoint-descriptions xmlns="http://www.osgi.org/xmlns/rsa/v1.0.0"> + <endpoint-description> + <property name="service.intents"> + <list> + <value>SOAP</value> + <value>HTTP</value> + </list> + </property> + <property name="endpoint.id" value="http://ws.acme.com:9000/hello" /> + <property name="objectClass" value="com.acme.Foo" /> + <property name="endpoint.package.version.com.acme" value="4.2" /> + <property name="service.imported.configs" value="com.acme" /> + <property name="com.acme.ws.xml"> + <xml> + <config xmlns="http://acme.com/defs"> + <port>1029</port> + <host>www.acme.com</host> + </config> + </xml> + </property> + </endpoint-description> +</endpoint-descriptions> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/celix/blob/2a670f26/remote_services/discovery_configured/src/discovery_impl.c ---------------------------------------------------------------------- diff --git a/remote_services/discovery_configured/src/discovery_impl.c b/remote_services/discovery_configured/src/discovery_impl.c new file mode 100644 index 0000000..35a0c71 --- /dev/null +++ b/remote_services/discovery_configured/src/discovery_impl.c @@ -0,0 +1,123 @@ +/** + * 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 <a href="mailto:[email protected]">Apache Celix Project Team</a> + * \copyright Apache License, Version 2.0 + */ +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <netdb.h> + +#include "celix_threads.h" +#include "bundle_context.h" +#include "utils.h" +#include "log_helper.h" + +#include "discovery.h" +#include "discovery_impl.h" + + +celix_status_t discovery_create(bundle_context_pt context, discovery_pt *discovery) { + celix_status_t status; + + *discovery = malloc(sizeof(struct discovery)); + if (!*discovery) { + status = CELIX_ENOMEM; + } + else { + (*discovery)->context = context; + (*discovery)->poller = NULL; + (*discovery)->server = NULL; + + (*discovery)->listenerReferences = hashMap_create(serviceReference_hashCode, NULL, serviceReference_equals2, NULL); + (*discovery)->discoveredServices = hashMap_create(utils_stringHash, NULL, utils_stringEquals, NULL); + + status = celixThreadMutex_create(&(*discovery)->listenerReferencesMutex, NULL); + status = celixThreadMutex_create(&(*discovery)->discoveredServicesMutex, NULL); + + logHelper_create(context, &(*discovery)->loghelper); + } + + return status; +} + +celix_status_t discovery_start(discovery_pt discovery) { + celix_status_t status; + + logHelper_start(discovery->loghelper); + + status = endpointDiscoveryPoller_create(discovery, discovery->context, &discovery->poller); + if (status != CELIX_SUCCESS) { + return CELIX_BUNDLE_EXCEPTION; + } + + status = endpointDiscoveryServer_create(discovery, discovery->context, &discovery->server); + if (status != CELIX_SUCCESS) { + return CELIX_BUNDLE_EXCEPTION; + } + + return status; +} + +celix_status_t discovery_stop(discovery_pt discovery) { + celix_status_t status; + + status = endpointDiscoveryServer_destroy(discovery->server); + status = endpointDiscoveryPoller_destroy(discovery->poller); + + logHelper_stop(discovery->loghelper); + + return status; +} + +celix_status_t discovery_destroy(discovery_pt discovery) { + celix_status_t status = CELIX_SUCCESS; + + discovery->context = NULL; + discovery->poller = NULL; + discovery->server = NULL; + + celixThreadMutex_lock(&discovery->discoveredServicesMutex); + + hashMap_destroy(discovery->discoveredServices, false, false); + discovery->discoveredServices = NULL; + + celixThreadMutex_unlock(&discovery->discoveredServicesMutex); + + celixThreadMutex_destroy(&discovery->discoveredServicesMutex); + + celixThreadMutex_lock(&discovery->listenerReferencesMutex); + + hashMap_destroy(discovery->listenerReferences, false, false); + discovery->listenerReferences = NULL; + + celixThreadMutex_unlock(&discovery->listenerReferencesMutex); + + celixThreadMutex_destroy(&discovery->listenerReferencesMutex); + + logHelper_destroy(&discovery->loghelper); + + free(discovery); + + return status; +} http://git-wip-us.apache.org/repos/asf/celix/blob/2a670f26/remote_services/discovery_configured/src/discovery_impl.h ---------------------------------------------------------------------- diff --git a/remote_services/discovery_configured/src/discovery_impl.h b/remote_services/discovery_configured/src/discovery_impl.h new file mode 100644 index 0000000..0414eac --- /dev/null +++ b/remote_services/discovery_configured/src/discovery_impl.h @@ -0,0 +1,62 @@ +/** + *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 Sep 29, 2011 + * \author <a href="mailto:[email protected]">Apache Celix Project Team</a> + * \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 "log_helper.h" + +#define DEFAULT_SERVER_IP "127.0.0.1" +#define DEFAULT_SERVER_PORT "9999" +#define DEFAULT_SERVER_PATH "/org.apache.celix.discovery.configured" +#define DEFAULT_POLL_ENDPOINTS "http://localhost:9999/org.apache.celix.discovery.configured" + + +struct discovery { + bundle_context_pt context; + + celix_thread_mutex_t listenerReferencesMutex; + celix_thread_mutex_t discoveredServicesMutex; + + hash_map_pt listenerReferences; //key=serviceReference, value=nop + hash_map_pt discoveredServices; //key=endpointId (string), value=endpoint_description_pt + + endpoint_discovery_poller_pt poller; + endpoint_discovery_server_pt server; + + log_helper_pt loghelper; +}; + +#endif /* DISCOVERY_IMPL_H_ */ http://git-wip-us.apache.org/repos/asf/celix/blob/2a670f26/remote_services/discovery_etcd/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/remote_services/discovery_etcd/CMakeLists.txt b/remote_services/discovery_etcd/CMakeLists.txt index b8955de..4168281 100644 --- a/remote_services/discovery_etcd/CMakeLists.txt +++ b/remote_services/discovery_etcd/CMakeLists.txt @@ -20,42 +20,19 @@ if (RSA_DISCOVERY_ETCD) find_package(CURL REQUIRED) find_package(LibXml2 REQUIRED) find_package(Jansson REQUIRED) - - include_directories("${CURL_INCLUDE_DIR}") - include_directories("${JANSSON_INCLUDE_DIR}") - include_directories("${LIBXML2_INCLUDE_DIR}") - include_directories("${PROJECT_SOURCE_DIR}/etcdlib/public/include") - include_directories("${PROJECT_SOURCE_DIR}/utils/public/include") - include_directories("${PROJECT_SOURCE_DIR}/remote_services/utils/private/include") - include_directories("${PROJECT_SOURCE_DIR}/remote_services/utils/public/include") - include_directories("${PROJECT_SOURCE_DIR}/remote_services/discovery/private/include") - include_directories("${PROJECT_SOURCE_DIR}/remote_services/discovery_etcd/private/include") - include_directories("${PROJECT_SOURCE_DIR}/remote_services/endpoint_listener/public/include") - include_directories("${PROJECT_SOURCE_DIR}/remote_services/remote_service_admin/public/include") - include_directories("${PROJECT_SOURCE_DIR}/log_service/public/include") - include_directories("private/include") add_bundle(discovery_etcd VERSION 0.9.0 SYMBOLIC_NAME "apache_celix_rsa_discovery_etcd" NAME "Apache Celix RSA Discovery ETCD" SOURCES - src/discovery_impl.c - src/etcd_watcher.c - ${PROJECT_SOURCE_DIR}/remote_services/discovery/private/src/discovery_activator.c - ${PROJECT_SOURCE_DIR}/remote_services/discovery/private/src/discovery.c - ${PROJECT_SOURCE_DIR}/remote_services/discovery/private/src/endpoint_descriptor_reader.c - ${PROJECT_SOURCE_DIR}/remote_services/discovery/private/src/endpoint_descriptor_writer.c - ${PROJECT_SOURCE_DIR}/remote_services/discovery/private/src/endpoint_discovery_poller.c - ${PROJECT_SOURCE_DIR}/remote_services/discovery/private/src/endpoint_discovery_server.c - ${PROJECT_SOURCE_DIR}/remote_services/remote_service_admin/private/src/endpoint_description.c - ${PROJECT_SOURCE_DIR}/remote_services/utils/private/src/civetweb.c + src/discovery_impl.c + src/etcd_watcher.c ) target_link_libraries(discovery_etcd PRIVATE Celix::log_helper) - target_include_directories(discovery_etcd PRIVATE src ../discovery/private/include) + target_include_directories(discovery_etcd PRIVATE src ) + target_include_directories(discovery_etcd PRIVATE ${CURL_INCLUDE_DIR} ${JANSSON_INCLUDE_DIR} ${LIBXML2_INCLUDE_DIR}) + target_link_libraries(discovery_etcd PRIVATE ${CURL_LIBRARIES} ${LIBXML2_LIBRARIES} ${JANSSON_LIBRARIES} discovery_common) install_bundle(discovery_etcd) - - target_link_libraries(discovery_etcd PRIVATE Celix::etcdlib ${CURL_LIBRARIES} ${LIBXML2_LIBRARIES} ${JANSSON_LIBRARIES}) - endif (RSA_DISCOVERY_ETCD) http://git-wip-us.apache.org/repos/asf/celix/blob/2a670f26/remote_services/discovery_shm/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/remote_services/discovery_shm/CMakeLists.txt b/remote_services/discovery_shm/CMakeLists.txt index 862096c..dff403d 100644 --- a/remote_services/discovery_shm/CMakeLists.txt +++ b/remote_services/discovery_shm/CMakeLists.txt @@ -19,40 +19,25 @@ celix_subproject(RSA_DISCOVERY_SHM "Option to enable building the Discovery (SHM if (RSA_DISCOVERY_SHM) find_package(CURL REQUIRED) find_package(LibXml2 REQUIRED) - - include_directories("${CURL_INCLUDE_DIR}") - include_directories("${LIBXML2_INCLUDE_DIR}") - include_directories("${PROJECT_SOURCE_DIR}/utils/public/include") - include_directories("${PROJECT_SOURCE_DIR}/remote_services/utils/private/include") - include_directories("${PROJECT_SOURCE_DIR}/remote_services/utils/public/include") - include_directories("${PROJECT_SOURCE_DIR}/remote_services/discovery/private/include") - include_directories("${PROJECT_SOURCE_DIR}/remote_services/discovery_shm/private/include") - include_directories("${PROJECT_SOURCE_DIR}/remote_services/endpoint_listener/public/include") - include_directories("${PROJECT_SOURCE_DIR}/remote_services/remote_service_admin/public/include") - include_directories("${PROJECT_SOURCE_DIR}/log_service/public/include") - include_directories("private/include") add_bundle(discovery_shm VERSION 0.0.1 SYMBOLIC_NAME "apache_celix_rsa_discovery_shm" NAME "Apache Celix RSA Discovery SHM" SOURCES - private/src/discovery_shm - private/src/discovery_shmWatcher - private/src/discovery_impl - ${PROJECT_SOURCE_DIR}/remote_services/discovery/private/src/discovery_activator.c - ${PROJECT_SOURCE_DIR}/remote_services/discovery/private/src/discovery.c - ${PROJECT_SOURCE_DIR}/remote_services/discovery/private/src/endpoint_descriptor_reader.c - ${PROJECT_SOURCE_DIR}/remote_services/discovery/private/src/endpoint_descriptor_writer.c - ${PROJECT_SOURCE_DIR}/remote_services/discovery/private/src/endpoint_discovery_poller.c - ${PROJECT_SOURCE_DIR}/remote_services/discovery/private/src/endpoint_discovery_server.c - ${PROJECT_SOURCE_DIR}/remote_services/remote_service_admin/private/src/endpoint_description.c - ${PROJECT_SOURCE_DIR}/remote_services/utils/private/src/civetweb.c - ${PROJECT_SOURCE_DIR}/log_service/public/src/log_helper.c + src/discovery_shm.c + src/discovery_shmWatcher.c + src/discovery_impl.c ) + target_include_directories(discovery_shm PRIVATE + src + ${LIBXML2_INCLUDE_DIR} + ${CURL_INCLUDE_DIR} + ) + target_link_libraries(discovery_shm PRIVATE Celix::framework ${CURL_LIBRARIES} ${LIBXML2_LIBRARIES} discovery_common) install_bundle(discovery_shm) - target_link_libraries(discovery_shm celix_framework ${CURL_LIBRARIES} ${LIBXML2_LIBRARIES}) + endif (RSA_DISCOVERY_SHM) http://git-wip-us.apache.org/repos/asf/celix/blob/2a670f26/remote_services/discovery_shm/private/include/discovery_impl.h ---------------------------------------------------------------------- diff --git a/remote_services/discovery_shm/private/include/discovery_impl.h b/remote_services/discovery_shm/private/include/discovery_impl.h deleted file mode 100644 index c7206bd..0000000 --- a/remote_services/discovery_shm/private/include/discovery_impl.h +++ /dev/null @@ -1,66 +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 <a href="mailto:[email protected]">Apache Celix Project Team</a> - * \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 { - bundle_context_pt context; - - celix_thread_mutex_t listenerReferencesMutex; - celix_thread_mutex_t discoveredServicesMutex; - - hash_map_pt listenerReferences; //key=serviceReference, value=nop - hash_map_pt discoveredServices; //key=endpointId (string), value=endpoint_description_pt - - shm_watcher_pt watcher; - endpoint_discovery_poller_pt poller; - endpoint_discovery_server_pt server; - - log_helper_pt loghelper; -}; - -#endif /* DISCOVERY_H_ */ http://git-wip-us.apache.org/repos/asf/celix/blob/2a670f26/remote_services/discovery_shm/private/include/discovery_shm.h ---------------------------------------------------------------------- diff --git a/remote_services/discovery_shm/private/include/discovery_shm.h b/remote_services/discovery_shm/private/include/discovery_shm.h deleted file mode 100644 index 9c4593b..0000000 --- a/remote_services/discovery_shm/private/include/discovery_shm.h +++ /dev/null @@ -1,56 +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 <a href="mailto:[email protected]">Apache Celix Project Team</a> - * \copyright Apache License, Version 2.0 - */ - - - -#ifndef _DISCOVERY_SHM_H_ -#define _DISCOVERY_SHM_H_ - -#include <celix_errno.h> - -#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_pt; - -/* creates a new shared memory block */ -celix_status_t discoveryShm_create(shmData_pt* data); -celix_status_t discoveryShm_attach(shmData_pt* data); -celix_status_t discoveryShm_set(shmData_pt data, char *key, char* value); -celix_status_t discoveryShm_get(shmData_pt data, char* key, char* value); -celix_status_t discoveryShm_getKeys(shmData_pt data, char** keys, int* size); -celix_status_t discoveryShm_remove(shmData_pt data, char* key); -celix_status_t discoveryShm_detach(shmData_pt data); -celix_status_t discoveryShm_destroy(shmData_pt data); - -#endif http://git-wip-us.apache.org/repos/asf/celix/blob/2a670f26/remote_services/discovery_shm/private/include/discovery_shmWatcher.h ---------------------------------------------------------------------- diff --git a/remote_services/discovery_shm/private/include/discovery_shmWatcher.h b/remote_services/discovery_shm/private/include/discovery_shmWatcher.h deleted file mode 100644 index ff70f72..0000000 --- a/remote_services/discovery_shm/private/include/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 <a href="mailto:[email protected]">Apache Celix Project Team</a> - * \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_pt; - -celix_status_t discoveryShmWatcher_create(discovery_pt discovery); -celix_status_t discoveryShmWatcher_destroy(discovery_pt discovery); - - -#endif /* DISCOVERY_SHM_WATCHER_H_ */ http://git-wip-us.apache.org/repos/asf/celix/blob/2a670f26/remote_services/discovery_shm/private/src/discovery_impl.c ---------------------------------------------------------------------- diff --git a/remote_services/discovery_shm/private/src/discovery_impl.c b/remote_services/discovery_shm/private/src/discovery_impl.c deleted file mode 100644 index 2604595..0000000 --- a/remote_services/discovery_shm/private/src/discovery_impl.c +++ /dev/null @@ -1,163 +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 <a href="mailto:[email protected]">Apache Celix Project Team</a> - * \copyright Apache License, Version 2.0 - */ -#include <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include <stdbool.h> -#include <netdb.h> -#include <netinet/in.h> - -#include "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(bundle_context_pt context, discovery_pt *discovery) { - celix_status_t status = CELIX_SUCCESS; - - *discovery = calloc(1, sizeof(struct discovery)); - if (!*discovery) { - status = CELIX_ENOMEM; - } else { - (*discovery)->context = context; - (*discovery)->poller = NULL; - (*discovery)->server = NULL; - - (*discovery)->listenerReferences = hashMap_create(serviceReference_hashCode, NULL, serviceReference_equals2, NULL); - (*discovery)->discoveredServices = hashMap_create(utils_stringHash, NULL, utils_stringEquals, NULL); - - celixThreadMutex_create(&(*discovery)->listenerReferencesMutex, NULL); - celixThreadMutex_create(&(*discovery)->discoveredServicesMutex, NULL); - - if (logHelper_create(context, &(*discovery)->loghelper) == CELIX_SUCCESS) { - logHelper_start((*discovery)->loghelper); - } - } - - return status; -} - - - -celix_status_t discovery_destroy(discovery_pt discovery) { - celix_status_t status = CELIX_SUCCESS; - - discovery->context = NULL; - discovery->poller = NULL; - discovery->server = NULL; - - celixThreadMutex_lock(&discovery->discoveredServicesMutex); - - hashMap_destroy(discovery->discoveredServices, false, false); - discovery->discoveredServices = NULL; - - celixThreadMutex_unlock(&discovery->discoveredServicesMutex); - - celixThreadMutex_destroy(&discovery->discoveredServicesMutex); - - celixThreadMutex_lock(&discovery->listenerReferencesMutex); - - hashMap_destroy(discovery->listenerReferences, false, false); - discovery->listenerReferences = NULL; - - celixThreadMutex_unlock(&discovery->listenerReferencesMutex); - - celixThreadMutex_destroy(&discovery->listenerReferencesMutex); - - - - - free(discovery); - - return status; -} - -celix_status_t discovery_start(discovery_pt discovery) { - celix_status_t status; - - status = endpointDiscoveryPoller_create(discovery, discovery->context, &discovery->poller); - if (status == CELIX_SUCCESS) { - status = endpointDiscoveryServer_create(discovery, discovery->context, &discovery->server); - } - - if (status == CELIX_SUCCESS) { - status = discoveryShmWatcher_create(discovery); - } - - return status; -} - -celix_status_t discovery_stop(discovery_pt discovery) { - celix_status_t status; - - 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->discoveredServicesMutex); - - iter = hashMapIterator_create(discovery->discoveredServices); - while (hashMapIterator_hasNext(iter)) { - hash_map_entry_pt entry = hashMapIterator_nextEntry(iter); - endpoint_description_pt endpoint = hashMapEntry_getValue(entry); - - discovery_informEndpointListeners(discovery, endpoint, false); - } - hashMapIterator_destroy(iter); - - celixThreadMutex_unlock(&discovery->discoveredServicesMutex); - - logHelper_stop(discovery->loghelper); - logHelper_destroy(&discovery->loghelper); - } - - return status; -} - - - http://git-wip-us.apache.org/repos/asf/celix/blob/2a670f26/remote_services/discovery_shm/private/src/discovery_shm.c ---------------------------------------------------------------------- diff --git a/remote_services/discovery_shm/private/src/discovery_shm.c b/remote_services/discovery_shm/private/src/discovery_shm.c deleted file mode 100644 index 1b1170e..0000000 --- a/remote_services/discovery_shm/private/src/discovery_shm.c +++ /dev/null @@ -1,284 +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 <a href="mailto:[email protected]">Apache Celix Project Team</a> - * \copyright Apache License, Version 2.0 - */ - - - -#include <stdio.h> -#include <pthread.h> -#include <stdlib.h> -#include <string.h> -#include <time.h> - -#include <sys/types.h> -#include <sys/sem.h> -#include <sys/shm.h> - -#include <celix_errno.h> -#include <celix_threads.h> - -#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* shmAdress; - -static celix_status_t discoveryShm_removeWithIndex(shmData_pt 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_pt* data) { - celix_status_t status; - - shmData_pt 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 ((shmAdress = 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(shmAdress, shmData, sizeof(struct shmData)); - (*data) = shmAdress; - } - } - - free(shmData); - - return status; -} - -celix_status_t discoveryShm_attach(shmData_pt* 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_pt 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_pt 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_pt 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_pt 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_pt 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_pt 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_pt data) { - celix_status_t status = CELIX_BUNDLE_EXCEPTION; - - if (data->numOfEntries == 0) { - status = discoveryShm_destroy(data); - } - else if (shmdt(shmAdress) == 0) { - status = CELIX_SUCCESS; - } - - return status; -} - -celix_status_t discoveryShm_destroy(shmData_pt data) { - celix_status_t status = CELIX_BUNDLE_EXCEPTION; - - if (shmctl(data->shmId, IPC_RMID, 0) == 0) { - status = CELIX_SUCCESS; - } - - return status; - -} http://git-wip-us.apache.org/repos/asf/celix/blob/2a670f26/remote_services/discovery_shm/private/src/discovery_shmWatcher.c ---------------------------------------------------------------------- diff --git a/remote_services/discovery_shm/private/src/discovery_shmWatcher.c b/remote_services/discovery_shm/private/src/discovery_shmWatcher.c deleted file mode 100644 index 6460de8..0000000 --- a/remote_services/discovery_shm/private/src/discovery_shmWatcher.c +++ /dev/null @@ -1,246 +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 <a href="mailto:[email protected]">Apache Celix Project Team</a> - * \copyright Apache License, Version 2.0 - */ - -#include <stdbool.h> -#include <stdlib.h> -#include <unistd.h> -#include <string.h> -#include <stdio.h> - - -#include "celix_log.h" -#include "constants.h" -#include "discovery_impl.h" - -#include "discovery_shm.h" -#include "discovery_shmWatcher.h" - -#include "endpoint_discovery_poller.h" - -struct shm_watcher { - shmData_pt 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(bundle_context_pt 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_pt discovery) { - celix_status_t status = CELIX_SUCCESS; - shm_watcher_pt watcher = discovery->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_pt discovery = (discovery_pt) data; - shm_watcher_pt watcher = discovery->watcher; - char localNodePath[MAX_LOCALNODE_LENGTH]; - char url[MAX_LOCALNODE_LENGTH]; - - if (discoveryShmWatcher_getLocalNodePath(discovery->context, &localNodePath[0]) != CELIX_SUCCESS) { - logHelper_log(discovery->loghelper, OSGI_LOGSERVICE_WARNING, "Cannot retrieve local discovery path."); - } - - if (endpointDiscoveryServer_getUrl(discovery->server, &url[0]) != 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) { - logHelper_log(discovery->loghelper, OSGI_LOGSERVICE_WARNING, "Cannot set local discovery registration."); - } - - discoveryShmWatcher_syncEndpoints(discovery); - sleep(5); - } - - return NULL; -} - -celix_status_t discoveryShmWatcher_create(discovery_pt discovery) { - celix_status_t status = CELIX_SUCCESS; - shm_watcher_pt watcher = NULL; - - watcher = calloc(1, sizeof(*watcher)); - - if (!watcher) { - status = CELIX_ENOMEM; - } else { - status = discoveryShm_attach(&(watcher->shmData)); - - if (status != CELIX_SUCCESS) { - logHelper_log(discovery->loghelper, OSGI_LOGSERVICE_DEBUG, "Attaching to Shared Memory Failed. Trying to create."); - - status = discoveryShm_create(&(watcher->shmData)); - - if (status != CELIX_SUCCESS) { - logHelper_log(discovery->loghelper, OSGI_LOGSERVICE_ERROR, "Failed to create Shared Memory Segment."); - } - } - - if (status == CELIX_SUCCESS) { - discovery->watcher = watcher; - } - else{ - discovery->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_pt discovery) { - celix_status_t status; - shm_watcher_pt watcher = discovery->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 { - logHelper_log(discovery->loghelper, OSGI_LOGSERVICE_WARNING, "Cannot remove local discovery registration."); - } - - - return status; -} - http://git-wip-us.apache.org/repos/asf/celix/blob/2a670f26/remote_services/discovery_shm/src/discovery_impl.c ---------------------------------------------------------------------- diff --git a/remote_services/discovery_shm/src/discovery_impl.c b/remote_services/discovery_shm/src/discovery_impl.c new file mode 100644 index 0000000..2604595 --- /dev/null +++ b/remote_services/discovery_shm/src/discovery_impl.c @@ -0,0 +1,163 @@ +/** + * 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 <a href="mailto:[email protected]">Apache Celix Project Team</a> + * \copyright Apache License, Version 2.0 + */ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <stdbool.h> +#include <netdb.h> +#include <netinet/in.h> + +#include "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(bundle_context_pt context, discovery_pt *discovery) { + celix_status_t status = CELIX_SUCCESS; + + *discovery = calloc(1, sizeof(struct discovery)); + if (!*discovery) { + status = CELIX_ENOMEM; + } else { + (*discovery)->context = context; + (*discovery)->poller = NULL; + (*discovery)->server = NULL; + + (*discovery)->listenerReferences = hashMap_create(serviceReference_hashCode, NULL, serviceReference_equals2, NULL); + (*discovery)->discoveredServices = hashMap_create(utils_stringHash, NULL, utils_stringEquals, NULL); + + celixThreadMutex_create(&(*discovery)->listenerReferencesMutex, NULL); + celixThreadMutex_create(&(*discovery)->discoveredServicesMutex, NULL); + + if (logHelper_create(context, &(*discovery)->loghelper) == CELIX_SUCCESS) { + logHelper_start((*discovery)->loghelper); + } + } + + return status; +} + + + +celix_status_t discovery_destroy(discovery_pt discovery) { + celix_status_t status = CELIX_SUCCESS; + + discovery->context = NULL; + discovery->poller = NULL; + discovery->server = NULL; + + celixThreadMutex_lock(&discovery->discoveredServicesMutex); + + hashMap_destroy(discovery->discoveredServices, false, false); + discovery->discoveredServices = NULL; + + celixThreadMutex_unlock(&discovery->discoveredServicesMutex); + + celixThreadMutex_destroy(&discovery->discoveredServicesMutex); + + celixThreadMutex_lock(&discovery->listenerReferencesMutex); + + hashMap_destroy(discovery->listenerReferences, false, false); + discovery->listenerReferences = NULL; + + celixThreadMutex_unlock(&discovery->listenerReferencesMutex); + + celixThreadMutex_destroy(&discovery->listenerReferencesMutex); + + + + + free(discovery); + + return status; +} + +celix_status_t discovery_start(discovery_pt discovery) { + celix_status_t status; + + status = endpointDiscoveryPoller_create(discovery, discovery->context, &discovery->poller); + if (status == CELIX_SUCCESS) { + status = endpointDiscoveryServer_create(discovery, discovery->context, &discovery->server); + } + + if (status == CELIX_SUCCESS) { + status = discoveryShmWatcher_create(discovery); + } + + return status; +} + +celix_status_t discovery_stop(discovery_pt discovery) { + celix_status_t status; + + 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->discoveredServicesMutex); + + iter = hashMapIterator_create(discovery->discoveredServices); + while (hashMapIterator_hasNext(iter)) { + hash_map_entry_pt entry = hashMapIterator_nextEntry(iter); + endpoint_description_pt endpoint = hashMapEntry_getValue(entry); + + discovery_informEndpointListeners(discovery, endpoint, false); + } + hashMapIterator_destroy(iter); + + celixThreadMutex_unlock(&discovery->discoveredServicesMutex); + + logHelper_stop(discovery->loghelper); + logHelper_destroy(&discovery->loghelper); + } + + return status; +} + + + http://git-wip-us.apache.org/repos/asf/celix/blob/2a670f26/remote_services/discovery_shm/src/discovery_impl.h ---------------------------------------------------------------------- diff --git a/remote_services/discovery_shm/src/discovery_impl.h b/remote_services/discovery_shm/src/discovery_impl.h new file mode 100644 index 0000000..c7206bd --- /dev/null +++ b/remote_services/discovery_shm/src/discovery_impl.h @@ -0,0 +1,66 @@ +/** + *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 <a href="mailto:[email protected]">Apache Celix Project Team</a> + * \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 { + bundle_context_pt context; + + celix_thread_mutex_t listenerReferencesMutex; + celix_thread_mutex_t discoveredServicesMutex; + + hash_map_pt listenerReferences; //key=serviceReference, value=nop + hash_map_pt discoveredServices; //key=endpointId (string), value=endpoint_description_pt + + shm_watcher_pt watcher; + endpoint_discovery_poller_pt poller; + endpoint_discovery_server_pt server; + + log_helper_pt loghelper; +}; + +#endif /* DISCOVERY_H_ */ http://git-wip-us.apache.org/repos/asf/celix/blob/2a670f26/remote_services/discovery_shm/src/discovery_shm.c ---------------------------------------------------------------------- diff --git a/remote_services/discovery_shm/src/discovery_shm.c b/remote_services/discovery_shm/src/discovery_shm.c new file mode 100644 index 0000000..1b1170e --- /dev/null +++ b/remote_services/discovery_shm/src/discovery_shm.c @@ -0,0 +1,284 @@ +/** + * 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 <a href="mailto:[email protected]">Apache Celix Project Team</a> + * \copyright Apache License, Version 2.0 + */ + + + +#include <stdio.h> +#include <pthread.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> + +#include <sys/types.h> +#include <sys/sem.h> +#include <sys/shm.h> + +#include <celix_errno.h> +#include <celix_threads.h> + +#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* shmAdress; + +static celix_status_t discoveryShm_removeWithIndex(shmData_pt 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_pt* data) { + celix_status_t status; + + shmData_pt 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 ((shmAdress = 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(shmAdress, shmData, sizeof(struct shmData)); + (*data) = shmAdress; + } + } + + free(shmData); + + return status; +} + +celix_status_t discoveryShm_attach(shmData_pt* 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_pt 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_pt 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_pt 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_pt 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_pt 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_pt 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_pt data) { + celix_status_t status = CELIX_BUNDLE_EXCEPTION; + + if (data->numOfEntries == 0) { + status = discoveryShm_destroy(data); + } + else if (shmdt(shmAdress) == 0) { + status = CELIX_SUCCESS; + } + + return status; +} + +celix_status_t discoveryShm_destroy(shmData_pt data) { + celix_status_t status = CELIX_BUNDLE_EXCEPTION; + + if (shmctl(data->shmId, IPC_RMID, 0) == 0) { + status = CELIX_SUCCESS; + } + + return status; + +} http://git-wip-us.apache.org/repos/asf/celix/blob/2a670f26/remote_services/discovery_shm/src/discovery_shm.h ---------------------------------------------------------------------- diff --git a/remote_services/discovery_shm/src/discovery_shm.h b/remote_services/discovery_shm/src/discovery_shm.h new file mode 100644 index 0000000..9c4593b --- /dev/null +++ b/remote_services/discovery_shm/src/discovery_shm.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. + */ + +/* + * shm.h + * + * \date 26 Jul 2014 + * \author <a href="mailto:[email protected]">Apache Celix Project Team</a> + * \copyright Apache License, Version 2.0 + */ + + + +#ifndef _DISCOVERY_SHM_H_ +#define _DISCOVERY_SHM_H_ + +#include <celix_errno.h> + +#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_pt; + +/* creates a new shared memory block */ +celix_status_t discoveryShm_create(shmData_pt* data); +celix_status_t discoveryShm_attach(shmData_pt* data); +celix_status_t discoveryShm_set(shmData_pt data, char *key, char* value); +celix_status_t discoveryShm_get(shmData_pt data, char* key, char* value); +celix_status_t discoveryShm_getKeys(shmData_pt data, char** keys, int* size); +celix_status_t discoveryShm_remove(shmData_pt data, char* key); +celix_status_t discoveryShm_detach(shmData_pt data); +celix_status_t discoveryShm_destroy(shmData_pt data); + +#endif http://git-wip-us.apache.org/repos/asf/celix/blob/2a670f26/remote_services/discovery_shm/src/discovery_shmWatcher.c ---------------------------------------------------------------------- diff --git a/remote_services/discovery_shm/src/discovery_shmWatcher.c b/remote_services/discovery_shm/src/discovery_shmWatcher.c new file mode 100644 index 0000000..6460de8 --- /dev/null +++ b/remote_services/discovery_shm/src/discovery_shmWatcher.c @@ -0,0 +1,246 @@ +/** + * 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 <a href="mailto:[email protected]">Apache Celix Project Team</a> + * \copyright Apache License, Version 2.0 + */ + +#include <stdbool.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <stdio.h> + + +#include "celix_log.h" +#include "constants.h" +#include "discovery_impl.h" + +#include "discovery_shm.h" +#include "discovery_shmWatcher.h" + +#include "endpoint_discovery_poller.h" + +struct shm_watcher { + shmData_pt 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(bundle_context_pt 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_pt discovery) { + celix_status_t status = CELIX_SUCCESS; + shm_watcher_pt watcher = discovery->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_pt discovery = (discovery_pt) data; + shm_watcher_pt watcher = discovery->watcher; + char localNodePath[MAX_LOCALNODE_LENGTH]; + char url[MAX_LOCALNODE_LENGTH]; + + if (discoveryShmWatcher_getLocalNodePath(discovery->context, &localNodePath[0]) != CELIX_SUCCESS) { + logHelper_log(discovery->loghelper, OSGI_LOGSERVICE_WARNING, "Cannot retrieve local discovery path."); + } + + if (endpointDiscoveryServer_getUrl(discovery->server, &url[0]) != 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) { + logHelper_log(discovery->loghelper, OSGI_LOGSERVICE_WARNING, "Cannot set local discovery registration."); + } + + discoveryShmWatcher_syncEndpoints(discovery); + sleep(5); + } + + return NULL; +} + +celix_status_t discoveryShmWatcher_create(discovery_pt discovery) { + celix_status_t status = CELIX_SUCCESS; + shm_watcher_pt watcher = NULL; + + watcher = calloc(1, sizeof(*watcher)); + + if (!watcher) { + status = CELIX_ENOMEM; + } else { + status = discoveryShm_attach(&(watcher->shmData)); + + if (status != CELIX_SUCCESS) { + logHelper_log(discovery->loghelper, OSGI_LOGSERVICE_DEBUG, "Attaching to Shared Memory Failed. Trying to create."); + + status = discoveryShm_create(&(watcher->shmData)); + + if (status != CELIX_SUCCESS) { + logHelper_log(discovery->loghelper, OSGI_LOGSERVICE_ERROR, "Failed to create Shared Memory Segment."); + } + } + + if (status == CELIX_SUCCESS) { + discovery->watcher = watcher; + } + else{ + discovery->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_pt discovery) { + celix_status_t status; + shm_watcher_pt watcher = discovery->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 { + logHelper_log(discovery->loghelper, OSGI_LOGSERVICE_WARNING, "Cannot remove local discovery registration."); + } + + + return status; +} + http://git-wip-us.apache.org/repos/asf/celix/blob/2a670f26/remote_services/discovery_shm/src/discovery_shmWatcher.h ---------------------------------------------------------------------- diff --git a/remote_services/discovery_shm/src/discovery_shmWatcher.h b/remote_services/discovery_shm/src/discovery_shmWatcher.h new file mode 100644 index 0000000..ff70f72 --- /dev/null +++ b/remote_services/discovery_shm/src/discovery_shmWatcher.h @@ -0,0 +1,40 @@ +/** + * 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 <a href="mailto:[email protected]">Apache Celix Project Team</a> + * \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_pt; + +celix_status_t discoveryShmWatcher_create(discovery_pt discovery); +celix_status_t discoveryShmWatcher_destroy(discovery_pt discovery); + + +#endif /* DISCOVERY_SHM_WATCHER_H_ */ http://git-wip-us.apache.org/repos/asf/celix/blob/2a670f26/remote_services/examples/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/remote_services/examples/CMakeLists.txt b/remote_services/examples/CMakeLists.txt index c7c5444..2b1f35f 100644 --- a/remote_services/examples/CMakeLists.txt +++ b/remote_services/examples/CMakeLists.txt @@ -17,6 +17,7 @@ celix_subproject(RSA_EXAMPLES "Option to enable building the RSA examples" ON DEPS LAUNCHER shell_tui log_writer RSA_TOPOLOGY_MANAGER) if (RSA_EXAMPLES) + add_subdirectory(calculator_api) add_subdirectory(calculator_service) add_subdirectory(calculator_shell) @@ -47,13 +48,13 @@ if (RSA_EXAMPLES) add_deploy(remote-services-dfi NAME "server" GROUP "remote-services/remote-services-dfi" - BUNDLES discovery_etcd topology_manager remote_service_admin_dfi calculator Celix::shell Celix::shell_tui log_service log_writer + BUNDLES discovery_etcd topology_manager remote_service_admin_dfi calculator Celix::shell Celix::shell_tui Celix::log_service Celix::log_writer_stdout ) add_deploy("remote-services-dfi-client" NAME "client" GROUP "remote-services/remote-services-dfi" - BUNDLES topology_manager remote_service_admin_dfi Celix::shell Celix::shell_tui log_service log_writer calculator_shell discovery_etcd + BUNDLES topology_manager remote_service_admin_dfi Celix::shell Celix::shell_tui Celix::log_service Celix::log_writer_stdout calculator_shell discovery_etcd ) endif () endif (RSA_EXAMPLES) http://git-wip-us.apache.org/repos/asf/celix/blob/2a670f26/remote_services/examples/calculator_api/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/remote_services/examples/calculator_api/CMakeLists.txt b/remote_services/examples/calculator_api/CMakeLists.txt new file mode 100644 index 0000000..76acfa7 --- /dev/null +++ b/remote_services/examples/calculator_api/CMakeLists.txt @@ -0,0 +1,22 @@ +# 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. + +add_library(calculator_api INTERFACE) +target_include_directories(calculator_api INTERFACE include) +set_target_properties(calculator_api PROPERTIES + "INTERFACE_CALCULATOR_DESCRIPTOR" + "${CMAKE_CURRENT_LIST_DIR}/include/org.apache.celix.calc.api.Calculator2.descriptor") \ No newline at end of file http://git-wip-us.apache.org/repos/asf/celix/blob/2a670f26/remote_services/examples/calculator_api/include/calculator_service.h ---------------------------------------------------------------------- diff --git a/remote_services/examples/calculator_api/include/calculator_service.h b/remote_services/examples/calculator_api/include/calculator_service.h new file mode 100644 index 0000000..8e2f0dc --- /dev/null +++ b/remote_services/examples/calculator_api/include/calculator_service.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. + */ +/* + * calculator_service.h + * + * \date Oct 5, 2011 + * \author <a href="mailto:[email protected]">Apache Celix Project Team</a> + * \copyright Apache License, Version 2.0 + */ + +#ifndef CALCULATOR_SERVICE_H_ +#define CALCULATOR_SERVICE_H_ + +#define CALCULATOR_SERVICE "org.apache.celix.calc.api.Calculator" +#define CALCULATOR2_SERVICE "org.apache.celix.calc.api.Calculator2" + + +typedef struct calculator *calculator_pt; + +typedef struct calculator_service *calculator_service_pt; + +/* + * The calculator service definition corresponds to the following Java interface: + * + * interface Calculator { + * double add(double a, double b); + * double sub(double a, double b); + * double sqrt(double a); + * } + */ +struct calculator_service { + calculator_pt calculator; + int (*add)(calculator_pt calculator, double a, double b, double *result); + int (*sub)(calculator_pt calculator, double a, double b, double *result); + int (*sqrt)(calculator_pt calculator, double a, double *result); +}; + + + +#endif /* CALCULATOR_SERVICE_H_ */ http://git-wip-us.apache.org/repos/asf/celix/blob/2a670f26/remote_services/examples/calculator_api/include/org.apache.celix.calc.api.Calculator2.descriptor ---------------------------------------------------------------------- diff --git a/remote_services/examples/calculator_api/include/org.apache.celix.calc.api.Calculator2.descriptor b/remote_services/examples/calculator_api/include/org.apache.celix.calc.api.Calculator2.descriptor new file mode 100644 index 0000000..b784838 --- /dev/null +++ b/remote_services/examples/calculator_api/include/org.apache.celix.calc.api.Calculator2.descriptor @@ -0,0 +1,11 @@ +:header +type=interface +name=calculator +version=1.3.0 +:annotations +classname=org.example.Calculator +:types +:methods +add(DD)D=add(#am=handle;PDD#am=pre;*D)N +sub(DD)D=sub(#am=handle;PDD#am=pre;*D)N +sqrt(D)D=sqrt(#am=handle;PD#am=pre;*D)N http://git-wip-us.apache.org/repos/asf/celix/blob/2a670f26/remote_services/examples/calculator_service/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/remote_services/examples/calculator_service/CMakeLists.txt b/remote_services/examples/calculator_service/CMakeLists.txt index 5e8040c..ff4d2d7 100644 --- a/remote_services/examples/calculator_service/CMakeLists.txt +++ b/remote_services/examples/calculator_service/CMakeLists.txt @@ -15,21 +15,15 @@ # specific language governing permissions and limitations # under the License. -include_directories("../../../utils/public/include") -include_directories("../../utils/public/include") -include_directories("../../remote_service_admin/public/include") -include_directories("private/include") -include_directories("public/include") - -add_bundle(calculator SOURCES - private/src/calculator_impl - private/src/calculator_activator - - private/include/calculator_impl.h - +add_bundle(calculator + SOURCES + src/calculator_impl + src/calculator_activator SYMBOLIC_NAME "apache_celix_remoting_calculator_impl" VERSION 0.0.1 ) +target_include_directories(calculator PRIVATE src) +target_link_libraries(calculator PRIVATE Celix::remote_service_admin_api calculator_api) -bundle_files(calculator public/include/org.apache.celix.calc.api.Calculator2.descriptor - DESTINATION .) +get_target_property(DESCR calculator_api INTERFACE_CALCULATOR_DESCRIPTOR) +bundle_files(calculator ${DESCR} DESTINATION .)
