style
[psensor.git] / src / server / server.c
index 673964c..e50984c 100644 (file)
@@ -1,21 +1,21 @@
 /*
   Copyright (C) 2010-2011 wpitchoune@gmail.com
-
-    This program is free software; you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
-
-    This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
-
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301 USA
-*/
* Copyright (C) 2010-2011 jeanfi@gmail.com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* General Public License for more details.
+ *
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
+ */
 #include <locale.h>
 #include <libintl.h>
 #define _(str) gettext(str)
 
 #ifdef HAVE_GTOP
 #include "sysinfo.h"
-#endif
-
-#ifdef HAVE_LUA
-#include "server_lua.h"
+#include "cpu.h"
 #endif
 
 #include "psensor_json.h"
-#include "plib/url.h"
-#include "plib/plib_io.h"
+#include "url.h"
+#include "p_io.h"
 #include "server.h"
 
 static const char *program_name;
@@ -77,7 +74,7 @@ static int server_stop_requested;
 void print_version()
 {
        printf("psensor-server %s\n", VERSION);
-       printf(_("Copyright (C) %s wpitchoune@gmail.com\n\
+       printf(_("Copyright (C) %s jeanfi@gmail.com\n\
 License GPLv2: GNU GPL version 2 or later \
 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n\
 This is free software: you are free to change and redistribute it.\n\
@@ -95,15 +92,15 @@ void print_help()
        puts("");
        puts("Options:");
        puts(_("\
-  -h, --help          display this help and exit\n\
-  -v, --version       display version information and exit"));
+  -h, --help           display this help and exit\n\
+  -v, --version                display version information and exit"));
 
        puts("");
 
        puts(_("\
-  -d,--debug     run in debug mode\n\
-  -p,--port=PORT webserver port\n\
-  -w,--wdir=DIR  directory containing webserver pages"));
+  -d,--debug           run in debug mode\n\
+  -p,--port=PORT       webserver port\n\
+  -w,--wdir=DIR                directory containing webserver pages"));
 
        puts("");
 
@@ -113,19 +110,6 @@ void print_help()
 }
 
 /*
-  Returns '1' if the path denotates a Lua file, otherwise returns 0.
- */
-int is_path_lua(const char *path)
-{
-       char *dot = rindex(path, '.');
-
-       if (dot && !strcasecmp(dot, ".lua"))
-               return 1;
-
-       return 0;
-}
-
-/*
   Returns the file path corresponding to a given URL
 */
 char *get_path(const char *url, const char *www_dir)
@@ -134,7 +118,7 @@ char *get_path(const char *url, const char *www_dir)
        char *res;
 
        if (!strlen(url) || !strcmp(url, ".") || !strcmp(url, "/"))
-               p = "/index.lua";
+               p = "/index.html";
        else
                p = url;
 
@@ -146,8 +130,13 @@ char *get_path(const char *url, const char *www_dir)
        return res;
 }
 
+#if MHD_VERSION >= 0x00090200
+static int
+file_reader(void *cls, uint64_t pos, char *buf, size_t max)
+#else
 static int
 file_reader(void *cls, uint64_t pos, char *buf, int max)
+#endif
 {
        FILE *file = cls;
 
@@ -160,20 +149,28 @@ create_response_api(const char *nurl,
                    const char *method,
                    unsigned int *rp_code)
 {
+       struct MHD_Response *resp;
+       struct psensor *s;
        char *page = NULL;
 
        if (!strcmp(nurl, URL_BASE_API_1_0_SENSORS))  {
 
                page = sensors_to_json_string(server_data.sensors);
 
+#ifdef HAVE_GTOP
+       } else if (!strcmp(nurl, URL_API_1_0_SYSINFO)) {
+
+               page = sysinfo_to_json_string(&server_data.psysinfo);
+       } else if (!strcmp(nurl, URL_API_1_0_CPU_USAGE)) {
+               page = sensor_to_json_string(server_data.cpu_usage);
+#endif
        } else if (!strncmp(nurl, URL_BASE_API_1_0_SENSORS,
                            strlen(URL_BASE_API_1_0_SENSORS))
                   && nurl[strlen(URL_BASE_API_1_0_SENSORS)] == '/') {
 
                const char *sid = nurl + strlen(URL_BASE_API_1_0_SENSORS) + 1;
 
-               struct psensor *s
-                       = psensor_list_get_by_id(server_data.sensors, sid);
+               s = psensor_list_get_by_id(server_data.sensors, sid);
 
                if (s)
                        page = sensor_to_json_string(s);
@@ -188,29 +185,14 @@ create_response_api(const char *nurl,
        if (page) {
                *rp_code = MHD_HTTP_OK;
 
-               return MHD_create_response_from_data
-                       (strlen(page), page, MHD_YES, MHD_NO);
-       }
-
-       return NULL;
-}
-
-struct MHD_Response *
-create_response_lua(const char *nurl,
-                   const char *method,
-                   unsigned int *rp_code,
-                   const char *fpath)
-{
-#ifdef HAVE_LUA
-       char *page = lua_to_html_page(&server_data, fpath);
+               resp = MHD_create_response_from_data(strlen(page), page,
+                                                    MHD_YES, MHD_NO);
 
-       if (page) {
-               *rp_code = MHD_HTTP_OK;
+               MHD_add_response_header(resp, MHD_HTTP_HEADER_CONTENT_TYPE,
+                                       "application/json");
 
-               return MHD_create_response_from_data
-                       (strlen(page), page, MHD_YES, MHD_NO);
+               return resp;
        }
-#endif
 
        return NULL;
 }
@@ -259,12 +241,7 @@ create_response(const char *nurl, const char *method, unsigned int *rp_code)
        } else {
                char *fpath = get_path(nurl, server_data.www_dir);
 
-               if (is_path_lua(fpath))
-                       resp = create_response_lua
-                               (nurl, method, rp_code, fpath);
-               else
-                       resp = create_response_file
-                               (nurl, method, rp_code, fpath);
+               resp = create_response_file(nurl, method, rp_code, fpath);
 
                free(fpath);
        }
@@ -294,7 +271,6 @@ cbk_http_request(void *cls,
        int ret;
        char *nurl;
        unsigned int resp_code;
-       char *page = NULL;
 
        if (strcmp(method, "GET"))
                return MHD_NO;
@@ -345,6 +321,7 @@ int main(int argc, char *argv[])
 #endif
 
        server_data.www_dir = DEFAULT_WWW_DIR;
+       server_data.psysinfo.interfaces = NULL;
 
        while ((optc = getopt_long(argc, argv,
                                   "vhp:w:d", long_options, NULL)) != -1) {
@@ -378,12 +355,13 @@ int main(int argc, char *argv[])
                exit(EXIT_FAILURE);
        }
 
-       if (!lmsensor_init()) {
-               fprintf(stderr, _("ERROR: failed to init lm-sensors\n"));
-               exit(EXIT_FAILURE);
-       }
+       psensor_init();
 
-       server_data.sensors = get_all_sensors(1);
+       server_data.sensors = get_all_sensors(600);
+
+#ifdef HAVE_GTOP
+       server_data.cpu_usage = create_cpu_usage_sensor(600);
+#endif
 
        if (!*server_data.sensors)
                fprintf(stderr, _("ERROR: no sensors detected\n"));
@@ -397,15 +375,16 @@ int main(int argc, char *argv[])
                exit(EXIT_FAILURE);
        }
 
-       printf(_("Web server started on port: %d\n"), port);
-       printf(_("WWW directory: %s\n"), server_data.www_dir);
-       printf(_("URL: http://localhost:%d\n"), port);
+       log_printf(LOG_INFO, _("Web server started on port: %d"), port);
+       log_printf(LOG_INFO, _("WWW directory: %s"), server_data.www_dir);
+       log_printf(LOG_INFO, _("URL: http://localhost:%d"), port);
 
        while (!server_stop_requested) {
                pthread_mutex_lock(&mutex);
 
 #ifdef HAVE_GTOP
                sysinfo_update(&server_data.psysinfo);
+               cpu_usage_sensor_update(server_data.cpu_usage);
 #endif
                psensor_list_update_measures(server_data.sensors);
 
@@ -417,11 +396,15 @@ int main(int argc, char *argv[])
 
        /* sanity cleanup for valgrind */
        psensor_list_free(server_data.sensors);
+#ifdef HAVE_GTOP
+       psensor_free(server_data.cpu_usage);
+#endif
        free(server_data.www_dir);
        sensors_cleanup();
 
 #ifdef HAVE_GTOP
        sysinfo_cleanup();
+       cpu_cleanup();
 #endif
 
        return EXIT_SUCCESS;