Skip to content

Commit

Permalink
lua: port MI commands to jsonrpc
Browse files Browse the repository at this point in the history
  • Loading branch information
rvlad-patrascu committed Jan 30, 2019
1 parent e40efad commit 0b9f10d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 48 deletions.
28 changes: 28 additions & 0 deletions modules/lua/doc/lua_admin.xml
Expand Up @@ -172,5 +172,33 @@ if (lua_exec("mongo_alias")) {
</para>
</section>
</section>

<section id="exported_mi_functions" xreflabel="Exported MI Functions">
<title>Exported MI Functions</title>
<section id="mi_watch" xreflabel="watch">
<title>
<function moreinfo="none">watch</function>
</title>
<para>
Name: <emphasis>watch</emphasis>
</para>
<para>Parameters: <emphasis>none</emphasis></para>
<itemizedlist>
<listitem>
<para><emphasis>action</emphasis> (optional) - 'add' or 'delete'</para>
</listitem>
<listitem>
<para><emphasis>extension</emphasis> (optional) - required if
<emphasis>action</emphasis> is provided</para>
</listitem>
</itemizedlist>
<para>MI FIFO Command Format:</para>
<programlisting format="linespecific">
opensipsctl fifo watch
</programlisting>
</section>

</section>

</chapter>

9 changes: 9 additions & 0 deletions modules/lua/siplua.c
Expand Up @@ -72,6 +72,15 @@ static mi_export_t mi_cmds[] = {
{ 0, 0, 0, 0, 0, 0 }
};

static mi_export_t mi_cmds[] = {
{ "watch", 0,0,0, {
{siplua_mi_watch, {0}},
{siplua_mi_watch_2, {"action", "extension", 0}},
{EMPTY_MI_RECIPE}}
},
{EMPTY_MI_EXPORT}
};

/*
* Module interface
*/
Expand Down
78 changes: 33 additions & 45 deletions modules/lua/sipluami.c
Expand Up @@ -29,58 +29,46 @@
#include "sipwatch.h"
#include "sipluami.h"

#define SIPLUAMI_USAGE "usage: watch [add | delete | show] [extension]"

struct mi_root *siplua_mi_reload(struct mi_root *cmd_tree, void *param)
mi_response_t *siplua_mi_watch(const mi_params_t *params,
struct mi_handler *async_hdl)
{
struct mi_root *answer;
int i;
mi_response_t *resp;
mi_item_t *resp_arr;

answer = init_mi_tree(200, "xOK", 3);
addf_mi_node_child(&answer->node, 0, "pid", 3, "%d", (int)getpid());
return answer;
}
resp = init_mi_result_object(&resp_arr);
if (!resp)
return 0;

struct mi_root *siplua_mi_bla(struct mi_root *cmd_tree, void *param)
{
return init_mi_tree(200, MI_OK_S, MI_OK_LEN);
sipwatch_lock();
for (i = 0; i < siplua_watch->nb; ++i)
if (add_mi_string_fmt(resp_arr, MI_SSTR("extension"), "%s",
siplua_watch->ext[i].str) < 0) {
sipwatch_unlock();
free_mi_response(resp);
return 0;
}

sipwatch_unlock();
return resp;
}

struct mi_root *siplua_mi_watch(struct mi_root *cmd_tree, void *param)
mi_response_t *siplua_mi_watch_2(const mi_params_t *params,
struct mi_handler *async_hdl)
{
struct mi_root *answer;
struct mi_node *node;
str action;
str action, extension;

if (get_mi_string_param(params, "action", &action.s, &action.len) < 0)
return init_mi_param_error();
if (get_mi_string_param(params, "extension", &extension.s, &extension.len) < 0)
return init_mi_param_error();

node = cmd_tree->node.kids;
if (!node)
return init_mi_tree(200, SIPLUAMI_USAGE, sizeof(SIPLUAMI_USAGE) - 1);
action = node->value;
node = node->next;
if (action.len == 3 && !strncmp("add", action.s, action.len))
{
if (!node)
return init_mi_tree(200, "usage: missing extension", 24);
sipwatch_add(node->value.s, node->value.len);
}
if (action.len == 6 && !strncmp("delete", action.s, action.len))
{
if (!node)
return init_mi_tree(200, "usage: missing extension", 24);
sipwatch_delete(node->value.s, node->value.len);
}
if (action.len == 4 && !strncmp("show", action.s, action.len))
{
int i;
sipwatch_add(extension.s, extension.len);
else if (action.len == 6 && !strncmp("delete", action.s, action.len))
sipwatch_delete(extension.s, extension.len);
else
return init_mi_error(400, MI_SSTR("Bad action, should be 'add' or 'delete'"));

answer = init_mi_tree(200, "xOK", 3);
answer->node.flags |= MI_IS_ARRAY;
sipwatch_lock();
for (i = 0; i < siplua_watch->nb; ++i)
addf_mi_node_child(&answer->node, 0, "extension", 9, "%s",
siplua_watch->ext[i].str);
sipwatch_unlock();
return answer;
}
answer = init_mi_tree(200, "xOK", 3);
return answer;
return init_mi_result_ok();
}
7 changes: 4 additions & 3 deletions modules/lua/sipluami.h
Expand Up @@ -22,8 +22,9 @@
#ifndef SIPLUAMI_H_
# define SIPLUAMI_H_

struct mi_root *siplua_mi_reload(struct mi_root *cmd_tree, void *param);
struct mi_root *siplua_mi_bla(struct mi_root *cmd_tree, void *param);
struct mi_root *siplua_mi_watch(struct mi_root *cmd_tree, void *param);
mi_response_t *siplua_mi_watch(const mi_params_t *params,
struct mi_handler *async_hdl);
mi_response_t *siplua_mi_watch_2(const mi_params_t *params,
struct mi_handler *async_hdl);

#endif /* !SIPLUAMI_H_ */

0 comments on commit 0b9f10d

Please sign in to comment.