summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDean Jansa <djansa@redhat.com>2005-10-06 20:06:41 +0000
committerNathan Straz <nstraz@redhat.com>2008-09-23 09:37:45 -0400
commit5da52098beb4fa5af4b592b3f050fcc12e0bd05b (patch)
treef61ee7f110582435e76be49acc1187173e3709f5
parent89d2504db378c5b091db5229484af5c58851fa5f (diff)
downloadqarsh-5da52098beb4fa5af4b592b3f050fcc12e0bd05b.zip
qarsh-5da52098beb4fa5af4b592b3f050fcc12e0bd05b.tar.gz
qarsh-5da52098beb4fa5af4b592b3f050fcc12e0bd05b.tar.xz
Added interfaces to get/set the hbeat state.
-rw-r--r--hbeat.c72
-rw-r--r--hbeat.h11
-rw-r--r--hbeat_test.c54
3 files changed, 109 insertions, 28 deletions
diff --git a/hbeat.c b/hbeat.c
index e0255a3..6242d38 100644
--- a/hbeat.c
+++ b/hbeat.c
@@ -17,7 +17,7 @@
struct hbeat_s {
char *host;
int max_timeout;
- enum {HOST_ALIVE, HOST_QUIET, HOST_TIMEOUT, HOST_REBOOT} rhost_state;
+ hbeat_state_t rhost_state;
unsigned int last_rhost_btime;
time_t start_quiet_time;
};
@@ -85,38 +85,76 @@ hbeat(hbeat_t hbh)
struct hbeat_s *hbeatp = hbh;
unsigned int hbeat;
time_t current_time;
+ int retval;
/* User disabled heart beating */
if (!hbeatp->max_timeout) {
+ hbeatp->rhost_state = HOST_HBEAT_DISABLED;
return 1;
}
hbeat = btime(hbeatp->host);
current_time = time(NULL);
- if (!hbeat && (hbeatp->rhost_state == HOST_QUIET)) {
- if (current_time - hbeatp->start_quiet_time
- > hbeatp->max_timeout) {
- hbeatp->rhost_state = HOST_TIMEOUT;
- return 0;
- }
- } else {
- hbeatp->rhost_state = HOST_QUIET;
- hbeatp->start_quiet_time = time(NULL);
+ if (!hbeat) {
+ switch (hbeatp->rhost_state) {
+ case HOST_ALIVE:
+ hbeatp->rhost_state = HOST_QUIET;
+ hbeatp->start_quiet_time = time(NULL);
+ retval = 1;
+ break;
+
+ case HOST_QUIET:
+ if (current_time - hbeatp->start_quiet_time
+ > hbeatp->max_timeout) {
+ hbeatp->rhost_state = HOST_TIMEOUT;
+ retval = 0;
+ } else {
+ retval = 1;
+ }
+ break;
+
+ case HOST_TIMEOUT:
+ case HOST_REBOOT:
+ retval = 0;
+ break;
+
+ case HOST_HBEAT_DISABLED:
+ retval = 1;
+ break;
+ }
}
if (hbeat) {
- if (hbeatp->last_rhost_btime == 0) {
+ if (hbeatp->rhost_state == HOST_REBOOT) {
+ retval = 0;
+ } else {
hbeatp->last_rhost_btime = hbeat;
hbeatp->rhost_state = HOST_ALIVE;
hbeatp->start_quiet_time = 0;
- }
+ retval = 1;
- if (abs(hbeat - hbeatp->last_rhost_btime) > 5) {
- hbeatp->rhost_state = HOST_REBOOT;
- return 0;
- }
+ if (abs(hbeat - hbeatp->last_rhost_btime) > 5) {
+ hbeatp->rhost_state = HOST_REBOOT;
+ retval = 0;
+ }
+ }
}
- return 1;
+ return retval;
+}
+
+
+hbeat_state_t
+hbeat_getstate(hbeat_t hbh)
+{
+ return ((struct hbeat_s *)hbh)->rhost_state;
+}
+
+
+void
+hbeat_setstate(hbeat_t hbh, hbeat_state_t state)
+{
+ ((struct hbeat_s *)hbh)->rhost_state = state;
+ return;
}
diff --git a/hbeat.h b/hbeat.h
index 1a7b4b8..fb6d6ac 100644
--- a/hbeat.h
+++ b/hbeat.h
@@ -4,6 +4,13 @@
typedef void * hbeat_t;
+typedef enum {
+ HOST_ALIVE, HOST_QUIET, HOST_TIMEOUT, HOST_REBOOT, HOST_HBEAT_DISABLED
+} hbeat_state_t;
+
+
extern hbeat_t hbeat_init(const char *host, int max_timeout);
-void hbeat_free(hbeat_t hbh);
-unsigned int hbeat(hbeat_t hbh);
+extern void hbeat_free(hbeat_t hbh);
+extern unsigned int hbeat(hbeat_t hbh);
+extern hbeat_state_t hbeat_getstate(hbeat_t hbh);
+extern void hbeat_setstate(hbeat_t hbh, hbeat_state_t state);
diff --git a/hbeat_test.c b/hbeat_test.c
index 8ea00ee..2a88bc1 100644
--- a/hbeat_test.c
+++ b/hbeat_test.c
@@ -1,24 +1,60 @@
#include <stdio.h>
#include "hbeat.h"
+char *state2str(hbeat_state_t state);
+
int
main(void)
{
- hbeat_t hb;
+ hbeat_t hb1, hb2, hb3;
int i;
- int timeout_max = 10;
+ int timeout_max = 5;
- hb = hbeat_init("link-13", timeout_max);
+ hb1 = hbeat_init("link-13", timeout_max);
+ hb2 = hbeat_init("link-14", timeout_max);
+ hb3 = hbeat_init("link-15", timeout_max);
- for (i = 0; i < 10; i++) {
- printf("hbeat() = %d\n", hbeat(hb));
+ for (i = 0; i < 200; i++) {
+ printf("hbeat(1) = %d\n", hbeat(hb1));
+ printf("hbeat state = %s\n", state2str(hbeat_getstate(hb1)));
+ printf("hbeat(2) = %d\n", hbeat(hb2));
+ printf("hbeat state = %s\n", state2str(hbeat_getstate(hb2)));
+ printf("hbeat(3) = %d\n", hbeat(hb3));
+ printf("hbeat state = %s\n", state2str(hbeat_getstate(hb3)));
sleep(1);
}
- hbeat_free(hb);
+ hbeat_free(hb1);
+ hbeat_free(hb2);
+ hbeat_free(hb3);
return 0;
}
-
-
-
+
+char *
+state2str(hbeat_state_t state)
+{
+ char *cp;
+
+ switch (state) {
+ case HOST_ALIVE:
+ cp = "HOST_ALIVE";
+ break;
+ case HOST_QUIET:
+ cp = "HOST_QUIET";
+ break;
+ case HOST_TIMEOUT:
+ cp = "HOST_TIMEOUT";
+ break;
+ case HOST_REBOOT:
+ cp = "HOST_REBOOT";
+ break;
+ case HOST_HBEAT_DISABLED:
+ cp = "HOST_HBEAT_DISABLED";
+ break;
+ }
+
+ return cp;
+
+}
+