async_mod.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**
  2. * $Id$
  3. *
  4. * Copyright (C) 2011 Daniel-Constantin Mierla (asipto.com)
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * This file is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. *
  14. * This file is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. */
  24. #include <stdio.h>
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include "../../sr_module.h"
  29. #include "../../dprint.h"
  30. #include "../../ut.h"
  31. #include "../../pvar.h"
  32. #include "../../timer_proc.h"
  33. #include "../../route_struct.h"
  34. #include "../../modules/tm/tm_load.h"
  35. #include "async_sleep.h"
  36. MODULE_VERSION
  37. static int async_workers = 1;
  38. static int mod_init(void);
  39. static int child_init(int);
  40. static void mod_destroy(void);
  41. static int w_async_sleep(struct sip_msg* msg, char* sec, char* str2);
  42. static int fixup_async_sleep(void** param, int param_no);
  43. static int w_async_route(struct sip_msg* msg, char* rt, char* sec);
  44. static int fixup_async_route(void** param, int param_no);
  45. /* tm */
  46. struct tm_binds tmb;
  47. static cmd_export_t cmds[]={
  48. {"async_route", (cmd_function)w_async_route, 2, fixup_async_route,
  49. 0, REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  50. {"async_sleep", (cmd_function)w_async_sleep, 1, fixup_async_sleep,
  51. 0, REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  52. {0, 0, 0, 0, 0, 0}
  53. };
  54. static param_export_t params[]={
  55. {"workers", INT_PARAM, &async_workers},
  56. {0, 0, 0}
  57. };
  58. struct module_exports exports = {
  59. "async",
  60. DEFAULT_DLFLAGS, /* dlopen flags */
  61. cmds,
  62. params,
  63. 0,
  64. 0, /* exported MI functions */
  65. 0, /* exported pseudo-variables */
  66. 0, /* extra processes */
  67. mod_init, /* module initialization function */
  68. 0, /* response function */
  69. mod_destroy, /* destroy function */
  70. child_init /* per child init function */
  71. };
  72. /**
  73. * init module function
  74. */
  75. static int mod_init(void)
  76. {
  77. if (load_tm_api( &tmb ) == -1)
  78. {
  79. LM_ERR("cannot load the TM-functions\n");
  80. return -1;
  81. }
  82. if(async_init_timer_list()<0) {
  83. LM_ERR("cannot initialize internal structure\n");
  84. return -1;
  85. }
  86. register_dummy_timers(async_workers);
  87. return 0;
  88. }
  89. /**
  90. * @brief Initialize async module children
  91. */
  92. static int child_init(int rank)
  93. {
  94. if (rank!=PROC_MAIN)
  95. return 0;
  96. if(fork_dummy_timer(PROC_TIMER, "ASYNC MOD TIMER", 1 /*socks flag*/,
  97. async_timer_exec, NULL, 1 /*sec*/)<0) {
  98. LM_ERR("failed to register timer routine as process\n");
  99. return -1; /* error */
  100. }
  101. return 0;
  102. }
  103. /**
  104. * destroy module function
  105. */
  106. static void mod_destroy(void)
  107. {
  108. async_destroy_timer_list();
  109. }
  110. static int w_async_sleep(struct sip_msg* msg, char* sec, char* str2)
  111. {
  112. int s;
  113. async_param_t *ai;
  114. if(msg==NULL)
  115. return -1;
  116. ai = (async_param_t*)sec;
  117. if(fixup_get_ivalue(msg, ai->pinterval, &s)!=0)
  118. {
  119. LM_ERR("no async sleep time value\n");
  120. return -1;
  121. }
  122. if(ai->type==0)
  123. {
  124. if(async_sleep(msg, s, ai->u.paction)<0)
  125. return -1;
  126. /* force exit in config */
  127. return 0;
  128. }
  129. return -1;
  130. }
  131. static int fixup_async_sleep(void** param, int param_no)
  132. {
  133. async_param_t *ap;
  134. if(param_no!=1)
  135. return 0;
  136. ap = (async_param_t*)pkg_malloc(sizeof(async_param_t));
  137. if(ap==NULL)
  138. {
  139. LM_ERR("no more pkg\n");
  140. return -1;
  141. }
  142. memset(ap, 0, sizeof(async_param_t));
  143. ap->u.paction = get_action_from_param(param, param_no);
  144. if(fixup_igp_null(param, param_no)<0)
  145. return -1;
  146. ap->pinterval = (gparam_t*)(*param);
  147. *param = (void*)ap;
  148. return 0;
  149. }
  150. static int w_async_route(struct sip_msg* msg, char* rt, char* sec)
  151. {
  152. cfg_action_t *act;
  153. int s;
  154. str rn;
  155. int ri;
  156. if(msg==NULL)
  157. return -1;
  158. if(fixup_get_svalue(msg, (gparam_t*)rt, &rn)!=0)
  159. {
  160. LM_ERR("no async route block name\n");
  161. return -1;
  162. }
  163. if(fixup_get_ivalue(msg, (gparam_t*)sec, &s)!=0)
  164. {
  165. LM_ERR("no async interval value\n");
  166. return -1;
  167. }
  168. ri = route_get(&main_rt, rn.s);
  169. if(ri<0)
  170. {
  171. LM_ERR("unable to find route block [%.*s]\n", rn.len, rn.s);
  172. return -1;
  173. }
  174. act = main_rt.rlist[ri];
  175. if(act==NULL)
  176. {
  177. LM_ERR("empty action lists in route block [%.*s]\n", rn.len, rn.s);
  178. return -1;
  179. }
  180. if(async_sleep(msg, s, act)<0)
  181. return -1;
  182. /* force exit in config */
  183. return 0;
  184. }
  185. static int fixup_async_route(void** param, int param_no)
  186. {
  187. if(param_no==1)
  188. {
  189. if(fixup_spve_null(param, 1)<0)
  190. return -1;
  191. return 0;
  192. } else if(param_no==2) {
  193. if(fixup_igp_null(param, 1)<0)
  194. return -1;
  195. }
  196. return 0;
  197. }