| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // | ||
| 2 | // Copyright (c) 2022 Vinnie Falco (vinnie dot falco at gmail dot com) | ||
| 3 | // | ||
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
| 6 | // | ||
| 7 | // Official repository: https://github.com/cppalliance/beast2 | ||
| 8 | // | ||
| 9 | |||
| 10 | #include <boost/beast2/application.hpp> | ||
| 11 | #include <boost/beast2/logger.hpp> | ||
| 12 | #include <boost/beast2/detail/except.hpp> | ||
| 13 | #include <mutex> | ||
| 14 | #include <vector> | ||
| 15 | |||
| 16 | namespace boost { | ||
| 17 | namespace beast2 { | ||
| 18 | |||
| 19 | enum application::state : char | ||
| 20 | { | ||
| 21 | none, | ||
| 22 | starting, | ||
| 23 | running, | ||
| 24 | stopping, | ||
| 25 | stopped | ||
| 26 | }; | ||
| 27 | |||
| 28 | struct application::impl | ||
| 29 | { | ||
| 30 | std::mutex m; | ||
| 31 | state st = state::none; | ||
| 32 | rts::context services; | ||
| 33 | }; | ||
| 34 | |||
| 35 | ✗ | application:: | |
| 36 | ~application() | ||
| 37 | { | ||
| 38 | { | ||
| 39 | ✗ | std::lock_guard<std::mutex> lock(impl_->m); | |
| 40 | ✗ | if( impl_->st != state::stopped && | |
| 41 | ✗ | impl_->st != state::none) | |
| 42 | { | ||
| 43 | // stop() hasn't returned yet | ||
| 44 | ✗ | detail::throw_invalid_argument(); | |
| 45 | } | ||
| 46 | ✗ | } | |
| 47 | ✗ | delete impl_; | |
| 48 | ✗ | } | |
| 49 | |||
| 50 | ✗ | application:: | |
| 51 | ✗ | application() | |
| 52 | ✗ | : impl_(new impl) | |
| 53 | { | ||
| 54 | ✗ | } | |
| 55 | |||
| 56 | void | ||
| 57 | ✗ | application:: | |
| 58 | start() | ||
| 59 | { | ||
| 60 | { | ||
| 61 | ✗ | std::lock_guard<std::mutex> lock(impl_->m); | |
| 62 | ✗ | if(impl_->st != state::none) | |
| 63 | { | ||
| 64 | // can't call twice | ||
| 65 | ✗ | detail::throw_invalid_argument(); | |
| 66 | } | ||
| 67 | ✗ | impl_->st = state::starting; | |
| 68 | ✗ | } | |
| 69 | ✗ | auto v = get_elements(); | |
| 70 | ✗ | for(std::size_t i = 0; i < v.size(); ++i) | |
| 71 | { | ||
| 72 | try | ||
| 73 | { | ||
| 74 | ✗ | v[i].start(); | |
| 75 | } | ||
| 76 | ✗ | catch(std::exception const&) | |
| 77 | { | ||
| 78 | { | ||
| 79 | ✗ | std::lock_guard<std::mutex> lock(impl_->m); | |
| 80 | ✗ | impl_->st = state::stopping; | |
| 81 | ✗ | } | |
| 82 | do | ||
| 83 | { | ||
| 84 | ✗ | v[i].stop(); | |
| 85 | } | ||
| 86 | ✗ | while(i-- != 0); | |
| 87 | { | ||
| 88 | ✗ | std::lock_guard<std::mutex> lock(impl_->m); | |
| 89 | ✗ | impl_->st = state::stopped; | |
| 90 | ✗ | } | |
| 91 | ✗ | throw; | |
| 92 | ✗ | } | |
| 93 | } | ||
| 94 | { | ||
| 95 | ✗ | std::lock_guard<std::mutex> lock(impl_->m); | |
| 96 | ✗ | impl_->st = state::running; | |
| 97 | ✗ | } | |
| 98 | ✗ | } | |
| 99 | |||
| 100 | void | ||
| 101 | ✗ | application:: | |
| 102 | stop() | ||
| 103 | { | ||
| 104 | { | ||
| 105 | ✗ | std::lock_guard<std::mutex> lock(impl_->m); | |
| 106 | ✗ | if(impl_->st != state::running) | |
| 107 | ✗ | detail::throw_invalid_argument(); | |
| 108 | ✗ | impl_->st = state::stopping; | |
| 109 | ✗ | } | |
| 110 | |||
| 111 | ✗ | auto v = get_elements(); | |
| 112 | ✗ | for(std::size_t i = v.size(); i--;) | |
| 113 | ✗ | v[i].stop(); | |
| 114 | |||
| 115 | { | ||
| 116 | ✗ | std::lock_guard<std::mutex> lock(impl_->m); | |
| 117 | ✗ | impl_->st = state::stopped; | |
| 118 | ✗ | } | |
| 119 | ✗ | } | |
| 120 | |||
| 121 | rts::context& | ||
| 122 | ✗ | application:: | |
| 123 | services() noexcept | ||
| 124 | { | ||
| 125 | ✗ | return impl_->services; | |
| 126 | } | ||
| 127 | |||
| 128 | } // beast2 | ||
| 129 | } // boost | ||
| 130 |