manager_endpoints.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@digium.com>
  7. * David M. Lee, II <dlee@digium.com>
  8. *
  9. * See http://www.asterisk.org for more information about
  10. * the Asterisk project. Please do not directly contact
  11. * any of the maintainers of this project for assistance;
  12. * the project provides a web site, mailing lists and IRC
  13. * channels for your use.
  14. *
  15. * This program is free software, distributed under the terms of
  16. * the GNU General Public License Version 2. See the LICENSE file
  17. * at the top of the source tree.
  18. */
  19. /*! \file
  20. *
  21. * \brief The Asterisk Management Interface - AMI (endpoint handling)
  22. *
  23. * \author Joshua Colp <jcolp@digium.com>
  24. * \author David M. Lee, II <dlee@digium.com>
  25. *
  26. */
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/callerid.h"
  30. #include "asterisk/channel.h"
  31. #include "asterisk/manager.h"
  32. #include "asterisk/stasis_message_router.h"
  33. #include "asterisk/pbx.h"
  34. #include "asterisk/stasis_endpoints.h"
  35. static struct stasis_message_router *endpoint_router;
  36. static void manager_endpoints_shutdown(void)
  37. {
  38. stasis_message_router_unsubscribe_and_join(endpoint_router);
  39. endpoint_router = NULL;
  40. }
  41. static void endpoint_state_cb(void *data, struct stasis_subscription *sub,
  42. struct stasis_message *message)
  43. {
  44. stasis_publish(ast_manager_get_topic(), message);
  45. }
  46. int manager_endpoints_init(void)
  47. {
  48. struct stasis_topic *endpoint_topic;
  49. int ret = 0;
  50. if (endpoint_router) {
  51. /* Already initialized */
  52. return 0;
  53. }
  54. ast_register_cleanup(manager_endpoints_shutdown);
  55. endpoint_topic = ast_endpoint_topic_all_cached();
  56. if (!endpoint_topic) {
  57. return -1;
  58. }
  59. endpoint_router = stasis_message_router_create(endpoint_topic);
  60. if (!endpoint_router) {
  61. return -1;
  62. }
  63. ret |= stasis_message_router_add(endpoint_router, ast_endpoint_state_type(), endpoint_state_cb, NULL);
  64. /* If somehow we failed to add any routes, just shut down the whole
  65. * thing and fail it.
  66. */
  67. if (ret) {
  68. manager_endpoints_shutdown();
  69. return -1;
  70. }
  71. return 0;
  72. }