ms_msg_list.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /**
  2. * $Id$
  3. *
  4. * MSILO module
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  8. * This file is part of Kamailio, a free SIP server.
  9. *
  10. * Kamailio is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * Kamailio is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * History:
  25. * --------
  26. * 2003-03-11 major locking changes: not it uses locking.h (andrei)
  27. */
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include <stdio.h>
  31. #include "../../mem/mem.h"
  32. #include "../../mem/shm_mem.h"
  33. #include "../../dprint.h"
  34. #include "ms_msg_list.h"
  35. /**
  36. * create a new element
  37. */
  38. msg_list_el msg_list_el_new(void)
  39. {
  40. msg_list_el mle = NULL;
  41. mle = (msg_list_el)shm_malloc(sizeof(t_msg_list_el));
  42. if(mle == NULL)
  43. return NULL;
  44. mle->next = NULL;
  45. mle->prev = NULL;
  46. mle->msgid = 0;
  47. mle->flag = MS_MSG_NULL;
  48. return mle;
  49. }
  50. /**
  51. * free an element
  52. */
  53. void msg_list_el_free(msg_list_el mle)
  54. {
  55. if(mle)
  56. shm_free(mle);
  57. }
  58. /**
  59. * free a list of elements
  60. */
  61. void msg_list_el_free_all(msg_list_el mle)
  62. {
  63. msg_list_el p0, p1;
  64. if(!mle)
  65. return;
  66. p0 = mle;
  67. while(p0)
  68. {
  69. p1 = p0;
  70. p0 = p0->next;
  71. msg_list_el_free(p1);
  72. }
  73. }
  74. /**
  75. * init a list
  76. */
  77. msg_list msg_list_init(void)
  78. {
  79. msg_list ml = NULL;
  80. ml = (msg_list)shm_malloc(sizeof(t_msg_list));
  81. if(ml == NULL)
  82. return NULL;
  83. /* init locks */
  84. if (lock_init(&ml->sem_sent)==0){
  85. LM_CRIT("could not initialize a lock\n");
  86. goto clean;
  87. };
  88. if (lock_init(&ml->sem_done)==0){
  89. LM_CRIT("could not initialize a lock\n");
  90. lock_destroy(&ml->sem_sent);
  91. goto clean;
  92. };
  93. ml->nrsent = 0;
  94. ml->nrdone = 0;
  95. ml->lsent = NULL;
  96. ml->ldone = NULL;
  97. return ml;
  98. clean:
  99. shm_free(ml);
  100. return NULL;
  101. }
  102. /**
  103. * free a list
  104. */
  105. void msg_list_free(msg_list ml)
  106. {
  107. msg_list_el p0, p1;
  108. if(!ml)
  109. return;
  110. lock_destroy(&ml->sem_sent);
  111. lock_destroy(&ml->sem_done);
  112. if(ml->nrsent>0 && ml->lsent)
  113. { // free sent list
  114. p0 = ml->lsent;
  115. ml->lsent = NULL;
  116. ml->nrsent = 0;
  117. while(p0)
  118. {
  119. p1 = p0->next;
  120. msg_list_el_free(p0);
  121. p0 = p1;
  122. }
  123. }
  124. if(ml->nrdone>0 && ml->ldone)
  125. { // free done list
  126. p0 = ml->ldone;
  127. ml->ldone = NULL;
  128. ml->nrdone = 0;
  129. while(p0)
  130. {
  131. p1 = p0->next;
  132. msg_list_el_free(p0);
  133. p0 = p1;
  134. }
  135. }
  136. shm_free(ml);
  137. }
  138. /**
  139. * check if a message is in list
  140. */
  141. int msg_list_check_msg(msg_list ml, int mid)
  142. {
  143. msg_list_el p0, p1;
  144. if(!ml || mid==0)
  145. goto errorx;
  146. LM_DBG("checking msgid=%d\n", mid);
  147. lock_get(&ml->sem_sent);
  148. p0 = p1 = ml->lsent;
  149. while(p0)
  150. {
  151. if(p0->msgid==mid)
  152. goto exist;
  153. p1 = p0;
  154. p0 = p0->next;
  155. }
  156. p0 = msg_list_el_new();
  157. if(!p0)
  158. {
  159. LM_ERR("failed to create new msg elem.\n");
  160. goto error;
  161. }
  162. p0->msgid = mid;
  163. p0->flag |= MS_MSG_SENT;
  164. if(p1)
  165. {
  166. p1->next = p0;
  167. p0->prev = p1;
  168. goto done;
  169. }
  170. ml->lsent = p0;
  171. done:
  172. ml->nrsent++;
  173. lock_release(&ml->sem_sent);
  174. LM_DBG("msg added to sent list.\n");
  175. return MSG_LIST_OK;
  176. exist:
  177. lock_release(&ml->sem_sent);
  178. LM_DBG("msg already in sent list.\n");
  179. return MSG_LIST_EXIST;
  180. error:
  181. lock_release(&ml->sem_sent);
  182. errorx:
  183. return MSG_LIST_ERR;
  184. }
  185. /**
  186. * set flag for message with mid
  187. */
  188. int msg_list_set_flag(msg_list ml, int mid, int fl)
  189. {
  190. msg_list_el p0;
  191. if(ml==0 || mid==0)
  192. {
  193. LM_ERR("bad param %p / %d\n", ml, fl);
  194. goto errorx;
  195. }
  196. lock_get(&ml->sem_sent);
  197. p0 = ml->lsent;
  198. while(p0)
  199. {
  200. if(p0->msgid==mid)
  201. {
  202. p0->flag |= fl;
  203. LM_DBG("mid:%d fl:%d\n", p0->msgid, fl);
  204. goto done;
  205. }
  206. p0 = p0->next;
  207. }
  208. done:
  209. lock_release(&ml->sem_sent);
  210. return MSG_LIST_OK;
  211. errorx:
  212. return MSG_LIST_ERR;
  213. }
  214. /**
  215. * check if the messages from list were sent
  216. */
  217. int msg_list_check(msg_list ml)
  218. {
  219. msg_list_el p0;
  220. msg_list_el p1;
  221. if(!ml)
  222. goto errorx;
  223. lock_get(&ml->sem_sent);
  224. if(ml->nrsent<=0)
  225. goto done;
  226. lock_get(&ml->sem_done);
  227. p0 = ml->lsent;
  228. while(p0)
  229. {
  230. p1 = p0->next;
  231. if(p0->flag & MS_MSG_DONE || p0->flag & MS_MSG_ERRO)
  232. {
  233. LM_DBG("mid:%d got reply\n", p0->msgid);
  234. if(p0->prev)
  235. (p0->prev)->next = p0->next;
  236. else
  237. ml->lsent = p0->next;
  238. if(p0->next)
  239. (p0->next)->prev = p0->prev;
  240. ml->nrsent--;
  241. if(!ml->nrsent)
  242. ml->lsent = NULL;
  243. if(ml->ldone)
  244. (ml->ldone)->prev = p0;
  245. p0->next = ml->ldone;
  246. p0->prev = NULL;
  247. ml->ldone = p0;
  248. ml->nrdone++;
  249. }
  250. p0 = p1;
  251. }
  252. lock_release(&ml->sem_done);
  253. done:
  254. lock_release(&ml->sem_sent);
  255. return MSG_LIST_OK;
  256. errorx:
  257. return MSG_LIST_ERR;
  258. }
  259. /**
  260. * reset a list
  261. * return old list
  262. */
  263. msg_list_el msg_list_reset(msg_list ml)
  264. {
  265. msg_list_el p0;
  266. if(!ml)
  267. return NULL;
  268. lock_get(&ml->sem_done);
  269. p0 = ml->ldone;
  270. ml->ldone = NULL;
  271. ml->nrdone = 0;
  272. lock_release(&ml->sem_done);
  273. return p0;
  274. }