notify.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser 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. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. *
  27. */
  28. #include "notify.h"
  29. #include "../../ut.h"
  30. #include "../../mem/shm_mem.h"
  31. #include "dlist.h"
  32. #include "udomain.h"
  33. str dom = STR_STATIC_INIT("location");
  34. void notify_watchers(struct urecord* _r, ucontact_t *_c, int state)
  35. {
  36. notify_cb_t* n;
  37. n = _r->watchers;
  38. while(n) {
  39. n->cb(&_r->uid, &_c->c, state, n->data);
  40. n = n->next;
  41. }
  42. }
  43. int add_watcher(struct urecord* _r, notcb_t _c, void* _d)
  44. {
  45. notify_cb_t* ptr;
  46. ucontact_t *c;
  47. ptr = (notify_cb_t*)shm_malloc(sizeof(notify_cb_t));
  48. if (ptr == 0) {
  49. LOG(L_ERR, "add_watcher(): No memory left\n");
  50. return -1;
  51. }
  52. ptr->cb = _c;
  53. ptr->data = _d;
  54. ptr->next = _r->watchers;
  55. _r->watchers = ptr;
  56. c = _r->contacts;
  57. while (c) {
  58. ptr->cb(&_r->uid, &c->c, PRES_ONLINE, ptr->data);
  59. c = c->next;
  60. }
  61. return 0;
  62. }
  63. int remove_watcher(struct urecord* _r, notcb_t _c, void* _d)
  64. {
  65. notify_cb_t* ptr, *prev = 0;;
  66. ptr = _r->watchers;
  67. while(ptr) {
  68. if ((ptr->cb == _c) && (ptr->data == _d)) {
  69. if (prev) prev->next = ptr->next;
  70. else _r->watchers = ptr->next;
  71. shm_free(ptr);
  72. return 0;
  73. }
  74. prev = ptr;
  75. ptr = ptr->next;
  76. }
  77. return 1;
  78. }
  79. int register_watcher(str* _f, str* _t, notcb_t _c, void* _data)
  80. {
  81. udomain_t* d;
  82. urecord_t* r;
  83. if (find_domain(&dom, &d) > 0) {
  84. LOG(L_ERR, "register_watcher(): Domain '%.*s' not found\n", dom.len, ZSW(dom.s));
  85. return -1;
  86. }
  87. lock_udomain(d);
  88. if (get_urecord(d, _t, &r) > 0) {
  89. if (insert_urecord(d, _t, &r) < 0) {
  90. unlock_udomain(d);
  91. LOG(L_ERR, "register_watcher(): Error while creating a new record\n");
  92. return -2;
  93. }
  94. }
  95. if (add_watcher(r, _c, _data) < 0) {
  96. LOG(L_ERR, "register_watcher(): Error while adding a watcher\n");
  97. release_urecord(r);
  98. unlock_udomain(d);
  99. return -3;
  100. }
  101. unlock_udomain(d);
  102. return 0;
  103. }
  104. int unregister_watcher(str* _f, str* _t, notcb_t _c, void* _data)
  105. {
  106. udomain_t* d;
  107. urecord_t* r;
  108. if (find_domain(&dom, &d) > 0) {
  109. LOG(L_ERR, "unregister_watcher(): Domain '%.*s' not found\n", dom.len, ZSW(dom.s));
  110. return -1;
  111. }
  112. lock_udomain(d);
  113. if (get_urecord(d, _t, &r) > 0) {
  114. unlock_udomain(d);
  115. DBG("unregister_watcher(): Record not found\n");
  116. return 0;
  117. }
  118. remove_watcher(r, _c, _data);
  119. release_urecord(r);
  120. unlock_udomain(d);
  121. return 0;
  122. }