aboutsummaryrefslogtreecommitdiffstats
path: root/src/cyn.c
diff options
context:
space:
mode:
authorJosé Bollo <jose.bollo@iot.bzh>2019-05-24 12:21:33 +0200
committerJosé Bollo <jose.bollo@iot.bzh>2019-05-24 12:21:33 +0200
commit395cd2eb66909c4396cdd7a3ae2ad8e4c5ec6ee8 (patch)
tree679d9b77ddf7b5b77163a901b7eacd4b4e7edf44 /src/cyn.c
parentc715090f1faaa2ec53ee84895d61c7f72fcd4772 (diff)
Add cacheid
The idea is to allow a client to disconnect and reconnect without losing its cache. Change-Id: I017176e5eb5f553b961a32ee466d1b7a175532bb Signed-off-by: José Bollo <jose.bollo@iot.bzh>
Diffstat (limited to 'src/cyn.c')
-rw-r--r--src/cyn.c46
1 files changed, 41 insertions, 5 deletions
diff --git a/src/cyn.c b/src/cyn.c
index f853a53..6b6b17a 100644
--- a/src/cyn.c
+++ b/src/cyn.c
@@ -17,6 +17,7 @@
#include <assert.h>
#include <stdlib.h>
+#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
@@ -52,6 +53,9 @@ static const void *lock;
static struct callback *awaiters;
static struct callback *observers;
static struct agent *agents;
+static uint32_t last_changeid;
+static uint32_t last_changeid_string;
+static char changeid_string[12];
static
int
@@ -92,6 +96,17 @@ addcb(
return 0;
}
+static
+void
+changed(
+) {
+ struct callback *c;
+
+ ++last_changeid;
+ for (c = observers; c ; c = c->next)
+ c->on_change_cb(c->closure);
+}
+
/** enter critical recoverable section */
int
cyn_enter(
@@ -147,7 +162,7 @@ cyn_leave(
bool commit
) {
int rc, rcp;
- struct callback *c, *e, **p;
+ struct callback *e, **p;
if (!magic)
return -EINVAL;
@@ -164,10 +179,8 @@ cyn_leave(
if (rc == 0) {
rcp = queue_play();
rc = db_transaction_end(rcp == 0) ?: rcp;
- if (rcp == 0) {
- for (c = observers; c ; c = c->next)
- c->on_change_cb(c->closure);
- }
+ if (rcp == 0)
+ changed();
}
}
queue_clear();
@@ -472,3 +485,26 @@ cyn_agent_remove(
free(agent);
return 0;
}
+
+void
+cyn_changeid_reset(
+) {
+ last_changeid = 1;
+}
+
+uint32_t
+cyn_changeid(
+) {
+ return last_changeid;
+}
+
+extern
+const char *
+cyn_changeid_string(
+) {
+ if (last_changeid != last_changeid_string) {
+ last_changeid_string = last_changeid;
+ snprintf(changeid_string, sizeof changeid_string, "%u", last_changeid);
+ }
+ return changeid_string;
+} \ No newline at end of file