module.c 6.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/* Icecast
 *
 * This program is distributed under the GNU General Public License, version 2.
 * A copy of this license is included with this source.
 *
 * Copyright 2018,      Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

13
#include <stdlib.h>
14 15 16 17 18 19 20
#include <string.h>

#include "common/thread/thread.h"
#include "common/avl/avl.h"

#include "refobject.h"
#include "module.h"
21
#include "cfgfile.h" /* for XMLSTR() */
22 23 24 25 26 27

struct module_tag {
    refobject_base_t __base;
    mutex_t lock;
    const module_client_handler_t *client_handlers;
    size_t client_handlers_len;
28 29
    module_setup_handler_t freecb;
    void *userdata;
30 31
    char *management_link_url;
    char *management_link_title;
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
};


struct module_container_tag {
    refobject_base_t __base;
    mutex_t lock;
    avl_tree *module;
};

static int compare_refobject_t_name(void *arg, void *a, void *b)
{
    return strcmp(refobject_get_name(a), refobject_get_name(b));
}

static void __module_container_free(refobject_t self, void **userdata)
{
    module_container_t *cont = REFOBJECT_TO_TYPE(self, module_container_t *);
    thread_mutex_destroy(&(cont->lock));
    avl_tree_free(cont->module, (avl_free_key_fun_type)refobject_unref);
}

53 54 55 56
REFOBJECT_DEFINE_TYPE(module_container_t,
        REFOBJECT_DEFINE_TYPE_FREE(__module_container_free)
        );

57 58
module_container_t *    module_container_new(void)
{
59
    module_container_t *ret = refobject_new__new(module_container_t, NULL, NULL, NULL);
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

    if (!ret)
        return NULL;

    thread_mutex_create(&(ret->lock));

    ret->module = avl_tree_new(compare_refobject_t_name, NULL);

    return ret;
}

int                     module_container_add_module(module_container_t *self, module_t *module)
{
    if (!self)
        return -1;

76 77 78
    if (refobject_ref(module) != 0)
        return -1;

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    thread_mutex_lock(&(self->lock));
    avl_insert(self->module, module);
    thread_mutex_unlock(&(self->lock));

    return 0;
}

int                     module_container_delete_module(module_container_t *self, const char *name)
{
    module_t *module;

    if (!self || !name)
        return -1;

    module = module_container_get_module(self, name);
    if (!module)
        return -1;

    thread_mutex_lock(&(self->lock));
    avl_delete(self->module, module, (avl_free_key_fun_type)refobject_unref);
    thread_mutex_unlock(&(self->lock));

    refobject_unref(module);

    return 0;
}

module_t *              module_container_get_module(module_container_t *self, const char *name)
{
108
    refobject_base_t *search;
109 110 111 112 113
    module_t *ret;

    if (!self || !name)
        return NULL;

114
    search = refobject_new__new(refobject_base_t, NULL, name, NULL);
115

116
    thread_mutex_lock(&(self->lock));
117 118 119
    if (avl_get_by_key(self->module, REFOBJECT_TO_TYPE(search, void *), (void**)&ret) != 0) {
        ret = NULL;
    }
120
    thread_mutex_unlock(&(self->lock));
121 122

    refobject_unref(search);
123
    refobject_ref(ret);
124 125 126 127

    return ret;
}

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
xmlNodePtr                      module_container_get_modulelist_as_xml(module_container_t *self)
{
    xmlNodePtr root;
    avl_node *avlnode;

    if (!self)
        return NULL;

    root = xmlNewNode(NULL, XMLSTR("modules"));
    if (!root)
        return NULL;

    thread_mutex_lock(&(self->lock));
    avlnode = avl_get_first(self->module);
    while (avlnode) {
        module_t *module = avlnode->key;
        xmlNodePtr node = xmlNewChild(root, NULL, XMLSTR("module"), NULL);

        xmlSetProp(node, XMLSTR("name"), XMLSTR(refobject_get_name(module)));
147 148 149 150
        if (module->management_link_url)
            xmlSetProp(node, XMLSTR("management-url"), XMLSTR(module->management_link_url));
        if (module->management_link_title)
            xmlSetProp(node, XMLSTR("management-title"), XMLSTR(module->management_link_title));
151 152 153 154 155 156 157 158

        avlnode = avl_get_next(avlnode);
    }
    thread_mutex_unlock(&(self->lock));

    return root;
}

159 160 161
static void __module_free(refobject_t self, void **userdata)
{
    module_t *mod = REFOBJECT_TO_TYPE(self, module_t *);
162 163 164 165 166 167 168

    if (mod->freecb)
        mod->freecb(mod, &(mod->userdata));

    if (mod->userdata)
        free(mod->userdata);

169 170 171
    free(mod->management_link_url);
    free(mod->management_link_title);

172 173 174
    thread_mutex_destroy(&(mod->lock));
}

175 176 177 178
REFOBJECT_DEFINE_TYPE(module_t,
        REFOBJECT_DEFINE_TYPE_FREE(__module_free)
        );

179 180
module_t *              module_new(const char *name, module_setup_handler_t newcb, module_setup_handler_t freecb, void *userdata)
{
181
    module_t *ret = refobject_new__new(module_t, NULL, name, NULL);
182 183 184 185 186 187

    if (!ret)
        return NULL;

    thread_mutex_create(&(ret->lock));

188 189 190 191 192 193 194 195 196 197
    ret->userdata = userdata;
    ret->freecb = freecb;

    if (newcb) {
        if (newcb(ret, &(ret->userdata)) != 0) {
            refobject_unref(ret);
            return NULL;
        }
    }

198 199 200
    return ret;
}

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
int                             module_add_link(module_t *self, const char *type, const char *url, const char *title)
{
    char *n_url = NULL;
    char *n_title = NULL;

    if (!self || !type)
        return -1;

    if (strcmp(type, "management-url") != 0)
        return -1;

    if (url) {
        n_url = strdup(url);
        if (!n_url)
            return -1;
    }

    if (title) {
        n_title = strdup(title);
        if (!n_title) {
            free(n_url);
            return -1;
        }
    }

    thread_mutex_lock(&(self->lock));
    free(self->management_link_url);
    free(self->management_link_title);

    self->management_link_url = n_url;
    self->management_link_title = n_title;
    thread_mutex_unlock(&(self->lock));

    return 0;
}

237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
const module_client_handler_t * module_get_client_handler(module_t *self, const char *name)
{
    size_t i;

    if (!self || !name)
        return NULL;

    thread_mutex_lock(&(self->lock));
    for (i = 0; i < self->client_handlers_len; i++) {
        if (self->client_handlers[i].name && strcmp(self->client_handlers[i].name, name) == 0) {
            thread_mutex_unlock(&(self->lock));
            return &(self->client_handlers[i]);
        }
    }
    thread_mutex_unlock(&(self->lock));

    return NULL;
}

int                             module_add_client_handler(module_t *self, const module_client_handler_t *handlers, size_t len)
{
    if (!self)
        return -1;

    thread_mutex_lock(&(self->lock));
    self->client_handlers = handlers;
    self->client_handlers_len = len;
    thread_mutex_unlock(&(self->lock));

    return 0;
}