Skip to content

Commit

Permalink
Merge 3c1b5f1 into 4de9ee9
Browse files Browse the repository at this point in the history
  • Loading branch information
ErjanAltena committed Jul 25, 2016
2 parents 4de9ee9 + 3c1b5f1 commit beea9ef
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 89 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ add_subdirectory(device_access)
add_subdirectory(deployment_admin)
add_subdirectory(remote_services)
add_subdirectory(remote_shell)
#add_subdirectory(shell_bonjour) shell_bonjour is still based on APR and old shell command service
add_subdirectory(shell_bonjour)
add_subdirectory(shell_tui)
add_subdirectory(shell)
add_subdirectory(log_writer)
Expand Down
6 changes: 2 additions & 4 deletions shell_bonjour/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@

celix_subproject(SHELL_BONJOUR "Option to enable building the Bonjour Shell (shell access by chat clients)" OFF DEPS LAUNCHER shell)
if (SHELL_BONJOUR)
if(NOT ${WITH_APR})
message(FATAL_ERROR "SHELL_BONJOUR requires APR, enable WITH_APR option.")
endif()
find_package(LibXml2 REQUIRED)

#TODO create/add FindDNS_SD.cmake and use it (with required)
Expand All @@ -35,14 +32,15 @@ if (SHELL_BONJOUR)
include_directories("private/include")

add_bundle(bonjour_shell
VERSION "1.0.0"
SOURCES
private/src/activator.c
private/src/bonjour_shell.c
)

target_link_libraries(bonjour_shell celix_framework celix_utils ${LIBXML2_LIBRARIES} ${DNS_SD_LIB})

add_deploy("bonjour_shell" BUNDLES
add_deploy("bonjour_shell_deploy" BUNDLES
shell
bonjour_shell
PROPERTIES "bonjour.shell.id=Apache Celix"
Expand Down
4 changes: 1 addition & 3 deletions shell_bonjour/private/include/bonjour_shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@
#ifndef BONJOUR_SHELL_H_
#define BONJOUR_SHELL_H_

#include <apr_pools.h>

#include <service_reference.h>

#include "celix_errno.h"

typedef struct bonjour_shell *bonjour_shell_pt;

celix_status_t bonjourShell_create(apr_pool_t *pool, char *id, bonjour_shell_pt *shell);
celix_status_t bonjourShell_create(char *id, bonjour_shell_pt *shell);
celix_status_t bonjourShell_destroy(bonjour_shell_pt shell);

celix_status_t bonjourShell_addShellService(void * handle, service_reference_pt reference, void * service);
Expand Down
104 changes: 48 additions & 56 deletions shell_bonjour/private/src/activator.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include <celix_errno.h>

#include <stdlib.h>
#include <apr_pools.h>

#include "bundle_activator.h"
#include "bundle_context.h"
Expand All @@ -40,79 +39,72 @@
#include <shell.h>

struct bundle_instance {
apr_pool_t *pool;
bonjour_shell_pt shell;
service_tracker_pt tracker;
bonjour_shell_pt shell;
service_tracker_pt tracker;
};

typedef struct bundle_instance *bundle_instance_pt;

celix_status_t bundleActivator_create(bundle_context_pt context, void **userData) {
celix_status_t status = CELIX_SUCCESS;
apr_pool_t *ctxpool;
apr_pool_t *pool;

status = bundleContext_getMemoryPool(context, &ctxpool);
apr_pool_create(&pool, ctxpool);
if (status == CELIX_SUCCESS) {
bundle_instance_pt bi = (bundle_instance_pt) apr_palloc(pool, sizeof(struct bundle_instance));
if (userData != NULL) {
bi->pool = pool;
bi->shell = NULL;
bi->tracker = NULL;
(*userData) = bi;
celix_status_t status = CELIX_SUCCESS;
struct bundle_instance *bi = calloc(1, sizeof(*bi));

if (bi) {
bi->shell = NULL;
bi->tracker = NULL;
(*userData) = bi;
} else {
status = CELIX_ENOMEM;
status = CELIX_ENOMEM;

}
}
return status;
return status;
}

celix_status_t bundleActivator_start(void * userData, bundle_context_pt context) {
celix_status_t status = CELIX_SUCCESS;
bundle_instance_pt bi = (bundle_instance_pt)userData;

char *uuid = NULL;
bundleContext_getProperty(context, OSGI_FRAMEWORK_FRAMEWORK_UUID, &uuid);
char *hostname = NULL;
bundleContext_getProperty(context, "HOSTNAME", &hostname);
char *bonjourShellId = NULL;
bundleContext_getProperty(context, "bonjour.shell.id", &bonjourShellId);

char id[128];
if (bonjourShellId != NULL) {
snprintf(id, 128, bonjourShellId);
} else if (hostname != NULL) {
snprintf(id, 128, "Celix-%.8s@%s", uuid, hostname);
} else {
snprintf(id, 128, "Celix-%.8s", uuid);
}
status = bonjourShell_create(bi->pool, id, &bi->shell);

service_tracker_customizer_pt cust = NULL;
serviceTrackerCustomizer_create(bi->shell, NULL, bonjourShell_addShellService, NULL, bonjourShell_removeShellService, &cust);
serviceTracker_create(context, (char *)OSGI_SHELL_SERVICE_NAME, cust, &bi->tracker);
serviceTracker_open(bi->tracker);


return status;
celix_status_t status = CELIX_SUCCESS;
bundle_instance_pt bi = (bundle_instance_pt) userData;

const char *uuid = NULL;
bundleContext_getProperty(context, OSGI_FRAMEWORK_FRAMEWORK_UUID, &uuid);
const char *hostname = NULL;
bundleContext_getProperty(context, "HOSTNAME", &hostname);
const char *bonjourShellId = NULL;
bundleContext_getProperty(context, "bonjour.shell.id", &bonjourShellId);

char id[128];
if (bonjourShellId != NULL) {
snprintf(id, 128, bonjourShellId);
} else if (hostname != NULL) {
snprintf(id, 128, "Celix-%.8s@%s", uuid, hostname);
} else {
snprintf(id, 128, "Celix-%.8s", uuid);
}
status = bonjourShell_create(id, &bi->shell);

service_tracker_customizer_pt cust = NULL;
serviceTrackerCustomizer_create(bi->shell, NULL, bonjourShell_addShellService, NULL, bonjourShell_removeShellService, &cust);
serviceTracker_create(context, (char *) OSGI_SHELL_SERVICE_NAME, cust, &bi->tracker);
serviceTracker_open(bi->tracker);


return status;
}

celix_status_t bundleActivator_stop(void * userData, bundle_context_pt context) {
celix_status_t status = CELIX_SUCCESS;
bundle_instance_pt bi = (bundle_instance_pt)userData;
celix_status_t status = CELIX_SUCCESS;
bundle_instance_pt bi = (bundle_instance_pt) userData;

serviceTracker_close(bi->tracker);
serviceTracker_close(bi->tracker);

return status;
return status;
}

celix_status_t bundleActivator_destroy(void * userData, bundle_context_pt context) {
celix_status_t status = CELIX_SUCCESS;
bundle_instance_pt bi = (bundle_instance_pt)userData;
celix_status_t status = CELIX_SUCCESS;
bundle_instance_pt bi = (bundle_instance_pt) userData;

serviceTracker_destroy(bi->tracker);
bonjourShell_destroy(bi->shell);
serviceTracker_destroy(bi->tracker);
bonjourShell_destroy(bi->shell);

return status;
return status;
}
66 changes: 41 additions & 25 deletions shell_bonjour/private/src/bonjour_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <sys/time.h>
Expand Down Expand Up @@ -78,7 +79,7 @@ struct connection_context {

static struct connection_context *currentContext = NULL; //TODO update shell to accept void * data next to callback

static void bonjourShell_addDataToCurrentContext(char *buff);
static void bonjourShell_addDataToCurrentContext(const char* out, const char* err);
static void bonjourShell_sendData(struct connection_context *context);

static celix_status_t bonjourShell_register(bonjour_shell_pt shell);
Expand All @@ -92,9 +93,9 @@ static void bonjourShell_parseXmlNode(bonjour_shell_pt shell, struct connection_
static void bonjourShell_parseStream(bonjour_shell_pt shell, struct connection_context *context);
static void bonjourShell_parseCommand(bonjour_shell_pt shell, struct connection_context *context);

celix_status_t bonjourShell_create(apr_pool_t *parentPool, char *id, bonjour_shell_pt *result) {
celix_status_t bonjourShell_create(char *id, bonjour_shell_pt *result) {
celix_status_t status = CELIX_SUCCESS;
bonjour_shell_pt shell = (bonjour_shell_pt) malloc(sizeof(struct bonjour_shell));
bonjour_shell_pt shell = (bonjour_shell_pt) calloc(1, sizeof(*shell));
if (shell != NULL) {
shell->id = strdup(id);
shell->running = true;
Expand Down Expand Up @@ -262,7 +263,7 @@ static void bonjourShell_acceptConnection(bonjour_shell_pt shell, int connection
xmlFreeTextWriter(context.writer);
}
}

arrayList_destroy(context.dataList);

}

Expand Down Expand Up @@ -322,29 +323,44 @@ static void bonjourShell_parseStream(bonjour_shell_pt shell, struct connection_c
}
}


static void bonjourShell_parseCommand(bonjour_shell_pt shell, struct connection_context *context) {
xmlChar *command = xmlTextReaderReadString(context->reader);

if (command != NULL) {
context->gotCommand = true;
currentContext = context;
pthread_mutex_lock(&shell->mutex);
if (shell->service != NULL) {
shell->service->executeCommand(shell->service->shell, (char *)command, bonjourShell_addDataToCurrentContext, bonjourShell_addDataToCurrentContext);
}
pthread_mutex_unlock(&shell->mutex);
}

if (command != NULL) {
xmlFree(command);
}
static void bonjourShell_parseCommand(bonjour_shell_pt shell, struct connection_context *context)
{
xmlChar *command = xmlTextReaderReadString(context->reader);

if (command != NULL) {
context->gotCommand = true;
currentContext = context;
pthread_mutex_lock(&shell->mutex);
if (shell->service != NULL) {
char *outbuf;
size_t outsize;
char *errbuf;
size_t errsize;

FILE *out = open_memstream(&outbuf, &outsize);
FILE *err = open_memstream(&errbuf, &errsize);

shell->service->executeCommand(shell->service->shell, (char *) command, out, err);

fclose(out);
fclose(err);
bonjourShell_addDataToCurrentContext(outbuf, errbuf);
free(outbuf);
free(errbuf);
}
pthread_mutex_unlock(&shell->mutex);
}

if (command != NULL) {
xmlFree(command);
}
}

static void bonjourShell_addDataToCurrentContext(char *buff) {
static void bonjourShell_addDataToCurrentContext(const char* out, const char* err) {
pthread_mutex_lock(&currentContext->mutex);
arrayList_add(currentContext->dataList, strdup(buff));
gettimeofday(&currentContext->lastUpdated, NULL);
arrayList_add(currentContext->dataList, strdup(out));
arrayList_add(currentContext->dataList, strdup(err));
gettimeofday(&currentContext->lastUpdated, NULL);
pthread_mutex_unlock(&currentContext->mutex);
pthread_cond_signal(&currentContext->dataAvailCond);
}
Expand Down Expand Up @@ -396,7 +412,7 @@ celix_status_t bonjourShell_destroy(bonjour_shell_pt shell) {

close(shell->listenSocket);
pthread_join(shell->listenThread, NULL);

free(shell->id);
free(shell);
return CELIX_SUCCESS;
}
Expand Down

0 comments on commit beea9ef

Please sign in to comment.