You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@celix.apache.org by pn...@apache.org on 2013/06/30 12:22:25 UTC

svn commit: r1498079 - in /incubator/celix/trunk/remote_services: ./ discovery_bonjour/ discovery_bonjour/private/ discovery_bonjour/private/include/ discovery_bonjour/private/src/ discovery_slp/ discovery_slp/private/ discovery_slp/private/include/ di...

Author: pnoltes
Date: Sun Jun 30 10:22:24 2013
New Revision: 1498079

URL: http://svn.apache.org/r1498079
Log:
CELIX-65: moved slp discovery to discovery_slp directory and added a - net yet complete - discovery_bonjour. CMakeLists.txt file still use discovery_slp as default.

Added:
    incubator/celix/trunk/remote_services/discovery_bonjour/
    incubator/celix/trunk/remote_services/discovery_bonjour/CMakeLists.txt   (with props)
    incubator/celix/trunk/remote_services/discovery_bonjour/private/
    incubator/celix/trunk/remote_services/discovery_bonjour/private/include/
    incubator/celix/trunk/remote_services/discovery_bonjour/private/include/discovery.h   (with props)
    incubator/celix/trunk/remote_services/discovery_bonjour/private/src/
    incubator/celix/trunk/remote_services/discovery_bonjour/private/src/discovery.c   (with props)
    incubator/celix/trunk/remote_services/discovery_bonjour/private/src/discovery_activator.c   (with props)
    incubator/celix/trunk/remote_services/discovery_slp/
    incubator/celix/trunk/remote_services/discovery_slp/CMakeLists.txt   (with props)
    incubator/celix/trunk/remote_services/discovery_slp/private/
    incubator/celix/trunk/remote_services/discovery_slp/private/include/
    incubator/celix/trunk/remote_services/discovery_slp/private/include/discovery.h   (with props)
    incubator/celix/trunk/remote_services/discovery_slp/private/src/
    incubator/celix/trunk/remote_services/discovery_slp/private/src/discovery.c   (with props)
    incubator/celix/trunk/remote_services/discovery_slp/private/src/discovery_activator.c   (with props)
Modified:
    incubator/celix/trunk/remote_services/CMakeLists.txt
    incubator/celix/trunk/remote_services/deploy.cmake

Modified: incubator/celix/trunk/remote_services/CMakeLists.txt
URL: http://svn.apache.org/viewvc/incubator/celix/trunk/remote_services/CMakeLists.txt?rev=1498079&r1=1498078&r2=1498079&view=diff
==============================================================================
--- incubator/celix/trunk/remote_services/CMakeLists.txt (original)
+++ incubator/celix/trunk/remote_services/CMakeLists.txt Sun Jun 30 10:22:24 2013
@@ -33,7 +33,8 @@ if (REMOTE_SERVICE_ADMIN)
     
     add_subdirectory(topology_manager)
     add_subdirectory(remote_service_admin)
-    add_subdirectory(discovery)
+    add_subdirectory(discovery_slp)
+    #add_subdirectory(discovery_bonjour)
     
     add_subdirectory(example_service)
     add_subdirectory(example_endpoint)

Modified: incubator/celix/trunk/remote_services/deploy.cmake
URL: http://svn.apache.org/viewvc/incubator/celix/trunk/remote_services/deploy.cmake?rev=1498079&r1=1498078&r2=1498079&view=diff
==============================================================================
--- incubator/celix/trunk/remote_services/deploy.cmake (original)
+++ incubator/celix/trunk/remote_services/deploy.cmake Sun Jun 30 10:22:24 2013
@@ -16,6 +16,9 @@
 # under the License.
 is_enabled(REMOTE_SERVICE_ADMIN)
 if (REMOTE_SERVICE_ADMIN)
-	 deploy("remote-services" BUNDLES discovery topology_manager remote_service_admin example example_endpoint shell shell_tui log_service log_writer)
-	deploy("remote-services-client" BUNDLES discovery topology_manager remote_service_admin example_proxy shell shell_tui log_service log_writer calc_shell)
+	#deploy("remote-services" BUNDLES discovery_bonjour topology_manager remote_service_admin example example_endpoint shell shell_tui log_service log_writer)
+	#deploy("remote-services-client" BUNDLES discovery_bonjour topology_manager remote_service_admin example_proxy shell shell_tui log_service log_writer calc_shell)
+	
+	deploy("remote-services" BUNDLES discovery_slp topology_manager remote_service_admin example example_endpoint shell shell_tui log_service log_writer)
+	deploy("remote-services-client" BUNDLES discovery_slp topology_manager remote_service_admin example_proxy shell shell_tui log_service log_writer calc_shell)
 endif (REMOTE_SERVICE_ADMIN)

Added: incubator/celix/trunk/remote_services/discovery_bonjour/CMakeLists.txt
URL: http://svn.apache.org/viewvc/incubator/celix/trunk/remote_services/discovery_bonjour/CMakeLists.txt?rev=1498079&view=auto
==============================================================================
--- incubator/celix/trunk/remote_services/discovery_bonjour/CMakeLists.txt (added)
+++ incubator/celix/trunk/remote_services/discovery_bonjour/CMakeLists.txt Sun Jun 30 10:22:24 2013
@@ -0,0 +1,38 @@
+# 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.
+
+#TODO find_package(DNS-SD REQUIRED)
+
+#TODO include_directories(${SLP_INCLUDE_DIRS})
+include_directories("/usr/include") #TODO check if this has impact on the generated project indexer paths
+include_directories("${PROJECT_SOURCE_DIR}/utils/public/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/endpoint_listener/public/include")
+include_directories("${PROJECT_SOURCE_DIR}/remote_services/remote_service_admin/public/include")
+include_directories(private/include)
+
+SET_HEADER(BUNDLE_SYMBOLICNAME "apache_celix_rsa_discovery_bonjour")
+SET_HEADERS("Bundle-Name: Apache Celix RSA BONJOUR Discovery")
+
+bundle(discovery_bonjour SOURCES 
+	private/src/discovery 
+	private/src/discovery_activator
+)
+
+target_link_libraries(discovery_bonjour celix_framework ${APRUTIL_LIBRARY}) # TODO ${SLP_LIBRARIES})
+

Propchange: incubator/celix/trunk/remote_services/discovery_bonjour/CMakeLists.txt
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/celix/trunk/remote_services/discovery_bonjour/private/include/discovery.h
URL: http://svn.apache.org/viewvc/incubator/celix/trunk/remote_services/discovery_bonjour/private/include/discovery.h?rev=1498079&view=auto
==============================================================================
--- incubator/celix/trunk/remote_services/discovery_bonjour/private/include/discovery.h (added)
+++ incubator/celix/trunk/remote_services/discovery_bonjour/private/include/discovery.h Sun Jun 30 10:22:24 2013
@@ -0,0 +1,48 @@
+/**
+ *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.
+ */
+/*
+ * topology_manager.h
+ *
+ *  \date       Sep 29, 2011
+ *  \author    	<a href="mailto:celix-dev@incubator.apache.org">Apache Celix Project Team</a>
+ *  \copyright	Apache License, Version 2.0
+ */
+
+#ifndef DISCOVERY_H_
+#define DISCOVERY_H_
+
+#include "endpoint_listener.h"
+
+typedef struct discovery *discovery_pt;
+
+celix_status_t discovery_create(apr_pool_t *pool, bundle_context_pt context, discovery_pt *discovery);
+celix_status_t discovery_stop(discovery_pt discovery);
+
+celix_status_t discovery_endpointAdded(void *handle, endpoint_description_pt endpoint, char *machtedFilter);
+celix_status_t discovery_endpointRemoved(void *handle, endpoint_description_pt endpoint, char *machtedFilter);
+
+celix_status_t discovery_endpointListenerAdding(void * handle, service_reference_pt reference, void **service);
+celix_status_t discovery_endpointListenerAdded(void * handle, service_reference_pt reference, void * service);
+celix_status_t discovery_endpointListenerModified(void * handle, service_reference_pt reference, void * service);
+celix_status_t discovery_endpointListenerRemoved(void * handle, service_reference_pt reference, void * service);
+
+celix_status_t discovery_updateEndpointListener(discovery_pt discovery, service_reference_pt reference, endpoint_listener_pt service);
+
+
+#endif /* DISCOVERY_H_ */

Propchange: incubator/celix/trunk/remote_services/discovery_bonjour/private/include/discovery.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/celix/trunk/remote_services/discovery_bonjour/private/src/discovery.c
URL: http://svn.apache.org/viewvc/incubator/celix/trunk/remote_services/discovery_bonjour/private/src/discovery.c?rev=1498079&view=auto
==============================================================================
--- incubator/celix/trunk/remote_services/discovery_bonjour/private/src/discovery.c (added)
+++ incubator/celix/trunk/remote_services/discovery_bonjour/private/src/discovery.c Sun Jun 30 10:22:24 2013
@@ -0,0 +1,328 @@
+/**
+ *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.c
+ *
+ *  \date       Oct 4, 2011
+ *  \author    	<a href="mailto:celix-dev@incubator.apache.org">Apache Celix Project Team</a>
+ *  \copyright	Apache License, Version 2.0
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <apr_strings.h>
+#include <unistd.h>
+#include <stdbool.h>
+
+#include <dns_sd.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 "discovery.h"
+
+static void *APR_THREAD_FUNC discovery_poll(apr_thread_t *thd, void *data);
+static void discovery_browseCallback(
+		    DNSServiceRef sdRef,
+		    DNSServiceFlags flags,
+		    uint32_t interfaceIndex,
+		    DNSServiceErrorType errorCode,
+		    const char                          *serviceName,
+		    const char                          *regtype,
+		    const char                          *replyDomain,
+		    void                                *context
+		);
+static void discovery_resolveCallback
+(
+    DNSServiceRef sdRef,
+    DNSServiceFlags flags,
+    uint32_t interfaceIndex,
+    DNSServiceErrorType errorCode,
+    const char                          *fullname,
+    const char                          *hosttarget,
+    uint16_t port,                                   /* In network byte order */
+    uint16_t txtLen,
+    const unsigned char                 *txtRecord,
+    void                                *context
+);
+static celix_status_t discovery_deregisterEndpoint(discovery_pt discovery, const char *serviceUrl);
+static celix_status_t discovery_addService(discovery_pt discovery, endpoint_description_pt endpoint);
+static celix_status_t discovery_removeService(discovery_pt discovery, endpoint_description_pt endpoint);
+
+static const char * const DEFAULT_RSA_PORT = "555";
+static const char * const OSGI_SERVICE_TYPE = "_osgi._udp";
+
+struct discovery {
+	bundle_context_pt context;
+	apr_pool_t *pool;
+
+	hash_map_pt listenerReferences;
+
+	bool running;
+	apr_thread_t *poll;
+	DNSServiceRef browseRef;
+
+	hash_map_pt slpServices;
+
+	char *rsaPort;
+
+	array_list_pt handled;
+	array_list_pt registered;
+};
+
+celix_status_t discovery_create(apr_pool_t *pool, bundle_context_pt context, discovery_pt *discovery) {
+	celix_status_t status = CELIX_SUCCESS;
+
+	*discovery = apr_palloc(pool, sizeof(**discovery));
+	if (!*discovery) {
+		status = CELIX_ENOMEM;
+	} else {
+		(*discovery)->context = context;
+		(*discovery)->pool = pool;
+		(*discovery)->listenerReferences = hashMap_create(serviceReference_hashCode, NULL, serviceReference_equals2, NULL);
+		(*discovery)->slpServices = hashMap_create(string_hash, NULL, string_equals, NULL);
+		(*discovery)->running = true;
+		(*discovery)->browseRef = NULL;
+		(*discovery)->rsaPort = getenv("RSA_PORT");
+		if ((*discovery)->rsaPort == NULL) {
+			printf("No RemoteServiceAdmin port set, set it using RSA_PORT! Using default port (%s)\n", DEFAULT_RSA_PORT);
+			(*discovery)->rsaPort = DEFAULT_RSA_PORT;
+		}
+		(*discovery)->handled = NULL;
+		arrayList_create(pool, &(*discovery)->handled);
+		(*discovery)->registered = NULL;
+		arrayList_create(pool, &(*discovery)->registered);
+
+		DNSServiceErrorType error = DNSServiceBrowse(
+				 &(*discovery)->browseRef,
+				   0,
+				   0,
+				   OSGI_SERVICE_TYPE,
+				   NULL, /* may be NULL */
+				   discovery_browseCallback,
+				   (*discovery)/* may be NULL */
+				   );
+
+		apr_thread_create(&(*discovery)->poll, NULL, discovery_poll, *discovery, (*discovery)->pool);
+	}
+
+	return status;
+}
+
+celix_status_t discovery_stop(discovery_pt discovery) {
+	celix_status_t status = CELIX_SUCCESS;
+
+	apr_status_t tstat;
+	discovery->running = false;
+	DNSServiceRefDeallocate(discovery->browseRef);
+	apr_status_t stat = apr_thread_join(&tstat, discovery->poll);
+	if (stat != APR_SUCCESS && tstat != APR_SUCCESS) {
+		status = CELIX_BUNDLE_EXCEPTION;
+	}
+
+	int i;
+	for (i = 0; i < arrayList_size(discovery->registered); i++) {
+		char *url = arrayList_get(discovery->registered, i);
+		discovery_deregisterEndpoint(discovery, url);
+	}
+
+	return status;
+}
+
+celix_status_t discovery_endpointAdded(void *handle, endpoint_description_pt endpoint, char *machtedFilter) {
+	celix_status_t status = CELIX_SUCCESS;
+	discovery_pt discovery = handle;
+
+	printf("discovery_endpointAdded CALLED\n");
+
+	DNSServiceRef sdRef = NULL;
+	DNSServiceErrorType error;
+	TXTRecordRef txtRecord;
+
+    TXTRecordCreate (&txtRecord, 256, NULL);//TODO search for correct default record size
+    char serviceId[16];
+    sprintf(serviceId, "%li", endpoint->serviceId);
+
+    TXTRecordSetValue(&txtRecord, "service", strlen(endpoint->service), endpoint->service);
+    TXTRecordSetValue(&txtRecord, "service.id", strlen(serviceId), serviceId);
+
+    hash_map_iterator_pt iter = hashMapIterator_create(endpoint->properties);
+    while (hashMapIterator_hasNext(iter))  {
+    	hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);
+    	char *key = hashMapEntry_getKey(entry);
+    	char *value = hashMapEntry_getValue(entry);
+    	TXTRecordSetValue(&txtRecord, key, strlen(value), value);
+    }
+
+    int port = atoi(discovery->rsaPort);
+    int portInNetworkByteOrder = ((port << 8) & 0xFF00) | ((port >> 8) & 0xFF); //TODO assuming little endian ?? correct? check with ifdef?
+
+    error = DNSServiceRegister (
+       &sdRef,
+       0,
+       0,
+       endpoint->service, /* may be NULL */
+       OSGI_SERVICE_TYPE,
+       NULL, /* may be NULL */
+       NULL, /* may be NULL */
+       portInNetworkByteOrder, /* In network byte order */
+       TXTRecordGetLength(&txtRecord),
+       TXTRecordGetBytesPtr(&txtRecord), /* may be NULL */
+       NULL, /* may be NULL */
+       NULL /* may be NULL */
+       );
+
+
+    printf("Registered record in dns-sd got error code %i\n", error);
+
+	return status;
+}
+
+celix_status_t discovery_endpointRemoved(void *handle, endpoint_description_pt endpoint, char *machtedFilter) {
+	celix_status_t status = CELIX_SUCCESS;
+	return status;
+}
+
+celix_status_t discovery_endpointListenerAdding(void * handle, service_reference_pt reference, void **service) {
+	celix_status_t status = CELIX_SUCCESS;
+	discovery_pt discovery = handle;
+
+	bundleContext_getService(discovery->context, reference, service);
+
+	return status;
+}
+
+celix_status_t discovery_endpointListenerAdded(void * handle, service_reference_pt reference, void * service) {
+	celix_status_t status = CELIX_SUCCESS;
+	discovery_pt discovery = handle;
+
+	service_registration_pt registration = NULL;
+	serviceReference_getServiceRegistration(reference, &registration);
+	properties_pt serviceProperties = NULL;
+	serviceRegistration_getProperties(registration, &serviceProperties);
+	char *discoveryListener = properties_get(serviceProperties, "DISCOVERY");
+
+	if (discoveryListener != NULL && strcmp(discoveryListener, "true") == 0) {
+		printf("DISCOVERY: EndpointListener Ignored - Discovery listener\n");
+	} else {
+		printf("DISCOVERY: EndpointListener Added - Add Scope\n");
+		discovery_updateEndpointListener(discovery, reference, (endpoint_listener_pt) service);
+	}
+
+	return status;
+}
+
+celix_status_t discovery_endpointListenerModified(void * handle, service_reference_pt reference, void * service) {
+	celix_status_t status = CELIX_SUCCESS;
+	discovery_pt discovery = handle;
+
+	printf("DISCOVERY: EndpointListener Modified - Update Scope TODO\n");
+
+	return status;
+}
+
+
+
+celix_status_t discovery_endpointListenerRemoved(void * handle, service_reference_pt reference, void * service) {
+	celix_status_t status = CELIX_SUCCESS;
+	discovery_pt discovery = handle;
+
+	printf("DISCOVERY: EndpointListener Removed\n");
+	hashMap_remove(discovery->listenerReferences, reference);
+
+	return status;
+}
+
+celix_status_t discovery_updateEndpointListener(discovery_pt discovery,
+		service_reference_pt reference, endpoint_listener_pt service) {
+	celix_status_t status = CELIX_SUCCESS;
+	return status;
+}
+
+static void *APR_THREAD_FUNC discovery_poll(apr_thread_t *thd, void *data) {
+	discovery_pt discovery = data;
+
+	while (discovery->running) {
+		DNSServiceProcessResult(discovery->browseRef);
+	}
+	apr_thread_exit(thd, APR_SUCCESS);
+
+	return NULL;
+}
+
+static void discovery_browseCallback(DNSServiceRef sdRef, DNSServiceFlags flags,
+		uint32_t interfaceIndex, DNSServiceErrorType errorCode,
+		const char *serviceName, const char *regtype, const char *replyDomain,
+		void *context) {
+	discovery_pt discovery = context;
+	if (flags & kDNSServiceFlagsAdd) {
+		printf("Added service with %s %s %s\n", serviceName, regtype,
+				replyDomain);
+		DNSServiceRef resolveRef = NULL;
+		DNSServiceErrorType resolveError = DNSServiceResolve(&resolveRef, 0, 0,
+				serviceName, regtype, replyDomain, discovery_resolveCallback,
+				context);
+		printf("Resolve return with error %i\n", resolveError);
+		DNSServiceProcessResult(resolveRef);
+	} else {
+		printf("Removed service with %s %s %s\n", serviceName, regtype,
+				replyDomain);
+	}
+}
+
+static void discovery_resolveCallback(DNSServiceRef sdRef,
+		DNSServiceFlags flags, uint32_t interfaceIndex,
+		DNSServiceErrorType errorCode, const char *fullname,
+		const char *hosttarget, uint16_t port, /* In network byte order */
+		uint16_t txtLen, const unsigned char *txtRecord, void *context) {
+	printf("In resolve callback!\n");
+	int length = TXTRecordGetCount(txtLen, txtRecord);
+	printf("Found txt record with item count %i\n|", length);
+	for (int i=0; i<length; i+=1) {
+		char key[128];
+		void *value = NULL;
+		int valueSize = 0;
+		TXTRecordGetItemAtIndex(txtLen, txtRecord, i, 128, key, &valueSize, &value);
+		printf("Found key=value %s=%s\n", key, value);
+	}
+}
+
+static celix_status_t discovery_deregisterEndpoint(discovery_pt discovery, const char *serviceUrl) {
+	celix_status_t status = CELIX_SUCCESS;
+	printf("DISCOVERY: Remove endpoint: %s\n", serviceUrl);
+	printf("TODO\n");
+
+	return status;
+}
+
+static celix_status_t discovery_addService(discovery_pt discovery, endpoint_description_pt endpoint) {
+	celix_status_t status = CELIX_SUCCESS;
+	//TODO should be called when dns-sd find a services, forward to listeners
+	return status;
+}
+
+static celix_status_t discovery_removeService(discovery_pt discovery, endpoint_description_pt endpoint) {
+	celix_status_t status = CELIX_SUCCESS;
+	//TODO should be called when dns-sd notices a removal of a services, forward to listeners
+	return status;
+}

Propchange: incubator/celix/trunk/remote_services/discovery_bonjour/private/src/discovery.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/celix/trunk/remote_services/discovery_bonjour/private/src/discovery_activator.c
URL: http://svn.apache.org/viewvc/incubator/celix/trunk/remote_services/discovery_bonjour/private/src/discovery_activator.c?rev=1498079&view=auto
==============================================================================
--- incubator/celix/trunk/remote_services/discovery_bonjour/private/src/discovery_activator.c (added)
+++ incubator/celix/trunk/remote_services/discovery_bonjour/private/src/discovery_activator.c Sun Jun 30 10:22:24 2013
@@ -0,0 +1,152 @@
+/**
+ *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.
+ */
+/*
+ * dependency_activator.c
+ *
+ *  \date       Sep 29, 2011
+ *  \author    	<a href="mailto:celix-dev@incubator.apache.org">Apache Celix Project Team</a>
+ *  \copyright	Apache License, Version 2.0
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <apr_strings.h>
+#include <apr_uuid.h>
+
+#include "bundle_activator.h"
+#include "service_tracker.h"
+#include "service_registration.h"
+#include "constants.h"
+
+#include "discovery.h"
+#include "endpoint_listener.h"
+#include "remote_constants.h"
+
+struct activator {
+	apr_pool_t *pool;
+	bundle_context_pt context;
+
+	discovery_pt discovery;
+
+	service_tracker_pt endpointListenerTracker;
+	service_registration_pt endpointListenerService;
+};
+
+celix_status_t discoveryActivator_createEPLTracker(struct activator *activator, service_tracker_pt *tracker);
+celix_status_t discoveryActivator_getUUID(struct activator *activator, char **uuidStr);
+
+celix_status_t bundleActivator_create(bundle_context_pt context, void **userData) {
+	celix_status_t status = CELIX_SUCCESS;
+	apr_pool_t *parentPool = NULL;
+	apr_pool_t *pool = NULL;
+	struct activator *activator = NULL;
+
+	bundleContext_getMemoryPool(context, &parentPool);
+	apr_pool_create(&pool, parentPool);
+	activator = apr_palloc(pool, sizeof(*activator));
+	if (!activator) {
+		status = CELIX_ENOMEM;
+	} else {
+		activator->pool = pool;
+		activator->context = context;
+		activator->endpointListenerTracker = NULL;
+		activator->endpointListenerService = NULL;
+
+		discovery_create(pool, context, &activator->discovery);
+
+		discoveryActivator_createEPLTracker(activator, &activator->endpointListenerTracker);
+
+		*userData = activator;
+	}
+
+	return status;
+}
+
+celix_status_t discoveryActivator_createEPLTracker(struct activator *activator,  service_tracker_pt *tracker) {
+	celix_status_t status = CELIX_SUCCESS;
+
+	service_tracker_customizer_pt customizer = NULL;
+
+	status = serviceTrackerCustomizer_create(activator->pool, activator->discovery, discovery_endpointListenerAdding,
+			discovery_endpointListenerAdded, discovery_endpointListenerModified, discovery_endpointListenerRemoved, &customizer);
+
+	if (status == CELIX_SUCCESS) {
+		status = serviceTracker_create(activator->pool, activator->context, "endpoint_listener", customizer, tracker);
+
+		serviceTracker_open(activator->endpointListenerTracker);
+	}
+
+	return status;
+}
+
+celix_status_t bundleActivator_start(void * userData, bundle_context_pt context) {
+	celix_status_t status = CELIX_SUCCESS;
+	struct activator *activator = userData;
+	apr_pool_t *pool = NULL;
+	apr_pool_create(&pool, activator->pool);
+
+	endpoint_listener_pt endpointListener = apr_palloc(pool, sizeof(*endpointListener));
+	endpointListener->handle = activator->discovery;
+	endpointListener->endpointAdded = discovery_endpointAdded;
+	endpointListener->endpointRemoved = discovery_endpointRemoved;
+
+	properties_pt props = properties_create();
+	properties_set(props, "DISCOVERY", "true");
+	char *uuid = NULL;
+	discoveryActivator_getUUID(activator, &uuid);
+	char *scope = apr_pstrcat(activator->pool, "(&(", OBJECTCLASS, "=*)(", ENDPOINT_FRAMEWORK_UUID, "=", uuid, "))", NULL);
+	properties_set(props, (char *) ENDPOINT_LISTENER_SCOPE, scope);
+	status = bundleContext_registerService(context, (char *) endpoint_listener_service, endpointListener, props, &activator->endpointListenerService);
+
+	return status;
+}
+
+celix_status_t bundleActivator_stop(void * userData, bundle_context_pt context) {
+	celix_status_t status = CELIX_SUCCESS;
+	struct activator *activator = userData;
+
+	discovery_stop(activator->discovery);
+	serviceTracker_close(activator->endpointListenerTracker);
+	serviceRegistration_unregister(activator->endpointListenerService);
+
+	return status;
+}
+
+celix_status_t bundleActivator_destroy(void * userData, bundle_context_pt context) {
+	celix_status_t status = CELIX_SUCCESS;
+	return status;
+}
+
+celix_status_t discoveryActivator_getUUID(struct activator *activator, char **uuidStr) {
+	celix_status_t status = CELIX_SUCCESS;
+
+	status = bundleContext_getProperty(activator->context, ENDPOINT_FRAMEWORK_UUID, uuidStr);
+	if (status == CELIX_SUCCESS) {
+		if (*uuidStr == NULL) {
+			apr_uuid_t uuid;
+			apr_uuid_get(&uuid);
+			*uuidStr = apr_palloc(activator->pool, APR_UUID_FORMATTED_LENGTH + 1);
+			apr_uuid_format(*uuidStr, &uuid);
+			setenv(ENDPOINT_FRAMEWORK_UUID, *uuidStr, 1);
+		}
+	}
+
+	return status;
+}

Propchange: incubator/celix/trunk/remote_services/discovery_bonjour/private/src/discovery_activator.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/celix/trunk/remote_services/discovery_slp/CMakeLists.txt
URL: http://svn.apache.org/viewvc/incubator/celix/trunk/remote_services/discovery_slp/CMakeLists.txt?rev=1498079&view=auto
==============================================================================
--- incubator/celix/trunk/remote_services/discovery_slp/CMakeLists.txt (added)
+++ incubator/celix/trunk/remote_services/discovery_slp/CMakeLists.txt Sun Jun 30 10:22:24 2013
@@ -0,0 +1,37 @@
+# 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(Slp REQUIRED)
+
+include_directories(${SLP_INCLUDE_DIRS})
+include_directories("${PROJECT_SOURCE_DIR}/utils/public/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/endpoint_listener/public/include")
+include_directories("${PROJECT_SOURCE_DIR}/remote_services/remote_service_admin/public/include")
+include_directories("private/include")
+
+SET_HEADER(BUNDLE_SYMBOLICNAME "apache_celix_rsa_discovery_slp")
+SET_HEADERS("Bundle-Name: Apache Celix RSA SLP Discovery")
+
+bundle(discovery_slp SOURCES 
+	private/src/discovery 
+	private/src/discovery_activator
+)
+
+target_link_libraries(discovery_slp celix_framework ${APRUTIL_LIBRARY} ${SLP_LIBRARIES})
+

Propchange: incubator/celix/trunk/remote_services/discovery_slp/CMakeLists.txt
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/celix/trunk/remote_services/discovery_slp/private/include/discovery.h
URL: http://svn.apache.org/viewvc/incubator/celix/trunk/remote_services/discovery_slp/private/include/discovery.h?rev=1498079&view=auto
==============================================================================
--- incubator/celix/trunk/remote_services/discovery_slp/private/include/discovery.h (added)
+++ incubator/celix/trunk/remote_services/discovery_slp/private/include/discovery.h Sun Jun 30 10:22:24 2013
@@ -0,0 +1,48 @@
+/**
+ *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.
+ */
+/*
+ * topology_manager.h
+ *
+ *  \date       Sep 29, 2011
+ *  \author    	<a href="mailto:celix-dev@incubator.apache.org">Apache Celix Project Team</a>
+ *  \copyright	Apache License, Version 2.0
+ */
+
+#ifndef DISCOVERY_H_
+#define DISCOVERY_H_
+
+#include "endpoint_listener.h"
+
+typedef struct discovery *discovery_pt;
+
+celix_status_t discovery_create(apr_pool_t *pool, bundle_context_pt context, discovery_pt *discovery);
+celix_status_t discovery_stop(discovery_pt discovery);
+
+celix_status_t discovery_endpointAdded(void *handle, endpoint_description_pt endpoint, char *machtedFilter);
+celix_status_t discovery_endpointRemoved(void *handle, endpoint_description_pt endpoint, char *machtedFilter);
+
+celix_status_t discovery_endpointListenerAdding(void * handle, service_reference_pt reference, void **service);
+celix_status_t discovery_endpointListenerAdded(void * handle, service_reference_pt reference, void * service);
+celix_status_t discovery_endpointListenerModified(void * handle, service_reference_pt reference, void * service);
+celix_status_t discovery_endpointListenerRemoved(void * handle, service_reference_pt reference, void * service);
+
+celix_status_t discovery_updateEndpointListener(discovery_pt discovery, service_reference_pt reference, endpoint_listener_pt service);
+
+
+#endif /* DISCOVERY_H_ */

Propchange: incubator/celix/trunk/remote_services/discovery_slp/private/include/discovery.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/celix/trunk/remote_services/discovery_slp/private/src/discovery.c
URL: http://svn.apache.org/viewvc/incubator/celix/trunk/remote_services/discovery_slp/private/src/discovery.c?rev=1498079&view=auto
==============================================================================
--- incubator/celix/trunk/remote_services/discovery_slp/private/src/discovery.c (added)
+++ incubator/celix/trunk/remote_services/discovery_slp/private/src/discovery.c Sun Jun 30 10:22:24 2013
@@ -0,0 +1,493 @@
+/**
+ *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.c
+ *
+ *  \date       Oct 4, 2011
+ *  \author    	<a href="mailto:celix-dev@incubator.apache.org">Apache Celix Project Team</a>
+ *  \copyright	Apache License, Version 2.0
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <apr_strings.h>
+#include <slp.h>
+#include <unistd.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 "discovery.h"
+
+struct discovery {
+	bundle_context_pt context;
+	apr_pool_t *pool;
+
+	hash_map_pt listenerReferences;
+
+	bool running;
+	apr_thread_t *slpPoll;
+
+	hash_map_pt slpServices;
+
+	char *rsaPort;
+
+	array_list_pt handled;
+	array_list_pt registered;
+};
+
+struct slp_service {
+	char *serviceUrl;
+	char *attributes;
+};
+
+typedef struct slp_service *slp_service_pt;
+
+celix_status_t discovery_informListener(discovery_pt discovery, endpoint_listener_pt listener, endpoint_description_pt endpoint);
+celix_status_t discovery_informListenerOfRemoval(discovery_pt discovery, endpoint_listener_pt listener, endpoint_description_pt endpoint);
+
+celix_status_t discovery_addService(discovery_pt discovery, endpoint_description_pt endpoint);
+celix_status_t discovery_removeService(discovery_pt discovery, endpoint_description_pt endpoint);
+
+static void *APR_THREAD_FUNC discovery_pollSLP(apr_thread_t *thd, void *data);
+SLPBoolean discovery_pollSLPCallback(SLPHandle hslp, const char* srvurl, unsigned short lifetime, SLPError errcode, void* cookie);
+SLPBoolean discovery_attributesCallback(SLPHandle hslp, const char *attributes, SLPError error, void *cookie);
+
+celix_status_t discovery_deregisterEndpoint(discovery_pt discovery, const char *serviceUrl);
+void discovery_deregistrationReport(SLPHandle hslp, SLPError errcode, void* cookie);
+
+celix_status_t discovery_create(apr_pool_t *pool, bundle_context_pt context, discovery_pt *discovery) {
+	celix_status_t status = CELIX_SUCCESS;
+
+	*discovery = apr_palloc(pool, sizeof(**discovery));
+	if (!*discovery) {
+		status = CELIX_ENOMEM;
+	} else {
+		(*discovery)->context = context;
+		(*discovery)->pool = pool;
+		(*discovery)->listenerReferences = hashMap_create(serviceReference_hashCode, NULL, serviceReference_equals2, NULL);
+		(*discovery)->slpServices = hashMap_create(string_hash, NULL, string_equals, NULL);
+		(*discovery)->running = true;
+		(*discovery)->rsaPort = getenv("RSA_PORT");
+		if ((*discovery)->rsaPort == NULL) {
+			printf("No RemoteServiceAdmin port set, set it using RSA_PORT!\n");
+		}
+		(*discovery)->handled = NULL;
+		arrayList_create(pool, &(*discovery)->handled);
+		(*discovery)->registered = NULL;
+		arrayList_create(pool, &(*discovery)->registered);
+
+		apr_thread_create(&(*discovery)->slpPoll, NULL, discovery_pollSLP, *discovery, (*discovery)->pool);
+	}
+
+	return status;
+}
+
+celix_status_t discovery_deregisterEndpoint(discovery_pt discovery, const char *serviceUrl) {
+	celix_status_t status = CELIX_SUCCESS;
+	printf("DISCOVERY: Remove endpoint: %s\n", serviceUrl);
+
+	SLPError err;
+	SLPError callbackerr;
+	SLPHandle slp;
+
+	err = SLPOpen("en", SLP_FALSE, &slp);
+	if (err != SLP_OK) {
+		status = CELIX_BUNDLE_EXCEPTION;
+	} else {
+		err = SLPDereg(slp, serviceUrl, discovery_deregistrationReport, &callbackerr);
+		if ((err != SLP_OK) || (callbackerr != SLP_OK)) {
+			printf("DISCOVERY: Error deregistering service (%s) with slp %i\n", serviceUrl, err);
+			status = CELIX_BUNDLE_EXCEPTION;
+		}
+		SLPClose(slp);
+	}
+
+	return status;
+}
+
+celix_status_t discovery_stop(discovery_pt discovery) {
+	celix_status_t status = CELIX_SUCCESS;
+
+	apr_status_t tstat;
+	discovery->running = false;
+	apr_status_t stat = apr_thread_join(&tstat, discovery->slpPoll);
+	if (stat != APR_SUCCESS && tstat != APR_SUCCESS) {
+		status = CELIX_BUNDLE_EXCEPTION;
+	}
+
+	int i;
+	for (i = 0; i < arrayList_size(discovery->registered); i++) {
+		char *url = arrayList_get(discovery->registered, i);
+		discovery_deregisterEndpoint(discovery, url);
+	}
+
+	return status;
+}
+
+celix_status_t discovery_removeService(discovery_pt discovery, endpoint_description_pt endpoint) {
+	celix_status_t status = CELIX_SUCCESS;
+	printf("DISCOVERY: Remove service (%s)\n", endpoint->service);
+
+	// Inform listeners of new endpoint
+	hash_map_iterator_pt iter = hashMapIterator_create(discovery->listenerReferences);
+	while (hashMapIterator_hasNext(iter)) {
+		hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);
+		service_reference_pt reference = hashMapEntry_getKey(entry);
+		endpoint_listener_pt listener = NULL;
+		bundleContext_getService(discovery->context, reference, (void**)&listener);
+		discovery_informListenerOfRemoval(discovery, listener, endpoint);
+	}
+
+	return status;
+}
+
+celix_status_t discovery_addService(discovery_pt discovery, endpoint_description_pt endpoint) {
+	celix_status_t status = CELIX_SUCCESS;
+
+	// Inform listeners of new endpoint
+	hash_map_iterator_pt iter = hashMapIterator_create(discovery->listenerReferences);
+	while (hashMapIterator_hasNext(iter)) {
+		hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);
+		service_reference_pt reference = hashMapEntry_getKey(entry);
+		endpoint_listener_pt listener = NULL;
+
+		service_registration_pt registration = NULL;
+		serviceReference_getServiceRegistration(reference, &registration);
+		properties_pt serviceProperties = NULL;
+		serviceRegistration_getProperties(registration, &serviceProperties);
+		char *scope = properties_get(serviceProperties, (char *) ENDPOINT_LISTENER_SCOPE);
+		filter_pt filter = filter_create(scope, discovery->pool);
+		bool matchResult = false;
+		filter_match(filter, endpoint->properties, &matchResult);
+		if (matchResult) {
+			printf("DISCOVERY: Add service (%s)\n", endpoint->service);
+			bundleContext_getService(discovery->context, reference, (void**)&listener);
+			discovery_informListener(discovery, listener, endpoint);
+		}
+	}
+
+	return status;
+}
+
+celix_status_t discovery_informListener(discovery_pt discovery, endpoint_listener_pt listener, endpoint_description_pt endpoint) {
+	celix_status_t status = CELIX_SUCCESS;
+	listener->endpointAdded(listener->handle, endpoint, NULL);
+	return status;
+}
+
+celix_status_t discovery_informListenerOfRemoval(discovery_pt discovery, endpoint_listener_pt listener, endpoint_description_pt endpoint) {
+	celix_status_t status = CELIX_SUCCESS;
+	listener->endpointRemoved(listener->handle, endpoint, NULL);
+	return status;
+}
+
+celix_status_t discovery_constructServiceUrl(discovery_pt discovery, endpoint_description_pt endpoint, char **serviceUrl) {
+	celix_status_t status = CELIX_SUCCESS;
+
+	if (*serviceUrl != NULL || discovery == NULL || endpoint == NULL) {
+		status = CELIX_ILLEGAL_ARGUMENT;
+	} else {
+		char host[APRMAXHOSTLEN + 1];
+		apr_sockaddr_t *sa;
+		char *ip;
+
+		apr_status_t stat = apr_gethostname(host, APRMAXHOSTLEN + 1, discovery->pool);
+		if (stat != APR_SUCCESS) {
+			status = CELIX_BUNDLE_EXCEPTION;
+		} else {
+			stat = apr_sockaddr_info_get(&sa, host, APR_INET, 0, 0, discovery->pool);
+			if (stat != APR_SUCCESS) {
+				status = CELIX_BUNDLE_EXCEPTION;
+			} else {
+				stat = apr_sockaddr_ip_get(&ip, sa);
+				if (stat != APR_SUCCESS) {
+					status = CELIX_BUNDLE_EXCEPTION;
+				} else {
+					*serviceUrl = apr_pstrcat(discovery->pool, "service:osgi.remote:http://", ip, ":", discovery->rsaPort, "/services/", endpoint->service, NULL);
+				}
+			}
+		}
+	}
+
+	return status;
+}
+
+void discovery_registrationReport(SLPHandle hslp, SLPError errcode, void* cookie) {
+	*(SLPError*)cookie = errcode;
+}
+
+celix_status_t discovery_endpointAdded(void *handle, endpoint_description_pt endpoint, char *machtedFilter) {
+	celix_status_t status = CELIX_SUCCESS;
+	printf("DISCOVERY: Endpoint for %s, with filter \"%s\" added\n", endpoint->service, machtedFilter);
+	discovery_pt discovery = handle;
+	SLPError err;
+	SLPError callbackerr;
+	SLPHandle slp;
+	char *serviceUrl = NULL;
+
+	err = SLPOpen("en", SLP_FALSE, &slp);
+	if (err != SLP_OK) {
+		status = CELIX_ILLEGAL_STATE;
+	} else {
+		status = discovery_constructServiceUrl(discovery, endpoint, &serviceUrl);
+		if (status == CELIX_SUCCESS) {
+			char *attributes = "";
+			hash_map_iterator_pt iter = hashMapIterator_create(endpoint->properties);
+			while (hashMapIterator_hasNext(iter)) {
+				hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);
+				char *key = hashMapEntry_getKey(entry);
+				char *value = hashMapEntry_getValue(entry);
+				if (strlen(attributes) != 0) {
+					attributes = apr_pstrcat(discovery->pool, attributes, ",", NULL);
+				}
+				attributes = apr_pstrcat(discovery->pool, attributes, "(", key, "=", value, ")", NULL);
+			}
+			err = SLPReg(slp, serviceUrl, SLP_LIFETIME_MAXIMUM, 0, attributes, SLP_TRUE, discovery_registrationReport, &callbackerr);
+			if ((err != SLP_OK) || (callbackerr != SLP_OK)) {
+				status = CELIX_ILLEGAL_STATE;
+				printf("DISCOVERY: Error registering service (%s) with slp %i\n", serviceUrl, err);
+			}
+			arrayList_add(discovery->registered, serviceUrl);
+		}
+		SLPClose(slp);
+	}
+
+	return status;
+}
+
+void discovery_deregistrationReport(SLPHandle hslp, SLPError errcode, void* cookie) {
+	*(SLPError*)cookie = errcode;
+}
+
+celix_status_t discovery_endpointRemoved(void *handle, endpoint_description_pt endpoint, char *machtedFilter) {
+	celix_status_t status = CELIX_SUCCESS;
+	printf("DISCOVERY: Endpoint for %s, with filter \"%s\" removed\n", endpoint->service, machtedFilter);
+
+	discovery_pt discovery = handle;
+	SLPError err;
+	SLPError callbackerr;
+	SLPHandle slp;
+	char *serviceUrl = NULL;
+
+	err = SLPOpen("en", SLP_FALSE, &slp);
+	if (err != SLP_OK) {
+		status = CELIX_ILLEGAL_STATE;
+	} else {
+		status = discovery_constructServiceUrl(discovery, endpoint, &serviceUrl);
+		if (status == CELIX_SUCCESS) {
+			status = discovery_deregisterEndpoint(discovery, serviceUrl);
+			int i;
+			for (i = 0; i < arrayList_size(discovery->registered); i++) {
+				char *url = arrayList_get(discovery->registered, i);
+				if (strcmp(url, serviceUrl) == 0) {
+					arrayList_remove(discovery->registered, i);
+				}
+			}
+		}
+	}
+
+	return status;
+}
+
+celix_status_t discovery_endpointListenerAdding(void * handle, service_reference_pt reference, void **service) {
+	celix_status_t status = CELIX_SUCCESS;
+	discovery_pt discovery = handle;
+
+	bundleContext_getService(discovery->context, reference, service);
+
+	return status;
+}
+
+celix_status_t discovery_endpointListenerAdded(void * handle, service_reference_pt reference, void * service) {
+	celix_status_t status = CELIX_SUCCESS;
+	discovery_pt discovery = handle;
+
+	service_registration_pt registration = NULL;
+	serviceReference_getServiceRegistration(reference, &registration);
+	properties_pt serviceProperties = NULL;
+	serviceRegistration_getProperties(registration, &serviceProperties);
+	char *discoveryListener = properties_get(serviceProperties, "DISCOVERY");
+
+	if (discoveryListener != NULL && strcmp(discoveryListener, "true") == 0) {
+		printf("DISCOVERY: EndpointListener Ignored - Discovery listener\n");
+	} else {
+		printf("DISCOVERY: EndpointListener Added - Add Scope\n");
+		discovery_updateEndpointListener(discovery, reference, (endpoint_listener_pt) service);
+	}
+
+	return status;
+}
+
+celix_status_t discovery_endpointListenerModified(void * handle, service_reference_pt reference, void * service) {
+	celix_status_t status = CELIX_SUCCESS;
+	discovery_pt discovery = handle;
+
+	printf("DISCOVERY: EndpointListener Modified - Update Scope\n");
+	discovery_updateEndpointListener(discovery, reference, (endpoint_listener_pt) service);
+
+	return status;
+}
+
+celix_status_t discovery_updateEndpointListener(discovery_pt discovery, service_reference_pt reference, endpoint_listener_pt service) {
+	celix_status_t status = CELIX_SUCCESS;
+	char *scope = "createScopeHere";
+
+	array_list_pt scopes = hashMap_get(discovery->listenerReferences, reference);
+	if (scopes == NULL) {
+		scopes = NULL;
+		arrayList_create(discovery->pool, &scopes);
+		hashMap_put(discovery->listenerReferences, reference, scopes);
+	}
+
+	if (!arrayList_contains(scopes, scope)) {
+		arrayList_add(scopes, scope);
+	}
+
+	hash_map_iterator_pt iter = hashMapIterator_create(discovery->slpServices);
+	while (hashMapIterator_hasNext(iter)) {
+		hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);
+		char *key = hashMapEntry_getKey(entry);
+		endpoint_description_pt value = hashMapEntry_getValue(entry);
+		discovery_informListener(discovery, service, value);
+	}
+
+	return status;
+}
+
+celix_status_t discovery_endpointListenerRemoved(void * handle, service_reference_pt reference, void * service) {
+	celix_status_t status = CELIX_SUCCESS;
+	discovery_pt discovery = handle;
+
+	printf("DISCOVERY: EndpointListener Removed\n");
+	hashMap_remove(discovery->listenerReferences, reference);
+
+	return status;
+}
+
+static void *APR_THREAD_FUNC discovery_pollSLP(apr_thread_t *thd, void *data) {
+	discovery_pt discovery = data;
+	SLPHandle slp;
+	SLPError err;
+
+	err = SLPOpen("en", SLP_FALSE, &slp);
+
+	while (discovery->running) {
+		SLPError err = SLP_TRUE;
+		SLPError callbackerr;
+		arrayList_clear(discovery->handled);
+		while (err == SLP_TRUE) {
+			err = SLPFindSrvs(slp, "osgi.remote", 0, 0, discovery_pollSLPCallback, data);
+		}
+
+		hash_map_iterator_pt iter = hashMapIterator_create(discovery->slpServices);
+		while (hashMapIterator_hasNext(iter)) {
+			hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);
+			char *key = hashMapEntry_getKey(entry);
+			endpoint_description_pt value = hashMapEntry_getValue(entry);
+
+			bool inUse = false;
+			int i;
+			for (i = 0; i < arrayList_size(discovery->handled); i++) {
+				char *url = arrayList_get(discovery->handled, i);
+				if (strcmp(url, key) == 0) {
+					inUse = true;
+					break;
+				}
+			}
+
+			if (!inUse) {
+				discovery_removeService(discovery, value);
+
+				hashMapIterator_remove(iter);
+			}
+		}
+
+		sleep(1);
+	}
+	SLPClose(slp);
+	apr_thread_exit(thd, APR_SUCCESS);
+
+	return NULL;
+}
+
+SLPBoolean discovery_pollSLPCallback(SLPHandle hslp, const char* srvurl, unsigned short lifetime, SLPError errcode, void *cookie) {
+	discovery_pt discovery = cookie;
+	if (errcode == SLP_OK) {
+
+		arrayList_add(discovery->handled, (void *) srvurl);
+		if (!hashMap_containsKey(discovery->slpServices, (void *) srvurl)) {
+			// service:osgi.remote:http://10.0.0.21:8080/services/example
+			if (strncmp(srvurl, "service:osgi.remote:", 20) == 0) {
+				const char *url = srvurl+20;
+				const char *srv = strrchr(url, '/')+1;
+
+				SLPHandle handle = NULL;
+				SLPError err = SLPOpen("en", SLP_FALSE, &handle);
+				err = SLP_TRUE;
+				slp_service_pt slpService = apr_palloc(discovery->pool, sizeof(*slpService));
+				while (err == SLP_TRUE) {
+					err = SLPFindAttrs(handle, srvurl, "", "", discovery_attributesCallback, slpService);
+				}
+
+				properties_pt props = properties_create();
+				char *track;
+				char *token = apr_strtok(slpService->attributes, ",", &track);
+				while (token != NULL) {
+					char *track2;
+					char *token2 = apr_strtok(token, "=", &track2);
+					char *token3 = apr_strtok(NULL, "=", &track2);
+					char *key = apr_pstrdup(discovery->pool, token2+1);
+					char *value = apr_pstrndup(discovery->pool, token3, strlen(token3) - 1);
+					properties_set(props, key, value);
+					token = apr_strtok(NULL, ",", &track);
+				}
+
+				endpoint_description_pt endpoint = apr_palloc(discovery->pool, sizeof(*endpoint));
+				endpoint->id = apr_pstrdup(discovery->pool, url);
+				endpoint->serviceId = 42;
+				endpoint->service = apr_pstrdup(discovery->pool, srv);
+				endpoint->properties = props;
+				discovery_addService(discovery, endpoint);
+
+				hashMap_put(discovery->slpServices, apr_pstrdup(discovery->pool, srvurl), endpoint);
+			}
+		}
+	} else if (errcode == SLP_LAST_CALL) {
+		return SLP_FALSE;
+	} else {
+	}
+
+	return SLP_TRUE;
+}
+
+SLPBoolean discovery_attributesCallback(SLPHandle hslp, const char *attributes, SLPError error, void *cookie) {
+	slp_service_pt slpService = cookie;
+	if (error == SLP_OK) {
+		slpService->attributes = strdup(attributes);
+	} else if (error == SLP_LAST_CALL) {
+		return SLP_FALSE;
+	}
+
+	return SLP_TRUE;
+}

Propchange: incubator/celix/trunk/remote_services/discovery_slp/private/src/discovery.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/celix/trunk/remote_services/discovery_slp/private/src/discovery_activator.c
URL: http://svn.apache.org/viewvc/incubator/celix/trunk/remote_services/discovery_slp/private/src/discovery_activator.c?rev=1498079&view=auto
==============================================================================
--- incubator/celix/trunk/remote_services/discovery_slp/private/src/discovery_activator.c (added)
+++ incubator/celix/trunk/remote_services/discovery_slp/private/src/discovery_activator.c Sun Jun 30 10:22:24 2013
@@ -0,0 +1,152 @@
+/**
+ *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.
+ */
+/*
+ * dependency_activator.c
+ *
+ *  \date       Sep 29, 2011
+ *  \author    	<a href="mailto:celix-dev@incubator.apache.org">Apache Celix Project Team</a>
+ *  \copyright	Apache License, Version 2.0
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <apr_strings.h>
+#include <apr_uuid.h>
+
+#include "bundle_activator.h"
+#include "service_tracker.h"
+#include "service_registration.h"
+#include "constants.h"
+
+#include "discovery.h"
+#include "endpoint_listener.h"
+#include "remote_constants.h"
+
+struct activator {
+	apr_pool_t *pool;
+	bundle_context_pt context;
+
+	discovery_pt discovery;
+
+	service_tracker_pt endpointListenerTracker;
+	service_registration_pt endpointListenerService;
+};
+
+celix_status_t discoveryActivator_createEPLTracker(struct activator *activator, service_tracker_pt *tracker);
+celix_status_t discoveryActivator_getUUID(struct activator *activator, char **uuidStr);
+
+celix_status_t bundleActivator_create(bundle_context_pt context, void **userData) {
+	celix_status_t status = CELIX_SUCCESS;
+	apr_pool_t *parentPool = NULL;
+	apr_pool_t *pool = NULL;
+	struct activator *activator = NULL;
+
+	bundleContext_getMemoryPool(context, &parentPool);
+	apr_pool_create(&pool, parentPool);
+	activator = apr_palloc(pool, sizeof(*activator));
+	if (!activator) {
+		status = CELIX_ENOMEM;
+	} else {
+		activator->pool = pool;
+		activator->context = context;
+		activator->endpointListenerTracker = NULL;
+		activator->endpointListenerService = NULL;
+
+		discovery_create(pool, context, &activator->discovery);
+
+		discoveryActivator_createEPLTracker(activator, &activator->endpointListenerTracker);
+
+		*userData = activator;
+	}
+
+	return status;
+}
+
+celix_status_t discoveryActivator_createEPLTracker(struct activator *activator,  service_tracker_pt *tracker) {
+	celix_status_t status = CELIX_SUCCESS;
+
+	service_tracker_customizer_pt customizer = NULL;
+
+	status = serviceTrackerCustomizer_create(activator->pool, activator->discovery, discovery_endpointListenerAdding,
+			discovery_endpointListenerAdded, discovery_endpointListenerModified, discovery_endpointListenerRemoved, &customizer);
+
+	if (status == CELIX_SUCCESS) {
+		status = serviceTracker_create(activator->pool, activator->context, "endpoint_listener", customizer, tracker);
+
+		serviceTracker_open(activator->endpointListenerTracker);
+	}
+
+	return status;
+}
+
+celix_status_t bundleActivator_start(void * userData, bundle_context_pt context) {
+	celix_status_t status = CELIX_SUCCESS;
+	struct activator *activator = userData;
+	apr_pool_t *pool = NULL;
+	apr_pool_create(&pool, activator->pool);
+
+	endpoint_listener_pt endpointListener = apr_palloc(pool, sizeof(*endpointListener));
+	endpointListener->handle = activator->discovery;
+	endpointListener->endpointAdded = discovery_endpointAdded;
+	endpointListener->endpointRemoved = discovery_endpointRemoved;
+
+	properties_pt props = properties_create();
+	properties_set(props, "DISCOVERY", "true");
+	char *uuid = NULL;
+	discoveryActivator_getUUID(activator, &uuid);
+	char *scope = apr_pstrcat(activator->pool, "(&(", OBJECTCLASS, "=*)(", ENDPOINT_FRAMEWORK_UUID, "=", uuid, "))", NULL);
+	properties_set(props, (char *) ENDPOINT_LISTENER_SCOPE, scope);
+	status = bundleContext_registerService(context, (char *) endpoint_listener_service, endpointListener, props, &activator->endpointListenerService);
+
+	return status;
+}
+
+celix_status_t bundleActivator_stop(void * userData, bundle_context_pt context) {
+	celix_status_t status = CELIX_SUCCESS;
+	struct activator *activator = userData;
+
+	discovery_stop(activator->discovery);
+	serviceTracker_close(activator->endpointListenerTracker);
+	serviceRegistration_unregister(activator->endpointListenerService);
+
+	return status;
+}
+
+celix_status_t bundleActivator_destroy(void * userData, bundle_context_pt context) {
+	celix_status_t status = CELIX_SUCCESS;
+	return status;
+}
+
+celix_status_t discoveryActivator_getUUID(struct activator *activator, char **uuidStr) {
+	celix_status_t status = CELIX_SUCCESS;
+
+	status = bundleContext_getProperty(activator->context, ENDPOINT_FRAMEWORK_UUID, uuidStr);
+	if (status == CELIX_SUCCESS) {
+		if (*uuidStr == NULL) {
+			apr_uuid_t uuid;
+			apr_uuid_get(&uuid);
+			*uuidStr = apr_palloc(activator->pool, APR_UUID_FORMATTED_LENGTH + 1);
+			apr_uuid_format(*uuidStr, &uuid);
+			setenv(ENDPOINT_FRAMEWORK_UUID, *uuidStr, 1);
+		}
+	}
+
+	return status;
+}

Propchange: incubator/celix/trunk/remote_services/discovery_slp/private/src/discovery_activator.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain