Kannel: Open Source WAP and SMS gateway  svn-r5335
heartbeat.c File Reference
#include <signal.h>
#include "gwlib/gwlib.h"
#include "msg.h"
#include "heartbeat.h"

Go to the source code of this file.

Data Structures

struct  hb_info
 

Functions

static int find_hb (void *item, void *pattern)
 
static void heartbeat_thread (void *arg)
 
long heartbeat_start (hb_send_func_t *send_func, double freq, hb_load_func_t *load_func)
 
void heartbeat_stop (long hb_thread)
 

Variables

static Listheartbeats = NULL
 

Function Documentation

◆ find_hb()

static int find_hb ( void *  item,
void *  pattern 
)
static

Definition at line 85 of file heartbeat.c.

References info().

Referenced by heartbeat_stop().

86 {
87  long *threadnrp;
88  struct hb_info *info;
89 
90  info = item;
91  threadnrp = pattern;
92 
93  return info->thread == *threadnrp;
94 }
void info(int err, const char *fmt,...)
Definition: log.c:672

◆ heartbeat_start()

long heartbeat_start ( hb_send_func_t send_func,
double  freq,
hb_load_func_t load_func 
)

Definition at line 126 of file heartbeat.c.

References DEFAULT_HEARTBEAT, hb_info::freq, gwlist_append(), gwlist_create, gwthread_create, heartbeat_thread(), heartbeats, info(), hb_info::load_func, and hb_info::send_func.

Referenced by main(), and run_smppbox().

128 {
129  struct hb_info *info;
130 
131  /* can't start with send_funct NULL */
132  if (send_func == NULL)
133  return -1;
134 
135  info = gw_malloc(sizeof(*info));
136  info->send_func = send_func;
137  info->freq = (freq <= 0 ? DEFAULT_HEARTBEAT : freq);
138  info->load_func = load_func;
139  info->running = 1;
141  if (info->thread >= 0) {
142  if (heartbeats == NULL)
145  return info->thread;
146  } else {
147  gw_free(info);
148  return -1;
149  }
150 }
void info(int err, const char *fmt,...)
Definition: log.c:672
void gwlist_append(List *list, void *item)
Definition: list.c:179
hb_send_func_t * send_func
Definition: heartbeat.c:72
double freq
Definition: heartbeat.c:73
static List * heartbeats
Definition: heartbeat.c:80
hb_load_func_t * load_func
Definition: heartbeat.c:74
#define gwthread_create(func, arg)
Definition: gwthread.h:90
#define DEFAULT_HEARTBEAT
Definition: heartbeat.h:67
#define gwlist_create()
Definition: list.h:136
static void heartbeat_thread(void *arg)
Definition: heartbeat.c:96

◆ heartbeat_stop()

void heartbeat_stop ( long  hb_thread)

Definition at line 160 of file heartbeat.c.

References ALL_HEARTBEATS, find_hb(), gw_assert(), gwlist_destroy(), gwlist_extract_first(), gwlist_extract_matching(), gwlist_len(), gwthread_join(), gwthread_wakeup(), heartbeats, info(), and warning().

Referenced by main().

161 {
162  List *matching_info;
163  struct hb_info *info;
164 
165  /*
166  * First, check if there are heartbeats to stop.
167  * If not, do not continue, otherwise this function will crash
168  */
169  if (heartbeats == NULL)
170  return;
171 
172  if (hb_thread == ALL_HEARTBEATS) {
173  while (NULL != (info = gwlist_extract_first(heartbeats))) {
174  gw_assert(info);
175  info->running = 0;
176  gwthread_wakeup(info->thread);
177  gwthread_join(info->thread);
178  gw_free(info);
179  }
180  } else {
181  matching_info = gwlist_extract_matching(heartbeats, &hb_thread, find_hb);
182  if (matching_info == NULL) {
183  warning(0, "Could not stop heartbeat %ld: not found.", hb_thread);
184  return;
185  }
186  gw_assert(gwlist_len(matching_info) == 1);
187  info = gwlist_extract_first(matching_info);
188  gwlist_destroy(matching_info, NULL);
189 
190  info->running = 0;
191  gwthread_wakeup(hb_thread);
192  gwthread_join(hb_thread);
193  gw_free(info);
194  }
195  if (gwlist_len(heartbeats) == 0) {
196  gwlist_destroy(heartbeats, NULL);
197  heartbeats = NULL;
198  }
199 }
void info(int err, const char *fmt,...)
Definition: log.c:672
gw_assert(wtls_machine->packet_to_send !=NULL)
void gwthread_join(long thread)
long gwlist_len(List *list)
Definition: list.c:166
List * gwlist_extract_matching(List *list, void *pat, gwlist_item_matches_t *cmp)
Definition: list.c:322
static int find_hb(void *item, void *pattern)
Definition: heartbeat.c:85
static List * heartbeats
Definition: heartbeat.c:80
void * gwlist_extract_first(List *list)
Definition: list.c:305
void warning(int err, const char *fmt,...)
Definition: log.c:660
#define ALL_HEARTBEATS
Definition: heartbeat.h:68
void gwthread_wakeup(long thread)
Definition: list.c:102
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)
Definition: list.c:145

◆ heartbeat_thread()

static void heartbeat_thread ( void *  arg)
static

Definition at line 96 of file heartbeat.c.

References gwthread_sleep(), info(), msg, and msg_create.

Referenced by heartbeat_start().

97 {
98  struct hb_info *info;
99  time_t last_hb;
100 
101  info = arg;
102  last_hb = 0;
103 
104  while (info->running) {
105  Msg *msg;
106 
107  gwthread_sleep(info->freq);
108 
109  /*
110  * Because the sleep can be interrupted, we might end up sending
111  * heartbeats faster than the configured heartbeat frequency.
112  * This is not bad unless we send them way too fast. Make sure
113  * our frequency is not more than twice the configured one.
114  */
115  if (difftime(time(NULL), last_hb) < info->freq / 2)
116  continue;
117 
118  msg = msg_create(heartbeat);
119  if (NULL != info->load_func)
120  msg->heartbeat.load = info->load_func();
121  info->send_func(msg);
122  last_hb = time(NULL);
123  }
124 }
void info(int err, const char *fmt,...)
Definition: log.c:672
#define msg_create(type)
Definition: msg.h:136
Definition: msg.h:79
void gwthread_sleep(double seconds)
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86

Variable Documentation

◆ heartbeats

List* heartbeats = NULL
static

Definition at line 80 of file heartbeat.c.

Referenced by heartbeat_start(), and heartbeat_stop().

See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.