Kannel: Open Source WAP and SMS gateway  svn-r5335
test_timerset.c File Reference
#include "gwlib/gwlib.h"
#include "gwlib/gw-timer.h"
#include "gw/msg.h"

Go to the source code of this file.

Data Structures

struct  TimerItem
 

Macros

#define MAX_RETRY   3
 
#define FREQ   10
 

Typedefs

typedef struct TimerItem TimerItem
 

Functions

static void timer_consumer_thread (void *arg)
 
static void timer_retry_cb (void *arg)
 
static void retry_consumer_thread (void *arg)
 
int main ()
 

Variables

static Timersettimerset = NULL
 
static Listq_timer = NULL
 
static Listq_retry = NULL
 

Macro Definition Documentation

◆ FREQ

#define FREQ   10

Definition at line 68 of file test_timerset.c.

Referenced by main(), retry_consumer_thread(), and timer_consumer_thread().

◆ MAX_RETRY

#define MAX_RETRY   3

Definition at line 67 of file test_timerset.c.

Referenced by retry_consumer_thread(), and timer_consumer_thread().

Typedef Documentation

◆ TimerItem

typedef struct TimerItem TimerItem

Function Documentation

◆ main()

int main ( void  )

Definition at line 154 of file test_timerset.c.

References debug(), FREQ, gw_timer_create(), gw_timer_elapsed_start(), gw_timerset_count(), gw_timerset_create(), gw_timerset_destroy(), gwlib_init(), gwlib_shutdown(), gwlist_add_producer(), gwlist_create, gwlist_destroy(), gwlist_remove_producer(), gwthread_create, gwthread_join(), gwthread_sleep(), TimerItem::msg, msg_create, q_retry, q_timer, retry_consumer_thread(), TimerItem::timer, timer_consumer_thread(), timer_retry_cb(), and timerset.

155 {
156  long t_timer;
157  long t_retry;
158  Msg *msg_a, *msg_b;
159  TimerItem *i_timer;
160  TimerItem *i_retry;
161 
162  gwlib_init();
163 
165 
166  /* setup timer thread to consume queue */
170 
171  /* setup thread to consume retry callback events */
175 
176  msg_a = msg_create(sms);
177  msg_b = msg_create(sms);
178 
179  msg_a->sms.resend_try = msg_b->sms.resend_try = 0;
180 
181  i_timer = gw_malloc(sizeof(TimerItem));
182  i_timer->msg = msg_a;
183  /* timed events are pushed into q_timer queue */
184  i_timer->timer = gw_timer_create(timerset, q_timer, NULL);
185  gw_timer_elapsed_start(i_timer->timer, FREQ, i_timer);
186  debug("",0,"%s: Msg %p retry %ld", __func__, msg_a, msg_a->sms.resend_try);
187 
188  i_retry = gw_malloc(sizeof(TimerItem));
189  i_retry->msg = msg_b;
190  /* timed events trigged the callback function */
191  i_retry->timer = gw_timer_create(timerset, NULL, timer_retry_cb);
192  gw_timer_elapsed_start(i_retry->timer, FREQ, i_retry);
193  debug("",0,"%s: Msg %p retry %ld", __func__, msg_b, msg_b->sms.resend_try);
194 
195  debug("",0,"%s: enter main loop", __func__);
196  do {
197  gwthread_sleep(2);
198  } while (gw_timerset_count(timerset) > 0);
199  debug("",0,"%s: exit main loop", __func__);
200 
202 
204  gwthread_join(t_timer);
205  gwlist_destroy(q_timer, NULL);
206 
208  gwthread_join(t_retry);
209  gwlist_destroy(q_retry, NULL);
210 
211  gwlib_shutdown();
212 
213  return 0;
214 }
void gw_timerset_destroy(Timerset *set)
Definition: gw-timer.c:203
static List * q_timer
Definition: test_timerset.c:78
void gwthread_join(long thread)
long gw_timerset_count(Timerset *set)
Definition: gw-timer.c:244
Timer * gw_timer_create(Timerset *set, List *outputlist, void(*callback)(void *))
Definition: gw-timer.c:255
#define msg_create(type)
Definition: msg.h:136
Timerset * gw_timerset_create(void)
Definition: gw-timer.c:190
static void timer_retry_cb(void *arg)
Definition: msg.h:79
void gwlist_remove_producer(List *list)
Definition: list.c:401
static List * q_retry
Definition: test_timerset.c:79
static void retry_consumer_thread(void *arg)
#define gwthread_create(func, arg)
Definition: gwthread.h:90
void gwthread_sleep(double seconds)
Timer * timer
Definition: smsbox.c:108
static void timer_consumer_thread(void *arg)
Definition: test_timerset.c:87
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:726
void gw_timer_elapsed_start(Timer *timer, int interval, void *data)
Definition: gw-timer.c:352
void gwlib_shutdown(void)
Definition: gwlib.c:94
#define gwlist_create()
Definition: list.h:136
#define FREQ
Definition: test_timerset.c:68
void gwlib_init(void)
Definition: gwlib.c:78
static Timerset * timerset
Definition: test_timerset.c:76
void gwlist_add_producer(List *list)
Definition: list.c:383
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)
Definition: list.c:145

◆ retry_consumer_thread()

static void retry_consumer_thread ( void *  arg)
static

Definition at line 131 of file test_timerset.c.

References debug(), FREQ, gw_timer_create(), gw_timer_elapsed_start(), gwlist_consume(), MAX_RETRY, TimerItem::msg, msg, msg_destroy(), TimerItem::timer, timer_retry_cb(), timerset, and warning().

Referenced by main().

132 {
133  List *q = arg;
134  Msg *msg;
135  TimerItem *i;
136 
137  while ((msg = gwlist_consume(q)) != NULL) {
138  if (++(msg->sms.resend_try) <= MAX_RETRY) {
139  i = gw_malloc(sizeof(TimerItem));
140  i->msg = msg;
143  debug("",0,"%s: Msg %p retry %ld", __func__,
144  msg, msg->sms.resend_try);
145  } else {
146  warning(0,"%s: Msg %p exceeded %d retries, discard!", __func__,
147  msg, MAX_RETRY);
148  msg_destroy(msg);
149  }
150  }
151 }
#define MAX_RETRY
Definition: test_timerset.c:67
Timer * gw_timer_create(Timerset *set, List *outputlist, void(*callback)(void *))
Definition: gw-timer.c:255
static void timer_retry_cb(void *arg)
Definition: msg.h:79
void msg_destroy(Msg *msg)
Definition: msg.c:132
void warning(int err, const char *fmt,...)
Definition: log.c:660
Timer * timer
Definition: smsbox.c:108
void * gwlist_consume(List *list)
Definition: list.c:427
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:726
void gw_timer_elapsed_start(Timer *timer, int interval, void *data)
Definition: gw-timer.c:352
#define FREQ
Definition: test_timerset.c:68
static Timerset * timerset
Definition: test_timerset.c:76
Definition: list.c:102
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86

◆ timer_consumer_thread()

static void timer_consumer_thread ( void *  arg)
static

Definition at line 87 of file test_timerset.c.

References debug(), FREQ, gw_timer_elapsed_destroy(), gw_timer_elapsed_start(), gwlist_consume(), MAX_RETRY, TimerItem::msg, msg_destroy(), TimerItem::timer, and warning().

Referenced by main().

88 {
89  List *q = arg;
90  TimerItem *i;
91 
92  while ((i = gwlist_consume(q)) != NULL) {
93  if (++(i->msg->sms.resend_try) <= MAX_RETRY) {
95  debug("",0,"%s: Msg %p retry %ld", __func__,
96  i->msg, i->msg->sms.resend_try);
97  } else {
98  warning(0,"%s: Msg %p exceeded %d retries, discard!", __func__,
99  i->msg, MAX_RETRY);
100  msg_destroy(i->msg);
102  gw_free(i);
103  }
104  }
105 }
#define MAX_RETRY
Definition: test_timerset.c:67
void msg_destroy(Msg *msg)
Definition: msg.c:132
void warning(int err, const char *fmt,...)
Definition: log.c:660
Timer * timer
Definition: smsbox.c:108
void gw_timer_elapsed_destroy(Timer *timer)
Definition: gw-timer.c:284
void * gwlist_consume(List *list)
Definition: list.c:427
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:726
void gw_timer_elapsed_start(Timer *timer, int interval, void *data)
Definition: gw-timer.c:352
#define FREQ
Definition: test_timerset.c:68
Definition: list.c:102

◆ timer_retry_cb()

static void timer_retry_cb ( void *  arg)
static

Definition at line 115 of file test_timerset.c.

References gw_timer_elapsed_destroy_cb(), gwlist_produce(), TimerItem::msg, q_retry, and TimerItem::timer.

Referenced by main(), and retry_consumer_thread().

116 {
117  TimerItem *i = arg;
118 
120 
122  gw_free(i);
123 }
void gw_timer_elapsed_destroy_cb(Timer *timer)
Definition: gw-timer.c:295
void gwlist_produce(List *list, void *item)
Definition: list.c:411
static List * q_retry
Definition: test_timerset.c:79
Timer * timer
Definition: smsbox.c:108

Variable Documentation

◆ q_retry

List* q_retry = NULL
static

Definition at line 79 of file test_timerset.c.

Referenced by main(), and timer_retry_cb().

◆ q_timer

List* q_timer = NULL
static

Definition at line 78 of file test_timerset.c.

Referenced by main().

◆ timerset

Timerset* timerset = NULL
static

Definition at line 76 of file test_timerset.c.

Referenced by main(), and retry_consumer_thread().

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