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