You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@celix.apache.org by pn...@apache.org on 2015/11/03 10:39:02 UTC

[07/13] celix git commit: CELIX-230: Refactored shell command for remote service calculator example. Added missing include in command.h

CELIX-230: Refactored shell command for remote service calculator example. Added missing include in command.h


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

Branch: refs/heads/feature/CELIX-269_depman
Commit: b63c0d9fc96ec198437f5360e4cfb6f1152a056b
Parents: 3bf493e
Author: Pepijn Noltes <pe...@gmail.com>
Authored: Tue Oct 27 16:54:44 2015 +0100
Committer: Pepijn Noltes <pe...@gmail.com>
Committed: Tue Oct 27 16:54:44 2015 +0100

----------------------------------------------------------------------
 .../examples/calculator_shell/CMakeLists.txt    |  1 -
 .../private/include/add_command.h               |  5 +-
 .../private/include/sqrt_command.h              |  5 +-
 .../private/include/sub_command.h               |  5 +-
 .../calculator_shell/private/src/add_command.c  | 46 +++++-----------
 .../private/src/calculator_shell_activator.c    | 55 +++++++++-----------
 .../calculator_shell/private/src/sqrt_command.c | 42 ++++-----------
 .../calculator_shell/private/src/sub_command.c  | 46 +++++-----------
 shell/CMakeLists.txt                            |  6 +--
 shell/public/include/command.h                  |  1 +
 10 files changed, 71 insertions(+), 141 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/celix/blob/b63c0d9f/remote_services/examples/calculator_shell/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/remote_services/examples/calculator_shell/CMakeLists.txt b/remote_services/examples/calculator_shell/CMakeLists.txt
index dcb8c8f..b93afca 100644
--- a/remote_services/examples/calculator_shell/CMakeLists.txt
+++ b/remote_services/examples/calculator_shell/CMakeLists.txt
@@ -32,7 +32,6 @@ bundle(calculator_shell SOURCES
     private/include/add_command.h
     private/include/sqrt_command.h
     private/include/sub_command.h
-    ../../../shell/public/src/command.c
 
 
         FILES

http://git-wip-us.apache.org/repos/asf/celix/blob/b63c0d9f/remote_services/examples/calculator_shell/private/include/add_command.h
----------------------------------------------------------------------
diff --git a/remote_services/examples/calculator_shell/private/include/add_command.h b/remote_services/examples/calculator_shell/private/include/add_command.h
index 1ff9d96..58a7bda 100644
--- a/remote_services/examples/calculator_shell/private/include/add_command.h
+++ b/remote_services/examples/calculator_shell/private/include/add_command.h
@@ -27,9 +27,6 @@
 #ifndef ADD_COMMAND_H_
 #define ADD_COMMAND_H_
 
-#include "command_impl.h"
-
-command_pt addCommand_create(bundle_context_pt context);
-void addCommand_destroy(command_pt command);
+void addCommand_execute(bundle_context_pt context, char *line, FILE *out, FILE *err);
 
 #endif /* ADD_COMMAND_H_ */

http://git-wip-us.apache.org/repos/asf/celix/blob/b63c0d9f/remote_services/examples/calculator_shell/private/include/sqrt_command.h
----------------------------------------------------------------------
diff --git a/remote_services/examples/calculator_shell/private/include/sqrt_command.h b/remote_services/examples/calculator_shell/private/include/sqrt_command.h
index 257abff..c9d07d2 100644
--- a/remote_services/examples/calculator_shell/private/include/sqrt_command.h
+++ b/remote_services/examples/calculator_shell/private/include/sqrt_command.h
@@ -27,9 +27,6 @@
 #ifndef SQRT_COMMAND_H_
 #define SQRT_COMMAND_H_
 
-#include "command_impl.h"
-
-command_pt sqrtCommand_create(bundle_context_pt context);
-void sqrtCommand_destroy(command_pt command);
+void sqrtCommand_execute(bundle_context_pt context, char *line, FILE *out, FILE *err);
 
 #endif /* SQRT_COMMAND_H_ */

http://git-wip-us.apache.org/repos/asf/celix/blob/b63c0d9f/remote_services/examples/calculator_shell/private/include/sub_command.h
----------------------------------------------------------------------
diff --git a/remote_services/examples/calculator_shell/private/include/sub_command.h b/remote_services/examples/calculator_shell/private/include/sub_command.h
index d584835..cf13616 100644
--- a/remote_services/examples/calculator_shell/private/include/sub_command.h
+++ b/remote_services/examples/calculator_shell/private/include/sub_command.h
@@ -27,9 +27,6 @@
 #ifndef SUB_COMMAND_H_
 #define SUB_COMMAND_H_
 
-#include "command_impl.h"
-
-command_pt subCommand_create(bundle_context_pt context);
-void subCommand_destroy(command_pt command);
+void subCommand_execute(bundle_context_pt context, char *line, FILE *out, FILE *err);
 
 #endif /* SUB_COMMAND_H_ */

http://git-wip-us.apache.org/repos/asf/celix/blob/b63c0d9f/remote_services/examples/calculator_shell/private/src/add_command.c
----------------------------------------------------------------------
diff --git a/remote_services/examples/calculator_shell/private/src/add_command.c b/remote_services/examples/calculator_shell/private/src/add_command.c
index 260add3..838ff25 100644
--- a/remote_services/examples/calculator_shell/private/src/add_command.c
+++ b/remote_services/examples/calculator_shell/private/src/add_command.c
@@ -27,6 +27,7 @@
 #include <stdlib.h>
 #include <ctype.h>
 #include <string.h>
+#include <command.h>
 
 #include "array_list.h"
 #include "bundle_context.h"
@@ -34,72 +35,53 @@
 #include "calculator_service.h"
 
 
-void addCommand_execute(command_pt command, char * line, void (*out)(char *), void (*err)(char *));
-celix_status_t addCommand_isNumeric(command_pt command, char *number, bool *ret);
+static celix_status_t addCommand_isNumeric(char *number, bool *ret);
 
-command_pt addCommand_create(bundle_context_pt context) {
-    command_pt command = (command_pt) calloc(1, sizeof(*command));
-    if (command) {
-		command->bundleContext = context;
-		command->name = "add";
-		command->shortDescription = "add the given doubles";
-		command->usage = "add <double> <double>";
-		command->executeCommand = addCommand_execute;
-    }
-    return command;
-}
-
-void addCommand_destroy(command_pt command) {
-	free(command);
-}
-
-void addCommand_execute(command_pt command, char *line, void (*out)(char *), void (*err)(char *)) {
+void addCommand_execute(bundle_context_pt context, char *line, FILE *out, FILE *err) {
 	celix_status_t status = CELIX_SUCCESS;
     service_reference_pt calculatorService = NULL;
 
-    status = bundleContext_getServiceReference(command->bundleContext, (char *) CALCULATOR_SERVICE, &calculatorService);
+    status = bundleContext_getServiceReference(context, (char *) CALCULATOR_SERVICE, &calculatorService);
     if (status == CELIX_SUCCESS) {
     	char *token = line;
     	strtok_r(line, " ", &token);
 		char *aStr = strtok_r(NULL, " ", &token);
 		bool numeric;
-		addCommand_isNumeric(command, aStr, &numeric);
+		addCommand_isNumeric(aStr, &numeric);
 		if (aStr != NULL && numeric) {
 			char *bStr = strtok_r(NULL, " ", &token);
-			addCommand_isNumeric(command, bStr, &numeric);
+			addCommand_isNumeric(bStr, &numeric);
 			if (bStr != NULL && numeric) {
 				calculator_service_pt calculator = NULL;
-				status = bundleContext_getService(command->bundleContext, calculatorService, (void *) &calculator);
+				status = bundleContext_getService(context, calculatorService, (void *) &calculator);
 				if (status == CELIX_SUCCESS) {
 					double a = atof(aStr);
 					double b = atof(bStr);
 					double result = 0;
 					status = calculator->add(calculator->calculator, a, b, &result);
 					if (status == CELIX_SUCCESS) {
-						char line[256];
-						sprintf(line, "CALCULATOR_SHELL: Add: %f + %f = %f\n", a, b, result);
-						out(line);
+						fprintf(out, "CALCULATOR_SHELL: Add: %f + %f = %f\n", a, b, result);
 					} else {
-						out("ADD: Unexpected exception in Calc service\n");
+						fprintf(err, "ADD: Unexpected exception in Calc service\n");
 					}
 				} else {
-					out("No calc service available\n");
+					fprintf(err, "No calc service available\n");
 				}
 			} else {
-				out("ADD: Requires 2 numerical parameter\n");
+				fprintf(err, "ADD: Requires 2 numerical parameter\n");
 			}
 		} else {
-			out("ADD: Requires 2 numerical parameter\n");
+			fprintf(err, "ADD: Requires 2 numerical parameter\n");
 			status = CELIX_ILLEGAL_ARGUMENT;
 		}
     } else {
-        out("No calc service available\n");
+		fprintf(err, "No calc service available\n");
     }
 
     //return status;
 }
 
-celix_status_t addCommand_isNumeric(command_pt command, char *number, bool *ret) {
+static celix_status_t addCommand_isNumeric(char *number, bool *ret) {
 	celix_status_t status = CELIX_SUCCESS;
 	*ret = true;
 	while(*number) {

http://git-wip-us.apache.org/repos/asf/celix/blob/b63c0d9f/remote_services/examples/calculator_shell/private/src/calculator_shell_activator.c
----------------------------------------------------------------------
diff --git a/remote_services/examples/calculator_shell/private/src/calculator_shell_activator.c b/remote_services/examples/calculator_shell/private/src/calculator_shell_activator.c
index d0fca76..275a67d 100644
--- a/remote_services/examples/calculator_shell/private/src/calculator_shell_activator.c
+++ b/remote_services/examples/calculator_shell/private/src/calculator_shell_activator.c
@@ -25,33 +25,30 @@
  */
 #include <stdlib.h>
 #include <string.h>
+#include <command.h>
 
 #include "bundle_activator.h"
 #include "bundle_context.h"
 #include "service_registration.h"
 
-#include "command_impl.h"
-
 #include "add_command.h"
 #include "sub_command.h"
 #include "sqrt_command.h"
 
 struct activator {
 	service_registration_pt addCommand;
-	command_pt addCmd;
+	command_service_pt addCmd;
 	command_service_pt addCmdSrv;
 
 	service_registration_pt subCommand;
-	command_pt subCmd;
+	command_service_pt subCmd;
 	command_service_pt subCmdSrv;
 
 	service_registration_pt sqrtCommand;
-	command_pt sqrtCmd;
+	command_service_pt sqrtCmd;
 	command_service_pt sqrtCmdSrv;
 };
 
-static celix_status_t calculatorShell_createCommandService(command_pt command, command_service_pt *commandService);
-
 celix_status_t bundleActivator_create(bundle_context_pt context, void **userData) {
 	celix_status_t status = CELIX_SUCCESS;
 	if (status == CELIX_SUCCESS) {
@@ -81,31 +78,31 @@ celix_status_t bundleActivator_start(void * userData, bundle_context_pt context)
 
 	struct activator * activator = (struct activator *) userData;
 
-	activator->addCmd = addCommand_create(context);
-	calculatorShell_createCommandService(activator->addCmd, &activator->addCmdSrv);
-	bundleContext_registerService(context, (char *) OSGI_SHELL_COMMAND_SERVICE_NAME, activator->addCmdSrv, NULL, &activator->addCommand);
+	activator->addCmdSrv = calloc(1, sizeof(*activator->addCmdSrv));
+	activator->addCmdSrv->handle = context;
+	activator->addCmdSrv->executeCommand = (void *)addCommand_execute;
+	properties_pt props = properties_create();
+	properties_set(props, OSGI_SHELL_COMMAND_NAME, "add");
+	bundleContext_registerService(context, OSGI_SHELL_COMMAND_SERVICE_NAME, activator->addCmdSrv, props, &activator->addCommand);
+
 
-	activator->subCmd = subCommand_create(context);
-	calculatorShell_createCommandService(activator->subCmd, &activator->subCmdSrv);
-	bundleContext_registerService(context, (char *) OSGI_SHELL_COMMAND_SERVICE_NAME, activator->subCmdSrv, NULL, &activator->subCommand);
+	activator->sqrtCmdSrv = calloc(1, sizeof(*activator->sqrtCmdSrv));
+	activator->sqrtCmdSrv->handle = context;
+	activator->sqrtCmdSrv->executeCommand = (void *)sqrtCommand_execute;
+	props = properties_create();
+	properties_set(props, OSGI_SHELL_COMMAND_NAME, "sqrt");
+	bundleContext_registerService(context, OSGI_SHELL_COMMAND_SERVICE_NAME, activator->sqrtCmdSrv, props, &activator->sqrtCommand);
 
-	activator->sqrtCmd = sqrtCommand_create(context);
-	calculatorShell_createCommandService(activator->sqrtCmd, &activator->sqrtCmdSrv);
-	bundleContext_registerService(context, (char *) OSGI_SHELL_COMMAND_SERVICE_NAME, activator->sqrtCmdSrv, NULL, &activator->sqrtCommand);
+	activator->subCmdSrv = calloc(1, sizeof(*activator->subCmdSrv));
+	activator->subCmdSrv->handle = context;
+	activator->subCmdSrv->executeCommand = (void *)subCommand_execute;
+	props = properties_create();
+	properties_set(props, OSGI_SHELL_COMMAND_NAME, "sub");
+	bundleContext_registerService(context, OSGI_SHELL_COMMAND_SERVICE_NAME, activator->subCmdSrv, props, &activator->subCommand);
 
 	return status;
 }
 
-static celix_status_t calculatorShell_createCommandService(command_pt command, command_service_pt *commandService) {
-	*commandService = calloc(1, sizeof(**commandService));
-	(*commandService)->command = command;
-	(*commandService)->executeCommand = command->executeCommand;
-	(*commandService)->getName = command_getName;
-	(*commandService)->getShortDescription = command_getShortDescription;
-	(*commandService)->getUsage = command_getUsage;
-
-	return CELIX_SUCCESS;
-}
 
 celix_status_t bundleActivator_stop(void * userData, bundle_context_pt context) {
     celix_status_t status = CELIX_SUCCESS;
@@ -119,9 +116,9 @@ celix_status_t bundleActivator_stop(void * userData, bundle_context_pt context)
 	free(activator->sqrtCmdSrv);
 
 	if (status == CELIX_SUCCESS) {
-        addCommand_destroy(activator->addCmd);
-        subCommand_destroy(activator->subCmd);
-        sqrtCommand_destroy(activator->sqrtCmd);
+        free(activator->addCmdSrv);
+		free(activator->sqrtCmdSrv);
+		free(activator->subCmdSrv);
 	}
 
 	return status;

http://git-wip-us.apache.org/repos/asf/celix/blob/b63c0d9f/remote_services/examples/calculator_shell/private/src/sqrt_command.c
----------------------------------------------------------------------
diff --git a/remote_services/examples/calculator_shell/private/src/sqrt_command.c b/remote_services/examples/calculator_shell/private/src/sqrt_command.c
index 87efde3..3cd0e5d 100644
--- a/remote_services/examples/calculator_shell/private/src/sqrt_command.c
+++ b/remote_services/examples/calculator_shell/private/src/sqrt_command.c
@@ -33,66 +33,46 @@
 #include "sqrt_command.h"
 #include "calculator_service.h"
 
+static celix_status_t sqrtCommand_isNumeric(char *number, bool *ret);
 
-void sqrtCommand_execute(command_pt command, char * line, void (*out)(char *), void (*err)(char *));
-celix_status_t sqrtCommand_isNumeric(command_pt command, char *number, bool *ret);
-
-command_pt sqrtCommand_create(bundle_context_pt context) {
-    command_pt command = (command_pt) calloc(1, sizeof(*command));
-    if (command) {
-		command->bundleContext = context;
-		command->name = "sqrt";
-		command->shortDescription = "calculates the square root of the given double";
-		command->usage = "sqrt <double>";
-		command->executeCommand = sqrtCommand_execute;
-    }
-    return command;
-}
-
-void sqrtCommand_destroy(command_pt command) {
-	free(command);
-}
-
-void sqrtCommand_execute(command_pt command, char *line, void (*out)(char *), void (*err)(char *)) {
+void sqrtCommand_execute(bundle_context_pt context, char *line, FILE *out, FILE *err) {
 	celix_status_t status = CELIX_SUCCESS;
     service_reference_pt calculatorService = NULL;
 
-    status = bundleContext_getServiceReference(command->bundleContext, (char *) CALCULATOR_SERVICE, &calculatorService);
+    status = bundleContext_getServiceReference(context, (char *) CALCULATOR_SERVICE, &calculatorService);
     if (status == CELIX_SUCCESS) {
     	char *token = line;
     	strtok_r(line, " ", &token);
 		char *aStr = strtok_r(NULL, " ", &token);
 		bool numeric;
-		sqrtCommand_isNumeric(command, aStr, &numeric);
+		sqrtCommand_isNumeric(aStr, &numeric);
 		if (aStr != NULL && numeric) {
 			calculator_service_pt calculator = NULL;
-			status = bundleContext_getService(command->bundleContext, calculatorService, (void *) &calculator);
+			status = bundleContext_getService(context, calculatorService, (void *) &calculator);
 			if (status == CELIX_SUCCESS) {
 				double a = atof(aStr);
 				double result = 0;
 				status = calculator->sqrt(calculator->calculator, a, &result);
 				if (status == CELIX_SUCCESS) {
-					char line[256];
-					sprintf(line, "CALCULATOR_SHELL: Sqrt: %f = %f\n", a, result);
-					out(line);
+					fprintf(out, "CALCULATOR_SHELL: Sqrt: %f = %f\n", a, result);
 				} else {
-					out("SQRT: Unexpected exception in Calc service\n");
+					fprintf(err, "SQRT: Unexpected exception in Calc service\n");
 				}
 			} else {
-				out("No calc service available\n");
+				fprintf(err, "No calc service available\n");
 			}
 		} else {
-			out("SQRT: Requires 1 numerical parameter\n");
+			fprintf(err, "SQRT: Requires 1 numerical parameter\n");
 			status = CELIX_ILLEGAL_ARGUMENT;
 		}
     } else {
-        out("No calc service available\n");
+		fprintf(err, "No calc service available\n");
     }
 
     //return status;
 }
 
-celix_status_t sqrtCommand_isNumeric(command_pt command, char *number, bool *ret) {
+static celix_status_t sqrtCommand_isNumeric(char *number, bool *ret) {
 	celix_status_t status = CELIX_SUCCESS;
 	*ret = true;
 	while(*number) {

http://git-wip-us.apache.org/repos/asf/celix/blob/b63c0d9f/remote_services/examples/calculator_shell/private/src/sub_command.c
----------------------------------------------------------------------
diff --git a/remote_services/examples/calculator_shell/private/src/sub_command.c b/remote_services/examples/calculator_shell/private/src/sub_command.c
index ff810e6..87fabc2 100644
--- a/remote_services/examples/calculator_shell/private/src/sub_command.c
+++ b/remote_services/examples/calculator_shell/private/src/sub_command.c
@@ -33,73 +33,53 @@
 #include "sub_command.h"
 #include "calculator_service.h"
 
+static celix_status_t subCommand_isNumeric(char *number, bool *ret);
 
-void subCommand_execute(command_pt command, char * line, void (*out)(char *), void (*err)(char *));
-celix_status_t subCommand_isNumeric(command_pt command, char *number, bool *ret);
-
-command_pt subCommand_create(bundle_context_pt context) {
-    command_pt command = (command_pt) calloc(1, sizeof(*command));
-    if (command) {
-		command->bundleContext = context;
-		command->name = "sub";
-		command->shortDescription = "subtract the given doubles";
-		command->usage = "sub <double> <double>";
-		command->executeCommand = subCommand_execute;
-    }
-    return command;
-}
-
-void subCommand_destroy(command_pt command) {
-	free(command);
-}
-
-void subCommand_execute(command_pt command, char *line, void (*out)(char *), void (*err)(char *)) {
+void subCommand_execute(bundle_context_pt context, char *line, FILE *out, FILE *err) {
 	celix_status_t status = CELIX_SUCCESS;
     service_reference_pt calculatorService = NULL;
 
-    status = bundleContext_getServiceReference(command->bundleContext, (char *) CALCULATOR_SERVICE, &calculatorService);
+    status = bundleContext_getServiceReference(context, (char *) CALCULATOR_SERVICE, &calculatorService);
     if (status == CELIX_SUCCESS) {
     	char *token = line;
     	strtok_r(line, " ", &token);
 		char *aStr = strtok_r(NULL, " ", &token);
 		bool numeric;
-		subCommand_isNumeric(command, aStr, &numeric);
+		subCommand_isNumeric(aStr, &numeric);
 		if (aStr != NULL && numeric) {
 			char *bStr = strtok_r(NULL, " ", &token);
-			subCommand_isNumeric(command, bStr, &numeric);
+			subCommand_isNumeric(bStr, &numeric);
 			if (bStr != NULL && numeric) {
 				calculator_service_pt calculator = NULL;
-				status = bundleContext_getService(command->bundleContext, calculatorService, (void *) &calculator);
+				status = bundleContext_getService(context, calculatorService, (void *) &calculator);
 				if (status == CELIX_SUCCESS) {
 					double a = atof(aStr);
 					double b = atof(bStr);
 					double result = 0;
 					status = calculator->sub(calculator->calculator, a, b, &result);
 					if (status == CELIX_SUCCESS) {
-						char line[256];
-						sprintf(line, "CALCULATOR_SHELL: Sub: %f - %f = %f\n", a, b, result);
-						out(line);
+						fprintf(out, "CALCULATOR_SHELL: Sub: %f - %f = %f\n", a, b, result);
 					} else {
-						out("SUB: Unexpected exception in Calc service\n");
+						fprintf(err, "SUB: Unexpected exception in Calc service\n");
 					}
 				} else {
-					out("No calc service available\n");
+					fprintf(err, "No calc service available\n");
 				}
 			} else {
-				out("SUB: Requires 2 numerical parameter\n");
+				fprintf(err, "SUB: Requires 2 numerical parameter\n");
 			}
 		} else {
-			out("SUB: Requires 2 numerical parameter\n");
+			fprintf(err, "SUB: Requires 2 numerical parameter\n");
 			status = CELIX_ILLEGAL_ARGUMENT;
 		}
     } else {
-        out("No calc service available\n");
+		fprintf(err, "No calc service available\n");
     }
 
     //return status;
 }
 
-celix_status_t subCommand_isNumeric(command_pt command, char *number, bool *ret) {
+static celix_status_t subCommand_isNumeric(char *number, bool *ret) {
 	celix_status_t status = CELIX_SUCCESS;
 	*ret = true;
 	while(*number) {

http://git-wip-us.apache.org/repos/asf/celix/blob/b63c0d9f/shell/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/shell/CMakeLists.txt b/shell/CMakeLists.txt
index 95bddd7..26252c4 100644
--- a/shell/CMakeLists.txt
+++ b/shell/CMakeLists.txt
@@ -24,9 +24,9 @@ if (SHELL)
 
     bundle(shell SOURCES
     	
-     	private/src/activator
-    	private/src/shell 
-    	private/src/lb_command
+	  private/src/activator
+	  private/src/shell
+	  private/src/lb_command
       private/src/start_command 
       private/src/stop_command
       private/src/install_command

http://git-wip-us.apache.org/repos/asf/celix/blob/b63c0d9f/shell/public/include/command.h
----------------------------------------------------------------------
diff --git a/shell/public/include/command.h b/shell/public/include/command.h
index fce2ca4..97872c7 100644
--- a/shell/public/include/command.h
+++ b/shell/public/include/command.h
@@ -28,6 +28,7 @@
 #define COMMAND_H_
 
 #include "celix_errno.h"
+#include <stdio.h>
 
 #define OSGI_SHELL_COMMAND_NAME "command.name"
 #define OSGI_SHELL_COMMAND_USAGE "command.usage"