rippled
Loading...
Searching...
No Matches
WorkFile.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2018 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_APP_MISC_DETAIL_WORKFILE_H_INCLUDED
21#define RIPPLE_APP_MISC_DETAIL_WORKFILE_H_INCLUDED
22
23#include <xrpld/app/misc/detail/Work.h>
24
25#include <xrpl/basics/ByteUtilities.h>
26#include <xrpl/basics/FileUtilities.h>
27#include <xrpl/beast/utility/instrumentation.h>
28
29#include <boost/asio/bind_executor.hpp>
30#include <boost/asio/io_context.hpp>
31#include <boost/asio/strand.hpp>
32
33namespace ripple {
34
35namespace detail {
36
37// Work with files
38class WorkFile : public Work, public std::enable_shared_from_this<WorkFile>
39{
40protected:
41 using error_code = boost::system::error_code;
42 // Override the definition in Work.h
44
45public:
47 std::function<void(error_code const&, response_type const&)>;
48
49public:
51 std::string const& path,
52 boost::asio::io_context& ios,
53 callback_type cb);
54 ~WorkFile();
55
56 void
57 run() override;
58
59 void
60 cancel() override;
61
62private:
65 boost::asio::io_context& ios_;
66 boost::asio::strand<boost::asio::io_context::executor_type> strand_;
67};
68
69//------------------------------------------------------------------------------
70
72 std::string const& path,
73 boost::asio::io_context& ios,
75 : path_(path)
76 , cb_(std::move(cb))
77 , ios_(ios)
78 , strand_(boost::asio::make_strand(ios))
79{
80}
81
83{
84 if (cb_)
85 cb_(make_error_code(boost::system::errc::interrupted), {});
86}
87
88void
90{
91 if (!strand_.running_in_this_thread())
92 return boost::asio::post(
93 ios_,
94 boost::asio::bind_executor(
96
97 error_code ec;
98 auto const fileContents = getFileContents(ec, path_, megabytes(1));
99
100 XRPL_ASSERT(cb_, "ripple::detail::WorkFile::run : callback is set");
101 cb_(ec, fileContents);
102 cb_ = nullptr;
103}
104
105void
107{
108 // Nothing to do. Either it finished in run, or it didn't start.
109}
110
111} // namespace detail
112
113} // namespace ripple
114
115#endif
T bind(T... args)
void cancel() override
Definition WorkFile.h:106
boost::system::error_code error_code
Definition WorkFile.h:41
void run() override
Definition WorkFile.h:89
callback_type cb_
Definition WorkFile.h:64
WorkFile(std::string const &path, boost::asio::io_context &ios, callback_type cb)
Definition WorkFile.h:71
boost::asio::strand< boost::asio::io_context::executor_type > strand_
Definition WorkFile.h:66
boost::asio::io_context & ios_
Definition WorkFile.h:65
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
constexpr auto megabytes(T value) noexcept
std::error_code make_error_code(ripple::TokenCodecErrc e)
std::string getFileContents(boost::system::error_code &ec, boost::filesystem::path const &sourcePath, std::optional< std::size_t > maxSize=std::nullopt)
STL namespace.