celix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bpe...@apache.org
Subject svn commit: r1659994 - /celix/trunk/examples/osgi-in-action/chapter04-correct-listener/private/src/listener_example.c
Date Sun, 15 Feb 2015 20:57:24 GMT
Author: bpetri
Date: Sun Feb 15 20:57:24 2015
New Revision: 1659994

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

CELIX-208: Removed APR


Modified:
    celix/trunk/examples/osgi-in-action/chapter04-correct-listener/private/src/listener_example.c

Modified: celix/trunk/examples/osgi-in-action/chapter04-correct-listener/private/src/listener_example.c
URL: http://svn.apache.org/viewvc/celix/trunk/examples/osgi-in-action/chapter04-correct-listener/private/src/listener_example.c?rev=1659994&r1=1659993&r2=1659994&view=diff
==============================================================================
--- celix/trunk/examples/osgi-in-action/chapter04-correct-listener/private/src/listener_example.c
(original)
+++ celix/trunk/examples/osgi-in-action/chapter04-correct-listener/private/src/listener_example.c
Sun Feb 15 20:57:24 2015
@@ -25,184 +25,172 @@
  */
 #include <stdlib.h>
 #include <stdio.h>
-#include <apr_general.h>
-#include <apr_thread_proc.h>
+#include <unistd.h>
 
 #include "bundle_activator.h"
 #include "log_service.h"
 
 struct listenerActivator {
-	apr_pool_t *pool;
+    bundle_context_pt context;
+    service_listener_pt listener;
 
-	bundle_context_pt context;
-	service_listener_pt listener;
+    celix_thread_t logger;
+    celix_thread_mutex_t logServiceReferencesLock;
 
-	apr_thread_mutex_t *logServiceReferencesLock;
-	array_list_pt logServiceReferences;
+    array_list_pt logServiceReferences;
 
-	bool running;
-	apr_thread_t *logger;
+    bool running;
 };
 
 void listenerExample_serviceChanged(service_listener_pt listener, service_event_pt event);
 celix_status_t listenerExample_getLogService(struct listenerActivator *activator, log_service_pt
*service);
 
-static void *APR_THREAD_FUNC listenerExample_logger(apr_thread_t *thd, void *activator);
+static void* listenerExample_logger(void* data);
 
 celix_status_t listenerExample_alternativeLog(struct listenerActivator *activator, char *message);
 
 celix_status_t ref_equals(void *a, void *b, bool *equals) {
-	return serviceReference_equals(a, b, equals);
+    return serviceReference_equals(a, b, equals);
 }
 
 celix_status_t bundleActivator_create(bundle_context_pt context, void **userData) {
-	celix_status_t status = CELIX_SUCCESS;
-	apr_pool_t *pool;
-	apr_pool_t *subpool;
-
-	status = bundleContext_getMemoryPool(context, &pool);
-	if (status == CELIX_SUCCESS) {
-		if (apr_pool_create(&subpool, pool) == APR_SUCCESS) {
-			*userData = apr_palloc(subpool, sizeof(struct listenerActivator));
-			if (!userData) {
-				status = CELIX_ENOMEM;
-			} else {
-				struct listenerActivator *activator = (*userData);
-				activator->pool = subpool;
-				activator->context = context;
-				activator->listener = NULL;
-				apr_thread_mutex_create(&activator->logServiceReferencesLock, 0, subpool);
-				activator->logServiceReferences = NULL;
-				arrayList_createWithEquals(ref_equals, &activator->logServiceReferences);
-				activator->running = false;
-				activator->logger = NULL;
-			}
-		} else {
-			status = CELIX_BUNDLE_EXCEPTION;
-		}
-	}
-	return status;
+    celix_status_t status = CELIX_SUCCESS;
+
+    *userData = calloc(1, sizeof(struct listenerActivator));
+    if (!userData) {
+        status = CELIX_ENOMEM;
+    } else {
+        struct listenerActivator *activator = (*userData);
+        activator->context = context;
+        activator->listener = NULL;
+        activator->logServiceReferences = NULL;
+        arrayList_createWithEquals(ref_equals, &activator->logServiceReferences);
+        activator->running = false;
+
+        status = celixThreadMutex_create(&activator->logServiceReferencesLock, NULL);
+    }
+
+    return status;
 }
 
 celix_status_t bundleActivator_start(void * userData, bundle_context_pt context) {
-	celix_status_t status = CELIX_SUCCESS;
-	struct listenerActivator *activator = userData;
+    celix_status_t status = CELIX_SUCCESS;
+    struct listenerActivator *activator = userData;
 
-	service_listener_pt listener = apr_palloc(activator->pool, sizeof(struct listenerActivator));
-	if (!listener) {
-		status = CELIX_ENOMEM;
-	} else {
-		char filter[30];
-		array_list_pt logServices = NULL;
-		apr_pool_t *pool;
-		sprintf(filter, "(objectClass=%s)", OSGI_LOGSERVICE_NAME);
-
-		bundleContext_getMemoryPool(context, &pool);
-
-		listener->handle = activator;
-		listener->serviceChanged = (void *) listenerExample_serviceChanged;
-		status = bundleContext_addServiceListener(context, listener, filter);
-		if (status == CELIX_SUCCESS) {
-			activator->listener = listener;
-		}
-
-		status = bundleContext_getServiceReferences(context, NULL, filter, &logServices);
-		if (status == CELIX_SUCCESS) {
-			int i;
-			for (i = 0; i < arrayList_size(logServices); i++) {
-				service_reference_pt logService = (service_reference_pt) arrayList_get(logServices, i);
-				service_event_pt event = apr_palloc(activator->pool, sizeof(*event));
-				event->reference = logService;
-				event->type = OSGI_FRAMEWORK_SERVICE_EVENT_REGISTERED;
-
-				listenerExample_serviceChanged(listener, event);
-			}
-			arrayList_destroy(logServices);
-		}
-
-		activator->running = true;
-		apr_thread_create(&activator->logger, NULL, listenerExample_logger, activator, activator->pool);
-	}
+    service_listener_pt listener = calloc(1, sizeof(struct listenerActivator));
+    if (!listener) {
+        status = CELIX_ENOMEM;
+    } else {
+        char filter[30];
+        array_list_pt logServices = NULL;
+        sprintf(filter, "(objectClass=%s)", OSGI_LOGSERVICE_NAME);
+
+        listener->handle = activator;
+        listener->serviceChanged = (void *) listenerExample_serviceChanged;
+        status = bundleContext_addServiceListener(context, listener, filter);
+        if (status == CELIX_SUCCESS) {
+            activator->listener = listener;
+        }
+
+        status = bundleContext_getServiceReferences(context, NULL, filter, &logServices);
+        if (status == CELIX_SUCCESS) {
+            int i;
+            for (i = 0; i < arrayList_size(logServices); i++) {
+                service_reference_pt logService = (service_reference_pt) arrayList_get(logServices,
i);
+                service_event_pt event = calloc(1, sizeof(*event));
+                event->reference = logService;
+                event->type = OSGI_FRAMEWORK_SERVICE_EVENT_REGISTERED;
+
+                listenerExample_serviceChanged(listener, event);
+            }
+            arrayList_destroy(logServices);
+        }
 
-	return status;
+        activator->running = true;
+
+        status = celixThread_create(&activator->logger, NULL, listenerExample_logger,
activator);
+    }
+
+    return status;
 }
 
 celix_status_t bundleActivator_stop(void * userData, bundle_context_pt context) {
-	celix_status_t status = CELIX_SUCCESS;
-	struct listenerActivator *activator = userData;
-	apr_status_t stat;
+    celix_status_t status = CELIX_SUCCESS;
+    struct listenerActivator *activator = userData;
 
-	activator->running = false;
-	apr_thread_join(&stat, activator->logger);
+    activator->running = false;
+    celixThread_join(activator->logger, NULL);
 
-	bundleContext_removeServiceListener(context, activator->listener);
+    bundleContext_removeServiceListener(context, activator->listener);
 
-	return status;
+    return status;
 }
 
 celix_status_t bundleActivator_destroy(void * userData, bundle_context_pt context) {
-	celix_status_t status = CELIX_SUCCESS;
-	struct listenerActivator *activator = userData;
-	arrayList_destroy(activator->logServiceReferences);
-	return status;
+    celix_status_t status = CELIX_SUCCESS;
+    struct listenerActivator *activator = userData;
+    arrayList_destroy(activator->logServiceReferences);
+    return status;
 }
 
 void listenerExample_serviceChanged(service_listener_pt listener, service_event_pt event)
{
-	struct listenerActivator *activator = listener->handle;
-	apr_thread_mutex_lock(activator->logServiceReferencesLock);
+    struct listenerActivator *activator = listener->handle;
+    celixThreadMutex_lock(&activator->logServiceReferencesLock);
 
-	switch (event->type) {
-	case OSGI_FRAMEWORK_SERVICE_EVENT_REGISTERED:
-		arrayList_add(activator->logServiceReferences, event->reference);
-		break;
+    switch (event->type) {
+    case OSGI_FRAMEWORK_SERVICE_EVENT_REGISTERED:
+        arrayList_add(activator->logServiceReferences, event->reference);
+        break;
 //	case MODIFIED:
 //		// only the service metadata has changed, so no need to do anything here
 //		break;
-	case OSGI_FRAMEWORK_SERVICE_EVENT_UNREGISTERING:
-		arrayList_remove(activator->logServiceReferences,
-				arrayList_indexOf(activator->logServiceReferences, event->reference));
-		break;
-	default:
-		break;
-	}
-	apr_thread_mutex_unlock(activator->logServiceReferencesLock);
+    case OSGI_FRAMEWORK_SERVICE_EVENT_UNREGISTERING:
+        arrayList_remove(activator->logServiceReferences, arrayList_indexOf(activator->logServiceReferences,
event->reference));
+        break;
+    default:
+        break;
+    }
+
+    celixThreadMutex_unlock(&activator->logServiceReferencesLock);
 }
 
 celix_status_t listenerExample_getLogService(struct listenerActivator *activator, log_service_pt
*service) {
-	celix_status_t status = CELIX_SUCCESS;
+    celix_status_t status = CELIX_SUCCESS;
+
+    celixThreadMutex_lock(&activator->logServiceReferencesLock);
+
+    if (arrayList_size(activator->logServiceReferences) > 0) {
+        service_reference_pt reference = arrayList_get(activator->logServiceReferences,
0);
+        status = bundleContext_getService(activator->context, reference, (void *) service);
+    }
+    celixThreadMutex_unlock(&activator->logServiceReferencesLock);
+
+    return status;
+}
+
+static void* listenerExample_logger(void* data) {
+    struct listenerActivator *activator = data;
+
+    while (activator->running) {
+        log_service_pt logService = NULL;
+        listenerExample_getLogService(activator, &logService);
+        if (logService != NULL) {
+            (*(logService->log))(logService->logger, OSGI_LOGSERVICE_INFO, "ping");
+        } else {
+            listenerExample_alternativeLog(activator, "No LogService available. Printing
to standard out.");
+        }
 
-	apr_thread_mutex_lock(activator->logServiceReferencesLock);
-	if (arrayList_size(activator->logServiceReferences) > 0) {
-		service_reference_pt reference = arrayList_get(activator->logServiceReferences, 0);
-		status = bundleContext_getService(activator->context, reference, (void *) service);
-	}
-	apr_thread_mutex_unlock(activator->logServiceReferencesLock);
-
-	return status;
-}
-
-static void *APR_THREAD_FUNC listenerExample_logger(apr_thread_t *thd, void *data) {
-	struct listenerActivator *activator = data;
-
-	while (activator->running) {
-		log_service_pt logService = NULL;
-		listenerExample_getLogService(activator, &logService);
-		if (logService != NULL) {
-			(*(logService->log))(logService->logger, OSGI_LOGSERVICE_INFO, "ping");
-		} else {
-			listenerExample_alternativeLog(activator, "No LogService available. Printing to standard
out.");
-		}
-		apr_sleep(5000000);
-	}
+        sleep(5);
+    }
 
-	return NULL;
+    return NULL;
 }
 
 celix_status_t listenerExample_alternativeLog(struct listenerActivator *activator, char *message)
{
-	celix_status_t status = CELIX_SUCCESS;
+    celix_status_t status = CELIX_SUCCESS;
 
-	printf("%s\n", message);
+    printf("%s\n", message);
 
-	return status;
+    return status;
 }
 



Mime
View raw message