From eb914ab56bf7122b74a13405c59d1c862c5e5015 Mon Sep 17 00:00:00 2001 From: Sumit Bose Date: Fri, 14 May 2010 10:57:19 +0200 Subject: [PATCH 3/6] Add offline callbacks --- src/providers/data_provider_be.c | 1 + src/providers/data_provider_callbacks.c | 38 +++++++++++++++++++++++++++++- src/providers/dp_backend.h | 10 +++++++- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/providers/data_provider_be.c b/src/providers/data_provider_be.c index 9a7f8cf..4ece1b6 100644 --- a/src/providers/data_provider_be.c +++ b/src/providers/data_provider_be.c @@ -167,6 +167,7 @@ void be_mark_offline(struct be_ctx *ctx) ctx->offstat.went_offline = time(NULL); ctx->offstat.offline = true; ctx->run_online_cb = true; + be_run_offline_cb(ctx); } static int be_check_online(DBusMessage *message, struct sbus_connection *conn) diff --git a/src/providers/data_provider_callbacks.c b/src/providers/data_provider_callbacks.c index 6d5b470..78d68e3 100644 --- a/src/providers/data_provider_callbacks.c +++ b/src/providers/data_provider_callbacks.c @@ -114,8 +114,6 @@ final: talloc_free(cb_ctx); } - - int be_add_online_cb(TALLOC_CTX *mem_ctx, struct be_ctx *ctx, be_callback_t cb, void *pvt, struct be_cb **online_cb) { @@ -169,3 +167,39 @@ void be_run_online_cb(struct be_ctx *be) { DEBUG(9, ("Online call back list is empty, nothing to do.\n")); } } + +int be_add_offline_cb(TALLOC_CTX *mem_ctx, struct be_ctx *ctx, be_callback_t cb, + void *pvt, struct be_cb **offline_cb) +{ + return be_add_cb(mem_ctx, ctx, cb, pvt, &ctx->offline_cb_list, offline_cb); +} + +void be_run_offline_cb(struct be_ctx *be) { + struct timeval soon; + struct tevent_timer *te; + struct be_cb_ctx *cb_ctx; + + if (be->offline_cb_list) { + DEBUG(3, ("Going offline. Running callbacks.\n")); + + cb_ctx = talloc(be, struct be_cb_ctx); + if (!cb_ctx) { + DEBUG(0, ("Out of memory. Could not invoke callbacks\n")); + return; + } + cb_ctx->be = be; + cb_ctx->callback = be->offline_cb_list; + + /* Delay 30ms so we don't block any other events */ + soon = tevent_timeval_current_ofs(0, 30000); + te = tevent_add_timer(be->ev, cb_ctx, soon, + be_run_cb_step, + cb_ctx); + if (!te) { + DEBUG(0, ("Out of memory. Could not invoke callbacks\n")); + talloc_free(cb_ctx); + } + } else { + DEBUG(9, ("Offline call back list is empty, nothing to do.\n")); + } +} diff --git a/src/providers/dp_backend.h b/src/providers/dp_backend.h index 43d79ba..fc5e558 100644 --- a/src/providers/dp_backend.h +++ b/src/providers/dp_backend.h @@ -95,10 +95,11 @@ struct be_ctx { struct be_failover_ctx *be_fo; /* Functions to be invoked when the - * backend goes online + * backend goes online or offline */ struct be_cb *online_cb_list; bool run_online_cb; + struct be_cb *offline_cb_list; struct be_offline_status offstat; @@ -148,6 +149,13 @@ int be_add_online_cb(TALLOC_CTX *mem_ctx, struct be_cb **online_cb); void be_run_online_cb(struct be_ctx *be); +int be_add_offline_cb(TALLOC_CTX *mem_ctx, + struct be_ctx *ctx, + be_callback_t cb, + void *pvt, + struct be_cb **online_cb); +void be_run_offline_cb(struct be_ctx *be); + /* from data_provider_fo.c */ typedef void (be_svc_callback_fn_t)(void *, struct fo_server *); -- 1.6.6.1