Bump version to 0.3.
[profile/ivi/settings-daemon.git] / src / daemon.cpp
1 /**
2  * @file daemon.cpp
3  *
4  * @brief Main settingsd source file.
5  *
6  * @copyright @par
7  * Copyright 2012, 2013 Intel Corporation All Rights Reserved.
8  * @par
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation;
12  * version 2.1 of the License.
13  * @par
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  * @par
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
22  * Boston, MA  02110-1301  USA
23  */
24
25 #include "../lib/config.hpp"
26 #include "../lib/manager.hpp"
27
28 #include "configurator.hpp"
29 #include "websocket_server.hpp"
30
31 #include <settingsd/glib_traits.hpp>
32 #include <settingsd/unique_ptr.hpp>
33
34 #include <cstdlib>
35 #include <glib.h>
36 #include <iostream>
37 #include <thread>
38
39 /**
40  * settingsd program entry point.
41  *
42  * @param[in] argc The number of settingsd command line arguments.
43  * @param[in] argv The settingsd command line option/argument vector.
44  */
45 int
46 main(int argc, char * argv[])
47 {
48   std::cerr << PACKAGE_STRING << '\n';
49
50   try {
51     using namespace ivi;
52
53     settings::configurator config(argc, argv, IVI_SETTINGS_DBUS_NAME);
54
55     settings::manager manager(config.settings_dir());
56
57     // The websocket server will run in its own thread.
58     std::thread(
59       [&config, &manager](){
60         // Exceptions are not propagated to other threads with out
61         // explicit code to do that.  Handle exceptions in this
62         // thread.
63         try {
64           // Tie the lifetime of the websocket_server to this thread,
65           // not the main thread, so that we have no lifetime related
66           // race conditions with respect to the underlying
67           // libwebsocket_context object.
68           settings::websocket_server server(config, manager);
69           server.run();
70         }
71         catch (std::exception & e) {
72           std::cerr << e.what() << '\n';
73
74           // Probably not something from which we can recover.  Just
75           // terminate the daemon.
76           std::exit(EXIT_FAILURE);
77         }
78       }).detach();
79
80     // Glib related events, including GDbus related signal handlers,
81     // will handled in this (main) thread.
82     settings::unique_ptr<GMainLoop> const loop(g_main_loop_new(nullptr, false));
83     g_main_loop_run(loop.get());
84   }
85   catch (std::exception & e) {
86     std::cerr << e.what() << '\n';
87     return -1;
88   }
89
90   return 0;
91 }
92
93
94 // Local Variables:
95 // mode:c++
96 // c-basic-offset:2
97 // indent-tabs-mode: nil
98 // End: