Skip to content

Commit

Permalink
Changed event system to work without signals
Browse files Browse the repository at this point in the history
Fixed some more memory leaks
Better function arguments passing
  • Loading branch information
sharky76 committed Mar 26, 2016
1 parent 9849926 commit 8ed09c2
Show file tree
Hide file tree
Showing 17 changed files with 189 additions and 94 deletions.
6 changes: 3 additions & 3 deletions BuiltinServices/Expression.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ variant_t* process_template_impl(struct service_method_t* method, va_list args)

if(NULL != service_stack)
{
variant_t* token_table_var = stack_pop_front(service_stack->stack);
hash_table_t* token_table = (hash_table_t*)variant_get_ptr(token_table_var);
//variant_t* token_table_var = stack_pop_front(service_stack->stack);
hash_table_t* token_table = (hash_table_t*)service_stack->data_storage;//variant_get_ptr(token_table_var);

// Replace all tokens starting with "$" with matching values from token table
char* template = (char*)variant_get_string(template_var);
Expand Down Expand Up @@ -111,7 +111,7 @@ variant_t* process_template_impl(struct service_method_t* method, va_list args)
++tokens;
}

variant_free(token_table_var);
//variant_free(token_table_var);
}

// Remove last space...
Expand Down
32 changes: 16 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ COMMON_OBJ=$(OUTDIR)/Conditional.o $(OUTDIR)/Expression.o \
$(OUTDIR)/cli_sensor.o $(OUTDIR)/cli_service.o \
$(OUTDIR)/command_class.o $(OUTDIR)/command_parser.o \
$(OUTDIR)/config.o $(OUTDIR)/data_callbacks.o \
$(OUTDIR)/device_callbacks.o $(OUTDIR)/event_manager.o \
$(OUTDIR)/http_server.o $(OUTDIR)/logging_modules.o \
$(OUTDIR)/operator.o $(OUTDIR)/parser_dfa.o $(OUTDIR)/resolver.o \
$(OUTDIR)/scene.o $(OUTDIR)/scene_action.o $(OUTDIR)/scene_manager.o \
$(OUTDIR)/device_callbacks.o $(OUTDIR)/http_server.o \
$(OUTDIR)/logging_modules.o $(OUTDIR)/operator.o \
$(OUTDIR)/parser_dfa.o $(OUTDIR)/resolver.o $(OUTDIR)/scene.o \
$(OUTDIR)/scene_action.o $(OUTDIR)/scene_manager.o \
$(OUTDIR)/script_action_handler.o $(OUTDIR)/service_manager.o \
$(OUTDIR)/user_manager.o $(OUTDIR)/vty_io.o \
$(OUTDIR)/zwave-automation-engine.o
Expand All @@ -45,10 +45,10 @@ ALL_OBJ=$(OUTDIR)/Conditional.o $(OUTDIR)/Expression.o \
$(OUTDIR)/cli_sensor.o $(OUTDIR)/cli_service.o \
$(OUTDIR)/command_class.o $(OUTDIR)/command_parser.o \
$(OUTDIR)/config.o $(OUTDIR)/data_callbacks.o \
$(OUTDIR)/device_callbacks.o $(OUTDIR)/event_manager.o \
$(OUTDIR)/http_server.o $(OUTDIR)/logging_modules.o \
$(OUTDIR)/operator.o $(OUTDIR)/parser_dfa.o $(OUTDIR)/resolver.o \
$(OUTDIR)/scene.o $(OUTDIR)/scene_action.o $(OUTDIR)/scene_manager.o \
$(OUTDIR)/device_callbacks.o $(OUTDIR)/http_server.o \
$(OUTDIR)/logging_modules.o $(OUTDIR)/operator.o \
$(OUTDIR)/parser_dfa.o $(OUTDIR)/resolver.o $(OUTDIR)/scene.o \
$(OUTDIR)/scene_action.o $(OUTDIR)/scene_manager.o \
$(OUTDIR)/script_action_handler.o $(OUTDIR)/service_manager.o \
$(OUTDIR)/user_manager.o $(OUTDIR)/vty_io.o \
$(OUTDIR)/zwave-automation-engine.o -l:libreadline.a -l:libncurses.a \
Expand Down Expand Up @@ -107,10 +107,10 @@ COMMON_OBJ=$(OUTDIR)/Conditional.o $(OUTDIR)/Expression.o \
$(OUTDIR)/cli_sensor.o $(OUTDIR)/cli_service.o \
$(OUTDIR)/command_class.o $(OUTDIR)/command_parser.o \
$(OUTDIR)/config.o $(OUTDIR)/data_callbacks.o \
$(OUTDIR)/device_callbacks.o $(OUTDIR)/event_manager.o \
$(OUTDIR)/http_server.o $(OUTDIR)/logging_modules.o \
$(OUTDIR)/operator.o $(OUTDIR)/parser_dfa.o $(OUTDIR)/resolver.o \
$(OUTDIR)/scene.o $(OUTDIR)/scene_action.o $(OUTDIR)/scene_manager.o \
$(OUTDIR)/device_callbacks.o $(OUTDIR)/http_server.o \
$(OUTDIR)/logging_modules.o $(OUTDIR)/operator.o \
$(OUTDIR)/parser_dfa.o $(OUTDIR)/resolver.o $(OUTDIR)/scene.o \
$(OUTDIR)/scene_action.o $(OUTDIR)/scene_manager.o \
$(OUTDIR)/script_action_handler.o $(OUTDIR)/service_manager.o \
$(OUTDIR)/user_manager.o $(OUTDIR)/vty_io.o \
$(OUTDIR)/zwave-automation-engine.o
Expand All @@ -122,10 +122,10 @@ ALL_OBJ=$(OUTDIR)/Conditional.o $(OUTDIR)/Expression.o \
$(OUTDIR)/cli_sensor.o $(OUTDIR)/cli_service.o \
$(OUTDIR)/command_class.o $(OUTDIR)/command_parser.o \
$(OUTDIR)/config.o $(OUTDIR)/data_callbacks.o \
$(OUTDIR)/device_callbacks.o $(OUTDIR)/event_manager.o \
$(OUTDIR)/http_server.o $(OUTDIR)/logging_modules.o \
$(OUTDIR)/operator.o $(OUTDIR)/parser_dfa.o $(OUTDIR)/resolver.o \
$(OUTDIR)/scene.o $(OUTDIR)/scene_action.o $(OUTDIR)/scene_manager.o \
$(OUTDIR)/device_callbacks.o $(OUTDIR)/http_server.o \
$(OUTDIR)/logging_modules.o $(OUTDIR)/operator.o \
$(OUTDIR)/parser_dfa.o $(OUTDIR)/resolver.o $(OUTDIR)/scene.o \
$(OUTDIR)/scene_action.o $(OUTDIR)/scene_manager.o \
$(OUTDIR)/script_action_handler.o $(OUTDIR)/service_manager.o \
$(OUTDIR)/user_manager.o $(OUTDIR)/vty_io.o \
$(OUTDIR)/zwave-automation-engine.o -l:libreadline.a -l:libncurses.a \
Expand Down
26 changes: 19 additions & 7 deletions builtin_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

DECLARE_LOGGER(BuiltinService)

static hash_table_t* service_stack_table;

builtin_service_t* builtin_service_create(hash_table_t* service_table, const char* name, const char* description)
{
builtin_service_t* new_service = calloc(1, sizeof(builtin_service_t));
Expand Down Expand Up @@ -34,33 +36,40 @@ void service_stack_delete(void* arg)
service_stack_t* s = (service_stack_t*)arg;
free(s->service_name);
stack_free(s->stack);
variant_hash_free(s->data_storage);
free(s);
}

void builtin_service_stack_create(const char* service_name)
{
uint32_t key = crc32(0, service_name, strlen(service_name));

if(NULL == service_stack_table)
{
service_stack_table = variant_hash_init();
}

if(NULL != variant_hash_get(service_stack_table, key))
{
return;
}

service_stack_t* s = malloc(sizeof(service_stack_t));
s->service_name = strdup(service_name);
s->stack = stack_create();

uint32_t key = crc32(0, service_name, strlen(service_name));
s->data_storage = variant_hash_init();
variant_hash_insert(service_stack_table, key, variant_create_ptr(DT_PTR, s, service_stack_delete));
}

void builtin_service_stack_add(const char* service_name, variant_t* data)
void builtin_service_stack_add(const char* service_name, uint32_t value_key, variant_t* value)
{
uint32_t key = crc32(0, service_name, strlen(service_name));
variant_t* stack_var = variant_hash_get(service_stack_table, key);

if(NULL != stack_var)
{
service_stack_t* s = (service_stack_t*)variant_get_ptr(stack_var);
stack_push_front(s->stack, data);
variant_hash_insert(s->data_storage, value_key, value);
}
}

Expand All @@ -77,9 +86,12 @@ service_stack_t* builtin_service_stack_get(const char* service_name)
return NULL;
}

void builtin_service_stack_clear(const char* service_name)
void builtin_service_stack_clear()
{
uint32_t key = crc32(0, service_name, strlen(service_name));
variant_hash_remove(service_stack_table, key);
if(NULL != service_stack_table)
{
variant_hash_free(service_stack_table);
service_stack_table = NULL;
}
}

7 changes: 3 additions & 4 deletions builtin_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ typedef struct service_stack_t
{
char* service_name;
variant_stack_t* stack;
hash_table_t* data_storage;
} service_stack_t;

static hash_table_t* service_stack_table;

builtin_service_t* builtin_service_create(hash_table_t* service_table, const char* name, const char* description);
void builtin_service_add_method(builtin_service_t* service, const char* name, const char* help, int args, variant_t* (*eval_callback)(struct service_method_t*, va_list));
void builtin_service_stack_create(const char* service_name);
void builtin_service_stack_add(const char* service_name, variant_t* data);
void builtin_service_stack_add(const char* service_name, uint32_t value_key, variant_t* value);
service_stack_t* builtin_service_stack_get(const char* service_name);
void builtin_service_stack_clear(const char* service_name);
void builtin_service_stack_clear();

20 changes: 10 additions & 10 deletions cli_scene.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ bool cmd_scene_disable(vty_t* vty, variant_stack_t* params);
void show_scene_helper(scene_t* scene, void* arg);
void show_scene_action_helper(action_t* action, void* arg);
void show_scene_action_env_helper(env_t* env, void* arg);
void show_scene_action_token_helper(env_t* env, void* arg);
void show_scene_action_token_helper(method_stack_item_t* env, void* arg);

USING_LOGGER(Scene);

Expand Down Expand Up @@ -91,8 +91,8 @@ cli_command_t scene_action_command_list[] = {
};

cli_command_t scene_template_command_list[] = {
{"template-token WORD value LINE", cmd_add_action_token, "Add token value to command action"},
{"no template-token WORD", cmd_del_action_token, "Remove token value from command action"},
{"argument class WORD name WORD value LINE", cmd_add_action_token, "Add argument to command action"},
{"no argument class WORD name WORD", cmd_del_action_token, "Remove argument from command action"},
//{"end", cmd_exit_node, "Exit environment configuration"},
{NULL, NULL, NULL}
};
Expand Down Expand Up @@ -230,11 +230,11 @@ bool cmd_add_action_environment(vty_t* vty, variant_stack_t* params)
bool cmd_add_action_token(vty_t* vty, variant_stack_t* params)
{
char command[1024] = {0};
cli_assemble_line(params, 3, command);
cli_assemble_line(params, 6, command);
scene_t* scene = scene_manager_get_scene(scene_node->context);
action_t* action = scene_get_action(scene, scene_template_token_node->context);
scene_action_add_environment(action,
variant_get_string(stack_peek_at(params, 1)),
scene_action_add_method_stack_item(action, variant_get_string(stack_peek_at(params, 2)),
variant_get_string(stack_peek_at(params, 4)),
command);
}

Expand All @@ -243,7 +243,7 @@ bool cmd_del_action_token(vty_t* vty, variant_stack_t* params)
scene_t* scene = scene_manager_get_scene(scene_node->context);
action_t* action = scene_get_action(scene, scene_template_token_node->context);

scene_action_del_environment(action, variant_get_string(stack_peek_at(params, 2)));
scene_action_del_method_stack_item(action, variant_get_string(stack_peek_at(params, 3)), variant_get_string(stack_peek_at(params, 5)));
}

bool cmd_delete_scene_action_script(vty_t* vty, variant_stack_t* params)
Expand Down Expand Up @@ -409,7 +409,7 @@ void show_scene_action_helper(action_t* action, void* arg)
break;
case A_COMMAND:
vty_write(vty, " action command %s\n", action->path);
scene_action_for_each_environment(action, show_scene_action_token_helper, vty);
scene_action_for_each_method_stack_item(action, show_scene_action_token_helper, vty);
vty_write(vty, " !\n");
break;
case A_SCENE:
Expand All @@ -435,8 +435,8 @@ void show_scene_action_env_helper(env_t* env, void* arg)
vty_write(vty, " environment %s value %s\n", env->name, env->value);
}

void show_scene_action_token_helper(env_t* env, void* arg)
void show_scene_action_token_helper(method_stack_item_t* env, void* arg)
{
vty_t* vty = (vty_t*)arg;
vty_write(vty, " template-token %s value %s\n", env->name, env->value);
vty_write(vty, " argument class %s name %s value %s\n", env->stack_name, env->name, env->value);
}
2 changes: 1 addition & 1 deletion event_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void event_received(int sig);

//pthread_cond_t event_received_condition;
//pthread_mutex_t event_received_cond_mutex;
sem_t event_semaphore;
extern sem_t event_semaphore;

DECLARE_LOGGER(Event)

Expand Down
54 changes: 53 additions & 1 deletion libZaeUtil/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,38 @@
#include <stdlib.h>
#include <signal.h>
#include <pthread.h>
#include <semaphore.h>
#include "logger.h"

variant_stack_t* event_list;
variant_stack_t* registered_handlers;

pthread_mutex_t event_list_mutex;
sem_t event_semaphore;

void event_destroy(void* arg);

DECLARE_LOGGER(Event)

void* event_handle_event(void* arg);

void event_manager_init()
{
LOG_ADVANCED(Event, "Initializing event manager");
event_list = stack_create();
registered_handlers = stack_create();

// Init the semaphore
sem_init(&event_semaphore, 0, 0);

// Now create event thread
pthread_t event_thread;
pthread_create(&event_thread, NULL, event_handle_event, NULL);
pthread_detach(event_thread);

LOG_ADVANCED(Event, "Event manager initialized");
}

event_t* event_create(int source_id, variant_t* data)
{
event_t* new_event = (event_t*)malloc(sizeof(event_t));
Expand All @@ -28,7 +54,8 @@ void event_post(event_t* event)
pthread_mutex_lock(&event_list_mutex);
stack_push_back(event_list, variant_create_ptr(DT_PTR, event, NULL));
pthread_mutex_unlock(&event_list_mutex);
raise(SIGUSR1);
sem_post(&event_semaphore);
//raise(SIGUSR1);
}

event_t* event_receive()
Expand All @@ -40,3 +67,28 @@ event_t* event_receive()
free(event_variant);
return event;
}

void event_register_handler(void (*event_handler)(event_t*))
{
stack_push_back(registered_handlers, variant_create_ptr(DT_PTR, event_handler, NULL));
}

void* event_handle_event(void* arg)
{
while(true)
{
sem_wait(&event_semaphore);
LOG_DEBUG(Event, "Event received");
event_t* event = event_receive();
LOG_ADVANCED(Event, "Received event id %d", event->source_id);

stack_for_each(registered_handlers, handler_var)
{
void (*event_handler)(event_t*);

event_handler = (void(*)(event_t*))variant_get_ptr(handler_var);
event_handler(event);
}
event_delete(event);
}
}
2 changes: 2 additions & 0 deletions libZaeUtil/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ typedef struct service_event_data_t
char* data;
} service_event_data_t;

void event_manager_init();
void event_register_handler(void (*event_handler)(event_t*));
event_t* event_create(int source_id, variant_t* data);
void event_delete(event_t* event);
void event_post(event_t* event);
Expand Down
6 changes: 6 additions & 0 deletions libZaeUtil/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -419,3 +419,9 @@ variant_t* variant_hash_iterator_value(hash_iterator_t* it)
{
return it->hash_table->node_array[it->index]->data;
}

uint32_t variant_hash_iterator_key(hash_iterator_t* it)
{
return it->hash_table->node_array[it->index]->key;
}

2 changes: 2 additions & 0 deletions libZaeUtil/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,5 @@ hash_iterator_t* variant_hash_end(hash_table_t* hash_table);
bool variant_hash_iterator_is_end(hash_iterator_t* it);
hash_iterator_t* variant_hash_iterator_next(hash_iterator_t* it);
variant_t* variant_hash_iterator_value(hash_iterator_t* it);
uint32_t variant_hash_iterator_key(hash_iterator_t* it);

Loading

0 comments on commit 8ed09c2

Please sign in to comment.