celix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pnol...@apache.org
Subject [33/50] celix git commit: CELIX-351: Refactoring of shell_tui to use service_tracker instead of service listener
Date Mon, 11 Apr 2016 12:00:42 GMT
CELIX-351: Refactoring of shell_tui to use service_tracker instead of service listener


Project: http://git-wip-us.apache.org/repos/asf/celix/repo
Commit: http://git-wip-us.apache.org/repos/asf/celix/commit/91ba2a16
Tree: http://git-wip-us.apache.org/repos/asf/celix/tree/91ba2a16
Diff: http://git-wip-us.apache.org/repos/asf/celix/diff/91ba2a16

Branch: refs/heads/master
Commit: 91ba2a168f76da578d6032e66349f6740f52dc91
Parents: e6bff9f
Author: Pepijn Noltes <pepijnnoltes@gmail.com>
Authored: Thu Feb 11 15:28:41 2016 +0100
Committer: Pepijn Noltes <pepijnnoltes@gmail.com>
Committed: Thu Feb 11 15:28:41 2016 +0100

----------------------------------------------------------------------
 shell_tui/private/include/shell_tui.h |  8 ++---
 shell_tui/private/src/activator.c     | 55 ++++++++++++++++--------------
 shell_tui/private/src/shell_tui.c     | 54 ++++++++++-------------------
 3 files changed, 51 insertions(+), 66 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/celix/blob/91ba2a16/shell_tui/private/include/shell_tui.h
----------------------------------------------------------------------
diff --git a/shell_tui/private/include/shell_tui.h b/shell_tui/private/include/shell_tui.h
index fec60c9..2d7c251 100644
--- a/shell_tui/private/include/shell_tui.h
+++ b/shell_tui/private/include/shell_tui.h
@@ -33,19 +33,19 @@
 #include "celix_threads.h"
 #include "service_reference.h"
 #include "shell.h"
+#include "service_tracker.h"
 
 struct shellTuiActivator {
     bundle_context_pt context;
     shell_service_pt shell;
-    service_reference_pt reference;
-    struct serviceListener * listener;
+    service_tracker_pt tracker;
     bool running;
     celix_thread_t runnable;
 };
 
 typedef struct shellTuiActivator * shell_tui_activator_pt;
 
-celix_status_t shellTui_initializeService(shell_tui_activator_pt activator);
-celix_status_t shellTui_serviceChanged(service_listener_pt listener, service_event_pt event);
+celix_status_t shellTui_start(shell_tui_activator_pt activator);
+celix_status_t shellTui_stop(shell_tui_activator_pt activator);
 
 #endif /* SHELL_TUI_H_ */

http://git-wip-us.apache.org/repos/asf/celix/blob/91ba2a16/shell_tui/private/src/activator.c
----------------------------------------------------------------------
diff --git a/shell_tui/private/src/activator.c b/shell_tui/private/src/activator.c
index 1e79bea..1759e38 100644
--- a/shell_tui/private/src/activator.c
+++ b/shell_tui/private/src/activator.c
@@ -30,8 +30,20 @@
 
 
 #include "shell_tui.h"
+#include "service_tracker.h"
 
 
+static celix_status_t activator_addShellService(void *handle, service_reference_pt ref, void
*svc) {
+    shell_tui_activator_pt act = (shell_tui_activator_pt) handle;
+    act->shell = svc;
+    return CELIX_SUCCESS;
+}
+
+static celix_status_t activator_removeShellService(void *handle, service_reference_pt ref,
void *svc) {
+    shell_tui_activator_pt act = (shell_tui_activator_pt) handle;
+    act->shell = svc;
+    return CELIX_SUCCESS;
+}
 
 celix_status_t bundleActivator_create(bundle_context_pt context, void **userData) {
 	celix_status_t status = CELIX_SUCCESS;
@@ -50,42 +62,30 @@ celix_status_t bundleActivator_create(bundle_context_pt context, void
**userData
 }
 
 celix_status_t bundleActivator_start(void * userData, bundle_context_pt context) {
-	celix_status_t status;
+	celix_status_t status = CELIX_SUCCESS;
 
 	shell_tui_activator_pt act = (shell_tui_activator_pt) userData;
-	service_listener_pt listener = (service_listener_pt) calloc(1, sizeof(*listener));
 
-	act->context = context;
-	act->running = true;
+    service_tracker_customizer_pt cust = NULL;
+    serviceTrackerCustomizer_create(userData, NULL, activator_addShellService, NULL, activator_removeShellService,
&cust);
+    serviceTracker_create(context, (char *) OSGI_SHELL_SERVICE_NAME, cust, &act->tracker);
+    serviceTracker_open(act->tracker);
 
-	act->listener = listener;
-	act->listener->handle = act;
-	act->listener->serviceChanged = (void *) shellTui_serviceChanged;
-	status = bundleContext_addServiceListener(context, act->listener, "(objectClass=shellService)");
-
-	if (status == CELIX_SUCCESS) {
-		shellTui_initializeService(act);
-	}
+    shellTui_start(act);
 
 	return status;
 }
 
 celix_status_t bundleActivator_stop(void * userData, bundle_context_pt context) {
-	celix_status_t status;
-	shell_tui_activator_pt activator = (shell_tui_activator_pt) userData;
-
-	status = bundleContext_removeServiceListener(context, activator->listener);
-
-	if (status == CELIX_SUCCESS) {
-		free(activator->listener);
-
-		activator->running = false;
-		activator->listener = NULL;
-		activator->context = NULL;
-		activator->running = false;
+	celix_status_t status = CELIX_SUCCESS;
+	shell_tui_activator_pt act = (shell_tui_activator_pt) userData;
 
-		celixThread_join(activator->runnable, NULL);
-	}
+    if (act != NULL) {
+        shellTui_stop(act);
+        if (act->tracker != NULL) {
+            serviceTracker_close(act->tracker);
+        }
+    }
 
 	return status;
 }
@@ -93,6 +93,9 @@ celix_status_t bundleActivator_stop(void * userData, bundle_context_pt context)
 celix_status_t bundleActivator_destroy(void * userData, bundle_context_pt context) {
 	shell_tui_activator_pt activator = (shell_tui_activator_pt) userData;
 
+    if (activator->tracker != NULL) {
+        serviceTracker_destroy(activator->tracker);
+    }
 	free(activator);
 
 	return CELIX_SUCCESS;

http://git-wip-us.apache.org/repos/asf/celix/blob/91ba2a16/shell_tui/private/src/shell_tui.c
----------------------------------------------------------------------
diff --git a/shell_tui/private/src/shell_tui.c b/shell_tui/private/src/shell_tui.c
index 08bc3f8..fbff74e 100644
--- a/shell_tui/private/src/shell_tui.c
+++ b/shell_tui/private/src/shell_tui.c
@@ -34,6 +34,8 @@
 #include "shell.h"
 #include "shell_tui.h"
 #include "utils.h"
+#include <signal.h>
+#include <unistd.h>
 
 
 static void* shellTui_runnable(void *data) {
@@ -54,13 +56,13 @@ static void* shellTui_runnable(void *data) {
             needPrompt = false;
         }
         FD_ZERO(&rfds);
-        FD_SET(0, &rfds);
+        FD_SET(STDIN_FILENO, &rfds);
 
         tv.tv_sec = 1;
         tv.tv_usec = 0;
 
-        if (select(1, &rfds, NULL, NULL, &tv)) {
-            fgets(in, 256, stdin);
+        if (select(1, &rfds, NULL, NULL, &tv) > 0) {
+            fgets(in, sizeof(in)-1, stdin);
             needPrompt = true;
             memset(dline, 0, 256);
             strncpy(dline, in, 256);
@@ -70,52 +72,32 @@ static void* shellTui_runnable(void *data) {
                 continue;
             }
 
-            act->shell->executeCommand(act->shell->shell, line, stdout, stderr);
+            if (act->shell != NULL) {
+                act->shell->executeCommand(act->shell->shell, line, stdout, stderr);
+            } else {
+                fprintf(stderr, "Shell service not available\n");
+            }
         }
     }
 
     return NULL;
 }
 
-celix_status_t shellTui_initializeService(shell_tui_activator_pt activator) {
+celix_status_t shellTui_start(shell_tui_activator_pt activator) {
 
     celix_status_t status = CELIX_SUCCESS;
 
-    if (activator->shell != NULL) {
-        status = CELIX_ILLEGAL_ARGUMENT;
-    } else {
-        status = bundleContext_getServiceReference(activator->context, (char *) OSGI_SHELL_SERVICE_NAME,
&activator->reference);
-
-        if (status != CELIX_SUCCESS || activator->reference != NULL) {
-            void *shell_svc = NULL;
-            bundleContext_getService(activator->context, activator->reference, &shell_svc);
-            activator->shell = (shell_service_pt) shell_svc;
-
-            celixThread_create(&activator->runnable, NULL, shellTui_runnable, activator);
-        }
-    }
+    activator->running = true;
+    celixThread_create(&activator->runnable, NULL, shellTui_runnable, activator);
 
     return status;
 }
 
-celix_status_t shellTui_serviceChanged(service_listener_pt listener, service_event_pt event)
{
+celix_status_t shellTui_stop(shell_tui_activator_pt act) {
     celix_status_t status = CELIX_SUCCESS;
-
-    bool result = false;
-    shell_tui_activator_pt act = (shell_tui_activator_pt) listener->handle;
-    bool equals = false;
-
-    serviceReference_equals(act->reference, event->reference, &equals);
-
-    if ((event->type == OSGI_FRAMEWORK_SERVICE_EVENT_REGISTERED) && (act->reference
== NULL)) {
-        status = shellTui_initializeService(act);
-    } else if ((event->type == OSGI_FRAMEWORK_SERVICE_EVENT_UNREGISTERING) &&
(equals)) {
-        bundleContext_ungetService(act->context, act->reference, &result);
-        bundleContext_ungetServiceReference(act->context,act->reference);
-        act->reference = NULL;
-        act->shell = NULL;
-
-    }
-
+    act->running = false;
+    celixThread_kill(act->runnable, SIGUSR1);
+    celixThread_join(act->runnable, NULL);
     return status;
 }
+


Mime
View raw message