changes to the heartbeat code.

Harrie Hazewinkel <[email protected]>
Newsgroups gmane.comp.mobile.kannel.devel
Message-ID <7153465.1022630219@localhost>
Hi,

I have attached a patch to group the heartbeat code.
If I hear/see no objections I will commit this.

It changes:
1) adds a stop all heartbeat functionality by by
      means of the heartbeat_thread value of '-1'
      (ALL_HEARTBEATS) as the heartbeat_stop function.
2) move the heartbeat frequentie default value in the
      heartbeat.h (IMHO, where it belongs)
3) changes the wapbox.c and smsbox.c where the
      returned thread_nr is only used for providing a warning
      and the heartbeatthread_stop does a stop ALL_HEARTBEATS.


regards,

Harrie

Internet Management Consulting
mailto:[email protected]                http ://www.mod-snmp.com/
heartbeat.patch (application/octet-stream, 5.8 KB)
diff -ru ../orig-gateway/gw/bb.h ./gw/bb.h
--- ../orig-gateway/gw/bb.h	Tue May 28 20:46:12 2002
+++ ./gw/bb.h	Tue May 28 21:48:28 2002
@@ -13,7 +13,6 @@
 #define BB_DEFAULT_WAPBOX_PORT	13002
 #define BB_DEFAULT_HTTP_PORT	13000
 
-#define BB_DEFAULT_HEARTBEAT	30
 #define BB_DEFAULT_MAX_QUEUE	1000
 
 #endif
diff -ru ../orig-gateway/gw/heartbeat.c ./gw/heartbeat.c
--- ../orig-gateway/gw/heartbeat.c	Tue May 28 20:46:33 2002
+++ ./gw/heartbeat.c	Tue May 28 23:44:03 2002
@@ -73,7 +73,7 @@
 
     info = gw_malloc(sizeof(*info));
     info->send_func = send_func;
-    info->freq = freq;
+    info->freq = (freq <= 0 ? DEFAULT_HEARTBEAT : freq);
     info->load_func = load_func;
     info->running = 1;
     info->thread = gwthread_create(heartbeat_thread, info);
@@ -88,27 +88,44 @@
     }
 }
 
+/*
+ * function : heartbeat_stop
+ * arguments: long hb_thread, the thread number of the heartbeat
+ *            that is wished to be stopped.
+ *            if hb_thread == ALL_HEARTBEATS then all heartbeats
+ *            are stopped.
+ * returns  : -
+ */
 void heartbeat_stop(long hb_thread)
 {
     List *matching_info;
     struct hb_info *info;
 
-    matching_info = list_extract_matching(heartbeats, &hb_thread, find_hb);
-    if (matching_info == NULL) {
-        warning(0, "Could not stop heartbeat %ld: not found.", hb_thread);
-	return;
+    if (hb_thread == ALL_HEARTBEATS) {
+        while (NULL != (info = list_extract_first(heartbeats))) {
+            gw_assert(info);
+            info->running = 0;
+            gwthread_wakeup(info->thread);
+            gwthread_join(info->thread);
+            gw_free(info);
+        }
+    } else {
+        matching_info = list_extract_matching(heartbeats, &hb_thread, find_hb);
+        if (matching_info == NULL) {
+            warning(0, "Could not stop heartbeat %ld: not found.", hb_thread);
+	    return;
+        }
+        gw_assert(list_len(matching_info) == 1);
+        info = list_extract_first(matching_info);
+        list_destroy(matching_info, NULL);
+     
+        info->running = 0;
+        gwthread_wakeup(hb_thread);
+        gwthread_join(hb_thread);
+        gw_free(info);
     }
-    gw_assert(list_len(matching_info) == 1);
-    info = list_extract_first(matching_info);
-    list_destroy(matching_info, NULL);
- 
-    info->running = 0;
-    gwthread_wakeup(hb_thread);
-    gwthread_join(hb_thread);
-
-    gw_free(info);
     if (list_len(heartbeats) == 0) {
-	list_destroy(heartbeats, NULL);
-	heartbeats = NULL;
+        list_destroy(heartbeats, NULL);
+        heartbeats = NULL;
     }
 }
diff -ru ../orig-gateway/gw/heartbeat.h ./gw/heartbeat.h
--- ../orig-gateway/gw/heartbeat.h	Tue May 28 20:46:33 2002
+++ ./gw/heartbeat.h	Tue May 28 22:49:41 2002
@@ -8,6 +8,8 @@
 #include "gwlib/gwlib.h"
 #include "msg.h"
 
+#define DEFAULT_HEARTBEAT    30
+#define ALL_HEARTBEATS       -1
 /*
  * Signature for a function that returns the current load value.
  */
Binary files ../orig-gateway/gw/heartbeat.o and ./gw/heartbeat.o differ
diff -ru ../orig-gateway/gw/smsbox.c ./gw/smsbox.c
--- ../orig-gateway/gw/smsbox.c	Tue May 28 20:46:12 2002
+++ ./gw/smsbox.c	Tue May 28 22:07:10 2002
@@ -46,7 +46,6 @@
 static Octstr *xmlrpc_url = NULL;
 static Octstr *bb_host;
 static char *pid_file;
-static int heartbeat_freq;
 static Octstr *accepted_chars = NULL;
 static int only_try_http = 0;
 static URLTranslationList *translations = NULL;
@@ -2692,7 +2691,6 @@
     bb_port = BB_DEFAULT_SMSBOX_PORT;
     bb_ssl = 0;
     bb_host = octstr_create(BB_DEFAULT_HOST);
-    heartbeat_freq = BB_DEFAULT_HEARTBEAT;
     logfile = NULL;
     lvl = 0;
 
@@ -2853,8 +2851,8 @@
 int main(int argc, char **argv)
 {
     int cf_index;
-    long heartbeat_thread;
     Octstr *cfg_name;
+    double heartbeat_freq = DEFAULT_HEARTBEAT;
 
     gwlib_init();
     cf_index = get_and_set_debugs(argc, argv, check_args);
@@ -2897,14 +2895,16 @@
     connect_to_bearerbox(bb_host, bb_port, bb_ssl, NULL /* bb_our_host */);
 	/* XXX add our_host if required */
 
-    heartbeat_thread = heartbeat_start(write_to_bearerbox, heartbeat_freq,
-				       outstanding_requests);
+    if (0 > heartbeat_start(write_to_bearerbox, heartbeat_freq,
+				       outstanding_requests)) {
+        info(0, GW_NAME "Could not start heartbeat.");
+    }
 
     read_messages_from_bearerbox();
 
     info(0, GW_NAME " smsbox terminating.");
 
-    heartbeat_stop(heartbeat_thread);
+    heartbeat_stop(ALL_HEARTBEATS);
     http_close_all_ports();
     gwthread_join_every(sendsms_thread);
     list_remove_producer(smsbox_requests);
diff -ru ../orig-gateway/gw/wapbox.c ./gw/wapbox.c
--- ../orig-gateway/gw/wapbox.c	Tue May 28 20:46:13 2002
+++ ./gw/wapbox.c	Tue May 28 22:07:49 2002
@@ -40,8 +40,6 @@
 static Octstr *bearerbox_host;
 static long bearerbox_port = BB_DEFAULT_WAPBOX_PORT;
 static int bearerbox_ssl = 0;
-static long heartbeat_freq = BB_DEFAULT_HEARTBEAT;
-static long heartbeat_thread;
 static Counter *sequence_counter = NULL;
 int wtp_forced_sar = 0;
 int wsp_smart_errors = 0;
@@ -410,6 +408,7 @@
     Msg *msg;
     Octstr *filename;
     Cfg *cfg;
+    double heartbeat_freq =  DEFAULT_HEARTBEAT;
     
     gwlib_init();
     cf_index = get_and_set_debugs(argc, argv, NULL);
@@ -467,8 +466,10 @@
         wap_push_ota_bb_address_set(bearerbox_host);
 	    
     program_status = running;
-    heartbeat_thread = heartbeat_start(write_to_bearerbox, heartbeat_freq, 
-    	    	    	    	       wap_appl_get_load);
+    if (0 > heartbeat_start(write_to_bearerbox, heartbeat_freq, 
+    	    	    	    	       wap_appl_get_load)) {
+        info(0, GW_NAME "Could not start heartbeat.");
+    }
 
     while (program_status != shutting_down) {
 	WAPEvent *dgram;
@@ -520,7 +521,7 @@
     info(0, GW_NAME " wapbox terminating.");
     
     program_status = shutting_down;
-    heartbeat_stop(heartbeat_thread);
+    heartbeat_stop(ALL_HEARTBEATS);
     counter_destroy(sequence_counter);
 
     if (cfg)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.