Author: abroekhuis
Date: Thu Dec  9 11:51:04 2010
New Revision: 1043905

URL: http://svn.apache.org/viewvc?rev=1043905&view=rev
Log:
CELIX-1 initial code import

Added:
    incubator/celix/trunk/shell/
    incubator/celix/trunk/shell/CMakeLists.txt
    incubator/celix/trunk/shell/MANIFEST/
    incubator/celix/trunk/shell/MANIFEST/MANIFEST.MF
    incubator/celix/trunk/shell/command.c
    incubator/celix/trunk/shell/command.h
    incubator/celix/trunk/shell/command_private.h
    incubator/celix/trunk/shell/ps_command.c
    incubator/celix/trunk/shell/ps_command.h
    incubator/celix/trunk/shell/shell.c
    incubator/celix/trunk/shell/shell.h
    incubator/celix/trunk/shell/shell_private.h
    incubator/celix/trunk/shell/start_command.c
    incubator/celix/trunk/shell/start_command.h
    incubator/celix/trunk/shell/stop_command.c
    incubator/celix/trunk/shell/stop_command.h
    incubator/celix/trunk/shell_tui/
    incubator/celix/trunk/shell_tui/CMakeLists.txt
    incubator/celix/trunk/shell_tui/MANIFEST/
    incubator/celix/trunk/shell_tui/MANIFEST/MANIFEST.MF
    incubator/celix/trunk/shell_tui/shell_tui.c

Added: incubator/celix/trunk/shell/CMakeLists.txt
URL: 
http://svn.apache.org/viewvc/incubator/celix/trunk/shell/CMakeLists.txt?rev=1043905&view=auto
==============================================================================
--- incubator/celix/trunk/shell/CMakeLists.txt (added)
+++ incubator/celix/trunk/shell/CMakeLists.txt Thu Dec  9 11:51:04 2010
@@ -0,0 +1,6 @@
+add_library(shell SHARED shell command ps_command start_command stop_command)
+include_directories("${PROJECT_SOURCE_DIR}/celix")
+target_link_libraries(shell framework)
+
+bundle(shell)
+package(shell FILES shell.h command.h)
\ No newline at end of file

Added: incubator/celix/trunk/shell/MANIFEST/MANIFEST.MF
URL: 
http://svn.apache.org/viewvc/incubator/celix/trunk/shell/MANIFEST/MANIFEST.MF?rev=1043905&view=auto
==============================================================================
--- incubator/celix/trunk/shell/MANIFEST/MANIFEST.MF (added)
+++ incubator/celix/trunk/shell/MANIFEST/MANIFEST.MF Thu Dec  9 11:51:04 2010
@@ -0,0 +1,5 @@
+Bundle-SymbolicName: shell
+Bundle-Version: 1.0.0
+library: shell
+Export-Service: shellService,commandService
+Import-Service: shellService,commandService

Added: incubator/celix/trunk/shell/command.c
URL: 
http://svn.apache.org/viewvc/incubator/celix/trunk/shell/command.c?rev=1043905&view=auto
==============================================================================
--- incubator/celix/trunk/shell/command.c (added)
+++ incubator/celix/trunk/shell/command.c Thu Dec  9 11:51:04 2010
@@ -0,0 +1,26 @@
+/*
+ * command.c
+ *
+ *  Created on: Aug 13, 2010
+ *      Author: alexanderb
+ */
+
+#include <stdio.h>
+
+#include "command_private.h"
+
+char * command_getName(COMMAND command) {
+       return command->name;
+}
+
+char * command_getUsage(COMMAND command) {
+       return command->usage;
+}
+
+char * command_getShortDescription(COMMAND command) {
+       return command->shortDescription;
+}
+
+void command_execute(COMMAND command, char * line, void (*out)(char *), void 
(*err)(char *)) {
+       command->executeCommand(command, line, out, err);
+}

Added: incubator/celix/trunk/shell/command.h
URL: 
http://svn.apache.org/viewvc/incubator/celix/trunk/shell/command.h?rev=1043905&view=auto
==============================================================================
--- incubator/celix/trunk/shell/command.h (added)
+++ incubator/celix/trunk/shell/command.h Thu Dec  9 11:51:04 2010
@@ -0,0 +1,26 @@
+/*
+ * command.h
+ *
+ *  Created on: Aug 13, 2010
+ *      Author: alexanderb
+ */
+
+#ifndef COMMAND_H_
+#define COMMAND_H_
+
+static const char * const COMMAND_SERVICE_NAME = "commandService";
+
+typedef struct command * COMMAND;
+
+struct commandService {
+       COMMAND command;
+       char * (*getName)(COMMAND command);
+       char * (*getUsage)(COMMAND command);
+       char * (*getShortDescription)(COMMAND command);
+       void (*executeCommand)(COMMAND command, char * commandLine, void 
(*out)(char *), void (*error)(char *));
+};
+
+
+
+
+#endif /* COMMAND_H_ */

Added: incubator/celix/trunk/shell/command_private.h
URL: 
http://svn.apache.org/viewvc/incubator/celix/trunk/shell/command_private.h?rev=1043905&view=auto
==============================================================================
--- incubator/celix/trunk/shell/command_private.h (added)
+++ incubator/celix/trunk/shell/command_private.h Thu Dec  9 11:51:04 2010
@@ -0,0 +1,25 @@
+/*
+ * command_private.h
+ *
+ *  Created on: Aug 13, 2010
+ *      Author: alexanderb
+ */
+
+#ifndef COMMAND_PRIVATE_H_
+#define COMMAND_PRIVATE_H_
+
+#include "command.h"
+#include "headers.h"
+
+struct command {
+       char * name;
+       char * usage;
+       char * shortDescription;
+
+       BUNDLE_CONTEXT bundleContext;
+
+       void (*executeCommand)(COMMAND command, char * commandLine, void 
(*out)(char *), void (*error)(char *));
+};
+
+
+#endif /* COMMAND_PRIVATE_H_ */

Added: incubator/celix/trunk/shell/ps_command.c
URL: 
http://svn.apache.org/viewvc/incubator/celix/trunk/shell/ps_command.c?rev=1043905&view=auto
==============================================================================
--- incubator/celix/trunk/shell/ps_command.c (added)
+++ incubator/celix/trunk/shell/ps_command.c Thu Dec  9 11:51:04 2010
@@ -0,0 +1,61 @@
+/*
+ * ps_command.c
+ *
+ *  Created on: Aug 13, 2010
+ *      Author: alexanderb
+ */
+#include <stdlib.h>
+
+#include "command_private.h"
+#include "array_list.h"
+#include "bundle_context.h"
+#include "bundle_archive.h"
+#include "module.h"
+#include "bundle.h"
+
+char * psCommand_stateString(BUNDLE_STATE state);
+void psCommand_execute(COMMAND command, char * line, void (*out)(char *), void 
(*err)(char *));
+
+COMMAND psCommand_create(BUNDLE_CONTEXT context) {
+       COMMAND command = (COMMAND) malloc(sizeof(*command));
+       command->bundleContext = context;
+       command->name = "ps";
+       command->shortDescription = "list installed bundles.";
+       command->usage = "ps [-l | -s | -u]";
+       command->executeCommand = psCommand_execute;
+       return command;
+}
+
+void psCommand_execute(COMMAND command, char * commandline, void (*out)(char 
*), void (*err)(char *)) {
+       ARRAY_LIST bundles = bundleContext_getBundles(command->bundleContext);
+
+       char line[256];
+       sprintf(line, "  %-5s %-12s %s\n", "ID", "State", "Name");
+       int i;
+       out(line);
+       for (i = 0; i < arrayList_size(bundles); i++) {
+               BUNDLE bundle = arrayList_get(bundles, i);
+               long id = bundleArchive_getId(bundle_getArchive(bundle));
+               char * name = module_getSymbolicName(bundle_getModule(bundle));
+               char * state = psCommand_stateString(bundle_getState(bundle));
+               sprintf(line, "  %-5ld %-12s %s\n", id, state, name);
+               out(line);
+       }
+}
+
+char * psCommand_stateString(BUNDLE_STATE state) {
+       switch (state) {
+               case BUNDLE_ACTIVE:
+                       return "Active      ";
+               case BUNDLE_INSTALLED:
+                       return "Installed   ";
+               case BUNDLE_RESOLVED:
+                       return "Resolved    ";
+               case BUNDLE_STARTING:
+                       return "Starting    ";
+               case BUNDLE_STOPPING:
+                       return "Stopping    ";
+               default:
+                       return "Unknown     ";
+       }
+}

Added: incubator/celix/trunk/shell/ps_command.h
URL: 
http://svn.apache.org/viewvc/incubator/celix/trunk/shell/ps_command.h?rev=1043905&view=auto
==============================================================================
--- incubator/celix/trunk/shell/ps_command.h (added)
+++ incubator/celix/trunk/shell/ps_command.h Thu Dec  9 11:51:04 2010
@@ -0,0 +1,13 @@
+/*
+ * ps_command.h
+ *
+ *  Created on: Aug 13, 2010
+ *      Author: alexanderb
+ */
+
+#ifndef PS_COMMAND_H_
+#define PS_COMMAND_H_
+
+COMMAND psCommand_create(BUNDLE_CONTEXT context);
+
+#endif /* PS_COMMAND_H_ */

Added: incubator/celix/trunk/shell/shell.c
URL: 
http://svn.apache.org/viewvc/incubator/celix/trunk/shell/shell.c?rev=1043905&view=auto
==============================================================================
--- incubator/celix/trunk/shell/shell.c (added)
+++ incubator/celix/trunk/shell/shell.c Thu Dec  9 11:51:04 2010
@@ -0,0 +1,146 @@
+/*
+ * shell.c
+ *
+ *  Created on: Aug 13, 2010
+ *      Author: alexanderb
+ */
+#include <stdlib.h>
+#include <string.h>
+
+#include "shell_private.h"
+#include "bundle_activator.h"
+#include "command_private.h"
+#include "headers.h"
+#include "bundle_context.h"
+#include "hashtable_itr.h"
+#include "service_registration.h"
+
+#include "ps_command.h"
+#include "start_command.h"
+#include "stop_command.h"
+
+#include "utils.h"
+
+struct shellServiceActivator {
+       SHELL shell;
+       SERVICE_REGISTRATION registration;
+};
+
+SHELL shell_create() {
+       SHELL shell = (SHELL) malloc(sizeof(*shell));
+       shell->commandNameMap = hashMap_create(string_hash, NULL, 
string_equals, NULL);
+       shell->commandReferenceMap = hashMap_create(NULL, NULL, NULL, NULL);
+       return shell;
+}
+
+ARRAY_LIST shell_getCommands(SHELL shell) {
+       ARRAY_LIST commands = arrayList_create();
+       HASH_MAP_ITERATOR iter = hashMapIterator_create(shell->commandNameMap);
+       while (hashMapIterator_hasNext(iter)) {
+               char * name = hashMapIterator_nextKey(iter);
+               arrayList_add(commands, name);
+       }
+       return commands;
+}
+
+char * shell_getCommandUsage(SHELL shell, char * commandName) {
+       COMMAND command = hashMap_get(shell->commandNameMap, commandName);
+       return (command == NULL) ? NULL : command->usage;
+}
+
+char * shell_getCommandDescription(SHELL shell, char * commandName) {
+       COMMAND command = hashMap_get(shell->commandNameMap, commandName);
+       return (command == NULL) ? NULL : command->shortDescription;
+}
+
+SERVICE_REFERENCE shell_getCommandReference(SHELL shell, char * command) {
+       HASH_MAP_ITERATOR iter = 
hashMapIterator_create(shell->commandReferenceMap);
+       while (hashMapIterator_hasNext(iter)) {
+               HASH_MAP_ENTRY entry = hashMapIterator_nextEntry(iter);
+               COMMAND cmd = (COMMAND) hashMapEntry_getValue(entry);
+               if (strcmp(cmd->name, command) == 0) {
+                       return (SERVICE_REFERENCE) hashMapEntry_getValue(entry);
+               }
+       }
+       return NULL;
+}
+
+void shell_executeCommand(SHELL shell, char * commandLine, void (*out)(char 
*), void (*error)(char *)) {
+       unsigned int pos = strcspn(commandLine, " ");
+       char * commandName = (pos != strlen(commandLine)) ? string_ndup((char 
*)commandLine, pos) : commandLine;
+       COMMAND command = shell_getCommand(shell, commandName);
+       if (command != NULL) {
+               command->executeCommand(command, commandLine, out, error);
+       }
+}
+
+COMMAND shell_getCommand(SHELL shell, char * commandName) {
+       COMMAND command = hashMap_get(shell->commandNameMap, commandName);
+       return (command == NULL) ? NULL : command;
+}
+
+void shell_addCommand(SHELL shell, SERVICE_REFERENCE reference) {
+       void * cmd = bundleContext_getService(shell->bundleContext, reference);
+       COMMAND command = (COMMAND) cmd;
+       hashMap_put(shell->commandNameMap, command->name, command);
+       hashMap_put(shell->commandReferenceMap, reference, command);
+}
+
+void shell_removeCommand(SHELL shell, SERVICE_REFERENCE reference) {
+       COMMAND command = (COMMAND) hashMap_remove(shell->commandReferenceMap, 
reference);
+       if (command != NULL) {
+               hashMap_remove(shell->commandNameMap, command->name);
+       }
+}
+
+void shell_serviceChanged(SERVICE_LISTENER listener, SERVICE_EVENT event) {
+       SHELL shell = (SHELL) listener->handle;
+       if (event->type == REGISTERED) {
+               shell_addCommand(shell, event->reference);
+       }
+}
+
+void * bundleActivator_create() {
+       struct shellServiceActivator * activator = malloc(sizeof(*activator));
+       SHELL shell = shell_create();
+       activator->shell = shell;
+       return activator;
+}
+
+void bundleActivator_start(void * userData, BUNDLE_CONTEXT context) {
+       struct shellServiceActivator * activator = (struct 
shellServiceActivator *) userData;
+       activator->shell->bundleContext = context;
+
+       SHELL_SERVICE shellService = (SHELL_SERVICE) 
malloc(sizeof(*shellService));
+       shellService->shell = activator->shell;
+       shellService->getCommands = shell_getCommands;
+       shellService->getCommandDescription = shell_getCommandDescription;
+       shellService->getCommandUsage = shell_getCommandUsage;
+       shellService->getCommandReference = shell_getCommandReference;
+       shellService->executeCommand = shell_executeCommand;
+
+       activator->registration = bundleContext_registerService(context, (char 
*) SHELL_SERVICE_NAME, shellService, NULL);
+
+       SERVICE_LISTENER listener = (SERVICE_LISTENER) 
malloc(sizeof(*listener));
+       listener->handle = activator->shell;
+       listener->serviceChanged = (void *) shell_serviceChanged;
+       addServiceListener(context, listener, "(objectClass=commandService)");
+
+       COMMAND psCommand = psCommand_create(context);
+       bundleContext_registerService(context, (char *) COMMAND_SERVICE_NAME, 
psCommand, NULL);
+
+       COMMAND startCommand = startCommand_create(context);
+       bundleContext_registerService(context, (char *) COMMAND_SERVICE_NAME, 
startCommand, NULL);
+
+       COMMAND stopCommand = stopCommand_create(context);
+       bundleContext_registerService(context, (char *) COMMAND_SERVICE_NAME, 
stopCommand, NULL);
+}
+
+void bundleActivator_stop(void * userData, BUNDLE_CONTEXT context) {
+       struct shellServiceActivator * activator = (struct 
shellServiceActivator *) userData;
+       serviceRegistration_unregister(activator->registration);
+}
+
+void bundleActivator_destroy(void * userData) {
+
+}

Added: incubator/celix/trunk/shell/shell.h
URL: 
http://svn.apache.org/viewvc/incubator/celix/trunk/shell/shell.h?rev=1043905&view=auto
==============================================================================
--- incubator/celix/trunk/shell/shell.h (added)
+++ incubator/celix/trunk/shell/shell.h Thu Dec  9 11:51:04 2010
@@ -0,0 +1,29 @@
+/*
+ * shell.h
+ *
+ *  Created on: Aug 12, 2010
+ *      Author: alexanderb
+ */
+
+#ifndef SHELL_H_
+#define SHELL_H_
+
+#include "headers.h"
+#include "array_list.h"
+
+static const char * const SHELL_SERVICE_NAME = "shellService";
+
+typedef struct shell * SHELL;
+
+struct shellService {
+       SHELL shell;
+       ARRAY_LIST (*getCommands)(SHELL shell);
+       char * (*getCommandUsage)(SHELL shell, char * commandName);
+       char * (*getCommandDescription)(SHELL shell, char * commandName);
+       SERVICE_REFERENCE (*getCommandReference)(SHELL shell, char * command);
+       void (*executeCommand)(SHELL shell, char * commandLine, void 
(*out)(char *), void (*error)(char *));
+};
+
+typedef struct shellService * SHELL_SERVICE;
+
+#endif /* SHELL_H_ */

Added: incubator/celix/trunk/shell/shell_private.h
URL: 
http://svn.apache.org/viewvc/incubator/celix/trunk/shell/shell_private.h?rev=1043905&view=auto
==============================================================================
--- incubator/celix/trunk/shell/shell_private.h (added)
+++ incubator/celix/trunk/shell/shell_private.h Thu Dec  9 11:51:04 2010
@@ -0,0 +1,31 @@
+/*
+ * shell_private.h
+ *
+ *  Created on: Aug 13, 2010
+ *      Author: alexanderb
+ */
+
+#ifndef SHELL_PRIVATE_H_
+#define SHELL_PRIVATE_H_
+
+#include "headers.h"
+#include "shell.h"
+#include "hash_map.h"
+#include "command.h"
+
+struct shell {
+       BUNDLE_CONTEXT bundleContext;
+       HASH_MAP commandReferenceMap;
+       HASH_MAP commandNameMap;
+};
+
+SHELL shell_create();
+char * shell_getCommandUsage(SHELL shell, char * commandName);
+char * shell_getCommandDescription(SHELL shell, char * commandName);
+SERVICE_REFERENCE shell_getCommandReference(SHELL shell, char * command);
+void shell_executeCommand(SHELL shell, char * commandLine, void (*out)(char 
*), void (*error)(char *));
+
+COMMAND shell_getCommand(SHELL shell, char * commandName);
+
+
+#endif /* SHELL_PRIVATE_H_ */

Added: incubator/celix/trunk/shell/start_command.c
URL: 
http://svn.apache.org/viewvc/incubator/celix/trunk/shell/start_command.c?rev=1043905&view=auto
==============================================================================
--- incubator/celix/trunk/shell/start_command.c (added)
+++ incubator/celix/trunk/shell/start_command.c Thu Dec  9 11:51:04 2010
@@ -0,0 +1,43 @@
+/*
+ * start_command.c
+ *
+ *  Created on: Aug 20, 2010
+ *      Author: alexanderb
+ */
+#include <stdlib.h>
+#include <string.h>
+
+#include "command_private.h"
+#include "array_list.h"
+#include "bundle_context.h"
+#include "bundle.h"
+
+void startCommand_execute(COMMAND command, char * line, void (*out)(char *), 
void (*err)(char *));
+
+COMMAND startCommand_create(BUNDLE_CONTEXT context) {
+       COMMAND command = (COMMAND) malloc(sizeof(*command));
+       command->bundleContext = context;
+       command->name = "start";
+       command->shortDescription = "start bundle(s).";
+       command->usage = "start <id> [<id> ...]";
+       command->executeCommand = startCommand_execute;
+       return command;
+}
+
+
+void startCommand_execute(COMMAND command, char * line, void (*out)(char *), 
void (*err)(char *)) {
+       char delims[] = " ";
+       char * sub = NULL;
+       sub = strtok(line, delims);
+       sub = strtok(NULL, delims);
+       while (sub != NULL) {
+               long id = atol(sub);
+               BUNDLE bundle = 
bundleContext_getBundleById(command->bundleContext, id);
+               if (bundle != NULL) {
+                       startBundle(bundle, 0);
+               } else {
+                       err("Bundle id is invalid.");
+               }
+               sub = strtok(NULL, delims);
+       }
+}

Added: incubator/celix/trunk/shell/start_command.h
URL: 
http://svn.apache.org/viewvc/incubator/celix/trunk/shell/start_command.h?rev=1043905&view=auto
==============================================================================
--- incubator/celix/trunk/shell/start_command.h (added)
+++ incubator/celix/trunk/shell/start_command.h Thu Dec  9 11:51:04 2010
@@ -0,0 +1,13 @@
+/*
+ * start_command.h
+ *
+ *  Created on: Aug 20, 2010
+ *      Author: alexanderb
+ */
+
+#ifndef START_COMMAND_H_
+#define START_COMMAND_H_
+
+COMMAND startCommand_create(BUNDLE_CONTEXT context);
+
+#endif /* START_COMMAND_H_ */

Added: incubator/celix/trunk/shell/stop_command.c
URL: 
http://svn.apache.org/viewvc/incubator/celix/trunk/shell/stop_command.c?rev=1043905&view=auto
==============================================================================
--- incubator/celix/trunk/shell/stop_command.c (added)
+++ incubator/celix/trunk/shell/stop_command.c Thu Dec  9 11:51:04 2010
@@ -0,0 +1,43 @@
+/*
+ * stop_command.c
+ *
+ *  Created on: Aug 20, 2010
+ *      Author: alexanderb
+ */
+#include <stdlib.h>
+#include <string.h>
+
+#include "command_private.h"
+#include "array_list.h"
+#include "bundle_context.h"
+#include "bundle.h"
+
+void stopCommand_execute(COMMAND command, char * line, void (*out)(char *), 
void (*err)(char *));
+
+COMMAND stopCommand_create(BUNDLE_CONTEXT context) {
+       COMMAND command = (COMMAND) malloc(sizeof(*command));
+       command->bundleContext = context;
+       command->name = "stop";
+       command->shortDescription = "stop bundle(s).";
+       command->usage = "start <id> [<id> ...]";
+       command->executeCommand = stopCommand_execute;
+       return command;
+}
+
+
+void stopCommand_execute(COMMAND command, char * line, void (*out)(char *), 
void (*err)(char *)) {
+       char delims[] = " ";
+       char * sub = NULL;
+       sub = strtok(line, delims);
+       sub = strtok(NULL, delims);
+       while (sub != NULL) {
+               long id = atol(sub);
+               BUNDLE bundle = 
bundleContext_getBundleById(command->bundleContext, id);
+               if (bundle != NULL) {
+                       stopBundle(bundle, 0);
+               } else {
+                       err("Bundle id is invalid.");
+               }
+               sub = strtok(NULL, delims);
+       }
+}

Added: incubator/celix/trunk/shell/stop_command.h
URL: 
http://svn.apache.org/viewvc/incubator/celix/trunk/shell/stop_command.h?rev=1043905&view=auto
==============================================================================
--- incubator/celix/trunk/shell/stop_command.h (added)
+++ incubator/celix/trunk/shell/stop_command.h Thu Dec  9 11:51:04 2010
@@ -0,0 +1,13 @@
+/*
+ * stop_command.h
+ *
+ *  Created on: Aug 20, 2010
+ *      Author: alexanderb
+ */
+
+#ifndef STOP_COMMAND_H_
+#define STOP_COMMAND_H_
+
+COMMAND stopCommand_create(BUNDLE_CONTEXT context);
+
+#endif /* STOP_COMMAND_H_ */

Added: incubator/celix/trunk/shell_tui/CMakeLists.txt
URL: 
http://svn.apache.org/viewvc/incubator/celix/trunk/shell_tui/CMakeLists.txt?rev=1043905&view=auto
==============================================================================
--- incubator/celix/trunk/shell_tui/CMakeLists.txt (added)
+++ incubator/celix/trunk/shell_tui/CMakeLists.txt Thu Dec  9 11:51:04 2010
@@ -0,0 +1,7 @@
+add_library(shell_tui SHARED shell_tui)
+include_directories("${PROJECT_SOURCE_DIR}/celix")
+include_directories("${PROJECT_SOURCE_DIR}/shell")
+target_link_libraries(shell_tui framework)
+
+bundle(shell_tui)
+package(shell_tui)
\ No newline at end of file

Added: incubator/celix/trunk/shell_tui/MANIFEST/MANIFEST.MF
URL: 
http://svn.apache.org/viewvc/incubator/celix/trunk/shell_tui/MANIFEST/MANIFEST.MF?rev=1043905&view=auto
==============================================================================
--- incubator/celix/trunk/shell_tui/MANIFEST/MANIFEST.MF (added)
+++ incubator/celix/trunk/shell_tui/MANIFEST/MANIFEST.MF Thu Dec  9 11:51:04 
2010
@@ -0,0 +1,4 @@
+Bundle-SymbolicName: shell_tui
+Bundle-Version: 1.0.0
+library: shell_tui
+Import-Service: shellService

Added: incubator/celix/trunk/shell_tui/shell_tui.c
URL: 
http://svn.apache.org/viewvc/incubator/celix/trunk/shell_tui/shell_tui.c?rev=1043905&view=auto
==============================================================================
--- incubator/celix/trunk/shell_tui/shell_tui.c (added)
+++ incubator/celix/trunk/shell_tui/shell_tui.c Thu Dec  9 11:51:04 2010
@@ -0,0 +1,103 @@
+/*
+ * shell_tui.c
+ *
+ *  Created on: Aug 13, 2010
+ *      Author: alexanderb
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pthread.h>
+
+#include "bundle_context.h"
+#include "bundle_activator.h"
+#include "shell.h"
+#include "utils.h"
+
+struct shellTuiActivator {
+       BUNDLE_CONTEXT context;
+       SHELL_SERVICE shell;
+       SERVICE_REFERENCE reference;
+       bool running;
+       pthread_t runnable;
+};
+
+typedef struct shellTuiActivator * SHELL_TUI_ACTIVATOR;
+
+void shellTui_write(char * line) {
+       fprintf(stdout, "%s", line);
+}
+
+void * shellTui_runnable(void * data) {
+       SHELL_TUI_ACTIVATOR act = (SHELL_TUI_ACTIVATOR) data;
+
+       char in[256];
+       bool needPrompt = true;
+       while (act->running) {
+               if (needPrompt) {
+                       printf("-> ");
+                       needPrompt = false;
+               }
+               fgets(in, 256, stdin);
+               needPrompt = true;
+               char * line = string_trim(strdup(in));
+               if (strlen(line) == 0) {
+                       continue;
+               }
+               if (act->shell == NULL) {
+                       continue;
+               }
+               act->shell->executeCommand(act->shell->shell, line, 
shellTui_write, shellTui_write);
+       }
+       pthread_exit(NULL);
+}
+
+void shellTui_initializeService(SHELL_TUI_ACTIVATOR activator) {
+       if (activator->shell == NULL) {
+               activator->reference = 
bundleContext_getServiceReference(activator->context, (char *) 
SHELL_SERVICE_NAME);
+               if (activator->reference != NULL) {
+                       activator->shell = (SHELL_SERVICE) 
bundleContext_getService(activator->context, activator->reference);
+               }
+       }
+}
+
+void shellTui_serviceChanged(SERVICE_LISTENER listener, SERVICE_EVENT event) {
+       SHELL_TUI_ACTIVATOR act = (SHELL_TUI_ACTIVATOR) listener->handle;
+       if ((event->type == REGISTERED) && (act->reference == NULL)) {
+               shellTui_initializeService(act);
+       } else if ((event->type == UNREGISTERING) && (act->reference == 
event->reference)) {
+               // bundleContext_ungetService(act->reference);
+               act->reference = NULL;
+               act->shell = NULL;
+
+               shellTui_initializeService(act);
+       }
+}
+
+void * bundleActivator_create() {
+       SHELL_TUI_ACTIVATOR activator = (SHELL_TUI_ACTIVATOR) 
malloc(sizeof(*activator));
+       activator->shell = NULL;
+       return activator;
+}
+
+void bundleActivator_start(void * userData, BUNDLE_CONTEXT context) {
+       SHELL_TUI_ACTIVATOR act = (SHELL_TUI_ACTIVATOR) userData;
+       act->context = context;
+       act->running = true;
+
+       SERVICE_LISTENER listener = (SERVICE_LISTENER) 
malloc(sizeof(*listener));
+       listener->handle = act;
+       listener->serviceChanged = (void *) shellTui_serviceChanged;
+       addServiceListener(context, listener, "(objectClass=shellService)");
+
+       shellTui_initializeService(act);
+       pthread_create(&act->runnable, NULL, shellTui_runnable, act);
+}
+
+void bundleActivator_stop(void * userData, BUNDLE_CONTEXT context) {
+       SHELL_TUI_ACTIVATOR act = (SHELL_TUI_ACTIVATOR) userData;
+}
+
+void bundleActivator_destroy(void * userData) {
+
+}


Reply via email to