diff --git a/include/villas/node.h b/include/villas/node.h index 5332fa938..70f91543c 100644 --- a/include/villas/node.h +++ b/include/villas/node.h @@ -51,13 +51,6 @@ struct node config_setting_t *cfg; /**< A pointer to the libconfig object which instantiated this node */ }; -/** Create a node by allocating dynamic memory. - * - * @see node_type::create - * @param vt A pointer to the node-type table of virtual functions. - */ -struct node * node_create(struct node_type *vt); - /** Destroy node by freeing dynamically allocated memory. * * @see node_type::destroy diff --git a/include/villas/path.h b/include/villas/path.h index 49d74d4ca..f2d197096 100644 --- a/include/villas/path.h +++ b/include/villas/path.h @@ -73,9 +73,6 @@ struct path config_setting_t *cfg; /**< A pointer to the libconfig object which instantiated this path */ }; -/** Allocate memory for a new path */ -struct path * path_create(); - /** Initialize internal data structures. */ int path_init(struct path *p, struct cfg *cfg); diff --git a/lib/cfg.c b/lib/cfg.c index 9c70f855e..2c43f45b2 100644 --- a/lib/cfg.c +++ b/lib/cfg.c @@ -229,13 +229,13 @@ int cfg_parse(struct cfg *cfg, const char *uri) if (!p) cerror(cfg_node, "Invalid node type: %s", type); - struct node *n = node_create(&p->node); + struct node n; - ret = node_parse(n, cfg_node); + ret = node_parse(&n, cfg_node); if (ret) cerror(cfg_node, "Failed to parse node"); - list_push(&cfg->nodes, n); + list_push(&cfg->nodes, memdup(&n, sizeof(n))); } } @@ -248,26 +248,111 @@ int cfg_parse(struct cfg *cfg, const char *uri) for (int i = 0; i < config_setting_length(cfg_paths); i++) { config_setting_t *cfg_path = config_setting_get_elem(cfg_paths, i); - struct path *p = path_create(); + struct path p; - ret = path_parse(p, cfg_path, &cfg->nodes); + ret = path_parse(&p, cfg_path, &cfg->nodes); if (ret) cerror(cfg_path, "Failed to parse path"); - list_push(&cfg->paths, p); + list_push(&cfg->paths, memdup(&p, sizeof(p))); - if (p->reverse) { - struct path *r = path_create(); + if (p.reverse) { + struct path r; - ret = path_reverse(p, r); + ret = path_reverse(&p, &r); if (ret) - cerror(cfg_path, "Failed to reverse path %s", path_name(p)); + cerror(cfg_path, "Failed to reverse path %s", path_name(&p)); - list_push(&cfg->paths, r); + list_push(&cfg->paths, memdup(&r, sizeof(p))); } } } } + + cfg->state = STATE_PARSED; + return 0; +} + +int cfg_start(struct cfg *cfg) +{ + memory_init(cfg->hugepages); + rt_init(cfg->priority, cfg->affinity); + + api_start(&cfg->api); + web_start(&cfg->web); + + info("Start node types"); + list_foreach(struct node *n, &cfg->nodes) { INDENT + config_setting_t *cfg_root = config_root_setting(&cfg->cfg); + + node_type_start(n->_vt, cfg->cli.argc, cfg->cli.argv, cfg_root); + } + + info("Starting nodes"); + list_foreach(struct node *n, &cfg->nodes) { INDENT + int refs = list_count(&cfg->paths, (cmp_cb_t) path_uses_node, n); + if (refs > 0) + node_start(n); + else + warn("No path is using the node %s. Skipping...", node_name(n)); + } + + info("Starting paths"); + list_foreach(struct path *p, &cfg->paths) { INDENT + if (p->enabled) { + path_init(p, cfg); + path_start(p); + } + else + warn("Path %s is disabled. Skipping...", path_name(p)); + } + + cfg->state = STATE_STARTED; + + return 0; +} + +int cfg_stop(struct cfg *cfg) +{ + info("Stopping paths"); + list_foreach(struct path *p, &cfg->paths) { INDENT + path_stop(p); + } + + info("Stopping nodes"); + list_foreach(struct node *n, &cfg->nodes) { INDENT + node_stop(n); + } + + info("De-initializing node types"); + list_foreach(struct plugin *p, &plugins) { INDENT + if (p->type == PLUGIN_TYPE_NODE) + node_type_stop(&p->node); + } + + web_stop(&cfg->web); + api_stop(&cfg->api); + log_stop(&cfg->log); + + cfg->state = STATE_STOPPED; + + return 0; +} + +int cfg_destroy(struct cfg *cfg) +{ + config_destroy(&cfg->cfg); + + web_destroy(&cfg->web); + log_destroy(&cfg->log); + api_destroy(&cfg->api); + + list_destroy(&cfg->plugins, (dtor_cb_t) plugin_destroy, false); + list_destroy(&cfg->paths, (dtor_cb_t) path_destroy, true); + list_destroy(&cfg->nodes, (dtor_cb_t) node_destroy, true); + + cfg->state = STATE_DESTROYED; + return 0; } \ No newline at end of file diff --git a/lib/node.c b/lib/node.c index de1adbe70..06c59c87f 100644 --- a/lib/node.c +++ b/lib/node.c @@ -131,23 +131,7 @@ int node_reverse(struct node *n) return n->_vt->reverse ? n->_vt->reverse(n) : -1; } -struct node * node_create(struct node_type *vt) { - struct node *n = alloc(sizeof(struct node)); - - list_push(&vt->instances, n); - - n->_vt = vt; - n->_vd = alloc(n->_vt->size); - - if (n->_vt->create) - n->_vt->create(n); - - n->state = NODE_CREATED; - - return n; -} - int node_destroy(struct node *n) { if (n->_vt->destroy) diff --git a/lib/path.c b/lib/path.c index 758961e93..80033359a 100644 --- a/lib/path.c +++ b/lib/path.c @@ -182,11 +182,6 @@ const char * path_name(struct path *p) return p->_name; } -struct path * path_create() -{ - return (struct path *) alloc(sizeof(struct path)); -} - static int path_source_destroy(struct path_source *ps) { pool_destroy(&ps->pool);