rippled
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 <ripple/app/misc/detail/Work.h>
24 #include <ripple/basics/ByteUtilities.h>
25 #include <ripple/basics/FileUtilities.h>
26 #include <cassert>
27 #include <cerrno>
28 
29 namespace ripple {
30 
31 namespace detail {
32 
33 // Work with files
34 class WorkFile : public Work, public std::enable_shared_from_this<WorkFile>
35 {
36 protected:
37  using error_code = boost::system::error_code;
38  // Override the definition in Work.h
40 
41 public:
42  using callback_type =
43  std::function<void(error_code const&, response_type const&)>;
44 
45 public:
46  WorkFile(
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 
58 private:
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,
70  callback_type cb)
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 
81 void
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  assert(cb_);
92  cb_(ec, fileContents);
93  cb_ = nullptr;
94 }
95 
96 void
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
ripple::detail::WorkFile::ios_
boost::asio::io_service & ios_
Definition: WorkFile.h:61
ripple::detail::WorkFile
Definition: WorkFile.h:34
ripple::getFileContents
std::string getFileContents(boost::system::error_code &ec, boost::filesystem::path const &sourcePath, std::optional< std::size_t > maxSize=std::nullopt)
Definition: FileUtilities.cpp:25
std::bind
T bind(T... args)
std::string
STL class.
ripple::detail::WorkFile::error_code
boost::system::error_code error_code
Definition: WorkFile.h:37
std::function< void(error_code const &, response_type const &)>
std::enable_shared_from_this< WorkFile >::shared_from_this
T shared_from_this(T... args)
ripple::megabytes
constexpr auto megabytes(T value) noexcept
Definition: ByteUtilities.h:34
std::enable_shared_from_this
ripple::detail::WorkFile::cb_
callback_type cb_
Definition: WorkFile.h:60
ripple::detail::WorkFile::WorkFile
WorkFile(std::string const &path, boost::asio::io_service &ios, callback_type cb)
Definition: WorkFile.h:67
ripple::detail::WorkFile::~WorkFile
~WorkFile()
Definition: WorkFile.h:75
ripple::detail::WorkFile::run
void run() override
Definition: WorkFile.h:82
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::detail::WorkFile::path_
std::string path_
Definition: WorkFile.h:59
cerrno
std
STL namespace.
cassert
ripple::detail::WorkFile::strand_
boost::asio::io_service::strand strand_
Definition: WorkFile.h:62
ripple::detail::Work
Definition: Work.h:33
ripple::detail::WorkFile::cancel
void cancel() override
Definition: WorkFile.h:97