speechd-discuss
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 1/1] use GList instead of GHashTable for the output_modules


From: Andrei Kholodnyi
Subject: [PATCH 1/1] use GList instead of GHashTable for the output_modules
Date: Sun, 14 Nov 2010 00:00:32 +0100

there is no need to implement a hash table for the output_modules with
module names as keys.
output_modules keeps OutputModule variables where module name
defines as a parameter in it. We can at any time retrieve it.
Also we want to keep modules ordering in the same way it exists in conf file
and it is impossible to do with GHashTable.
---
 src/server/configuration.c |    4 +-
 src/server/module.c        |    5 +++-
 src/server/output.c        |   23 +++++++++++++-------
 src/server/parse.c         |    8 +++---
 src/server/speechd.c       |   48 ++++++++++++++++++++++----------------------
 src/server/speechd.h       |    6 ++--
 6 files changed, 52 insertions(+), 42 deletions(-)

diff --git a/src/server/configuration.c b/src/server/configuration.c
index 7612b8c..6306015 100644
--- a/src/server/configuration.c
+++ b/src/server/configuration.c
@@ -298,9 +298,9 @@ DOTCONF_CB(cb_AddModule)
         return NULL;
     }
 
-    MSG(5,"Module name=%s being inserted into hash table", cur_mod->name);
     assert(cur_mod->name != NULL);
-    g_hash_table_insert(output_modules, g_strdup(module_name), cur_mod);
+    output_modules = g_list_append(output_modules, cur_mod);
+    MSG(5,"Module name=%s being inserted into modules list", cur_mod->name);
 
     g_free(module_dbgfile);
     g_free(module_name);
diff --git a/src/server/module.c b/src/server/module.c
index fce6efc..78cb4c6 100644
--- a/src/server/module.c
+++ b/src/server/module.c
@@ -276,6 +276,7 @@ int
 reload_output_module(OutputModule *old_module)
 {
     OutputModule *new_module;
+    int pos;
 
     assert(old_module != NULL); assert(old_module->name != NULL);
 
@@ -296,7 +297,9 @@ reload_output_module(OutputModule *old_module)
         return -1;
     }
 
-    g_hash_table_replace(output_modules, new_module->name, new_module);
+    pos = g_list_index(output_modules, old_module);
+    output_modules = g_list_remove(output_modules, old_module);
+    output_modules = g_list_insert(output_modules, new_module, pos);
     destroy_module(old_module);
 
     return 0;
diff --git a/src/server/output.c b/src/server/output.c
index bb7f8ea..caf5403 100644
--- a/src/server/output.c
+++ b/src/server/output.c
@@ -83,10 +83,19 @@ OutputModule*
 get_output_module_by_name(char *name)
 {
   OutputModule *output;
-  output = g_hash_table_lookup(output_modules, name);
-  if (output == NULL || !output->working) output = NULL;
+  int i;
+
+  for (i = 0; i < g_list_length(output_modules); i++) {
+      output = g_list_nth_data(output_modules, i);
+      if(!strcmp(output->name, name)) {
+         if(output->working)
+             return output;
+         else
+             return NULL;
+      }
+  }
   
-  return output;
+  return NULL;
 }
 
 /* get_output_module tries to return a pointer to the
@@ -106,7 +115,6 @@ OutputModule*
 get_output_module(const TSpeechDMessage *message)
 {
   OutputModule *output = NULL;
-  GList *gl;
   int i, len;
 
   if (message->settings.output_module != NULL){
@@ -126,15 +134,14 @@ get_output_module(const TSpeechDMessage *message)
   MSG(3, "Couldn't load default output module, trying other modules");
 
   /* Try all other output modules other than dummy */
-  gl = g_hash_table_get_values(output_modules);
-  len = g_list_length(gl);
+  len = g_list_length(output_modules);
   for (i = 0; i < len; i++) {
-      output = g_list_nth_data(gl, i);
+      output = g_list_nth_data(output_modules, i);
       if (0 == strcmp(output->name, "dummy"))
            continue;
 
       if (output->working) {
-          MSG(3, "Output module %s seems to be working, using it", gl->data);
+          MSG(3, "Output module %s seems to be working, using it", 
output->name);
           return output;
       }
   }
diff --git a/src/server/parse.c b/src/server/parse.c
index 8ca9848..730a5c7 100644
--- a/src/server/parse.c
+++ b/src/server/parse.c
@@ -834,13 +834,13 @@ parse_list(const char* buf, const int bytes, const int 
fd, const TSpeechDSock *s
     }else if(TEST_CMD(list_type, "output_modules")){
         GString *result = g_string_new("");
        char *helper;
-       GList *gl = g_hash_table_get_keys(output_modules);
+       OutputModule *mod;
        int i, len;
 
-       len = g_list_length(gl);
+       len = g_list_length(output_modules);
        for (i = 0; i < len; i++) {
-           g_string_append_printf(result, C_OK_MODULES "-%s\r\n",
-           (char *)g_list_nth_data(gl, i));
+           mod = g_list_nth_data(output_modules, i);
+           g_string_append_printf(result, C_OK_MODULES "-%s\r\n", mod->name);
        }
 
        g_string_append(result, OK_MODULES_LIST_SENT);
diff --git a/src/server/speechd.c b/src/server/speechd.c
index e1a316e..10cf20c 100644
--- a/src/server/speechd.c
+++ b/src/server/speechd.c
@@ -443,27 +443,27 @@ speechd_client_terminate(gpointer key, gpointer value, 
gpointer user)
 
 /* --- OUTPUT MODULES MANAGING --- */
 
-gboolean
-speechd_modules_terminate(gpointer key, gpointer value, gpointer user)
+void
+speechd_modules_terminate(gpointer data, gpointer user_data)
 {
     OutputModule *module;
 
-    module = (OutputModule *) value;
+    module = (OutputModule *) data;
     if (module == NULL){
         MSG(2,"Error: Empty module, internal error");
-        return TRUE;
+        return;
     }
     unload_output_module(module);                     
 
-    return TRUE;
+    return;
 }
 
 void
-speechd_modules_reload(gpointer key, gpointer value, gpointer user)
+speechd_modules_reload(gpointer data, gpointer user_data)
 {
     OutputModule *module;
 
-    module = (OutputModule *) value;
+    module = (OutputModule *) data;
     if (module == NULL){
         MSG(2,"Empty module, internal error");
         return;
@@ -475,11 +475,11 @@ speechd_modules_reload(gpointer key, gpointer value, 
gpointer user)
 }
 
 void
-speechd_module_debug(gpointer key, gpointer value, gpointer user)
+speechd_module_debug(gpointer data, gpointer user_data)
 {
     OutputModule *module;
 
-    module = (OutputModule *) value;
+    module = (OutputModule *) data;
     if (module == NULL){
         MSG(2,"Empty module, internal error");
         return;
@@ -492,11 +492,11 @@ speechd_module_debug(gpointer key, gpointer value, 
gpointer user)
 
 
 void
-speechd_module_nodebug(gpointer key, gpointer value, gpointer user)
+speechd_module_nodebug(gpointer data, gpointer user_data)
 {
     OutputModule *module;
 
-    module = (OutputModule *) value;
+    module = (OutputModule *) data;
     if (module == NULL){
         MSG(2,"Empty module, internal error");
         return;
@@ -511,7 +511,7 @@ void
 speechd_reload_dead_modules(int sig)
 {
     /* Reload dead modules */
-    g_hash_table_foreach(output_modules, speechd_modules_reload, NULL);
+    g_list_foreach(output_modules, speechd_modules_reload, NULL);
 
     /* Make sure there aren't any more child processes left */
     while(waitpid(-1, NULL, WNOHANG) > 0);    
@@ -521,7 +521,7 @@ void
 speechd_modules_debug(void)
 {
     /* Redirect output to debug for all modules */
-    g_hash_table_foreach(output_modules, speechd_module_debug, NULL);
+    g_list_foreach(output_modules, speechd_module_debug, NULL);
 
 }
 
@@ -529,7 +529,7 @@ void
 speechd_modules_nodebug(void)
 {
     /* Redirect output to normal for all modules */
-    g_hash_table_foreach(output_modules, speechd_module_nodebug, NULL);
+    g_list_foreach(output_modules, speechd_module_nodebug, NULL);
 }
 
 
@@ -592,9 +592,6 @@ speechd_init()
     language_default_modules = g_hash_table_new(g_str_hash, g_str_equal);
     assert(language_default_modules != NULL);
 
-    output_modules = g_hash_table_new(g_str_hash, g_str_equal);
-    assert(output_modules != NULL);
-
     speechd_sockets_status_init();
 
     pause_requested = 0;
@@ -641,12 +638,12 @@ speechd_init()
     logging_init();
 
     /* Check for output modules */
-    if (g_hash_table_size(output_modules) == 0){
+    if (g_list_length(output_modules) == 0){
         DIE("No speech output modules were loaded - aborting...");
     }else{
         MSG(3,"Speech Dispatcher started with %d output module%s",
-            g_hash_table_size(output_modules),
-            g_hash_table_size(output_modules) > 1 ? "s" : "" );
+            g_list_length(output_modules),
+            g_list_length(output_modules) > 1 ? "s" : "" );
     }
 
 
@@ -659,8 +656,11 @@ speechd_load_configuration(int sig)
     configfile_t *configfile = NULL;
 
     /* Clean previous configuration */
-    assert(output_modules != NULL);
-    g_hash_table_foreach_remove(output_modules, speechd_modules_terminate, 
NULL);
+    if (output_modules != NULL) {
+        g_list_foreach(output_modules, speechd_modules_terminate, NULL);
+        g_list_free(output_modules);
+       output_modules = NULL;
+    }
 
     /* Make sure there aren't any more child processes left */
     while(waitpid(-1, NULL, WNOHANG) > 0);    
@@ -710,8 +710,8 @@ speechd_quit(int sig)
 
     MSG(2,"Closing open output modules...");
     /*  Call the close() function of each registered output module. */
-    g_hash_table_foreach_remove(output_modules, speechd_modules_terminate, 
NULL);
-    g_hash_table_destroy(output_modules);
+    g_list_foreach(output_modules, speechd_modules_terminate, NULL);
+    g_list_free(output_modules);
     
     MSG(2,"Closing server connection...");
     if(close(server_socket) == -1)
diff --git a/src/server/speechd.h b/src/server/speechd.h
index 08fc821..929301e 100644
--- a/src/server/speechd.h
+++ b/src/server/speechd.h
@@ -187,7 +187,7 @@ key_t speaking_sem_key;
 int speaking_sem_id;
 
 /* Table of all configured (and succesfully loaded) output modules */
-GHashTable *output_modules;    
+GList *output_modules;
 
 /* Table of settings for each active client (=each active socket)*/
 GHashTable *fd_settings;       
@@ -261,8 +261,8 @@ char* spd_get_path(char *filename, char* startdir);
 int speechd_connection_new(int server_socket);
 int speechd_connection_destroy(int fd);
 gboolean speechd_client_terminate(gpointer key, gpointer value, gpointer user);
-gboolean speechd_modules_terminate(gpointer key, gpointer value, gpointer 
user);
-void speechd_modules_reload(gpointer key, gpointer value, gpointer user);
+void speechd_modules_terminate(gpointer data, gpointer user_data);
+void speechd_modules_reload(gpointer data, gpointer user_data);
 void speechd_modules_debug(void);
 void speechd_modules_nodebug(void);
 
-- 
1.6.0.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]