Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
idnode, imagecache: introduce simple node system
  • Loading branch information
perexg committed Sep 10, 2015
1 parent 3b46b66 commit fc7b753
Show file tree
Hide file tree
Showing 9 changed files with 367 additions and 190 deletions.
8 changes: 7 additions & 1 deletion src/api.h
Expand Up @@ -25,7 +25,7 @@
#include "redblack.h"
#include "access.h"

#define TVH_API_VERSION 15
#define TVH_API_VERSION 16

/*
* Command hook
Expand Down Expand Up @@ -111,6 +111,12 @@ int api_idnode_handler
( access_t *perm, htsmsg_t *args, htsmsg_t **resp,
void (*handler)(access_t *perm, idnode_t *in), const char *op );

int api_idnode_load_simple
( access_t *perm, void *opaque, const char *op, htsmsg_t *args, htsmsg_t **resp );

int api_idnode_save_simple
( access_t *perm, void *opaque, const char *op, htsmsg_t *args, htsmsg_t **resp );

/*
* Service mapper
*/
Expand Down
78 changes: 76 additions & 2 deletions src/api/api_idnode.c
Expand Up @@ -237,8 +237,7 @@ api_idnode_load
{
int err = 0, meta, count = 0;
idnode_t *in;
htsmsg_t *uuids, *l = NULL, *m;
htsmsg_t *flist;
htsmsg_t *uuids, *l = NULL, *m, *flist;
htsmsg_field_t *f;
const char *uuid = NULL, *class;

Expand Down Expand Up @@ -320,6 +319,52 @@ api_idnode_load
return err;
}

int
api_idnode_load_simple
( access_t *perm, void *opaque, const char *op, htsmsg_t *args, htsmsg_t **resp )
{
int err = 0, meta;
htsmsg_t *l = NULL, *m, *flist;
const char *class;
idnode_t *in = (idnode_t *)opaque;

/* Class based */
if ((class = htsmsg_get_str(args, "class"))) {
pthread_mutex_lock(&global_lock);
err = api_idnode_load_by_class0(perm, (void*)in->in_class, NULL, args, resp);
pthread_mutex_unlock(&global_lock);
return err;
}

/* UUIDs */
meta = htsmsg_get_s32_or_default(args, "meta", 0);

flist = api_idnode_flist_conf(args, "list");

pthread_mutex_lock(&global_lock);

if (!idnode_perm(in, perm, NULL)) {
l = htsmsg_create_list();
m = idnode_serialize0(in, flist, 0, perm->aa_lang);
if (meta > 0)
htsmsg_add_msg(m, "meta", idclass_serialize0(in->in_class, flist, 0, perm->aa_lang));
htsmsg_add_msg(l, NULL, m);
} else {
err = EPERM;
}

pthread_mutex_unlock(&global_lock);

if (l) {
*resp = htsmsg_create_map();
htsmsg_add_msg(*resp, "entries", l);
}

htsmsg_destroy(flist);

return err;
}

static int
api_idnode_save
( access_t *perm, void *opaque, const char *op, htsmsg_t *args, htsmsg_t **resp )
Expand Down Expand Up @@ -384,6 +429,35 @@ api_idnode_save
return err;
}

int
api_idnode_save_simple
( access_t *perm, void *opaque, const char *op, htsmsg_t *args, htsmsg_t **resp )
{
int err = 0;
htsmsg_t *msg;
htsmsg_field_t *f;
idnode_t *in = (idnode_t *)opaque;

if (!(f = htsmsg_field_find(args, "node")))
return EINVAL;
if (!(msg = htsmsg_field_get_map(f)))
return EINVAL;

pthread_mutex_lock(&global_lock);

/* Single */
if (!idnode_perm(in, perm, msg)) {
idnode_update(in, msg);
idnode_perm_unset(in);
} else {
err = EPERM;
}

pthread_mutex_unlock(&global_lock);

return err;
}

int
api_idnode_tree
( access_t *perm, void *opaque, const char *op, htsmsg_t *args, htsmsg_t **resp )
Expand Down
35 changes: 10 additions & 25 deletions src/api/api_imagecache.c
Expand Up @@ -26,42 +26,27 @@
#if ENABLE_IMAGECACHE

static int
api_imagecache_load
( access_t *perm, void *opaque, const char *op, htsmsg_t *args, htsmsg_t **resp )
{
htsmsg_t *l;
pthread_mutex_lock(&global_lock);
*resp = htsmsg_create_map();
l = htsmsg_create_list();
htsmsg_add_msg(l, NULL, imagecache_get_config());
htsmsg_add_msg(*resp, "entries", l);
pthread_mutex_unlock(&global_lock);
return 0;
}

static int
api_imagecache_save
api_imagecache_clean
( access_t *perm, void *opaque, const char *op, htsmsg_t *args, htsmsg_t **resp )
{
int b;

pthread_mutex_lock(&global_lock);
if (imagecache_set_config(args))
imagecache_save();
if (!htsmsg_get_bool(args, "clean", &b) && b)
if (htsmsg_get_bool(args, "clean", &b))
return EINVAL;
if (b) {
pthread_mutex_lock(&global_lock);
imagecache_clean();
pthread_mutex_unlock(&global_lock);
*resp = htsmsg_create_map();
htsmsg_add_u32(*resp, "success", 1);
pthread_mutex_unlock(&global_lock);
}
return 0;
}

void
api_imagecache_init ( void )
{
static api_hook_t ah[] = {
{ "imagecache/config/load", ACCESS_ADMIN, api_imagecache_load, NULL },
{ "imagecache/config/save", ACCESS_ADMIN, api_imagecache_save, NULL },
{ "imagecache/config/load", ACCESS_ADMIN, api_idnode_load_simple, &imagecache_conf },
{ "imagecache/config/save", ACCESS_ADMIN, api_idnode_save_simple, &imagecache_conf },
{ "imagecache/config/clean", ACCESS_ADMIN, api_imagecache_clean, NULL },
{ NULL },
};

Expand Down
16 changes: 11 additions & 5 deletions src/idnode.c
Expand Up @@ -1314,9 +1314,11 @@ idnode_serialize0(idnode_t *self, htsmsg_t *list, int optmask, const char *lang)
const char *uuid, *s;

htsmsg_t *m = htsmsg_create_map();
uuid = idnode_uuid_as_sstr(self);
htsmsg_add_str(m, "uuid", uuid);
htsmsg_add_str(m, "id", uuid);
if (!idc->ic_snode) {
uuid = idnode_uuid_as_sstr(self);
htsmsg_add_str(m, "uuid", uuid);
htsmsg_add_str(m, "id", uuid);
}
htsmsg_add_str(m, "text", idnode_get_title(self, lang) ?: "");
if ((s = idclass_get_caption(idc, lang)))
htsmsg_add_str(m, "caption", s);
Expand Down Expand Up @@ -1587,8 +1589,12 @@ idnode_notify ( idnode_t *in, const char *action )
return;

while (ic) {
if (ic->ic_event)
notify_delayed(uuid, ic->ic_event, action);
if (ic->ic_event) {
if (!ic->ic_snode)
notify_delayed(uuid, ic->ic_event, action);
else
notify_reload(ic->ic_event);
}
ic = ic->ic_super;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/idnode.h
Expand Up @@ -64,6 +64,7 @@ struct idclass {
const property_t *ic_properties; ///< Property list
const char *ic_event; ///< Events to fire on add/delete/title
uint32_t ic_perm_def; ///< Default permissions
idnode_t *ic_snode; ///< Simple node

/* Callbacks */
idnode_set_t *(*ic_get_childs) (idnode_t *self);
Expand Down
104 changes: 48 additions & 56 deletions src/imagecache.c
Expand Up @@ -63,33 +63,46 @@ static RB_HEAD(,imagecache_image) imagecache_by_url;
SKEL_DECLARE(imagecache_skel, imagecache_image_t);

#if ENABLE_IMAGECACHE
struct imagecache_config imagecache_conf;
static const property_t imagecache_props[] = {
{
.type = PT_BOOL,
.id = "enabled",
.name = N_("Enabled"),
.off = offsetof(struct imagecache_config, enabled),
},
{
.type = PT_BOOL,
.id = "ignore_sslcert",
.name = N_("Ignore invalid SSL certificate"),
.off = offsetof(struct imagecache_config, ignore_sslcert),
},
{
.type = PT_U32,
.id = "ok_period",
.name = N_("Re-try period"),
.off = offsetof(struct imagecache_config, ok_period),
},
{
.type = PT_U32,
.id = "fail_period",
.name = N_("Re-try period of failed images"),
.off = offsetof(struct imagecache_config, fail_period),
},
{}
struct imagecache_config imagecache_conf = {
.idnode.in_class = &imagecache_class,
};

static void imagecache_save(idnode_t *self);

const idclass_t imagecache_class = {
.ic_snode = (idnode_t *)&imagecache_conf,
.ic_class = "imagecache",
.ic_caption = N_("Image Cache"),
.ic_event = "imagecache",
.ic_perm_def = ACCESS_ADMIN,
.ic_save = imagecache_save,
.ic_properties = (const property_t[]){
{
.type = PT_BOOL,
.id = "enabled",
.name = N_("Enabled"),
.off = offsetof(struct imagecache_config, enabled),
},
{
.type = PT_BOOL,
.id = "ignore_sslcert",
.name = N_("Ignore invalid SSL certificate"),
.off = offsetof(struct imagecache_config, ignore_sslcert),
},
{
.type = PT_U32,
.id = "ok_period",
.name = N_("Re-fetch period"),
.off = offsetof(struct imagecache_config, ok_period),
},
{
.type = PT_U32,
.id = "fail_period",
.name = N_("Re-try period (hours)"),
.off = offsetof(struct imagecache_config, fail_period),
},
{}
}
};

static pthread_cond_t imagecache_cond;
Expand Down Expand Up @@ -319,7 +332,7 @@ imagecache_init ( void )
/* Load settings */
#if ENABLE_IMAGECACHE
if ((m = hts_settings_load("imagecache/config"))) {
imagecache_set_config(m);
idnode_load(&imagecache_conf.idnode, m);
htsmsg_destroy(m);
}
#endif
Expand Down Expand Up @@ -401,38 +414,17 @@ imagecache_done ( void )

#if ENABLE_IMAGECACHE

/*
* Get config
*/
htsmsg_t *
imagecache_get_config ( void )
{
htsmsg_t *m = htsmsg_create_map();
prop_read_values(&imagecache_conf, imagecache_props, m, NULL, 0, NULL);
return m;
}

/*
* Set config
*/
int
imagecache_set_config ( htsmsg_t *m )
{
int save = prop_write_values(&imagecache_conf, imagecache_props, m, 0, NULL);
if (save)
pthread_cond_broadcast(&imagecache_cond);
return save;
}

/*
* Save
*/
void
imagecache_save ( void )
static void
imagecache_save ( idnode_t *self )
{
htsmsg_t *m = imagecache_get_config();
hts_settings_save(m, "imagecache/config");
notify_reload("imagecache");
htsmsg_t *c = htsmsg_create_map();
idnode_save(&imagecache_conf.idnode, c);
hts_settings_save(c, "imagecache/config");
htsmsg_destroy(c);
pthread_cond_broadcast(&imagecache_cond);
}

/*
Expand Down
9 changes: 4 additions & 5 deletions src/imagecache.h
Expand Up @@ -20,26 +20,25 @@
#define __IMAGE_CACHE_H__

#include <pthread.h>
#include "idnode.h"

struct imagecache_config {
int __unused__; // to avoid assert in prop.c (first member should be idnode_t)
idnode_t idnode;
int enabled;
int ignore_sslcert;
uint32_t ok_period;
uint32_t fail_period;
};

extern struct imagecache_config imagecache_conf;
extern const idclass_t imagecache_class;

extern pthread_mutex_t imagecache_mutex;

void imagecache_init ( void );
void imagecache_done ( void );

htsmsg_t *imagecache_get_config ( void );
int imagecache_set_config ( htsmsg_t *c );
void imagecache_save ( void );
void imagecache_clean ( void );
void imagecache_clean ( void );

// Note: will return 0 if invalid (must serve original URL)
uint32_t imagecache_get_id ( const char *url );
Expand Down

0 comments on commit fc7b753

Please sign in to comment.