Explorar o código

htable Add RPC command for listing one item

Also, add documentation for existing RPC commands.
Olle E. Johansson %!s(int64=12) %!d(string=hai) anos
pai
achega
6ff072a307
Modificáronse 3 ficheiros con 223 adicións e 7 borrados
  1. 71 7
      modules_k/htable/README
  2. 86 0
      modules_k/htable/doc/htable_admin.xml
  3. 66 0
      modules_k/htable/htable.c

+ 71 - 7
modules_k/htable/README

@@ -59,9 +59,15 @@ Alex Balashov
               6.2. sht_dump
               6.3. sht_delete
 
-        7. Event routes
+        7. Exported RPC Commands
 
-              7.1. htable:mod-init
+              7.1. htable.get
+              7.2. htable.delete
+              7.3. htable.dump
+
+        8. Event routes
+
+              8.1. htable:mod-init
 
    List of Examples
 
@@ -122,9 +128,15 @@ Chapter 1. Admin Guide
         6.2. sht_dump
         6.3. sht_delete
 
-   7. Event routes
+   7. Exported RPC Commands
+
+        7.1. htable.get
+        7.2. htable.delete
+        7.3. htable.dump
+
+   8. Event routes
 
-        7.1. htable:mod-init
+        8.1. htable:mod-init
 
 1. Overview
 
@@ -533,11 +545,63 @@ sht_rm_value_re("ha=>.*");
    Example (note the quoting when executing it via FIFO):
                 kamctl fifo sht_delete auth '"[email protected]::last_auth"'
 
-7. Event routes
+7. Exported RPC Commands
+
+   7.1. htable.get
+   7.2. htable.delete
+   7.3. htable.dump
+
+7.1.  htable.get
+
+   Lists one value in a hash table
+
+   Name: htable.get
+
+   Parameters:
+     * name : Name of the hash table to dump
+     * key : Key name of the hash table value to dump
+
+   Example:
+...
+# Dump $sht(students=>daniel)
+kamcmd htable.get students daniel
+...
+
+7.2.  htable.delete
+
+   Delete one value in a hash table
+
+   Name: htable.delete
+
+   Parameters:
+     * name : Name of the hash table to delete
+     * key : Key name of the hash table value to delete
+
+   Example:
+...
+# Delete $sht(students=>anna)
+kamcmd htable.get students anna
+...
+
+7.3.  htable.dump
+
+   Lists all the values in a hash table
+
+   Name: dhtable.dump
+
+   Parameters:
+     * name : Name of the hash table to dump
+
+   Example:
+...
+kamcmd htable.dump ipban
+...
+
+8. Event routes
 
-   7.1. htable:mod-init
+   8.1. htable:mod-init
 
-7.1.  htable:mod-init
+8.1.  htable:mod-init
 
    When defined, the module calls event_route[htable:mod-init] after all
    modules have been initialized. A typical use case is to initialise

+ 86 - 0
modules_k/htable/doc/htable_admin.xml

@@ -675,6 +675,92 @@ sht_rm_value_re("ha=>.*");
 		</programlisting>
     </section>
     </section>
+	<section>
+        <title>Exported RPC Commands</title>
+        <section>
+                <title>
+                <function moreinfo="none">htable.get</function>
+                </title>
+                <para>
+		Lists one value in a hash table
+                </para>
+                <para>
+                Name: <emphasis>htable.get</emphasis>
+                </para>
+                <para>Parameters:</para>
+                <itemizedlist>
+                        <listitem><para>name : Name of the hash table to dump</para>
+                        </listitem>
+                        <listitem><para>key : Key name of the hash table value to dump</para>
+                        </listitem>
+
+                </itemizedlist>
+                <para>
+                Example:
+                </para>
+<programlisting  format="linespecific">
+...
+# Dump $sht(students=>daniel)
+kamcmd htable.get students daniel
+...
+</programlisting>
+	</section>
+        <section>
+                <title>
+                <function moreinfo="none">htable.delete</function>
+                </title>
+                <para>
+		Delete one value in a hash table
+                </para>
+                <para>
+                Name: <emphasis>htable.delete</emphasis>
+                </para>
+                <para>Parameters:</para>
+                <itemizedlist>
+                        <listitem><para>name : Name of the hash table to delete</para>
+                        </listitem>
+                        <listitem><para>key : Key name of the hash table value to delete</para>
+                        </listitem>
+
+                </itemizedlist>
+                <para>
+                Example:
+                </para>
+<programlisting  format="linespecific">
+...
+# Delete $sht(students=>anna)
+kamcmd htable.get students anna
+...
+</programlisting>
+	</section>
+        <section>
+                <title>
+                <function moreinfo="none">htable.dump</function>
+                </title>
+                <para>
+		Lists all the values in a hash table
+                </para>
+                <para>
+                Name: <emphasis>dhtable.dump</emphasis>
+                </para>
+                <para>Parameters:</para>
+                <itemizedlist>
+                        <listitem><para>name : Name of the hash table to dump</para>
+                        </listitem>
+
+                </itemizedlist>
+                <para>
+                Example:
+                </para>
+<programlisting  format="linespecific">
+...
+kamcmd htable.dump ipban
+...
+</programlisting>
+	</section>
+
+	</section><!-- RPC commands -->
+
 	<section>
 	<title>Event routes</title>
 	<section>

+ 66 - 0
modules_k/htable/htable.c

@@ -526,6 +526,10 @@ static const char* htable_delete_doc[2] = {
 	"Delete one key from a hash table.",
 	0
 };
+static const char* htable_get_doc[2] = {
+	"Get one key from a hash table.",
+	0
+};
 
 static void htable_rpc_delete(rpc_t* rpc, void* c) {
 	str htname, keyname;
@@ -544,6 +548,67 @@ static void htable_rpc_delete(rpc_t* rpc, void* c) {
 	ht_del_cell(ht, &keyname);
 }
 
+/*! \brief RPC htable.get command to get one item */
+static void htable_rpc_get(rpc_t* rpc, void* c) {
+	str htname, keyname;
+	ht_t *ht;
+	ht_cell_t *htc;	/*!< One HT cell */
+	void* th;
+	void* ih;
+	void* vh;
+
+	if (rpc->scan(c, "SS", &htname, &keyname) < 2) {
+		rpc->fault(c, 500, "Not enough parameters (htable name and key name)");
+		return;
+	}
+
+	/* Find the htable */
+	ht = ht_get_table(&htname);
+	if (!ht) {
+		rpc->fault(c, 500, "No such htable");
+		return;
+	}
+
+	/* Find the  cell */
+	htc = ht_cell_pkg_copy(ht, &keyname, NULL);
+	if(htc == NULL) {
+		/* Print error message */
+		rpc->fault(c, 500, "Key name doesn't exist in htable.");
+		return;
+	}
+
+	/* add entry node */
+	if (rpc->add(c, "{", &th) < 0) {
+		rpc->fault(c, 500, "Internal error creating rpc");
+		goto error;
+	}
+
+	if(rpc->struct_add(th, "{", "item", &vh)<0) {
+		rpc->fault(c, 500, "Internal error creating rpc");
+		goto error;
+	}
+
+	if(htc->flags&AVP_VAL_STR) {
+		if(rpc->struct_add(vh, "SS", "name",  &htc->name.s, "value", &htc->value.s)<0)
+		{
+			rpc->fault(c, 500, "Internal error adding item");
+			goto error;
+		}
+	} else {
+		if(rpc->struct_add(vh, "Sd", "name",  &htc->name.s, "value", (int)htc->value.n))
+		{
+			rpc->fault(c, 500, "Internal error adding item");
+			goto error;
+		}
+	}
+	
+error:
+	/* Release the allocated memory */
+	ht_cell_pkg_free(htc);
+
+	return;
+}
+
 static void  htable_rpc_dump(rpc_t* rpc, void* c)
 {
 	str htname;
@@ -625,6 +690,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},
+	{"htable.get", htable_rpc_get, htable_get_doc, 0},
 	{0, 0, 0, 0}
 };