2 submesh.c -- submesh management
3 Copyright (C) 2019 Guus Sliepen <guus@meshlink.io>,
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "meshlink_internal.h"
28 #include "splay_tree.h"
33 void init_submeshes(meshlink_handle_t *mesh) {
34 mesh->submeshes = list_alloc((list_action_t)free_submesh);
37 void exit_submeshes(meshlink_handle_t *mesh) {
39 list_delete_list(mesh->submeshes);
42 mesh->submeshes = NULL;
45 submesh_t *new_submesh(void) {
46 submesh_t *s = xzalloc(sizeof(*s));
54 void free_submesh(submesh_t *s) {
62 static submesh_t *submesh_new(meshlink_handle_t *mesh, const char *submesh) {
66 s->name = xstrdup(submesh);
68 submesh_add(mesh, (submesh_t *)s);
72 submesh_t *create_submesh(meshlink_handle_t *mesh, const char *submesh) {
75 if(0 == strcmp(submesh, CORE_MESH)) {
76 logger(NULL, MESHLINK_ERROR, "Cannot create submesh handle for core mesh!\n");
77 meshlink_errno = MESHLINK_EINVAL;
81 if(!check_id(submesh)) {
82 logger(NULL, MESHLINK_ERROR, "Invalid SubMesh Id!\n");
83 meshlink_errno = MESHLINK_EINVAL;
87 s = lookup_submesh(mesh, submesh);
90 logger(NULL, MESHLINK_ERROR, "SubMesh Already exists!\n");
91 meshlink_errno = MESHLINK_EEXIST;
95 s = submesh_new(mesh, submesh);
97 meshlink_errno = MESHLINK_OK;
101 submesh_t *lookup_or_create_submesh(meshlink_handle_t *mesh, const char *submesh) {
104 if(0 == strcmp(submesh, CORE_MESH)) {
105 logger(NULL, MESHLINK_ERROR, "Cannot create submesh handle for core mesh!\n");
106 meshlink_errno = MESHLINK_EINVAL;
110 if(!check_id(submesh)) {
111 logger(NULL, MESHLINK_ERROR, "Invalid SubMesh Id!\n");
112 meshlink_errno = MESHLINK_EINVAL;
116 s = lookup_submesh(mesh, submesh);
119 meshlink_errno = MESHLINK_OK;
123 s = submesh_new(mesh, submesh);
125 meshlink_errno = MESHLINK_OK;
129 void submesh_add(meshlink_handle_t *mesh, submesh_t *s) {
131 list_insert_tail(mesh->submeshes, (void *)s);
134 void submesh_del(meshlink_handle_t *mesh, submesh_t *s) {
135 list_delete(mesh->submeshes, (void *)s);
138 submesh_t *lookup_submesh(struct meshlink_handle *mesh, const char *submesh_name) {
139 submesh_t *submesh = NULL;
141 if(!mesh->submeshes) {
145 for list_each(submesh_t, s, mesh->submeshes) {
146 if(!strcmp(submesh_name, s->name)) {
155 bool submesh_allows_node(const submesh_t *submesh, const node_t *node) {
156 if(!node->submesh || !submesh || submesh == node->submesh) {