speechd-discuss
[Top][All Lists]
Advanced

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

[PATCH 2/3] server - Implement shutdown timer


From: Luke Yelavich
Subject: [PATCH 2/3] server - Implement shutdown timer
Date: Thu, 30 Jul 2015 10:15:24 +1000

From: Luke Yelavich <address@hidden>
To: address@hidden

When the -t flag is given on the command-line along with a value greater than
0, the Speech Dispatcher server will automatically shut itself down after
the specified number of seconds, if no clients are connected.
---
 src/server/options.c | 11 +++++++++--
 src/server/speechd.c | 29 +++++++++++++++++++++++++++++
 src/server/speechd.h |  2 ++
 3 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/src/server/options.c b/src/server/options.c
index c88195e..42b2939 100644
--- a/src/server/options.c
+++ b/src/server/options.c
@@ -50,10 +50,11 @@ static const struct option spd_long_options[] = {
        {"version", 0, 0, 'v'},
        {"debug", 0, 0, 'D'},
        {"help", 0, 0, 'h'},
+       {"timeout", 1, 0, 't'},
        {0, 0, 0, 0}
 };
 
-static const char *const spd_short_options = "dsal:L:c:S:p:P:C:vDh";
+static const char *const spd_short_options = "dsal:L:c:S:p:P:C:t:vDh";
 
 void options_print_help(char *argv[])
 {
@@ -62,7 +63,7 @@ void options_print_help(char *argv[])
 
        printf(_("Usage: "));
        printf
-           ("%s [-{d|s}] [-l {1|2|3|4|5}] [-c com_method] [-S socket_path] [-p 
port] | [-v] | [-h]\n",
+           ("%s [-{d|s}] [-l {1|2|3|4|5}] [-c com_method] [-S socket_path] [-p 
port] [-t timeout] | [-v] | [-h]\n",
             argv[0]);
        printf(_("%s -- Common interface for Speech Synthesis %s\n\n"),
               "Speech Dispatcher", "(GNU GPL)");
@@ -85,6 +86,9 @@ void options_print_help(char *argv[])
               "unix_socket", "default");
        printf("-p, --port\t\t");
        printf(_("Specify a port number for '%s' method\n"), "inet_socket");
+       printf("-t, --timeout\t\t");
+       printf(_("Set time in seconds for the server to wait before it shuts 
down,\n\t\t\t"));
+       printf(_("if it has no clients connected\n"));
        printf("-P, --pid-file\t\t");
        printf(_("Set path to pid file\n"));
        printf("-C, --config-dir\t");
@@ -229,6 +233,9 @@ void options_parse(int argc, char *argv[])
                        options_print_help(argv);
                        exit(0);
                        break;
+               case 't':
+                       SPD_OPTION_SET_INT(server_timeout);
+                       break;
                default:
                        MSG(2, "Unrecognized option\n");
                        options_print_help(argv);
diff --git a/src/server/speechd.c b/src/server/speechd.c
index be5efcb..a515ccb 100644
--- a/src/server/speechd.c
+++ b/src/server/speechd.c
@@ -59,6 +59,9 @@ void destroy_pid_file();
 int server_socket;
 
 GMainLoop *main_loop = NULL;
+gint server_timeout_source = 0;
+
+int client_count = 0;
 
 static gboolean speechd_client_terminate(gpointer key, gpointer value, 
gpointer user);
 static gboolean speechd_reload_dead_modules(gpointer user_data);
@@ -73,6 +76,8 @@ static gboolean client_process_incoming (gint          fd,
                                  GIOCondition  condition,
                                  gpointer      data);
 
+void check_client_count(void);
+
 #ifdef __SUNPRO_C
 /* Added by Willie Walker - daemon is a gcc-ism
  */
@@ -392,6 +397,10 @@ int speechd_connection_new(int server_socket)
        new_fd_set->fd_source = g_unix_fd_add(client_socket, G_IO_IN, 
client_process_incoming, NULL);
 
        MSG(4, "Data structures for client on fd %d created", client_socket);
+
+       client_count++;
+       check_client_count();
+
        return 0;
 
 }
@@ -433,6 +442,9 @@ int speechd_connection_destroy(int fd)
 
        MSG(4, "Connection closed");
 
+       client_count--;
+       check_client_count();
+
        return 0;
 }
 
@@ -945,6 +957,21 @@ gboolean client_process_incoming (gint          fd,
        return TRUE;
 }
 
+void check_client_count(void)
+{
+       if (client_count <= 0
+           && SpeechdOptions.server_timeout_set) {
+               MSG(3, "Currently no clients connected, enabling shutdown 
timer.");
+               server_timeout_source = 
+                                       g_timeout_add_seconds(
+                                       SpeechdOptions.server_timeout,
+                                       speechd_quit, NULL);
+       } else {
+       MSG(3, "Clients connected, disabling shutdown timer.");
+               g_source_remove(server_timeout_source);
+       }
+}
+
 /* --- MAIN --- */
 
 int main(int argc, char *argv[])
@@ -1223,6 +1250,8 @@ int main(int argc, char *argv[])
        /* Now wait for clients and requests. */
        MSG(1, "Speech Dispatcher started and waiting for clients ...");
 
+       check_client_count();
+
        g_main_loop_run(main_loop);
 
        MSG(1, "Terminating...");
diff --git a/src/server/speechd.h b/src/server/speechd.h
index 1f6de7d..e5e620b 100644
--- a/src/server/speechd.h
+++ b/src/server/speechd.h
@@ -168,6 +168,8 @@ struct {
        char *debug_destination;
        char *debug_logfile;
        int max_history_messages;       /* Maximum of messages in history 
before they expire */
+       int server_timeout;
+       int server_timeout_set;
 } SpeechdOptions;
 
 struct {
-- 
2.4.6




reply via email to

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