<p>Jenkins2 <strong>merged</strong> this change.</p><p><a href="https://gerrit.asterisk.org/6229">View Change</a></p><div style="white-space:pre-wrap">Approvals:
  Joshua Colp: Looks good to me, but someone else must approve
  George Joseph: Looks good to me, approved
  Jenkins2: Approved for Submit

</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">manager: hook event is not being raised<br><br>When the iostream code went in it introduced a conditional that made it so the<br>hook event was not being raised even if a hook is present. This patch adds a<br>check to see if a hook is present in astman_append. If so then call into the<br>send_string function, which in turn raises the even for specified hook.<br><br>Also updated the ami hooks unit test, so the test could be automated.<br><br>ASTERISK-27200 #close<br><br>Change-Id: Iff37f02f9708195d8f23e68f959d6eab720e1e36<br>---<br>M main/manager.c<br>M tests/test_amihooks.c<br>2 files changed, 66 insertions(+), 5 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">diff --git a/main/manager.c b/main/manager.c<br>index d16a692..e67efe3 100644<br>--- a/main/manager.c<br>+++ b/main/manager.c<br>@@ -2905,14 +2905,13 @@<br>   return ret;<br> }<br> <br>-<br> /*!<br>  * helper function to send a string to the socket.<br>  * Return -1 on error (e.g. buffer full).<br>  */<br> static int send_string(struct mansession *s, char *string)<br> {<br>-  struct ast_iostream *stream = s->stream ? s->stream : s->session->stream;<br>+        struct ast_iostream *stream;<br>  int len, res;<br> <br>      /* It's a result from one of the hook's action invocation */<br>@@ -2924,6 +2923,8 @@<br>           s->hook->helper(EVENT_FLAG_HOOKRESPONSE, "HookResponse", string);<br>             return 0;<br>     }<br>+<br>+ stream = s->stream ? s->stream : s->session->stream;<br> <br>   len = strlen(string);<br>         ast_iostream_set_timeout_inactivity(stream, s->session->writetimeout);<br>@@ -2971,7 +2972,7 @@<br>           return;<br>       }<br> <br>- if (s->tcptls_session != NULL && s->tcptls_session->stream != NULL) {<br>+       if (s->hook || (s->tcptls_session != NULL && s->tcptls_session->stream != NULL)) {<br>                send_string(s, ast_str_buffer(buf));<br>  } else {<br>              ast_verbose("No connection stream in astman_append, should not happen\n");<br>diff --git a/tests/test_amihooks.c b/tests/test_amihooks.c<br>index 1297cb5..14cfbdd 100644<br>--- a/tests/test_amihooks.c<br>+++ b/tests/test_amihooks.c<br>@@ -22,8 +22,10 @@<br>  *<br>  * \author David Brooks <dbrooks@digium.com> based off of code written by Russell Bryant <russell@digium.com><br>  *<br>- * This is simply an example or test module illustrating the ability for a custom module<br>- * to hook into AMI. Registration for AMI events and sending of AMI actions is shown.<br>+ * This started, and continues to serves, as an example illustrating the ability<br>+ * for a custom module to hook into AMI. Registration for AMI events and sending<br>+ * of AMI actions is shown. A test has also been created that utilizes the original<br>+ * example in order to make sure the ami event hook gets raised.<br>  */<br> <br> /*** MODULEINFO<br>@@ -37,11 +39,66 @@<br> #include "asterisk/cli.h"<br> #include "asterisk/utils.h"<br> #include "asterisk/manager.h"<br>+#include "asterisk/test.h"<br>+<br>+#define CATEGORY "/main/amihooks/"<br>+<br>+AST_MUTEX_DEFINE_STATIC(hook_lock);<br>+ast_cond_t hook_cond;<br>+int done;<br>+<br>+static int wait_for_hook(struct ast_test *test)<br>+{<br>+ struct timeval start = ast_tvnow();<br>+  struct timespec timeout = {<br>+          .tv_sec = start.tv_sec + 2,<br>+          .tv_nsec = start.tv_usec * 1000<br>+      };<br>+   int res = 0;<br>+<br>+      ast_mutex_lock(&hook_lock);<br>+      while (!done) {<br>+              if (ast_cond_timedwait(&hook_cond, &hook_lock, &timeout) == ETIMEDOUT) {<br>+                 ast_test_status_update(test, "Test timed out while waiting for hook event\n");<br>+                     res = -1;<br>+                    break;<br>+               }<br>+    }<br>+    ast_mutex_unlock(&hook_lock);<br>+<br>+ return res;<br>+}<br>+<br>+AST_TEST_DEFINE(amihook_cli_send)<br>+{<br>+   switch (cmd) {<br>+       case TEST_INIT:<br>+              info->name = __func__;<br>+            info->category = CATEGORY;<br>+                info->summary = "Execute an action using an AMI hook";<br>+          info->description = info->summary;<br>+             return AST_TEST_NOT_RUN;<br>+     case TEST_EXECUTE:<br>+           break;<br>+       }<br>+<br>+ done = 0;<br>+    if (ast_cli_command(-1, "amihook send")) {<br>+         return AST_TEST_FAIL;<br>+        }<br>+<br>+ return wait_for_hook(test) ? AST_TEST_FAIL : AST_TEST_PASS;<br>+}<br> <br> /* The helper function is required by struct manager_custom_hook. See __manager_event for details */<br> static int amihook_helper(int category, const char *event, char *content)<br> {<br>     ast_log(LOG_NOTICE, "AMI Event: \nCategory: %d Event: %s\n%s\n", category, event, content);<br>+<br>+     ast_mutex_lock(&hook_lock);<br>+      done = 1;<br>+    ast_cond_signal(&hook_cond);<br>+     ast_mutex_unlock(&hook_lock);<br>     return 0;<br> }<br> <br>@@ -141,6 +198,7 @@<br> <br> static int unload_module(void)<br> {<br>+        AST_TEST_UNREGISTER(amihook_cli_send);<br>        ast_manager_unregister_hook(&test_hook);<br>  return ast_cli_unregister_multiple(cli_amihook_evt, ARRAY_LEN(cli_amihook_evt));<br> }<br>@@ -151,6 +209,8 @@<br> <br>  res = ast_cli_register_multiple(cli_amihook_evt, ARRAY_LEN(cli_amihook_evt));<br> <br>+     AST_TEST_REGISTER(amihook_cli_send);<br>+<br>       return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;<br> }<br> <br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/6229">change 6229</a>. To unsubscribe, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/6229"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 15 </div>
<div style="display:none"> Gerrit-MessageType: merged </div>
<div style="display:none"> Gerrit-Change-Id: Iff37f02f9708195d8f23e68f959d6eab720e1e36 </div>
<div style="display:none"> Gerrit-Change-Number: 6229 </div>
<div style="display:none"> Gerrit-PatchSet: 2 </div>
<div style="display:none"> Gerrit-Owner: Kevin Harwell <kharwell@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Jenkins2 </div>
<div style="display:none"> Gerrit-Reviewer: Joshua Colp <jcolp@digium.com> </div>