From 223d2477082580e09de4283b5e664ba0455376af Mon Sep 17 00:00:00 2001 From: Jan-Piet Mens Date: Thu, 22 Oct 2015 10:48:13 +0200 Subject: [PATCH] implement ronlydb --- recorder.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++- udata.h | 3 +++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/recorder.c b/recorder.c index 32f2eaa..4b03a92 100644 --- a/recorder.c +++ b/recorder.c @@ -493,8 +493,24 @@ void on_message(struct mosquitto *mosq, void *userdata, const struct mosquitto_m if ((json = json_decode(m->payload)) == NULL) { if ((json = csv_to_json(m->payload)) == NULL) { +#ifdef WITH_RONLY + /* + * If the base topic belongs to an RONLY user, store + * the payload. + */ + + char buf[BUFSIZ]; + long blen; + + blen = gcache_get(ud->ronlydb, UB(basetopic), buf, sizeof(buf)); + if (blen > 0) { + // puts("*** storing plain publis"); + putrec(ud, now, reltopic, username, device, bindump(m->payload, m->payloadlen)); + } +#else /* It's not JSON or it's not a location CSV; store it */ putrec(ud, now, reltopic, username, device, bindump(m->payload, m->payloadlen)); +#endif return; } } @@ -515,13 +531,45 @@ void on_message(struct mosquitto *mosq, void *userdata, const struct mosquitto_m */ if ((j = json_find_member(json, "r")) == NULL) { + + char buf[BUFSIZ]; + long blen; + r_ok = FALSE; + + /* + * This JSON payload might actually belong to an RONLY user + * but it doesn't have an `r:true' in it. Determine whether + * the basetopic belongs to such a user, and force r_ok + * accordingly. + */ + + blen = gcache_get(ud->ronlydb, UB(basetopic), buf, sizeof(buf)); + if (blen > 0) { + r_ok = TRUE; + // printf("*** forcing TRUE b/c ronlydb (blen=%ld)\n", blen); + } } else { r_ok = TRUE; if ((j->tag != JSON_BOOL) || (j->bool_ == FALSE)) { r_ok = FALSE; } } + + /* + * The payload contains an `r:true' so we can make a note of this + * base topic in RONLYdb. + */ + + if (r_ok == TRUE) { + int rc; + if ((rc =gcache_put(ud->ronlydb, UB(basetopic), m->payload)) != 0) + olog(LOG_ERR, "Cannot store %s in ronlydb: rc==%d", UB(basetopic), rc); + } else { + int rc; + if ((rc = gcache_del(ud->ronlydb, UB(basetopic))) != 0) + olog(LOG_ERR, "Cannot delete %s from ronlydb: rc==%d", UB(basetopic), rc); + } #endif _type = T_UNKNOWN; @@ -570,7 +618,9 @@ void on_message(struct mosquitto *mosq, void *userdata, const struct mosquitto_m case T_LOCATION: break; default: - putrec(ud, now, reltopic, username, device, bindump(m->payload, m->payloadlen)); + if (r_ok) { + putrec(ud, now, reltopic, username, device, bindump(m->payload, m->payloadlen)); + } goto cleanup; } @@ -934,6 +984,9 @@ int main(int argc, char **argv) #ifdef WITH_LMDB udata.gc = NULL; udata.t2t = NULL; /* Topic to TID */ +# ifdef WITH_RONLY + udata.ronlydb = NULL; /* RONLY db */ +# endif #endif #ifdef WITH_HTTP udata.mgserver = NULL; @@ -1117,6 +1170,13 @@ int main(int argc, char **argv) } gcache_close(gt); #endif /* !LUA */ +#ifdef WITH_RONLY + if ((gt = gcache_open(path, "ronlydb", FALSE)) == NULL) { + fprintf(stderr, "Cannot lmdb-open `ronly'\n"); + exit(2); + } + gcache_close(gt); +#endif /* !RONLY */ #endif exit(0); } @@ -1203,6 +1263,9 @@ int main(int argc, char **argv) # ifdef WITH_LUA ud->luadb = gcache_open(err, "luadb", FALSE); # endif +# ifdef WITH_RONLY + ud->ronlydb = gcache_open(err, "ronlydb", FALSE); +# endif #endif #ifdef WITH_LUA diff --git a/udata.h b/udata.h index 53675cc..0446b0f 100644 --- a/udata.h +++ b/udata.h @@ -22,6 +22,9 @@ struct udata { #ifdef WITH_LMDB struct gcache *gc; struct gcache *t2t; /* topic to tid */ +# ifdef WITH_RONLY + struct gcache *ronlydb; /* RONLY db */ +# endif #endif #ifdef WITH_HTTP struct mg_server *mgserver; /* Mongoose */