celix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bpe...@apache.org
Subject svn commit: r1660038 - in /celix/trunk/examples: ./ producer_consumer/ producer_consumer/consumer/ producer_consumer/consumer/private/ producer_consumer/consumer/private/src/ producer_consumer/database/ producer_consumer/database/private/ producer_cons...
Date Mon, 16 Feb 2015 07:54:31 GMT
Author: bpetri
Date: Mon Feb 16 07:54:30 2015
New Revision: 1660038

URL: http://svn.apache.org/r1660038
Log:

Added new producer_consumer example


Added:
    celix/trunk/examples/producer_consumer/
    celix/trunk/examples/producer_consumer/CMakeLists.txt
    celix/trunk/examples/producer_consumer/consumer/
    celix/trunk/examples/producer_consumer/consumer/CMakeLists.txt
    celix/trunk/examples/producer_consumer/consumer/private/
    celix/trunk/examples/producer_consumer/consumer/private/src/
    celix/trunk/examples/producer_consumer/consumer/private/src/activator.c
    celix/trunk/examples/producer_consumer/database/
    celix/trunk/examples/producer_consumer/database/CMakeLists.txt
    celix/trunk/examples/producer_consumer/database/private/
    celix/trunk/examples/producer_consumer/database/private/include/
    celix/trunk/examples/producer_consumer/database/private/include/reader_service_impl.h
    celix/trunk/examples/producer_consumer/database/private/include/writer_service_impl.h
    celix/trunk/examples/producer_consumer/database/private/src/
    celix/trunk/examples/producer_consumer/database/private/src/activator.c
    celix/trunk/examples/producer_consumer/database/private/src/reader.c
    celix/trunk/examples/producer_consumer/database/private/src/writer.c
    celix/trunk/examples/producer_consumer/database/public/
    celix/trunk/examples/producer_consumer/database/public/include/
    celix/trunk/examples/producer_consumer/database/public/include/data.h
    celix/trunk/examples/producer_consumer/database/public/include/database.h
    celix/trunk/examples/producer_consumer/database/public/include/reader_service.h
    celix/trunk/examples/producer_consumer/database/public/include/writer_service.h
    celix/trunk/examples/producer_consumer/producer/
    celix/trunk/examples/producer_consumer/producer/CMakeLists.txt
    celix/trunk/examples/producer_consumer/producer/private/
    celix/trunk/examples/producer_consumer/producer/private/src/
    celix/trunk/examples/producer_consumer/producer/private/src/activator.c
Modified:
    celix/trunk/examples/CMakeLists.txt
    celix/trunk/examples/deploy.cmake

Modified: celix/trunk/examples/CMakeLists.txt
URL: http://svn.apache.org/viewvc/celix/trunk/examples/CMakeLists.txt?rev=1660038&r1=1660037&r2=1660038&view=diff
==============================================================================
--- celix/trunk/examples/CMakeLists.txt (original)
+++ celix/trunk/examples/CMakeLists.txt Mon Feb 16 07:54:30 2015
@@ -22,6 +22,7 @@ if (EXAMPLES)
     
     add_subdirectory(whiteboard)
     add_subdirectory(echo_service)
+    add_subdirectory(producer_consumer)
     
     add_subdirectory(osgi-in-action/chapter04-correct-lookup)
     add_subdirectory(osgi-in-action/chapter04-correct-listener)

Modified: celix/trunk/examples/deploy.cmake
URL: http://svn.apache.org/viewvc/celix/trunk/examples/deploy.cmake?rev=1660038&r1=1660037&r2=1660038&view=diff
==============================================================================
--- celix/trunk/examples/deploy.cmake (original)
+++ celix/trunk/examples/deploy.cmake Mon Feb 16 07:54:30 2015
@@ -24,5 +24,6 @@ if (EXAMPLES)
 	deploy("wb" BUNDLES tracker publisherA publisherB shell shell_tui log_service log_writer)
 	deploy("wb_dp" BUNDLES tracker_depman publisherA publisherB shell shell_tui log_service
log_writer)
 	deploy("echo" BUNDLES echo_server echo_client shell shell_tui)
+	deploy("producer_consumer" BUNDLES producer consumer database shell shell_tui)
 	deploy("mongoose" BUNDLES shell shell_tui log_service apache_celix_examples_mongoose)
-endif (EXAMPLES)
+endif (EXAMPLES)
\ No newline at end of file

Added: celix/trunk/examples/producer_consumer/CMakeLists.txt
URL: http://svn.apache.org/viewvc/celix/trunk/examples/producer_consumer/CMakeLists.txt?rev=1660038&view=auto
==============================================================================
--- celix/trunk/examples/producer_consumer/CMakeLists.txt (added)
+++ celix/trunk/examples/producer_consumer/CMakeLists.txt Mon Feb 16 07:54:30 2015
@@ -0,0 +1,20 @@
+# 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_subdirectory(database)
+add_subdirectory(producer)
+add_subdirectory(consumer)

Added: celix/trunk/examples/producer_consumer/consumer/CMakeLists.txt
URL: http://svn.apache.org/viewvc/celix/trunk/examples/producer_consumer/consumer/CMakeLists.txt?rev=1660038&view=auto
==============================================================================
--- celix/trunk/examples/producer_consumer/consumer/CMakeLists.txt (added)
+++ celix/trunk/examples/producer_consumer/consumer/CMakeLists.txt Mon Feb 16 07:54:30 2015
@@ -0,0 +1,31 @@
+# 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.
+
+set(BUNDLE_SYMBOLICNAME "apache_celix_examples_consumer")
+set(BUNDLE_VERSION "1.0.0")
+set(BUNDLE_NAME "Apache Celix Consumer")
+
+
+include_directories("${PROJECT_SOURCE_DIR}/utils/public/include")
+include_directories("${PROJECT_SOURCE_DIR}/examples/producer_consumer/database/public/include")
+
+bundle(consumer
+	SOURCES
+	 	private/src/activator.c
+)
+
+target_link_libraries(consumer ${CELIX_LIBRARIES})

Added: celix/trunk/examples/producer_consumer/consumer/private/src/activator.c
URL: http://svn.apache.org/viewvc/celix/trunk/examples/producer_consumer/consumer/private/src/activator.c?rev=1660038&view=auto
==============================================================================
--- celix/trunk/examples/producer_consumer/consumer/private/src/activator.c (added)
+++ celix/trunk/examples/producer_consumer/consumer/private/src/activator.c Mon Feb 16 07:54:30
2015
@@ -0,0 +1,195 @@
+/**
+ * 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.
+ */
+
+/*
+ * activator.c
+ *
+ *  \date       16 Feb 2015
+ *  \author     <a href="mailto:dev@celix.apache.org">Apache Celix Project Team</a>
+ *  \copyright  Apache License, Version 2.0
+ */
+
+#include <stdlib.h>
+
+#include <unistd.h>
+
+#include <sys/time.h>
+
+#include <bundle_activator.h>
+#include <service_tracker.h>
+#include <constants.h>
+
+#include <array_list.h>
+
+#include "reader_service.h"
+#include "writer_service.h"
+#include "data.h"
+
+struct activator {
+    service_tracker_pt readerTracker;
+    service_tracker_pt writerTracker;
+    array_list_pt readerServices;
+    array_list_pt writerServices;
+    bool running;
+    celix_thread_t worker;
+};
+
+celix_status_t readerServiceAdded(void *handle, service_reference_pt reference, void *service);
+celix_status_t readerServiceRemoved(void *handle, service_reference_pt reference, void *service);
+
+celix_status_t writerServiceAdded(void *handle, service_reference_pt reference, void *service);
+celix_status_t writerServiceRemoved(void *handle, service_reference_pt reference, void *service);
+
+void *retrieveData(void *handle);
+
+celix_status_t bundleActivator_create(bundle_context_pt context, void **userData) {
+    celix_status_t status = CELIX_SUCCESS;
+    *userData = calloc(1, sizeof(struct activator));
+    if (*userData) {
+        ((struct activator *) *userData)->readerTracker = NULL;
+        ((struct activator *) *userData)->writerTracker = NULL;
+        ((struct activator *) *userData)->readerServices = NULL;
+        ((struct activator *) *userData)->writerServices = NULL;
+        ((struct activator *) *userData)->running = false;
+        ((struct activator *) *userData)->worker = celix_thread_default;
+    } else {
+        status = CELIX_ENOMEM;
+    }
+    return status;
+}
+
+celix_status_t bundleActivator_start(void * userData, bundle_context_pt context) {
+    celix_status_t status = CELIX_SUCCESS;
+    struct activator *activator = userData;
+
+    // create list for services
+    arrayList_create(&activator->readerServices);
+    arrayList_create(&activator->writerServices);
+
+    // start the thread
+    activator->running = true;
+    status = celixThread_create(&activator->worker, NULL, retrieveData, activator);
+
+    if (status == CELIX_SUCCESS) {
+        service_tracker_customizer_pt readerCustomizer = NULL;
+        service_tracker_customizer_pt writerCustomizer = NULL;
+        serviceTrackerCustomizer_create(userData, NULL, readerServiceAdded, NULL, readerServiceRemoved,
&readerCustomizer);
+        serviceTrackerCustomizer_create(userData, NULL, writerServiceAdded, NULL, writerServiceRemoved,
&writerCustomizer);
+
+        serviceTracker_create(context, WRITER_SERVICE_NAME, writerCustomizer, &activator->writerTracker);
+        serviceTracker_open(activator->writerTracker);
+
+        serviceTracker_create(context, READER_SERVICE_NAME, readerCustomizer, &activator->readerTracker);
+        serviceTracker_open(activator->readerTracker);
+    }
+
+    return status;
+}
+
+celix_status_t bundleActivator_stop(void * userData, bundle_context_pt context) {
+    celix_status_t status = CELIX_SUCCESS;
+    struct activator *activator = userData;
+
+    // stop the thread
+    activator->running = false;
+
+    celixThread_join(activator->worker, NULL);
+
+    serviceTracker_close(activator->readerTracker);
+    serviceTracker_close(activator->writerTracker);
+
+    serviceTracker_destroy(activator->readerTracker);
+    serviceTracker_destroy(activator->writerTracker);
+
+    // destroy the list of services
+    arrayList_destroy(activator->readerServices);
+    arrayList_destroy(activator->writerServices);
+
+    return status;
+}
+
+celix_status_t bundleActivator_destroy(void * userData, bundle_context_pt context) {
+    celix_status_t status = CELIX_SUCCESS;
+    struct activator *activator = userData;
+
+    free(activator);
+
+    return status;
+}
+
+celix_status_t readerServiceAdded(void *handle, service_reference_pt reference, void *service)
{
+    struct activator *activator = handle;
+    arrayList_add(activator->readerServices, service);
+    printf("Consumer: Reader Service Added.\n");
+
+    return CELIX_SUCCESS;
+}
+
+celix_status_t readerServiceRemoved(void *handle, service_reference_pt reference, void *service)
{
+    struct activator *activator = handle;
+    arrayList_removeElement(activator->readerServices, service);
+    printf("Consumer: Reader Service Removed.\n");
+
+    return CELIX_SUCCESS;
+}
+
+celix_status_t writerServiceAdded(void *handle, service_reference_pt reference, void *service)
{
+    struct activator *activator = handle;
+    arrayList_add(activator->writerServices, service);
+    printf("Consumer: Writer Service Added.\n");
+
+    return CELIX_SUCCESS;
+}
+
+celix_status_t writerServiceRemoved(void *handle, service_reference_pt reference, void *service)
{
+    struct activator *activator = handle;
+    arrayList_removeElement(activator->writerServices, service);
+    printf("Consumer: Writer Service Removed.\n");
+
+    return CELIX_SUCCESS;
+}
+
+void *retrieveData(void *handle) {
+    struct activator *activator = handle;
+
+    while (activator->running) {
+        int i;
+        for (i = 0; i < arrayList_size(activator->readerServices); i++) {
+            reader_service_pt service = arrayList_get(activator->readerServices, i);
+            data_pt retrievedData = NULL;
+
+            if (service->readerService_getNextData(service->handler, &retrievedData)
== CELIX_SUCCESS) {
+                printf(" Data #%d received.", retrievedData->id);
+
+                writer_service_pt writerService = arrayList_get(activator->writerServices,
0);
+                if (writerService && writerService->writerService_removeData(writerService->handler,
&retrievedData) == CELIX_SUCCESS) {
+                    printf(" and removed\n");
+                }
+
+            } else {
+                printf(" No data available\n");
+                sleep(5);
+            }
+
+        }
+
+    }
+
+    return NULL;
+}

Added: celix/trunk/examples/producer_consumer/database/CMakeLists.txt
URL: http://svn.apache.org/viewvc/celix/trunk/examples/producer_consumer/database/CMakeLists.txt?rev=1660038&view=auto
==============================================================================
--- celix/trunk/examples/producer_consumer/database/CMakeLists.txt (added)
+++ celix/trunk/examples/producer_consumer/database/CMakeLists.txt Mon Feb 16 07:54:30 2015
@@ -0,0 +1,34 @@
+# 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.
+
+set(BUNDLE_SYMBOLICNAME "apache_celix_examples_database")
+set(BUNDLE_VERSION "1.0.0")
+set(BUNDLE_NAME "Apache Celix Database")
+
+
+include_directories("${PROJECT_SOURCE_DIR}/utils/public/include")
+include_directories("${PROJECT_SOURCE_DIR}/examples/producer_consumer/database/private/include")
+include_directories("${PROJECT_SOURCE_DIR}/examples/producer_consumer/database/public/include")
+
+bundle(database 
+	SOURCES private/src/activator 
+		private/src/writer 
+		private/src/reader
+)
+
+target_link_libraries(database pthread ${CELIX_LIBRARIES})
+

Added: celix/trunk/examples/producer_consumer/database/private/include/reader_service_impl.h
URL: http://svn.apache.org/viewvc/celix/trunk/examples/producer_consumer/database/private/include/reader_service_impl.h?rev=1660038&view=auto
==============================================================================
--- celix/trunk/examples/producer_consumer/database/private/include/reader_service_impl.h
(added)
+++ celix/trunk/examples/producer_consumer/database/private/include/reader_service_impl.h
Mon Feb 16 07:54:30 2015
@@ -0,0 +1,39 @@
+/**
+ * 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.
+ */
+
+/*
+ * reader_service_impl.h
+ *
+ *  \date       16 Feb 2015
+ *  \author     <a href="mailto:dev@celix.apache.org">Apache Celix Project Team</a>
+ *  \copyright  Apache License, Version 2.0
+ */
+
+
+
+#ifndef READER_SERVICE_IMPL_H_
+#define READER_SERVICE_IMPL_H_
+
+#include "data.h"
+#include "database.h"
+
+celix_status_t readerService_getFirstData(database_handler_pt, data_pt firstData);
+celix_status_t readerService_getNextData(database_handler_pt, data_pt* nextData);
+
+#endif

Added: celix/trunk/examples/producer_consumer/database/private/include/writer_service_impl.h
URL: http://svn.apache.org/viewvc/celix/trunk/examples/producer_consumer/database/private/include/writer_service_impl.h?rev=1660038&view=auto
==============================================================================
--- celix/trunk/examples/producer_consumer/database/private/include/writer_service_impl.h
(added)
+++ celix/trunk/examples/producer_consumer/database/private/include/writer_service_impl.h
Mon Feb 16 07:54:30 2015
@@ -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.
+ */
+
+/*
+ * writer_service_impl.h
+ *
+ *  \date       16 Feb 2015
+ *  \author     <a href="mailto:dev@celix.apache.org">Apache Celix Project Team</a>
+ *  \copyright  Apache License, Version 2.0
+ */
+
+
+
+#ifndef WRITER_SERVICE_IMPL_H_
+#define WRITER_SERVICE_IMPL_H_
+
+#include "data.h"
+#include "database.h"
+
+celix_status_t writerService_storeData(database_handler_pt, data_pt data);
+celix_status_t writerService_removeData(database_handler_pt, data_pt* data);
+
+
+#endif

Added: celix/trunk/examples/producer_consumer/database/private/src/activator.c
URL: http://svn.apache.org/viewvc/celix/trunk/examples/producer_consumer/database/private/src/activator.c?rev=1660038&view=auto
==============================================================================
--- celix/trunk/examples/producer_consumer/database/private/src/activator.c (added)
+++ celix/trunk/examples/producer_consumer/database/private/src/activator.c Mon Feb 16 07:54:30
2015
@@ -0,0 +1,138 @@
+/**
+ * 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.
+ */
+
+/*
+ * activator.c
+ *
+ *  \date       16 Feb 2015
+ *  \author     <a href="mailto:dev@celix.apache.org">Apache Celix Project Team</a>
+ *  \copyright  Apache License, Version 2.0
+ */
+
+
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <sys/time.h>
+
+#include <bundle_activator.h>
+#include <service_tracker.h>
+#include <constants.h>
+
+#include <array_list.h>
+#include <pthread.h>
+
+#include "reader_service.h"
+#include "reader_service_impl.h"
+#include "writer_service.h"
+#include "writer_service_impl.h"
+
+#include "database.h"
+
+struct activator {
+	database_handler_pt databaseHandler;
+
+	service_registration_pt readerRegistration;
+	service_registration_pt writerRegistration;
+
+	reader_service_pt readerService;
+	writer_service_pt writerService;
+};
+
+
+
+celix_status_t bundleActivator_create(bundle_context_pt context, void **userData) {
+	celix_status_t status = CELIX_SUCCESS;
+
+	*userData = calloc (1, sizeof(struct activator));
+
+	if (*userData) {
+		((struct activator *) *userData)->readerService = NULL;
+		((struct activator *) *userData)->writerService = NULL;
+		((struct activator *) *userData)->databaseHandler = NULL;
+
+	} else {
+		status = CELIX_ENOMEM;
+	}
+	return status;
+}
+
+
+celix_status_t bundleActivator_start(void * userData, bundle_context_pt context) {
+	celix_status_t status = CELIX_SUCCESS;
+	struct activator *activator = userData;
+	reader_service_pt readerService = calloc(1, sizeof(struct reader_service));
+	writer_service_pt writerService = calloc(1, sizeof(struct writer_service));
+	database_handler_pt databaseHandler = calloc(1, sizeof(struct database_handler));
+
+	if (readerService && writerService && databaseHandler)
+	{
+	    status = celixThreadMutex_create(&databaseHandler->lock, NULL);
+
+	    if (status == CELIX_SUCCESS)
+	    {
+            arrayList_create(&databaseHandler->data);
+            databaseHandler->dataIndex = 0;
+
+            readerService->handler = databaseHandler;
+            readerService->readerService_getFirstData = readerService_getFirstData;
+            readerService->readerService_getNextData = readerService_getNextData;
+
+            writerService->handler = databaseHandler;
+            writerService->writerService_storeData = writerService_storeData;
+            writerService->writerService_removeData = writerService_removeData;
+
+            activator->readerService = readerService;
+            activator->writerService = writerService;
+
+            status = bundleContext_registerService(context, READER_SERVICE_NAME, activator->readerService,
NULL,  &activator->readerRegistration);
+
+            if (status == CELIX_SUCCESS)
+                status = bundleContext_registerService(context, WRITER_SERVICE_NAME, activator->writerService,
NULL,  &activator->writerRegistration);
+	    }
+	}
+	else
+	{
+		status = CELIX_ENOMEM;
+	}
+
+	return status;
+}
+
+celix_status_t bundleActivator_stop(void * userData, bundle_context_pt context) {
+	celix_status_t status = CELIX_SUCCESS;
+	struct activator *activator = userData;
+
+	serviceRegistration_unregister(activator->readerRegistration);
+	serviceRegistration_unregister(activator->writerRegistration);
+
+	return status;
+}
+
+celix_status_t bundleActivator_destroy(void * userData, bundle_context_pt context) {
+	celix_status_t status = CELIX_SUCCESS;
+	struct activator *activator = userData;
+
+	free(activator->readerService);
+	free(activator->writerService);
+	free(activator);
+
+	return status;
+}
+

Added: celix/trunk/examples/producer_consumer/database/private/src/reader.c
URL: http://svn.apache.org/viewvc/celix/trunk/examples/producer_consumer/database/private/src/reader.c?rev=1660038&view=auto
==============================================================================
--- celix/trunk/examples/producer_consumer/database/private/src/reader.c (added)
+++ celix/trunk/examples/producer_consumer/database/private/src/reader.c Mon Feb 16 07:54:30
2015
@@ -0,0 +1,73 @@
+/**
+ * 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.
+ */
+
+/*
+ * reader.c
+ *
+ *  \date       16 Feb 2015
+ *  \author     <a href="mailto:dev@celix.apache.org">Apache Celix Project Team</a>
+ *  \copyright  Apache License, Version 2.0
+ */
+
+
+
+#include <pthread.h>
+
+#include "array_list.h"
+#include "celix_errno.h"
+#include "data.h"
+#include "database.h"
+#include "reader_service.h"
+
+
+celix_status_t readerService_getFirstData(database_handler_pt handler, data_pt firstData)
+{
+	celix_status_t status = CELIX_BUNDLE_EXCEPTION;
+
+	celixThreadMutex_lock(&handler->lock);
+
+	if ((firstData = (data_pt) arrayList_remove(handler->data, 0)) != NULL)
+	{
+		handler->dataIndex--;
+		status = CELIX_SUCCESS;
+	}
+
+    celixThreadMutex_unlock(&handler->lock);
+
+	return status;
+}
+
+
+celix_status_t readerService_getNextData(database_handler_pt handler, data_pt* nextData)
+{
+	celix_status_t status = CELIX_BUNDLE_EXCEPTION;
+
+    celixThreadMutex_lock(&handler->lock);
+
+	if (((*nextData) = (data_pt) arrayList_get(handler->data, handler->dataIndex)) !=
NULL)
+	{
+		handler->dataIndex++;
+		status = CELIX_SUCCESS;
+	}
+
+    celixThreadMutex_unlock(&handler->lock);
+
+	return status;
+}
+

Added: celix/trunk/examples/producer_consumer/database/private/src/writer.c
URL: http://svn.apache.org/viewvc/celix/trunk/examples/producer_consumer/database/private/src/writer.c?rev=1660038&view=auto
==============================================================================
--- celix/trunk/examples/producer_consumer/database/private/src/writer.c (added)
+++ celix/trunk/examples/producer_consumer/database/private/src/writer.c Mon Feb 16 07:54:30
2015
@@ -0,0 +1,68 @@
+/**
+ * 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.
+ */
+
+/*
+ * writer.c
+ *
+ *  \date       16 Feb 2015
+ *  \author     <a href="mailto:dev@celix.apache.org">Apache Celix Project Team</a>
+ *  \copyright  Apache License, Version 2.0
+ */
+
+
+
+#include <stdlib.h>
+#include <pthread.h>
+
+#include "array_list.h"
+#include "celix_errno.h"
+#include "data.h"
+#include "database.h"
+
+#include "writer_service.h"
+
+celix_status_t writerService_storeData(database_handler_pt handler, data_pt newData)
+{
+	celix_status_t status = CELIX_BUNDLE_EXCEPTION;
+
+    celixThreadMutex_lock(&handler->lock);
+
+	if ( arrayList_add(handler->data, newData) == true)
+		status = CELIX_SUCCESS;
+
+    celixThreadMutex_unlock(&handler->lock);
+	return status;
+}
+
+
+celix_status_t writerService_removeData(database_handler_pt handler, data_pt* newData)
+{
+	celix_status_t status = CELIX_BUNDLE_EXCEPTION;
+
+    celixThreadMutex_lock(&handler->lock);
+
+	if ( arrayList_removeElement(handler->data, *newData) == true)
+		status = CELIX_SUCCESS;
+
+	free(*newData);
+
+    celixThreadMutex_unlock(&handler->lock);
+	return status;
+}
+

Added: celix/trunk/examples/producer_consumer/database/public/include/data.h
URL: http://svn.apache.org/viewvc/celix/trunk/examples/producer_consumer/database/public/include/data.h?rev=1660038&view=auto
==============================================================================
--- celix/trunk/examples/producer_consumer/database/public/include/data.h (added)
+++ celix/trunk/examples/producer_consumer/database/public/include/data.h Mon Feb 16 07:54:30
2015
@@ -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.
+ */
+
+/*
+ * data.h
+ *
+ *  \date       16 Feb 2015
+ *  \author     <a href="mailto:dev@celix.apache.org">Apache Celix Project Team</a>
+ *  \copyright  Apache License, Version 2.0
+ */
+
+#ifndef _DATA_H_
+#define _DATA_H_
+
+struct data {
+    int id;
+    char description[100];
+};
+
+typedef struct data* data_pt;
+
+#endif

Added: celix/trunk/examples/producer_consumer/database/public/include/database.h
URL: http://svn.apache.org/viewvc/celix/trunk/examples/producer_consumer/database/public/include/database.h?rev=1660038&view=auto
==============================================================================
--- celix/trunk/examples/producer_consumer/database/public/include/database.h (added)
+++ celix/trunk/examples/producer_consumer/database/public/include/database.h Mon Feb 16 07:54:30
2015
@@ -0,0 +1,44 @@
+/**
+ * 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.
+ */
+
+/*
+ * database.h
+ *
+ *  \date       16 Feb 2015
+ *  \author     <a href="mailto:dev@celix.apache.org">Apache Celix Project Team</a>
+ *  \copyright  Apache License, Version 2.0
+ */
+
+
+
+#ifndef _DATABASE_H_
+#define _DATABASE_H_
+
+#include "array_list.h"
+#include  "celix_threads.h"
+
+struct database_handler {
+	celix_thread_mutex_t lock;
+	array_list_pt data;
+	int dataIndex;
+};
+
+typedef struct database_handler* database_handler_pt;
+
+#endif

Added: celix/trunk/examples/producer_consumer/database/public/include/reader_service.h
URL: http://svn.apache.org/viewvc/celix/trunk/examples/producer_consumer/database/public/include/reader_service.h?rev=1660038&view=auto
==============================================================================
--- celix/trunk/examples/producer_consumer/database/public/include/reader_service.h (added)
+++ celix/trunk/examples/producer_consumer/database/public/include/reader_service.h Mon Feb
16 07:54:30 2015
@@ -0,0 +1,50 @@
+/**
+ * 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.
+ */
+
+/*
+ * reader_service.h
+ *
+ *  \date       16 Feb 2015
+ *  \author     <a href="mailto:dev@celix.apache.org">Apache Celix Project Team</a>
+ *  \copyright  Apache License, Version 2.0
+ */
+
+
+
+#ifndef READER_SERVICE_H_
+#define READER_SERVICE_H_
+
+#include "celix_errno.h"
+#include "data.h"
+#include "database.h"
+
+#define READER_SERVICE_NAME		"service.database.reader"
+
+
+struct reader_service {
+	database_handler_pt handler;
+	celix_status_t (*readerService_getFirstData)(database_handler_pt handle, data_pt firstData);
+	celix_status_t (*readerService_getNextData)(database_handler_pt handle, data_pt* nextData);
+};
+
+typedef struct reader_service* reader_service_pt;
+
+
+
+#endif

Added: celix/trunk/examples/producer_consumer/database/public/include/writer_service.h
URL: http://svn.apache.org/viewvc/celix/trunk/examples/producer_consumer/database/public/include/writer_service.h?rev=1660038&view=auto
==============================================================================
--- celix/trunk/examples/producer_consumer/database/public/include/writer_service.h (added)
+++ celix/trunk/examples/producer_consumer/database/public/include/writer_service.h Mon Feb
16 07:54:30 2015
@@ -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.
+ */
+
+/*
+ * writer_service.h
+ *
+ *  \date       16 Feb 2015
+ *  \author     <a href="mailto:dev@celix.apache.org">Apache Celix Project Team</a>
+ *  \copyright  Apache License, Version 2.0
+ */
+
+
+
+
+#ifndef WRITER_SERVICE_H_
+#define WRITER_SERVICE_H_
+
+#include "celix_errno.h"
+#include "data.h"
+#include "database.h"
+
+#define WRITER_SERVICE_NAME		"service.database.writer"
+
+struct writer_service {
+	void *handler;
+	celix_status_t (*writerService_storeData)(database_handler_pt handle, data_pt data);
+	celix_status_t (*writerService_removeData)(database_handler_pt handle, data_pt* data);
+};
+
+typedef struct writer_service* writer_service_pt;
+
+#endif

Added: celix/trunk/examples/producer_consumer/producer/CMakeLists.txt
URL: http://svn.apache.org/viewvc/celix/trunk/examples/producer_consumer/producer/CMakeLists.txt?rev=1660038&view=auto
==============================================================================
--- celix/trunk/examples/producer_consumer/producer/CMakeLists.txt (added)
+++ celix/trunk/examples/producer_consumer/producer/CMakeLists.txt Mon Feb 16 07:54:30 2015
@@ -0,0 +1,31 @@
+# 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.
+
+set(BUNDLE_SYMBOLICNAME "apache_celix_examples_producer")
+set(BUNDLE_VERSION "1.0.0")
+set(BUNDLE_NAME "Apache Celix Producer")
+
+
+include_directories("${PROJECT_SOURCE_DIR}/utils/public/include")
+include_directories("${PROJECT_SOURCE_DIR}/examples/producer_consumer/database/public/include")
+
+bundle(producer
+	SOURCES
+	 	private/src/activator.c
+)
+
+target_link_libraries(producer ${CELIX_LIBRARIES})
\ No newline at end of file

Added: celix/trunk/examples/producer_consumer/producer/private/src/activator.c
URL: http://svn.apache.org/viewvc/celix/trunk/examples/producer_consumer/producer/private/src/activator.c?rev=1660038&view=auto
==============================================================================
--- celix/trunk/examples/producer_consumer/producer/private/src/activator.c (added)
+++ celix/trunk/examples/producer_consumer/producer/private/src/activator.c Mon Feb 16 07:54:30
2015
@@ -0,0 +1,162 @@
+/**
+ * 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.
+ */
+
+/*
+ * activator.c
+ *
+ *  \date       16 Feb 2015
+ *  \author     <a href="mailto:dev@celix.apache.org">Apache Celix Project Team</a>
+ *  \copyright  Apache License, Version 2.0
+ */
+
+#include <stdlib.h>
+
+#include <unistd.h>
+
+#include <sys/time.h>
+#include <stdlib.h>
+#include <bundle_activator.h>
+#include <service_tracker.h>
+#include <constants.h>
+
+#include <array_list.h>
+
+#include "writer_service.h"
+#include "data.h"
+
+struct activator {
+    service_tracker_pt tracker;
+    array_list_pt readerServices;
+    bool running;
+    celix_thread_t worker;
+};
+
+celix_status_t writerServiceAdded(void *handle, service_reference_pt reference, void *service);
+celix_status_t writerServiceRemoved(void *handle, service_reference_pt reference, void *service);
+
+int globalDataId = 0;
+
+void *produceData(void *handle);
+
+celix_status_t bundleActivator_create(bundle_context_pt context, void **userData) {
+    celix_status_t status = CELIX_SUCCESS;
+    *userData = calloc(1, sizeof(struct activator));
+    if (*userData) {
+        ((struct activator *) *userData)->tracker = NULL;
+        ((struct activator *) *userData)->readerServices = NULL;
+        ((struct activator *) *userData)->running = false;
+        ((struct activator *) *userData)->worker = celix_thread_default;
+    } else {
+        status = CELIX_ENOMEM;
+    }
+    return status;
+}
+
+celix_status_t bundleActivator_start(void * userData, bundle_context_pt context) {
+    celix_status_t status = CELIX_SUCCESS;
+    struct activator *activator = userData;
+
+    // create list for services
+    arrayList_create(&activator->readerServices);
+
+    // start the thread
+    activator->running = true;
+    status = celixThread_create(&activator->worker, NULL, produceData, activator);
+
+    if (status == CELIX_SUCCESS) {
+        service_tracker_customizer_pt customizer = NULL;
+        serviceTrackerCustomizer_create(userData, NULL, writerServiceAdded, NULL, writerServiceRemoved,
&customizer);
+
+        serviceTracker_create(context, WRITER_SERVICE_NAME, customizer, &activator->tracker);
+        serviceTracker_open(activator->tracker);
+    }
+
+    return status;
+}
+
+celix_status_t bundleActivator_stop(void * userData, bundle_context_pt context) {
+    celix_status_t status = CELIX_SUCCESS;
+    struct activator *activator = userData;
+
+    // stop the thread
+    activator->running = false;
+    celixThread_join(activator->worker, NULL);
+
+    serviceTracker_close(activator->tracker);
+    serviceTracker_destroy(activator->tracker);
+
+    // destroy the list of services
+    arrayList_destroy(activator->readerServices);
+
+    return status;
+}
+
+celix_status_t bundleActivator_destroy(void * userData, bundle_context_pt context) {
+    celix_status_t status = CELIX_SUCCESS;
+    struct activator *activator = userData;
+
+    free(activator);
+
+    return status;
+}
+
+celix_status_t writerServiceAdded(void *handle, service_reference_pt reference, void *service)
{
+    struct activator *activator = handle;
+    arrayList_add(activator->readerServices, service);
+    printf("Producer: Writer Service Added.\n");
+
+    return CELIX_SUCCESS;
+}
+
+celix_status_t writerServiceRemoved(void *handle, service_reference_pt reference, void *service)
{
+    struct activator *activator = handle;
+    arrayList_removeElement(activator->readerServices, service);
+    printf("Producer: Writer Service Removed.\n");
+
+    return CELIX_SUCCESS;
+}
+
+void *produceData(void *handle) {
+    struct activator *activator = handle;
+
+    while (activator->running) {
+        int i;
+        for (i = 0; i < arrayList_size(activator->readerServices); i++) {
+            writer_service_pt service = arrayList_get(activator->readerServices, i);
+            data_pt newData = calloc(1, sizeof(struct data));
+
+            newData->id = globalDataId++;
+            snprintf(newData->description, 100, "%d : Some Description", newData->id);
+
+            if (service->writerService_storeData(service->handler, newData) == CELIX_SUCCESS)
{
+                printf(" Data #%d stored.\n", newData->id);
+            } else {
+                printf(" Could not store data. \n");
+            }
+
+            srand(time(NULL));
+            int r = rand() % 100;
+
+            if (r > 70)
+                sleep(10);
+        }
+    }
+
+    return NULL;
+}



Mime
View raw message