Kannel: Open Source WAP and SMS gateway  svn-r5335
heartbeat.c
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2018 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Kannel Group (http://www.kannel.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  * endorse or promote products derived from this software without
29  * prior written permission. For written permission, please
30  * contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  * nor may "Kannel" appear in their name, without prior written
34  * permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group. For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 /*
58  * heartbeat.c - thread for sending heartbeat Msgs to bearerbox
59  */
60 
61 #include <signal.h>
62 
63 #include "gwlib/gwlib.h"
64 #include "msg.h"
65 #include "heartbeat.h"
66 
67 /*
68  * Each running heartbeat gets one of these. They are collected in
69  * the heartbeats List.
70  */
71 struct hb_info {
73  double freq;
75  long thread;
76  volatile sig_atomic_t running;
77 };
78 
79 /* List of struct hb_info. */
80 static List *heartbeats = NULL;
81 
82 /*
83  * Look for a hb_info in a list, by thread number.
84  */
85 static int find_hb(void *item, void *pattern)
86 {
87  long *threadnrp;
88  struct hb_info *info;
89 
90  info = item;
91  threadnrp = pattern;
92 
93  return info->thread == *threadnrp;
94 }
95 
96 static void heartbeat_thread(void *arg)
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 }
125 
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 }
151 
152 /*
153  * function : heartbeat_stop
154  * arguments: long hb_thread, the thread number of the heartbeat
155  * that is wished to be stopped.
156  * if hb_thread == ALL_HEARTBEATS then all heartbeats
157  * are stopped.
158  * returns : -
159  */
160 void heartbeat_stop(long hb_thread)
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 gwlist_append(List *list, void *item)
Definition: list.c:179
hb_send_func_t * send_func
Definition: heartbeat.c:72
void gwthread_join(long thread)
long gwlist_len(List *list)
Definition: list.c:166
long hb_load_func_t(void)
Definition: heartbeat.h:72
void hb_send_func_t(Msg *hb)
Definition: heartbeat.h:78
#define msg_create(type)
Definition: msg.h:136
List * gwlist_extract_matching(List *list, void *pat, gwlist_item_matches_t *cmp)
Definition: list.c:322
double freq
Definition: heartbeat.c:73
void heartbeat_stop(long hb_thread)
Definition: heartbeat.c:160
static int find_hb(void *item, void *pattern)
Definition: heartbeat.c:85
static List * heartbeats
Definition: heartbeat.c:80
Definition: msg.h:79
void * gwlist_extract_first(List *list)
Definition: list.c:305
hb_load_func_t * load_func
Definition: heartbeat.c:74
void warning(int err, const char *fmt,...)
Definition: log.c:660
#define gwthread_create(func, arg)
Definition: gwthread.h:90
void gwthread_sleep(double seconds)
#define ALL_HEARTBEATS
Definition: heartbeat.h:68
volatile sig_atomic_t running
Definition: heartbeat.c:76
#define DEFAULT_HEARTBEAT
Definition: heartbeat.h:67
long heartbeat_start(hb_send_func_t *send_func, double freq, hb_load_func_t *load_func)
Definition: heartbeat.c:126
long thread
Definition: heartbeat.c:75
void gwthread_wakeup(long thread)
#define gwlist_create()
Definition: list.h:136
static void heartbeat_thread(void *arg)
Definition: heartbeat.c:96
Definition: list.c:102
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)
Definition: list.c:145
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.