Skip to content

Commit

Permalink
CELIX-351: Refactoring of shell_tui to use service_tracker instead of…
Browse files Browse the repository at this point in the history
… service listener
  • Loading branch information
pnoltes committed Feb 11, 2016
1 parent e6bff9f commit 91ba2a1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 66 deletions.
8 changes: 4 additions & 4 deletions shell_tui/private/include/shell_tui.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ */
55 changes: 29 additions & 26 deletions shell_tui/private/src/activator.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -50,49 +62,40 @@ 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;
}

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;
Expand Down
54 changes: 18 additions & 36 deletions shell_tui/private/src/shell_tui.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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;
}

0 comments on commit 91ba2a1

Please sign in to comment.