ms_msg_list.c 5.4 KB

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