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) {
38 list_delete_list(mesh->submeshes);
39 mesh->submeshes = NULL;
42 submesh_t *new_submesh(void) {
43 submesh_t *s = xzalloc(sizeof(*s));
51 void free_submesh(submesh_t *s) {
59 static submesh_t *submesh_new(meshlink_handle_t *mesh, const char *submesh) {
63 s->name = xstrdup(submesh);
65 submesh_add(mesh, (submesh_t *)s);
69 submesh_t *create_submesh(meshlink_handle_t *mesh, const char *submesh) {
72 if(0 == strcmp(submesh, CORE_MESH)) {
73 logger(NULL, MESHLINK_ERROR, "Cannot create submesh handle for core mesh!\n");
74 meshlink_errno = MESHLINK_EINVAL;
78 if(!check_id(submesh)) {
79 logger(NULL, MESHLINK_ERROR, "Invalid SubMesh Id!\n");
80 meshlink_errno = MESHLINK_EINVAL;
84 s = lookup_submesh(mesh, submesh);
87 logger(NULL, MESHLINK_ERROR, "SubMesh Already exists!\n");
88 meshlink_errno = MESHLINK_EEXIST;
92 s = submesh_new(mesh, submesh);
94 meshlink_errno = MESHLINK_OK;
98 submesh_t *lookup_or_create_submesh(meshlink_handle_t *mesh, const char *submesh) {
101 if(0 == strcmp(submesh, CORE_MESH)) {
102 logger(NULL, MESHLINK_ERROR, "Cannot create submesh handle for core mesh!\n");
103 meshlink_errno = MESHLINK_EINVAL;
107 if(!check_id(submesh)) {
108 logger(NULL, MESHLINK_ERROR, "Invalid SubMesh Id!\n");
109 meshlink_errno = MESHLINK_EINVAL;
113 s = lookup_submesh(mesh, submesh);
116 meshlink_errno = MESHLINK_OK;
120 s = submesh_new(mesh, submesh);
122 meshlink_errno = MESHLINK_OK;
126 void submesh_add(meshlink_handle_t *mesh, submesh_t *s) {
128 list_insert_tail(mesh->submeshes, (void *)s);
131 void submesh_del(meshlink_handle_t *mesh, submesh_t *s) {
132 list_delete(mesh->submeshes, (void *)s);
135 submesh_t *lookup_submesh(struct meshlink_handle *mesh, const char *submesh_name) {
136 submesh_t *submesh = NULL;
138 if(!mesh->submeshes) {
142 for list_each(submesh_t, s, mesh->submeshes) {
143 if(!strcmp(submesh_name, s->name)) {
152 bool submesh_allows_node(const submesh_t *submesh, const node_t *node) {
153 if(!node->submesh || !submesh || submesh == node->submesh) {