loader: Fix comments in struct ast_module.
[asterisk/asterisk.git] / main / manager_endpoints.c
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
20 /*! \file
21  *
22  * \brief The Asterisk Management Interface - AMI (endpoint handling)
23  *
24  * \author Joshua Colp <jcolp@digium.com>
25  * \author David M. Lee, II <dlee@digium.com>
26  *
27   */
28
29 #include "asterisk.h"
30
31 #include "asterisk/callerid.h"
32 #include "asterisk/channel.h"
33 #include "asterisk/manager.h"
34 #include "asterisk/stasis_message_router.h"
35 #include "asterisk/pbx.h"
36 #include "asterisk/stasis_endpoints.h"
37
38 static struct stasis_message_router *endpoint_router;
39
40 static void manager_endpoints_shutdown(void)
41 {
42         stasis_message_router_unsubscribe_and_join(endpoint_router);
43         endpoint_router = NULL;
44 }
45
46 static void endpoint_state_cb(void *data, struct stasis_subscription *sub,
47         struct stasis_message *message)
48 {
49         stasis_publish(ast_manager_get_topic(), message);
50 }
51
52 int manager_endpoints_init(void)
53 {
54         struct stasis_topic *endpoint_topic;
55         int ret = 0;
56
57         if (endpoint_router) {
58                 /* Already initialized */
59                 return 0;
60         }
61
62         ast_register_cleanup(manager_endpoints_shutdown);
63
64         endpoint_topic = ast_endpoint_topic_all_cached();
65         if (!endpoint_topic) {
66                 return -1;
67         }
68
69         endpoint_router = stasis_message_router_create(endpoint_topic);
70
71         if (!endpoint_router) {
72                 return -1;
73         }
74
75         ret |= stasis_message_router_add(endpoint_router, ast_endpoint_state_type(), endpoint_state_cb, NULL);
76         ret |= stasis_message_router_add(endpoint_router, ast_endpoint_contact_state_type(), endpoint_state_cb, NULL);
77
78         /* If somehow we failed to add any routes, just shut down the whole
79          * thing and fail it.
80          */
81         if (ret) {
82                 manager_endpoints_shutdown();
83                 return -1;
84         }
85
86         return 0;
87 }
88