소스 검색

modules/htable: added htable.reload rpc command

Juha Heinanen 12 년 전
부모
커밋
cc00df26d2
3개의 변경된 파일130개의 추가작업 그리고 6개의 파일을 삭제
  1. 23 6
      modules/htable/README
  2. 25 0
      modules/htable/doc/htable_admin.xml
  3. 82 0
      modules/htable/htable.c

+ 23 - 6
modules/htable/README

@@ -17,7 +17,7 @@ Alex Balashov
 
 
    <[email protected]>
    <[email protected]>
 
 
-   Copyright © 2008-2011 http://www.asipto.com
+   Copyright (c) 2008-2011 http://www.asipto.com
      __________________________________________________________________
      __________________________________________________________________
 
 
    Table of Contents
    Table of Contents
@@ -64,7 +64,8 @@ Alex Balashov
               7.1. htable.get htable key
               7.1. htable.get htable key
               7.2. htable.delete htable key
               7.2. htable.delete htable key
               7.3. htable.dump htable
               7.3. htable.dump htable
-              7.4. htable.listTables
+              7.4. htable.reload htable
+              7.5. htable.listTables
 
 
         8. Event routes
         8. Event routes
 
 
@@ -134,7 +135,8 @@ Chapter 1. Admin Guide
         7.1. htable.get htable key
         7.1. htable.get htable key
         7.2. htable.delete htable key
         7.2. htable.delete htable key
         7.3. htable.dump htable
         7.3. htable.dump htable
-        7.4. htable.listTables
+        7.4. htable.reload htable
+        7.5. htable.listTables
 
 
    8. Event routes
    8. Event routes
 
 
@@ -494,7 +496,7 @@ sht_rm_value_re("ha=>.*");
      * $shtval(htable=>key)
      * $shtval(htable=>key)
 
 
    Exported pseudo-variables are documented at
    Exported pseudo-variables are documented at
-   http://www.kamailio.org/dokuwiki/.
+   http://www.kamailio.org/wiki/.
 
 
 6. MI Commands
 6. MI Commands
 
 
@@ -552,7 +554,8 @@ sht_rm_value_re("ha=>.*");
    7.1. htable.get htable key
    7.1. htable.get htable key
    7.2. htable.delete htable key
    7.2. htable.delete htable key
    7.3. htable.dump htable
    7.3. htable.dump htable
-   7.4. htable.listTables
+   7.4. htable.reload htable
+   7.5. htable.listTables
 
 
 7.1.  htable.get htable key
 7.1.  htable.get htable key
 
 
@@ -600,7 +603,21 @@ kamcmd htable.get students anna
 kamcmd htable.dump ipban
 kamcmd htable.dump ipban
 ...
 ...
 
 
-7.4.  htable.listTables
+7.4.  htable.reload htable
+
+   Reload hash table from database.
+
+   Name: dhtable.reload
+
+   Parameters:
+     * htable : Name of the hash table to reload
+
+   Example:
+...
+kamcmd htable.reload ipban
+...
+
+7.5.  htable.listTables
 
 
    Lists all defined tables
    Lists all defined tables
 
 

+ 25 - 0
modules/htable/doc/htable_admin.xml

@@ -756,6 +756,31 @@ kamcmd htable.get students anna
 ...
 ...
 kamcmd htable.dump ipban
 kamcmd htable.dump ipban
 ...
 ...
+</programlisting>
+	</section>
+        <section>
+                <title>
+                <function moreinfo="none">htable.reload htable</function>
+                </title>
+                <para>
+		Reload hash table from database.
+                </para>
+                <para>
+                Name: <emphasis>dhtable.reload</emphasis>
+                </para>
+                <para>Parameters:</para>
+                <itemizedlist>
+                        <listitem><para>htable : Name of the hash table to reload</para>
+                        </listitem>
+
+                </itemizedlist>
+                <para>
+                Example:
+                </para>
+<programlisting  format="linespecific">
+...
+kamcmd htable.reload ipban
+...
 </programlisting>
 </programlisting>
 	</section>
 	</section>
         <section>
         <section>

+ 82 - 0
modules/htable/htable.c

@@ -534,6 +534,10 @@ static const char* htable_list_doc[2] = {
 	"List all htables.",
 	"List all htables.",
 	0
 	0
 };
 };
+static const char* htable_reload_doc[2] = {
+	"Reload hash table.",
+	0
+};
 
 
 static void htable_rpc_delete(rpc_t* rpc, void* c) {
 static void htable_rpc_delete(rpc_t* rpc, void* c) {
 	str htname, keyname;
 	str htname, keyname;
@@ -737,11 +741,89 @@ error:
 	return;
 	return;
 }
 }
 
 
+static void htable_rpc_reload(rpc_t* rpc, void* c)
+{
+	str htname;
+	ht_t *ht;
+	ht_t nht;
+	ht_cell_t *first;
+	ht_cell_t *it;
+	int i;
+
+	if(ht_db_url.len<=0) {
+		rpc->fault(c, 500, "No htable db_url");
+		return;
+	}
+	if(ht_db_init_con()!=0) {
+		rpc->fault(c, 500, "Failed to init htable db connection");
+		return;
+	}
+	if(ht_db_open_con()!=0) {
+		rpc->fault(c, 500, "Failed to open htable db connection");
+		return;
+	}
+
+	if (rpc->scan(c, "S", &htname) < 1)
+	{
+		rpc->fault(c, 500, "No htable name given");
+		return;
+	}
+	ht = ht_get_table(&htname);
+	if(ht==NULL)
+	{
+		rpc->fault(c, 500, "No such htable");
+		return;
+	}
+
+
+	memcpy(&nht, ht, sizeof(ht_t));
+	nht.entries = (ht_entry_t*)shm_malloc(nht.htsize*sizeof(ht_entry_t));
+	if(nht.entries == NULL)
+	{
+		ht_db_close_con();
+		rpc->fault(c, 500, "Mtree reload failed");
+		return;
+	}
+	memset(nht.entries, 0, nht.htsize*sizeof(ht_entry_t));
+
+	if(ht_db_load_table(&nht, &ht->dbtable, 0)<0)
+	{
+		ht_db_close_con();
+		rpc->fault(c, 500, "Mtree reload failed");
+		return;
+	}
+
+	/* replace old entries */
+	for(i=0; i<nht.htsize; i++)
+	{
+		lock_get(&ht->entries[i].lock);
+		first = ht->entries[i].first;
+		ht->entries[i].first = nht.entries[i].first;
+		ht->entries[i].esize = nht.entries[i].esize;
+		lock_release(&ht->entries[i].lock);
+		nht.entries[i].first = first;
+	}
+	/* free old entries */
+	for(i=0; i<nht.htsize; i++)
+	{
+		first = nht.entries[i].first;
+		while(first)
+		{
+			it = first;
+			first = first->next;
+			ht_cell_free(it);
+		}
+	}
+	ht_db_close_con();
+	return;
+}
+
 rpc_export_t htable_rpc[] = {
 rpc_export_t htable_rpc[] = {
 	{"htable.dump", htable_rpc_dump, htable_dump_doc, 0},
 	{"htable.dump", htable_rpc_dump, htable_dump_doc, 0},
 	{"htable.delete", htable_rpc_delete, htable_delete_doc, 0},
 	{"htable.delete", htable_rpc_delete, htable_delete_doc, 0},
 	{"htable.get", htable_rpc_get, htable_get_doc, 0},
 	{"htable.get", htable_rpc_get, htable_get_doc, 0},
 	{"htable.listTables", htable_rpc_list, htable_list_doc, 0},
 	{"htable.listTables", htable_rpc_list, htable_list_doc, 0},
+	{"htable.reload", htable_rpc_reload, htable_reload_doc, 0},
 	{0, 0, 0, 0}
 	{0, 0, 0, 0}
 };
 };