mirror of
https://github.com/asterisk/asterisk.git
synced 2025-10-13 16:21:01 +00:00
manager: hook event is not being raised
When the iostream code went in it introduced a conditional that made it so the hook event was not being raised even if a hook is present. This patch adds a check to see if a hook is present in astman_append. If so then call into the send_string function, which in turn raises the even for specified hook. Also updated the ami hooks unit test, so the test could be automated. ASTERISK-27200 #close Change-Id: Iff37f02f9708195d8f23e68f959d6eab720e1e36
This commit is contained in:
@@ -2905,14 +2905,13 @@ int ast_hook_send_action(struct manager_custom_hook *hook, const char *msg)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* helper function to send a string to the socket.
|
* helper function to send a string to the socket.
|
||||||
* Return -1 on error (e.g. buffer full).
|
* Return -1 on error (e.g. buffer full).
|
||||||
*/
|
*/
|
||||||
static int send_string(struct mansession *s, char *string)
|
static int send_string(struct mansession *s, char *string)
|
||||||
{
|
{
|
||||||
struct ast_iostream *stream = s->stream ? s->stream : s->session->stream;
|
struct ast_iostream *stream;
|
||||||
int len, res;
|
int len, res;
|
||||||
|
|
||||||
/* It's a result from one of the hook's action invocation */
|
/* It's a result from one of the hook's action invocation */
|
||||||
@@ -2925,6 +2924,8 @@ static int send_string(struct mansession *s, char *string)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
stream = s->stream ? s->stream : s->session->stream;
|
||||||
|
|
||||||
len = strlen(string);
|
len = strlen(string);
|
||||||
ast_iostream_set_timeout_inactivity(stream, s->session->writetimeout);
|
ast_iostream_set_timeout_inactivity(stream, s->session->writetimeout);
|
||||||
res = ast_iostream_write(stream, string, len);
|
res = ast_iostream_write(stream, string, len);
|
||||||
@@ -2971,7 +2972,7 @@ void astman_append(struct mansession *s, const char *fmt, ...)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (s->tcptls_session != NULL && s->tcptls_session->stream != NULL) {
|
if (s->hook || (s->tcptls_session != NULL && s->tcptls_session->stream != NULL)) {
|
||||||
send_string(s, ast_str_buffer(buf));
|
send_string(s, ast_str_buffer(buf));
|
||||||
} else {
|
} else {
|
||||||
ast_verbose("No connection stream in astman_append, should not happen\n");
|
ast_verbose("No connection stream in astman_append, should not happen\n");
|
||||||
|
@@ -22,8 +22,10 @@
|
|||||||
*
|
*
|
||||||
* \author David Brooks <dbrooks@digium.com> based off of code written by Russell Bryant <russell@digium.com>
|
* \author David Brooks <dbrooks@digium.com> based off of code written by Russell Bryant <russell@digium.com>
|
||||||
*
|
*
|
||||||
* This is simply an example or test module illustrating the ability for a custom module
|
* This started, and continues to serves, as an example illustrating the ability
|
||||||
* to hook into AMI. Registration for AMI events and sending of AMI actions is shown.
|
* for a custom module to hook into AMI. Registration for AMI events and sending
|
||||||
|
* of AMI actions is shown. A test has also been created that utilizes the original
|
||||||
|
* example in order to make sure the ami event hook gets raised.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*** MODULEINFO
|
/*** MODULEINFO
|
||||||
@@ -37,11 +39,66 @@
|
|||||||
#include "asterisk/cli.h"
|
#include "asterisk/cli.h"
|
||||||
#include "asterisk/utils.h"
|
#include "asterisk/utils.h"
|
||||||
#include "asterisk/manager.h"
|
#include "asterisk/manager.h"
|
||||||
|
#include "asterisk/test.h"
|
||||||
|
|
||||||
|
#define CATEGORY "/main/amihooks/"
|
||||||
|
|
||||||
|
AST_MUTEX_DEFINE_STATIC(hook_lock);
|
||||||
|
ast_cond_t hook_cond;
|
||||||
|
int done;
|
||||||
|
|
||||||
|
static int wait_for_hook(struct ast_test *test)
|
||||||
|
{
|
||||||
|
struct timeval start = ast_tvnow();
|
||||||
|
struct timespec timeout = {
|
||||||
|
.tv_sec = start.tv_sec + 2,
|
||||||
|
.tv_nsec = start.tv_usec * 1000
|
||||||
|
};
|
||||||
|
int res = 0;
|
||||||
|
|
||||||
|
ast_mutex_lock(&hook_lock);
|
||||||
|
while (!done) {
|
||||||
|
if (ast_cond_timedwait(&hook_cond, &hook_lock, &timeout) == ETIMEDOUT) {
|
||||||
|
ast_test_status_update(test, "Test timed out while waiting for hook event\n");
|
||||||
|
res = -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ast_mutex_unlock(&hook_lock);
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
AST_TEST_DEFINE(amihook_cli_send)
|
||||||
|
{
|
||||||
|
switch (cmd) {
|
||||||
|
case TEST_INIT:
|
||||||
|
info->name = __func__;
|
||||||
|
info->category = CATEGORY;
|
||||||
|
info->summary = "Execute an action using an AMI hook";
|
||||||
|
info->description = info->summary;
|
||||||
|
return AST_TEST_NOT_RUN;
|
||||||
|
case TEST_EXECUTE:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
done = 0;
|
||||||
|
if (ast_cli_command(-1, "amihook send")) {
|
||||||
|
return AST_TEST_FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return wait_for_hook(test) ? AST_TEST_FAIL : AST_TEST_PASS;
|
||||||
|
}
|
||||||
|
|
||||||
/* The helper function is required by struct manager_custom_hook. See __manager_event for details */
|
/* The helper function is required by struct manager_custom_hook. See __manager_event for details */
|
||||||
static int amihook_helper(int category, const char *event, char *content)
|
static int amihook_helper(int category, const char *event, char *content)
|
||||||
{
|
{
|
||||||
ast_log(LOG_NOTICE, "AMI Event: \nCategory: %d Event: %s\n%s\n", category, event, content);
|
ast_log(LOG_NOTICE, "AMI Event: \nCategory: %d Event: %s\n%s\n", category, event, content);
|
||||||
|
|
||||||
|
ast_mutex_lock(&hook_lock);
|
||||||
|
done = 1;
|
||||||
|
ast_cond_signal(&hook_cond);
|
||||||
|
ast_mutex_unlock(&hook_lock);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,6 +198,7 @@ static struct ast_cli_entry cli_amihook_evt[] = {
|
|||||||
|
|
||||||
static int unload_module(void)
|
static int unload_module(void)
|
||||||
{
|
{
|
||||||
|
AST_TEST_UNREGISTER(amihook_cli_send);
|
||||||
ast_manager_unregister_hook(&test_hook);
|
ast_manager_unregister_hook(&test_hook);
|
||||||
return ast_cli_unregister_multiple(cli_amihook_evt, ARRAY_LEN(cli_amihook_evt));
|
return ast_cli_unregister_multiple(cli_amihook_evt, ARRAY_LEN(cli_amihook_evt));
|
||||||
}
|
}
|
||||||
@@ -151,6 +209,8 @@ static int load_module(void)
|
|||||||
|
|
||||||
res = ast_cli_register_multiple(cli_amihook_evt, ARRAY_LEN(cli_amihook_evt));
|
res = ast_cli_register_multiple(cli_amihook_evt, ARRAY_LEN(cli_amihook_evt));
|
||||||
|
|
||||||
|
AST_TEST_REGISTER(amihook_cli_send);
|
||||||
|
|
||||||
return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
|
return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user