diff options
Diffstat (limited to 'server/src/http/ext/gpx.cpp')
| -rw-r--r-- | server/src/http/ext/gpx.cpp | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/server/src/http/ext/gpx.cpp b/server/src/http/ext/gpx.cpp new file mode 100644 index 0000000..c1f565f --- /dev/null +++ b/server/src/http/ext/gpx.cpp | |||
| @@ -0,0 +1,139 @@ | |||
| 1 | module; | ||
| 2 | |||
| 3 | #include <boost/beast/core.hpp> | ||
| 4 | |||
| 5 | module routemon:http.ext.gpx$impl; | ||
| 6 | |||
| 7 | import :http.ext.gpx; | ||
| 8 | |||
| 9 | namespace chrono = std::chrono; | ||
| 10 | |||
| 11 | namespace routemon::http::ext::gpx { | ||
| 12 | |||
| 13 | auto parse_error_category_impl::name() const noexcept -> char const* | ||
| 14 | { | ||
| 15 | return "gpx_parse"; | ||
| 16 | } | ||
| 17 | |||
| 18 | auto parse_error_category_impl::message(int condition) const noexcept | ||
| 19 | -> std::string | ||
| 20 | { | ||
| 21 | std::ignore = condition; | ||
| 22 | return "failed to parse GPX file"; | ||
| 23 | } | ||
| 24 | |||
| 25 | auto parse_error_category() noexcept -> parse_error_category_impl const& | ||
| 26 | { | ||
| 27 | static auto const inst = parse_error_category_impl{}; | ||
| 28 | return inst; | ||
| 29 | } | ||
| 30 | |||
| 31 | auto parse_error() noexcept -> std::error_code | ||
| 32 | { | ||
| 33 | return std::error_code{1, parse_error_category()}; | ||
| 34 | } | ||
| 35 | |||
| 36 | auto parse_result::set_exception(std::exception_ptr ex) noexcept -> void | ||
| 37 | { | ||
| 38 | res_ = ex; | ||
| 39 | } | ||
| 40 | |||
| 41 | auto parse_result::set_gpx_file(routemon::gpx::file&& f) noexcept -> void | ||
| 42 | { | ||
| 43 | res_ = std::move(f); | ||
| 44 | } | ||
| 45 | |||
| 46 | auto parse_result::init_parse_duration( | ||
| 47 | chrono::steady_clock::duration dur) noexcept -> void | ||
| 48 | { | ||
| 49 | init_parse_dur_ = dur; | ||
| 50 | } | ||
| 51 | |||
| 52 | [[nodiscard]] auto parse_result::init_parse_duration() const noexcept | ||
| 53 | -> chrono::steady_clock::duration | ||
| 54 | { | ||
| 55 | return init_parse_dur_; | ||
| 56 | } | ||
| 57 | |||
| 58 | auto parse_result::put_parse_duration( | ||
| 59 | chrono::steady_clock::duration dur) noexcept -> void | ||
| 60 | { | ||
| 61 | put_parse_dur_ = dur; | ||
| 62 | } | ||
| 63 | |||
| 64 | [[nodiscard]] auto parse_result::put_parse_duration() const noexcept | ||
| 65 | -> chrono::steady_clock::duration | ||
| 66 | { | ||
| 67 | return put_parse_dur_; | ||
| 68 | } | ||
| 69 | |||
| 70 | auto parse_result::finish_parse_duration( | ||
| 71 | chrono::steady_clock::duration dur) noexcept -> void | ||
| 72 | { | ||
| 73 | finish_parse_dur_ = dur; | ||
| 74 | } | ||
| 75 | |||
| 76 | [[nodiscard]] auto parse_result::finish_parse_duration() const noexcept | ||
| 77 | -> chrono::steady_clock::duration | ||
| 78 | { | ||
| 79 | return finish_parse_dur_; | ||
| 80 | } | ||
| 81 | |||
| 82 | auto parse_result::unwrap() -> routemon::gpx::file&& | ||
| 83 | { | ||
| 84 | return std::visit( | ||
| 85 | util::overloaded{ | ||
| 86 | [](std::exception_ptr ex) -> routemon::gpx::file&& | ||
| 87 | { | ||
| 88 | if (ex) | ||
| 89 | std::rethrow_exception(ex); | ||
| 90 | else | ||
| 91 | throw std::runtime_error{"no GPX file parse result available"}; | ||
| 92 | }, | ||
| 93 | [](routemon::gpx::file&& f) -> routemon::gpx::file&& | ||
| 94 | { return std::move(f); }, | ||
| 95 | }, | ||
| 96 | std::move(res_)); | ||
| 97 | } | ||
| 98 | |||
| 99 | auto body::reader::init( | ||
| 100 | boost::optional<std::uint64_t> /* n */, beast::error_code& ec) noexcept | ||
| 101 | -> void | ||
| 102 | { | ||
| 103 | auto const init_start = chrono::steady_clock::now(); | ||
| 104 | try | ||
| 105 | { | ||
| 106 | r_.init(); | ||
| 107 | ec = {}; | ||
| 108 | } | ||
| 109 | catch (std::exception& ex) | ||
| 110 | { | ||
| 111 | res_->set_exception(std::current_exception()); | ||
| 112 | ec = parse_error(); | ||
| 113 | } | ||
| 114 | res_->init_parse_duration(chrono::steady_clock::now() - init_start); | ||
| 115 | } | ||
| 116 | |||
| 117 | auto body::reader::finish(beast::error_code& ec) noexcept -> void | ||
| 118 | { | ||
| 119 | auto const finish_start = chrono::steady_clock::now(); | ||
| 120 | try | ||
| 121 | { | ||
| 122 | if (buf_.size() > 0) | ||
| 123 | { | ||
| 124 | r_.put(std::string_view{buf_}); | ||
| 125 | buf_.clear(); | ||
| 126 | } | ||
| 127 | |||
| 128 | res_->set_gpx_file(r_.finish()); | ||
| 129 | ec = {}; | ||
| 130 | } | ||
| 131 | catch (std::exception& ex) | ||
| 132 | { | ||
| 133 | res_->set_exception(std::current_exception()); | ||
| 134 | ec = parse_error(); | ||
| 135 | } | ||
| 136 | res_->finish_parse_duration(chrono::steady_clock::now() - finish_start); | ||
| 137 | } | ||
| 138 | |||
| 139 | } // namespace routemon::http::ext::gpx | ||