[sr-dev] git:master: htable(k): Implement "delete" mi/rpc function.

Andreas Granig agranig at linguin.org
Fri Apr 20 16:02:11 CEST 2012


Module: sip-router
Branch: master
Commit: 2f3ff59394450fa76cdbb46db4f8f605d6ad471e
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=2f3ff59394450fa76cdbb46db4f8f605d6ad471e

Author: Richard Fuchs <rfuchs at sipwise.com>
Committer: Andreas Granig <agranig at sipwise.com>
Date:   Fri Apr 20 15:59:28 2012 +0200

htable(k): Implement "delete" mi/rpc function.

- Lets you delete specific keys from an htable via the MI or
  RPC interface.

---

 modules_k/htable/doc/htable_admin.xml |   37 ++++++++++++++++++++-
 modules_k/htable/htable.c             |   57 +++++++++++++++++++++++++++++++++
 2 files changed, 92 insertions(+), 2 deletions(-)

diff --git a/modules_k/htable/doc/htable_admin.xml b/modules_k/htable/doc/htable_admin.xml
index 09ee23c..9749c9b 100644
--- a/modules_k/htable/doc/htable_admin.xml
+++ b/modules_k/htable/doc/htable_admin.xml
@@ -620,7 +620,7 @@ sht_rm_value_re("ha=>.*");
 		_empty_line_
 		</programlisting>
     </section>
-	<section>
+    <section>
 		<title>
 		<function moreinfo="none">sht_dump</function>
 		</title>
@@ -635,12 +635,45 @@ sht_rm_value_re("ha=>.*");
  		<para>
 		MI FIFO Command Format:
 		</para>
-        <programlisting  format="linespecific">
+		<programlisting  format="linespecific">
 		:sht_dump:_reply_fifo_file_
 		_hash_table_name_
 		_empty_line_
 		</programlisting>
     </section>
+    <section>
+		<title>
+		<function moreinfo="none">sht_delete</function>
+		</title>
+		<para>
+		Delete a key from a hash table via MI.
+		</para>
+		<para>
+		Name: <emphasis>sht_delete</emphasis>
+		</para>
+		<para>Parameters:</para>
+		<itemizedlist>
+			<listitem><para><emphasis>_hash_table_name: </emphasis>The table name to delete the key from</para></listitem>
+			<listitem><para><emphasis>_key_name: </emphasis>The key to delete from the htable</para></listitem>
+		</itemizedlist>
+
+ 		<para>
+		MI FIFO Command Format:
+		</para>
+		<programlisting  format="linespecific">
+		:sht_delete:_reply_fifo_file_
+		_hash_table_name_
+		_key_name_
+		_empty_line_
+		</programlisting>
+
+ 		<para>
+		Example (note the quoting when executing it via FIFO):
+		</para>
+		<programlisting  format="linespecific">
+		kamctl fifo sht_delete auth '"user at example.org::last_auth"'
+		</programlisting>
+    </section>
     </section>
 	<section>
 	<title>Event routes</title>
diff --git a/modules_k/htable/htable.c b/modules_k/htable/htable.c
index 66bde1c..702bda9 100644
--- a/modules_k/htable/htable.c
+++ b/modules_k/htable/htable.c
@@ -66,6 +66,7 @@ int ht_param(modparam_t type, void* val);
 
 static struct mi_root* ht_mi_reload(struct mi_root* cmd_tree, void* param);
 static struct mi_root* ht_mi_dump(struct mi_root* cmd_tree, void* param);
+static struct mi_root* ht_mi_delete(struct mi_root* cmd_tree, void* param);
 
 static pv_export_t mod_pvs[] = {
 	{ {"sht", sizeof("sht")-1}, PVT_OTHER, pv_get_ht_cell, pv_set_ht_cell,
@@ -87,6 +88,7 @@ static pv_export_t mod_pvs[] = {
 static mi_export_t mi_cmds[] = {
 	{ "sht_reload",     ht_mi_reload,  0,  0,  0},
 	{ "sht_dump",       ht_mi_dump,    0,  0,  0},
+	{ "sht_delete",     ht_mi_delete,  0,  0,  0},
 	{ 0, 0, 0, 0, 0}
 };
 
@@ -414,6 +416,39 @@ static struct mi_root* ht_mi_reload(struct mi_root* cmd_tree, void* param)
 	return init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
 }
 
+static struct mi_root* ht_mi_delete(struct mi_root* cmd_tree, void* param) {
+	struct mi_node *node;
+	str *htname, *key;
+	ht_t *ht;
+
+	node = cmd_tree->node.kids;
+	if (!node)
+		goto param_err;
+
+	htname = &node->value;
+	if (!htname->len)
+		goto param_err;
+
+	node = node->next;
+	if (!node)
+		goto param_err;
+
+	key = &node->value;
+	if (!key->len)
+		goto param_err;
+
+	ht = ht_get_table(htname);
+	if (!ht)
+		return init_mi_tree(404, MI_BAD_PARM_S, MI_BAD_PARM_LEN);
+
+	ht_del_cell(ht, key);
+
+	return init_mi_tree(200, MI_OK_S, MI_OK_LEN);
+
+param_err:
+	return init_mi_tree(400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
+}
+
 static struct mi_root* ht_mi_dump(struct mi_root* cmd_tree, void* param)
 {
 	struct mi_node* node;
@@ -487,6 +522,27 @@ static const char* htable_dump_doc[2] = {
 	"Dump the contents of hash table.",
 	0
 };
+static const char* htable_delete_doc[2] = {
+	"Delete one key from a hash table.",
+	0
+};
+
+static void htable_rpc_delete(rpc_t* rpc, void* c) {
+	str htname, keyname;
+	ht_t *ht;
+
+	if (rpc->scan(c, "SS", &htname, &keyname) < 2) {
+		rpc->fault(c, 500, "Not enough parameters (htable name & key name");
+		return;
+	}
+	ht = ht_get_table(&htname);
+	if (!ht) {
+		rpc->fault(c, 500, "No such htable");
+		return;
+	}
+
+	ht_del_cell(ht, &keyname);
+}
 
 static void  htable_rpc_dump(rpc_t* rpc, void* c)
 {
@@ -568,6 +624,7 @@ error:
 
 rpc_export_t htable_rpc[] = {
 	{"htable.dump", htable_rpc_dump, htable_dump_doc, 0},
+	{"htable.delete", htable_rpc_delete, htable_delete_doc, 0},
 	{0, 0, 0, 0}
 };
 




More information about the sr-dev mailing list