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 assert(!mesh->submeshes);
35 mesh->submeshes = list_alloc((list_action_t)free_submesh);
38 void exit_submeshes(meshlink_handle_t *mesh) {
40 list_delete_list(mesh->submeshes);
43 mesh->submeshes = NULL;
46 submesh_t *new_submesh(void) {
47 submesh_t *s = xzalloc(sizeof(*s));
55 void free_submesh(submesh_t *s) {
63 static submesh_t *submesh_new(meshlink_handle_t *mesh, const char *submesh) {
68 s->name = xstrdup(submesh);
70 submesh_add(mesh, (submesh_t *)s);
74 submesh_t *create_submesh(meshlink_handle_t *mesh, const char *submesh) {
78 if(0 == strcmp(submesh, CORE_MESH)) {
79 logger(NULL, MESHLINK_ERROR, "Cannot create submesh handle for core mesh!\n");
80 meshlink_errno = MESHLINK_EINVAL;
84 if(!check_id(submesh)) {
85 logger(NULL, MESHLINK_ERROR, "Invalid SubMesh Id!\n");
86 meshlink_errno = MESHLINK_EINVAL;
90 s = lookup_submesh(mesh, submesh);
93 logger(NULL, MESHLINK_ERROR, "SubMesh Already exists!\n");
94 meshlink_errno = MESHLINK_EEXIST;
98 s = submesh_new(mesh, submesh);
100 meshlink_errno = MESHLINK_OK;
104 submesh_t *lookup_or_create_submesh(meshlink_handle_t *mesh, const char *submesh) {
108 if(0 == strcmp(submesh, CORE_MESH)) {
109 logger(NULL, MESHLINK_ERROR, "Cannot create submesh handle for core mesh!\n");
110 meshlink_errno = MESHLINK_EINVAL;
114 if(!check_id(submesh)) {
115 logger(NULL, MESHLINK_ERROR, "Invalid SubMesh Id!\n");
116 meshlink_errno = MESHLINK_EINVAL;
120 s = lookup_submesh(mesh, submesh);
123 meshlink_errno = MESHLINK_OK;
127 s = submesh_new(mesh, submesh);
129 meshlink_errno = MESHLINK_OK;
133 void submesh_add(meshlink_handle_t *mesh, submesh_t *s) {
135 list_insert_tail(mesh->submeshes, (void *)s);
138 void submesh_del(meshlink_handle_t *mesh, submesh_t *s) {
139 list_delete(mesh->submeshes, (void *)s);
142 submesh_t *lookup_submesh(struct meshlink_handle *mesh, const char *submesh_name) {
143 assert(submesh_name);
145 submesh_t *submesh = NULL;
147 if(!mesh->submeshes) {
151 for list_each(submesh_t, s, mesh->submeshes) {
152 if(!strcmp(submesh_name, s->name)) {
161 bool submesh_allows_node(const submesh_t *submesh, const node_t *node) {
162 if(!node->submesh || !submesh || submesh == node->submesh) {