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 static submesh_t *new_submesh(void) {
34 return xzalloc(sizeof(submesh_t));
37 static void free_submesh(submesh_t *s) {
42 void init_submeshes(meshlink_handle_t *mesh) {
43 assert(!mesh->submeshes);
44 mesh->submeshes = list_alloc((list_action_t)free_submesh);
47 void exit_submeshes(meshlink_handle_t *mesh) {
49 list_delete_list(mesh->submeshes);
52 mesh->submeshes = NULL;
55 static submesh_t *submesh_add(meshlink_handle_t *mesh, const char *submesh) {
58 submesh_t *s = new_submesh();
59 s->name = xstrdup(submesh);
60 list_insert_tail(mesh->submeshes, (void *)s);
64 submesh_t *create_submesh(meshlink_handle_t *mesh, const char *submesh) {
67 if(0 == strcmp(submesh, CORE_MESH)) {
68 logger(NULL, MESHLINK_ERROR, "Cannot create submesh handle for core mesh!\n");
69 meshlink_errno = MESHLINK_EINVAL;
73 if(!check_id(submesh)) {
74 logger(NULL, MESHLINK_ERROR, "Invalid SubMesh Id!\n");
75 meshlink_errno = MESHLINK_EINVAL;
79 if(lookup_submesh(mesh, submesh)) {
80 logger(NULL, MESHLINK_ERROR, "SubMesh Already exists!\n");
81 meshlink_errno = MESHLINK_EEXIST;
85 return submesh_add(mesh, submesh);
88 submesh_t *lookup_or_create_submesh(meshlink_handle_t *mesh, const char *submesh) {
91 if(0 == strcmp(submesh, CORE_MESH)) {
92 logger(NULL, MESHLINK_ERROR, "Cannot create submesh handle for core mesh!\n");
93 meshlink_errno = MESHLINK_EINVAL;
97 if(!check_id(submesh)) {
98 logger(NULL, MESHLINK_ERROR, "Invalid SubMesh Id!\n");
99 meshlink_errno = MESHLINK_EINVAL;
103 submesh_t *s = lookup_submesh(mesh, submesh);
106 meshlink_errno = MESHLINK_OK;
110 return submesh_add(mesh, submesh);
113 submesh_t *lookup_submesh(struct meshlink_handle *mesh, const char *submesh_name) {
114 assert(submesh_name);
116 submesh_t *submesh = NULL;
118 if(!mesh->submeshes) {
122 for list_each(submesh_t, s, mesh->submeshes) {
123 if(!strcmp(submesh_name, s->name)) {
132 bool submesh_allows_node(const submesh_t *submesh, const node_t *node) {
133 if(!node->submesh || !submesh || submesh == node->submesh) {