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
29namespace ripple {
30
31namespace detail {
32
33// Work with files
34class WorkFile : public Work, public std::enable_shared_from_this<WorkFile>
35{
36protected:
37 using error_code = boost::system::error_code;
38 // Override the definition in Work.h
40
41public:
43 std::function<void(error_code const&, response_type const&)>;
44
45public:
47 std::string const& path,
48 boost::asio::io_service& ios,
49 callback_type cb);
50 ~WorkFile();
51
52 void
53 run() override;
54
55 void
56 cancel() override;
57
58private:
61 boost::asio::io_service& ios_;
62 boost::asio::io_service::strand strand_;
63};
64
65//------------------------------------------------------------------------------
66
68 std::string const& path,
69 boost::asio::io_service& ios,
71 : path_(path), cb_(std::move(cb)), ios_(ios), strand_(ios)
72{
73}
74
76{
77 if (cb_)
78 cb_(make_error_code(boost::system::errc::interrupted), {});
79}
80
81void
83{
84 if (!strand_.running_in_this_thread())
85 return ios_.post(
87
88 error_code ec;
89 auto const fileContents = getFileContents(ec, path_, megabytes(1));
90
91 XRPL_ASSERT(cb_, "ripple::detail::WorkFile::run : callback is set");
92 cb_(ec, fileContents);
93 cb_ = nullptr;
94}
95
96void
98{
99 // Nothing to do. Either it finished in run, or it didn't start.
100}
101
102} // namespace detail
103
104} // namespace ripple
105
106#endif
T bind(T... args)
boost::asio::io_service & ios_
Definition: WorkFile.h:61
void cancel() override
Definition: WorkFile.h:97
boost::system::error_code error_code
Definition: WorkFile.h:37
void run() override
Definition: WorkFile.h:82
callback_type cb_
Definition: WorkFile.h:60
WorkFile(std::string const &path, boost::asio::io_service &ios, callback_type cb)
Definition: WorkFile.h:67
boost::asio::io_service::strand strand_
Definition: WorkFile.h:62
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:25
constexpr auto megabytes(T value) noexcept
Definition: ByteUtilities.h:34
std::error_code make_error_code(ripple::TokenCodecErrc e)
Definition: token_errors.h:97
std::string getFileContents(boost::system::error_code &ec, boost::filesystem::path const &sourcePath, std::optional< std::size_t > maxSize=std::nullopt)
STL namespace.