added comment
[psensor.git] / src / server / server.c
index 90b3796..9312bd1 100644 (file)
@@ -76,7 +76,7 @@ static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
 
 static int server_stop_requested;
 
-void print_version()
+static void print_version()
 {
        printf("psensor-server %s\n", VERSION);
        printf(_("Copyright (C) %s jeanfi@gmail.com\n"
@@ -87,7 +87,7 @@ void print_version()
               "2010-2012");
 }
 
-void print_help()
+static void print_help()
 {
        printf(_("Usage: %s [OPTION]...\n"), program_name);
 
@@ -150,10 +150,8 @@ file_reader(void *cls, uint64_t pos, char *buf, int max)
        return fread(buf, 1, max, file);
 }
 
-struct MHD_Response *
-create_response_api(const char *nurl,
-                   const char *method,
-                   unsigned int *rp_code)
+static struct MHD_Response *
+create_response_api(const char *nurl, const char *method, unsigned int *rp_code)
 {
        struct MHD_Response *resp;
        struct psensor *s;
@@ -199,11 +197,10 @@ create_response_api(const char *nurl,
        return NULL;
 }
 
-struct MHD_Response *
-create_response_file(const char *nurl,
-                    const char *method,
-                    unsigned int *rp_code,
-                    const char *fpath)
+static struct MHD_Response *create_response_file(const char *nurl,
+                                                const char *method,
+                                                unsigned int *rp_code,
+                                                const char *fpath)
 {
        struct stat st;
        int ret;
@@ -238,7 +235,7 @@ create_response_file(const char *nurl,
        return NULL;
 }
 
-struct MHD_Response *
+static struct MHD_Response *
 create_response(const char *nurl, const char *method, unsigned int *rp_code)
 {
        struct MHD_Response *resp = NULL;
@@ -264,14 +261,13 @@ create_response(const char *nurl, const char *method, unsigned int *rp_code)
        }
 }
 
-static int
-cbk_http_request(void *cls,
-                struct MHD_Connection *connection,
-                const char *url,
-                const char *method,
-                const char *version,
-                const char *upload_data,
-                size_t *upload_data_size, void **ptr)
+static int cbk_http_request(void *cls,
+                           struct MHD_Connection *connection,
+                           const char *url,
+                           const char *method,
+                           const char *version,
+                           const char *upload_data,
+                           size_t *upload_data_size, void **ptr)
 {
        static int dummy;
        struct MHD_Response *response;
@@ -310,11 +306,34 @@ cbk_http_request(void *cls,
        return ret;
 }
 
+static void *slog_routine(void *data)
+{
+       char *path;
+
+       path = (char *)data;
+
+       log_debug("slog_routine");
+
+       slog_init(path, server_data.sensors);
+
+       while (1) {
+               pthread_mutex_lock(&mutex);
+               slog_write_sensors(server_data.sensors);
+               pthread_mutex_unlock(&mutex);
+
+               sleep(5);
+       }
+
+       /* not reachable but avoid compilation error. */
+       pthread_exit(0);
+}
+
 int main(int argc, char *argv[])
 {
        struct MHD_Daemon *d;
-       int port, opti, optc, cmdok;
+       int port, opti, optc, cmdok, ret;
        char *log_file, *slog_file;
+       pthread_t slog_thread;
 
        program_name = argv[0];
 
@@ -408,8 +427,14 @@ int main(int argc, char *argv[])
        log_info(_("WWW directory: %s"), server_data.www_dir);
        log_info(_("URL: http://localhost:%d"), port);
 
-       if (slog_file)
-               slog_init(slog_file, server_data.sensors);
+       if (slog_file) {
+               ret = pthread_create(&slog_thread,
+                                    NULL,
+                                    slog_routine,
+                                    slog_file);
+               if (ret)
+                       log_err(_("Failed to create sensor log thread."));
+       }
 
        while (!server_stop_requested) {
                pthread_mutex_lock(&mutex);
@@ -422,8 +447,6 @@ int main(int argc, char *argv[])
 
                psensor_log_measures(server_data.sensors);
 
-               slog_write_sensors(server_data.sensors);
-
                pthread_mutex_unlock(&mutex);
                sleep(5);
        }