diff options
author | Lukasz Stanislawski <l.stanislaws@samsung.com> | 2018-07-25 15:28:02 +0200 |
---|---|---|
committer | Lukasz Stanislawski <l.stanislaws@samsung.com> | 2018-07-25 15:53:08 +0200 |
commit | 3eaef9c1845ecd7707b2d1e76a40a2c7b189f79d (patch) | |
tree | d603dde493fa69c21a5224ebebdd9cc65b084858 | |
parent | 276b5c4ce8738a8bbd1b96a146114acbec5f1d60 (diff) | |
download | ttsd-worker-task-3eaef9c1845ecd7707b2d1e76a40a2c7b189f79d.tar.gz ttsd-worker-task-3eaef9c1845ecd7707b2d1e76a40a2c7b189f79d.tar.bz2 ttsd-worker-task-3eaef9c1845ecd7707b2d1e76a40a2c7b189f79d.zip |
task-factory: refactor
Remove code duplication.
Change-Id: I60adf02a3134d402a3bf0c1bdd12e4eb725a0844
-rw-r--r-- | src/task-factory.c | 296 |
1 files changed, 81 insertions, 215 deletions
diff --git a/src/task-factory.c b/src/task-factory.c index 194e3a4..d03202a 100644 --- a/src/task-factory.c +++ b/src/task-factory.c @@ -28,65 +28,22 @@ #include "macros.h" #include "ipc.h" -static task_t *create_system_report_task(); -static task_t *create_load_avg_report_task(); -static task_t *create_process_report_task(const config_data_process_t *config); -static task_t *create_top_report_task(struct config_data_top options); - -static void execute_scan_system(task_t *task); -static void execute_scan_load_avg(task_t *task); -static void execute_scan_process(task_t *task); -static void execute_scan_top(task_t *task); - -static void release_system_task(task_t *task); -static void release_process_task(task_t *task); -static void release_top_task(task_t *task); -static void release_load_avg_task(task_t *task); - -/** - * @brief Process task structure. - */ -typedef struct process_task -{ - task_t task; - report_generator_process_t *report_generator; - JsonBuilder *builder; - JsonGenerator *generator; -} process_task_t; - -/** - * @brief System task structure. - */ -typedef struct system_task +struct base_task { task_t task; - report_generator_system_t *report_generator; + report_generator_t *report_gen; JsonBuilder *builder; JsonGenerator *generator; -} system_task_t; - -/** - * @brief Load avg task structure. - */ -typedef struct load_avg_task -{ - task_t task; - report_generator_load_avg_t *report_generator; - JsonBuilder *builder; - JsonGenerator *generator; -} load_avg_task_t; +}; +static task_t *create_system_report_task(); +static task_t *create_load_avg_report_task(); +static task_t *create_process_report_task(const config_data_process_t *config); +static task_t *create_top_report_task(struct config_data_top options); -/** - * @brief Top task structure. - */ -typedef struct top_task -{ - task_t task; - report_generator_top_t *report_generator; - JsonBuilder *builder; - JsonGenerator *generator; -} top_task_t; +static void base_task_init(struct base_task *base, report_generator_t *generator); +static void base_task_execute(task_t *task); +static void base_task_release(task_t *task); task_t *task_factory_create_task(const config_t *config) { @@ -105,170 +62,112 @@ task_t *task_factory_create_task(const config_t *config) } } -static task_t *create_system_report_task() +static void base_task_init(struct base_task *base, report_generator_t *generator) { - system_task_t *_system_task = (system_task_t *)g_malloc(sizeof(system_task_t)); - - _system_task->task.release = release_system_task; - _system_task->report_generator = report_generator_system_new_generator(); - _system_task->task.execute = execute_scan_system; - _system_task->builder = json_builder_new(); - _system_task->generator = json_generator_new(); - - return &_system_task->task; + base->task.execute = base_task_execute; + base->task.release = base_task_release; + base->builder = json_builder_new(); + base->generator = json_generator_new(); + base->report_gen = generator; } -static task_t *create_load_avg_report_task() +static void base_task_execute(task_t *task) { - load_avg_task_t *_load_avg_task = (load_avg_task_t*)g_malloc(sizeof(load_avg_task_t)); - - _load_avg_task->report_generator = report_generator_load_avg_new_generator(); - _load_avg_task->task.execute = execute_scan_load_avg; - _load_avg_task->task.release = release_load_avg_task; - _load_avg_task->builder = json_builder_new(); - _load_avg_task->generator = json_generator_new(); - - return &_load_avg_task->task; -} + struct base_task *_base_task = container_of(task, struct base_task, task); -static bool _report_generator_process_filter_cb(struct process *proc, void *user_data) -{ - config_data_process_t *data = user_data; + json_builder_reset(_base_task->builder); - for (int i = 0; i < data->n_matchers; ++i) { - struct matcher *match = &data->matchers[i]; - if (match->app_id && process_get_appid(proc)) { - if (strcmp(match->app_id, process_get_appid(proc)) == 0) - return true; - } - if (match->exe && process_get_exe(proc)) { - if (strcmp(match->exe, process_get_exe(proc)) == 0) - return true; - } + if (report_generator_refresh((report_generator_t*)_base_task->report_gen) != true) { + return; } - return false; -} -static task_t *create_process_report_task(const config_data_process_t *config) -{ - process_task_t *_process_task = (process_task_t *)g_malloc(sizeof(process_task_t)); - - _process_task->task.release = release_process_task; - _process_task->report_generator = report_generator_process_new_generator(); - _process_task->task.execute = execute_scan_process; - _process_task->builder = json_builder_new(); - _process_task->generator = json_generator_new(); + report_generator_generate((report_generator_t*)_base_task->report_gen, _base_task->builder); - report_generator_process_set_filter(_process_task->report_generator, - _report_generator_process_filter_cb, - config); + JsonNode *root = json_builder_get_root(_base_task->builder); + json_generator_set_root(_base_task->generator, root); + char *json_report = json_generator_to_data(_base_task->generator, NULL); - return &_process_task->task; + ipc_send_report(json_report); + g_free(json_report); } -static void execute_scan_system(task_t *task) +static void base_task_release(task_t *task) { - ON_NULL_RETURN(task); - - system_task_t *_system_task = container_of(task, system_task_t, task); - - json_builder_reset(_system_task->builder); - - if (report_generator_refresh((report_generator_t*)_system_task->report_generator) != true) { - return; - } - - report_generator_generate((report_generator_t*)_system_task->report_generator, _system_task->builder); - - JsonNode *root = json_builder_get_root(_system_task->builder); - json_generator_set_root(_system_task->generator, root); - char *json_report = json_generator_to_data(_system_task->generator, NULL); - - ipc_send_report(json_report); - g_free(json_report); + struct base_task *_base_task = container_of(task, struct base_task, task); + report_generator_release(_base_task->report_gen); + g_object_unref(_base_task->builder); + g_object_unref(_base_task->generator); } -static void execute_scan_load_avg(task_t *task) +static task_t *create_system_report_task() { - ON_NULL_RETURN(task); - - load_avg_task_t *_load_avg_task = container_of(task, load_avg_task_t, task); - - json_builder_reset(_load_avg_task->builder); + struct base_task *_task = (struct base_task*)g_malloc(sizeof(struct base_task)); - if (report_generator_refresh((report_generator_t*)_load_avg_task->report_generator) != true) { - return; + report_generator_system_t *generator = report_generator_system_new_generator(); + if (!generator) { + free(_task); + return NULL; } - report_generator_generate((report_generator_t*)_load_avg_task->report_generator, _load_avg_task->builder); - - JsonNode *root = json_builder_get_root(_load_avg_task->builder); - json_generator_set_root(_load_avg_task->generator, root); - char *json_report = json_generator_to_data(_load_avg_task->generator, NULL); + base_task_init(_task, (report_generator_t*)generator); - ipc_send_report(json_report); - g_free(json_report); + return &_task->task; } -static void execute_scan_process(task_t *task) +static task_t *create_load_avg_report_task() { - ON_NULL_RETURN(task); - - process_task_t *_process_task = container_of(task, process_task_t, task); + struct base_task *_task = (struct base_task*)g_malloc(sizeof(struct base_task)); - json_builder_reset(_process_task->builder); - - if (report_generator_refresh((report_generator_t*)_process_task->report_generator) != true) { - return; + report_generator_load_avg_t *generator = report_generator_load_avg_new_generator(); + if (!generator) { + free(_task); + return NULL; } - report_generator_generate((report_generator_t*)_process_task->report_generator, _process_task->builder); - - JsonNode *root = json_builder_get_root(_process_task->builder); - json_generator_set_root(_process_task->generator, root); - char *json_report = json_generator_to_data(_process_task->generator, NULL); + base_task_init(_task, (report_generator_t*)generator); - ipc_send_report(json_report); - g_free(json_report); + return &_task->task; } -static void release_system_task(task_t *task) +static bool _report_generator_process_filter_cb(struct process *proc, void *user_data) { - ON_NULL_RETURN(task); - - system_task_t *_system_task = container_of(task, system_task_t, task); + config_data_process_t *data = user_data; - report_generator_release((report_generator_t*)_system_task->report_generator); - g_object_unref(_system_task->builder); - g_object_unref(_system_task->generator); - g_free(_system_task); + for (int i = 0; i < data->n_matchers; ++i) { + struct matcher *match = &data->matchers[i]; + if (match->app_id && process_get_appid(proc)) { + if (strcmp(match->app_id, process_get_appid(proc)) == 0) + return true; + } + if (match->exe && process_get_exe(proc)) { + if (strcmp(match->exe, process_get_exe(proc)) == 0) + return true; + } + } + return false; } -static void release_load_avg_task(task_t *task) +static task_t *create_process_report_task(const config_data_process_t *config) { - load_avg_task_t *_load_avg_task = container_of(task, load_avg_task_t, task); + struct base_task *_task = (struct base_task*)g_malloc(sizeof(struct base_task)); - report_generator_release((report_generator_t*)_load_avg_task->report_generator); - g_object_unref(_load_avg_task->builder); - g_object_unref(_load_avg_task->generator); - g_free(_load_avg_task); -} + report_generator_process_t *generator = report_generator_process_new_generator(); + if (!generator) { + free(_task); + return NULL; + } -static void release_process_task(task_t *task) -{ - ON_NULL_RETURN(task); + report_generator_process_set_filter(generator, + _report_generator_process_filter_cb, + config); - process_task_t *_process_task = container_of(task, process_task_t, task); + base_task_init(_task, (report_generator_t*)generator); - report_generator_release((report_generator_t*)_process_task->report_generator); - g_object_unref(_process_task->builder); - g_object_unref(_process_task->generator); - g_free(_process_task); + return &_task->task; } static task_t *create_top_report_task(struct config_data_top options) { - top_task_t *_top_task; report_generator_top_type_e type; switch (options.subject) { @@ -282,48 +181,15 @@ static task_t *create_top_report_task(struct config_data_top options) return NULL; } - _top_task = (top_task_t *)g_malloc(sizeof(top_task_t)); - _top_task->task.execute = execute_scan_top; - _top_task->task.release = release_top_task; - _top_task->report_generator = report_generator_top_new_generator(type, options.top); - if (!_top_task->report_generator) { - g_free(_top_task); - return NULL; - } - _top_task->builder = json_builder_new(); - _top_task->generator = json_generator_new(); - - return &_top_task->task; -} - -static void release_top_task(task_t *task) -{ - ON_NULL_RETURN(task); - - top_task_t *_top_task = container_of(task, top_task_t, task); - g_object_unref(_top_task->builder); - g_object_unref(_top_task->generator); - report_generator_release((report_generator_t*)_top_task->report_generator); - g_free(_top_task); -} + struct base_task *_task = (struct base_task*)g_malloc(sizeof(struct base_task)); -static void execute_scan_top(task_t *task) -{ - ON_NULL_RETURN(task); - top_task_t *_top_task = container_of(task, top_task_t, task); - - json_builder_reset(_top_task->builder); - - if (report_generator_refresh((report_generator_t*)_top_task->report_generator) != true) { - return; + report_generator_top_t *generator = report_generator_top_new_generator(type, options.top); + if (!generator) { + free(_task); + return NULL; } - report_generator_generate((report_generator_t*)_top_task->report_generator, _top_task->builder); + base_task_init(_task, (report_generator_t*)generator); - JsonNode *root = json_builder_get_root(_top_task->builder); - json_generator_set_root(_top_task->generator, root); - char *json_report = json_generator_to_data(_top_task->generator, NULL); - - ipc_send_report(json_report); - g_free(json_report); + return &_task->task; } |