diff options
| author | Felix Morgner <felix.morgner@gmail.com> | 2018-11-29 22:28:07 +0100 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@gmail.com> | 2018-11-29 22:28:07 +0100 |
| commit | 962912ff747e4800720a9b5ccbcd40230421b3d1 (patch) | |
| tree | 3b7dbd80b28887940449baef99d2a0a8957d21bc /src | |
| parent | 1125802463013dd5d4e38e142886ecb5fbafa2d9 (diff) | |
| download | wanda-962912ff747e4800720a9b5ccbcd40230421b3d1.tar.xz wanda-962912ff747e4800720a9b5ccbcd40230421b3d1.zip | |
wanda: fix shutdown code
Diffstat (limited to 'src')
| -rw-r--r-- | src/control_interface.cpp | 29 | ||||
| -rw-r--r-- | src/control_interface.hpp | 2 | ||||
| -rw-r--r-- | src/main.cpp | 28 | ||||
| -rw-r--r-- | src/wallpaper.cpp | 23 | ||||
| -rw-r--r-- | src/wallpaper.hpp | 14 |
5 files changed, 60 insertions, 36 deletions
diff --git a/src/control_interface.cpp b/src/control_interface.cpp index b662da5..2b368dd 100644 --- a/src/control_interface.cpp +++ b/src/control_interface.cpp @@ -2,13 +2,13 @@ #include <boost/system/error_code.hpp> +#include <unistd.h> + #include <algorithm> #include <iterator> #include <string> #include <utility> -#include <iostream> - namespace wanda { @@ -40,11 +40,6 @@ boost::system::error_code control_interface::start() return error; } - if (auto error = boost::system::error_code{}; m_acceptor.set_option(boost::asio::socket_base::reuse_address(true), error)) - { - return error; - } - if (auto error = boost::system::error_code{}; m_acceptor.bind(m_endpoint, error)) { return error; @@ -61,10 +56,21 @@ boost::system::error_code control_interface::start() } } +boost::system::error_code control_interface::shutdown() +{ + for(auto & connection : m_connections) + { + connection->close(); + } + + auto error = boost::system::error_code{}; + return m_acceptor.close(error); +} + void control_interface::perform_accept() { m_acceptor.async_accept(m_socket, [that = shared_from_this(), this](auto const &error) { - if (error) + if (error && error != boost::asio::error::operation_aborted) { // TODO: Handle error } @@ -73,7 +79,6 @@ void control_interface::perform_accept() auto [connection, inserted] = m_connections.insert(make_control_connection(std::move(m_socket))); if(inserted) { - std::cout << "Accepted a new connection\n"; (*connection)->add(shared_from_this()); (*connection)->start(); } @@ -82,14 +87,8 @@ void control_interface::perform_accept() }); } -void control_interface::on_received(control_connection::pointer, std::string message) -{ - std::cout << "Received '" << message << "'\n"; -} - void control_interface::on_close(control_connection::pointer connection) { - std::cout << "Connection closed\n"; m_connections.erase(connection); } diff --git a/src/control_interface.hpp b/src/control_interface.hpp index f95362c..c69aeb4 100644 --- a/src/control_interface.hpp +++ b/src/control_interface.hpp @@ -31,8 +31,8 @@ struct control_interface : control_connection::listener, keyed<control_interface control_interface(key, boost::asio::io_service &service, protocol::endpoint endpoint); boost::system::error_code start(); + boost::system::error_code shutdown(); - void on_received(control_connection::pointer connection, std::string message) override; void on_close(control_connection::pointer) override; private: diff --git a/src/main.cpp b/src/main.cpp index 6f8dcf8..169efd9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,8 @@ +#include "control_interface.hpp" #include "filesystem.hpp" -#include "setting.hpp" #include "optional.hpp" -#include "control_interface.hpp" +#include "setting.hpp" +#include "wallpaper.hpp" #include <boost/asio.hpp> @@ -13,19 +14,6 @@ namespace { -void set_wallpaper(std::filesystem::path wallpaper) -{ - using namespace wanda::literals; - using namespace wanda::std_ext; - using namespace std::string_literals; - - with("org.gnome.desktop.background"_setting, [&](auto &setting) { - with(setting["picture-uri"_key], [&](auto &value) { - value = "file://" + wallpaper.native(); - }) || [] { std::cerr << "No such key!\n"; }; - }) || [] { std::cerr << "No such setting!\n"; }; -} - constexpr auto image_filter = [](auto const &path) { static auto const extensions = std::set<std::filesystem::path>{ std::filesystem::path{".jpg"}, @@ -48,22 +36,22 @@ int main() with(wanda::scan({"/usr/share/backgrounds"}, image_filter), [](auto const &list) { auto wallpaper = wanda::random_pick(list); - std::cout << "changing wallpaper to " << wallpaper << '\n'; - set_wallpaper(wallpaper); + wanda::set_wallpaper(wallpaper); auto service = boost::asio::io_service{}; auto interface = wanda::make_interface(service, ".wanda_interface"); auto status = interface->start(); - if(status) + if (status) { return; } auto signals = boost::asio::signal_set{service, SIGINT}; - signals.async_wait([&](auto const & error, auto const signal){ - if(!error && signal == SIGINT) + signals.async_wait([&](auto const &error, auto const signal) { + if (!error && signal == SIGINT) { + interface->shutdown(); service.stop(); } }); diff --git a/src/wallpaper.cpp b/src/wallpaper.cpp new file mode 100644 index 0000000..39abb60 --- /dev/null +++ b/src/wallpaper.cpp @@ -0,0 +1,23 @@ +#include "optional.hpp" +#include "setting.hpp" +#include "wallpaper.hpp" + +#include <iostream> + +namespace wanda +{ + +void set_wallpaper(std::filesystem::path wallpaper) +{ + using namespace wanda::literals; + using namespace wanda::std_ext; + using namespace std::string_literals; + + with("org.gnome.desktop.background"_setting, [&](auto &setting) { + with(setting["picture-uri"_key], [&](auto &value) { + value = "file://" + wallpaper.native(); + }) || [] { std::cerr << "No such key!\n"; }; + }) || [] { std::cerr << "No such setting!\n"; }; +} + +} // namespace wanda
\ No newline at end of file diff --git a/src/wallpaper.hpp b/src/wallpaper.hpp new file mode 100644 index 0000000..6222557 --- /dev/null +++ b/src/wallpaper.hpp @@ -0,0 +1,14 @@ +#ifndef WANDA_WALLPAPER_HPP +#define WANDA_WALLPAPER_HPP + +#include <filesystem> + +namespace wanda +{ + +void set_wallpaper(std::filesystem::path wallpaper); + +} // wanda + + +#endif
\ No newline at end of file |
