gnunet_chat_group.c (2651B)
1 /* 2 This file is part of GNUnet. 3 Copyright (C) 2021--2024 GNUnet e.V. 4 5 GNUnet is free software: you can redistribute it and/or modify it 6 under the terms of the GNU Affero General Public License as published 7 by the Free Software Foundation, either version 3 of the License, 8 or (at your option) any later version. 9 10 GNUnet is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Affero General Public License for more details. 14 15 You should have received a copy of the GNU Affero General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 SPDX-License-Identifier: AGPL3.0-or-later 19 */ 20 /* 21 * @author Tobias Frisch 22 * @file gnunet_chat_group.c 23 */ 24 25 #include "gnunet_chat_group.h" 26 #include "gnunet_chat_util.h" 27 28 #include "gnunet_chat_group_intern.c" 29 #include <gnunet/gnunet_scheduler_lib.h> 30 31 static const unsigned int initial_map_size_of_context = 8; 32 static const uint16_t group_regex_compression = 6; 33 34 struct GNUNET_CHAT_Group* 35 group_create_from_context (struct GNUNET_CHAT_Handle *handle, 36 struct GNUNET_CHAT_Context *context) 37 { 38 GNUNET_assert((handle) && (context)); 39 40 struct GNUNET_CHAT_Group* group = GNUNET_new(struct GNUNET_CHAT_Group); 41 42 group->handle = handle; 43 group->context = context; 44 45 group->destruction = NULL; 46 47 group->announcement = NULL; 48 group->search = NULL; 49 50 group->registry = GNUNET_CONTAINER_multipeermap_create( 51 initial_map_size_of_context, GNUNET_NO); 52 53 group->user_pointer = NULL; 54 55 return group; 56 } 57 58 void 59 group_destroy (struct GNUNET_CHAT_Group* group) 60 { 61 GNUNET_assert(group); 62 63 if (group->destruction) 64 GNUNET_SCHEDULER_cancel(group->destruction); 65 66 if (group->registry) 67 GNUNET_CONTAINER_multipeermap_destroy(group->registry); 68 69 if (group->search) 70 GNUNET_REGEX_search_cancel(group->search); 71 72 if (group->announcement) 73 GNUNET_REGEX_announce_cancel(group->announcement); 74 75 GNUNET_free(group); 76 } 77 78 void 79 group_publish (struct GNUNET_CHAT_Group* group) 80 { 81 GNUNET_assert( 82 (group) && 83 (group->context) && 84 (group->context->topic) && 85 (group->handle) && 86 (group->handle->cfg) 87 ); 88 89 char* topic = NULL; 90 GNUNET_asprintf ( 91 &topic, 92 "GNUNET_CHAT_%s", 93 group->context->topic 94 ); 95 96 group->announcement = GNUNET_REGEX_announce( 97 group->handle->cfg, topic, 98 GNUNET_TIME_relative_get_minute_(), 99 group_regex_compression 100 ); 101 102 group->search = GNUNET_REGEX_search( 103 group->handle->cfg, topic, 104 search_group_by_topic, group 105 ); 106 107 GNUNET_free(topic); 108 }