summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: a08b303)
raw | patch | inline | side by side (parent: a08b303)
author | Sebastian Harl <sh@tokkee.org> | |
Fri, 27 Jun 2014 16:46:57 +0000 (18:46 +0200) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Fri, 27 Jun 2014 16:46:57 +0000 (18:46 +0200) |
src/core/store-private.h | patch | blob | history | |
src/core/store.c | patch | blob | history | |
src/core/store_lookup.c | patch | blob | history |
index e501b2cd830169b2b7de01660207c976ca66f089..603648ca057b51ced94b5a7ecd05bdadd615acbc 100644 (file)
--- a/src/core/store-private.h
+++ b/src/core/store-private.h
sdb_data_t value;
} sdb_attribute_t;
-#define SDB_ATTR(obj) ((sdb_attribute_t *)(obj))
-#define SDB_CONST_ATTR(obj) ((const sdb_attribute_t *)(obj))
+#define ATTR(obj) ((sdb_attribute_t *)(obj))
+#define CONST_ATTR(obj) ((const sdb_attribute_t *)(obj))
+
+typedef struct {
+ sdb_store_base_t super;
+
+ sdb_llist_t *attributes;
+} sdb_service_t;
+#define SVC(obj) ((sdb_service_t *)(obj))
+#define CONST_SVC(obj) ((const sdb_service_t *)(obj))
typedef struct {
sdb_store_base_t super;
sdb_llist_t *services;
sdb_llist_t *attributes;
-} sdb_store_obj_t;
-#define SDB_STORE_OBJ(obj) ((sdb_store_obj_t *)(obj))
-#define SDB_CONST_STORE_OBJ(obj) ((const sdb_store_obj_t *)(obj))
+} sdb_host_t;
+#define HOST(obj) ((sdb_host_t *)(obj))
+#define CONST_HOST(obj) ((const sdb_host_t *)(obj))
-/* shortcuts for accessing the sdb_store_obj_t attributes
- * of inheriting objects */
+/* shortcuts for accessing service/host attributes */
#define _last_update super.last_update
#define _interval super.interval
diff --git a/src/core/store.c b/src/core/store.c
index bb37e26a7d9e074466f70d724a8943acabd03747..ccb5f0a954d97e0f867bc2bb2067573245e30133 100644 (file)
--- a/src/core/store.c
+++ b/src/core/store.c
* private types
*/
-static sdb_type_t sdb_store_obj_type;
+static sdb_type_t sdb_host_type;
+static sdb_type_t sdb_service_type;
static sdb_type_t sdb_attribute_type;
static int
} /* store_base_destroy */
static int
-sdb_store_obj_init(sdb_object_t *obj, va_list ap)
+sdb_host_init(sdb_object_t *obj, va_list ap)
{
- sdb_store_obj_t *sobj = SDB_STORE_OBJ(obj);
+ sdb_host_t *sobj = HOST(obj);
int ret;
/* this will consume the first argument (type) of ap */
if (! sobj->attributes)
return -1;
return 0;
-} /* sdb_store_obj_init */
+} /* sdb_host_init */
static void
-sdb_store_obj_destroy(sdb_object_t *obj)
+sdb_host_destroy(sdb_object_t *obj)
{
- sdb_store_obj_t *sobj = SDB_STORE_OBJ(obj);
-
+ sdb_host_t *sobj = HOST(obj);
assert(obj);
store_base_destroy(obj);
sdb_llist_destroy(sobj->services);
if (sobj->attributes)
sdb_llist_destroy(sobj->attributes);
-} /* sdb_store_obj_destroy */
+} /* sdb_host_destroy */
+
+static int
+sdb_service_init(sdb_object_t *obj, va_list ap)
+{
+ sdb_service_t *sobj = SVC(obj);
+ int ret;
+
+ /* this will consume the first argument (type) of ap */
+ ret = store_base_init(obj, ap);
+ if (ret)
+ return ret;
+
+ sobj->attributes = sdb_llist_create();
+ if (! sobj->attributes)
+ return -1;
+ return 0;
+} /* sdb_service_init */
+
+static void
+sdb_service_destroy(sdb_object_t *obj)
+{
+ sdb_service_t *sobj = SVC(obj);
+ assert(obj);
+
+ store_base_destroy(obj);
+
+ if (sobj->attributes)
+ sdb_llist_destroy(sobj->attributes);
+} /* sdb_service_destroy */
static int
sdb_attr_init(sdb_object_t *obj, va_list ap)
value = va_arg(ap, const sdb_data_t *);
if (value)
- if (sdb_data_copy(&SDB_ATTR(obj)->value, value))
+ if (sdb_data_copy(&ATTR(obj)->value, value))
return -1;
return 0;
} /* sdb_attr_init */
assert(obj);
store_base_destroy(obj);
- sdb_data_free_datum(&SDB_ATTR(obj)->value);
+ sdb_data_free_datum(&ATTR(obj)->value);
} /* sdb_attr_destroy */
-static sdb_type_t sdb_store_obj_type = {
- sizeof(sdb_store_obj_t),
+static sdb_type_t sdb_host_type = {
+ sizeof(sdb_host_t),
+ sdb_host_init,
+ sdb_host_destroy
+};
- sdb_store_obj_init,
- sdb_store_obj_destroy
+static sdb_type_t sdb_service_type = {
+ sizeof(sdb_service_t),
+ sdb_service_init,
+ sdb_service_destroy
};
static sdb_type_t sdb_attribute_type = {
sizeof(sdb_attribute_t),
-
sdb_attr_init,
sdb_attr_destroy
};
* private helper functions
*/
-static sdb_store_obj_t *
+static sdb_host_t *
lookup_host(const char *name)
{
- return SDB_STORE_OBJ(sdb_llist_search_by_name(host_list, name));
+ return HOST(sdb_llist_search_by_name(host_list, name));
} /* lookup_host */
/* The obj_lock has to be acquired before calling this function. */
}
if (hostname) {
- sdb_store_obj_t *host;
+ sdb_host_t *host;
host_cname = sdb_plugin_cname(strdup(hostname));
if (! host_cname) {
else {
sdb_store_base_t *new;
- if (type == SDB_ATTRIBUTE)
+ if (type == SDB_ATTRIBUTE) {
/* the value will be updated by the caller */
new = STORE_BASE(sdb_object_create(name, sdb_attribute_type,
type, last_update, NULL));
- else
- new = STORE_BASE(sdb_object_create(name, sdb_store_obj_type,
- type, last_update));
+ }
+ else {
+ sdb_type_t t;
+ t = type == SDB_HOST ? sdb_host_type : sdb_service_type;
+ new = STORE_BASE(sdb_object_create(name, t, type, last_update));
+ }
if (! new) {
char errbuf[1024];
sdb_strbuf_append(buf, "{\"name\": \"%s\", ", SDB_OBJ(sobj)->name);
if (type == SDB_ATTRIBUTE) {
- char tmp[sdb_data_strlen(&SDB_ATTR(sobj)->value) + 1];
- sdb_data_format(&SDB_ATTR(sobj)->value, tmp, sizeof(tmp),
+ char tmp[sdb_data_strlen(&ATTR(sobj)->value) + 1];
+ sdb_data_format(&ATTR(sobj)->value, tmp, sizeof(tmp),
SDB_DOUBLE_QUOTED);
sdb_strbuf_append(buf, "\"value\": %s, \"last_update\": \"%s\", "
"\"update_interval\": \"%s\"}", tmp, time_str,
_Bool
sdb_store_has_host(const char *name)
{
- sdb_store_obj_t *host;
+ sdb_host_t *host;
if (! name)
return NULL;
sdb_store_base_t *
sdb_store_get_host(const char *name)
{
- sdb_store_obj_t *host;
+ sdb_host_t *host;
if (! name)
return NULL;
if (status >= 0) {
assert(updated_attr);
- sdb_data_free_datum(&SDB_ATTR(updated_attr)->value);
- if (sdb_data_copy(&SDB_ATTR(updated_attr)->value, value)) {
+ sdb_data_free_datum(&ATTR(updated_attr)->value);
+ if (sdb_data_copy(&ATTR(updated_attr)->value, value)) {
sdb_object_deref(SDB_OBJ(updated_attr));
status = -1;
}
int
sdb_store_host_tojson(sdb_store_base_t *h, sdb_strbuf_t *buf, int flags)
{
- sdb_store_obj_t *host;
+ sdb_host_t *host;
char time_str[64];
char interval_str[64];
if ((! h) || (h->type != SDB_HOST) || (! buf))
return -1;
- host = SDB_STORE_OBJ(h);
+ host = HOST(h);
if (! sdb_strftime(time_str, sizeof(time_str),
"%F %T %z", host->_last_update))
index f57cb05cab292fa53d86b56c936d35403fc77351..66f3f22dcfb9b5ec7a9fd447135ab493c7360f92 100644 (file)
--- a/src/core/store_lookup.c
+++ b/src/core/store_lookup.c
} /* lookup_iter */
static sdb_store_base_t *
-attr_get(sdb_store_base_t *host, const char *name)
+attr_get(sdb_host_t *host, const char *name)
{
sdb_llist_iter_t *iter = NULL;
sdb_store_base_t *attr = NULL;
- assert(host->type == SDB_HOST);
-
- iter = sdb_llist_get_iter(SDB_STORE_OBJ(host)->attributes);
+ iter = sdb_llist_get_iter(host->attributes);
while (sdb_llist_iter_has_next(iter)) {
- sdb_attribute_t *a = SDB_ATTR(sdb_llist_iter_get_next(iter));
+ sdb_attribute_t *a = ATTR(sdb_llist_iter_get_next(iter));
if (strcasecmp(name, SDB_OBJ(a)->name))
continue;
{
sdb_attribute_t *attr;
- attr = SDB_ATTR(attr_get(obj, ATTR_C(cond)->name));
+ attr = ATTR(attr_get(HOST(obj), ATTR_C(cond)->name));
if (! attr)
return INT_MAX;
if (attr->value.type != ATTR_C(cond)->value.type)
return match_string(&NAME_M(m)->name, obj->super.name);
break;
case SDB_SERVICE:
- iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->services);
+ iter = sdb_llist_get_iter(HOST(obj)->services);
break;
case SDB_ATTRIBUTE:
- iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->attributes);
+ iter = sdb_llist_get_iter(HOST(obj)->attributes);
break;
}
assert(m->type == MATCHER_ATTR);
assert(ATTR_M(m)->name);
- attr = SDB_ATTR(attr_get(obj, ATTR_M(m)->name));
+ attr = ATTR(attr_get(HOST(obj), ATTR_M(m)->name));
if (attr) {
char buf[sdb_data_strlen(&attr->value) + 1];
if (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0)