manager_endpoints.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. ret |= stasis_message_router_add(endpoint_router, ast_endpoint_contact_state_type(), endpoint_state_cb, NULL);
  65. /* If somehow we failed to add any routes, just shut down the whole
  66. * thing and fail it.
  67. */
  68. if (ret) {
  69. manager_endpoints_shutdown();
  70. return -1;
  71. }
  72. return 0;
  73. }