Audacity 3.2.0
RemoteProjectSnapshot.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*!********************************************************************
3
4 Audacity: A Digital Audio Editor
5
6 RemoteProjectSnapshot.h
7
8 Dmitry Vedenko
9
10**********************************************************************/
11#pragma once
12
13#include <atomic>
14#include <chrono>
15#include <condition_variable>
16#include <future>
17#include <functional>
18#include <memory>
19#include <mutex>
20#include <string>
21#include <thread>
22#include <unordered_set>
23
24#include "CloudSyncDTO.h"
25#include "NetworkUtils.h"
26
28{
29class IResponse;
30using ResponsePtr = std::shared_ptr<IResponse>;
31} // namespace audacity::network_manager
32
34{
35
37{
39
40 int64_t BlocksDownloaded { 0 };
41 int64_t BlocksTotal { 0 };
42
43 bool ProjectDownloaded { false };
44
45 bool IsComplete() const noexcept;
46};
47
49 std::function<void(RemoteProjectSnapshotState)>;
50
52 : public std::enable_shared_from_this<RemoteProjectSnapshot>
53{
54 struct Tag
55 {
56 };
57
58public:
60 Tag, ProjectInfo projectInfo, SnapshotInfo snapshotInfo, std::string path,
61 RemoteProjectSnapshotStateCallback callback, bool downloadDetached);
62
64
69
70 static std::shared_ptr<RemoteProjectSnapshot> Sync(
71 ProjectInfo projectInfo, SnapshotInfo snapshotInfo, std::string path,
72 RemoteProjectSnapshotStateCallback callback, bool downloadDetached);
73
74 void Cancel();
75
76 TransferStats GetTransferStats() const;
77
78 std::string_view GetProjectId() const;
79
80private:
81 enum class State
82 {
83 Downloading,
84 Cancelled,
85 Failed,
87 };
88
90 std::function<void(audacity::network_manager::ResponsePtr)>;
91
92 std::string AttachOriginalDB();
93
94 void SetupBlocksCopy(
95 const std::string& dbName, std::unordered_set<std::string> blocks);
96
97 std::unordered_set<std::string>
98 CalculateKnownBlocks(const std::string& attachedDbName) const;
99
100 void DoCancel();
101
102 void
103 DownloadBlob(std::string url, SuccessHandler onSuccess, int retries = 3);
104
105 void
106 OnProjectBlobDownloaded(audacity::network_manager::ResponsePtr response);
107 void OnBlockDownloaded(
108 std::string blockHash, audacity::network_manager::ResponsePtr response);
109
110 void OnFailure(ResponseResult result);
111 void RemoveResponse(audacity::network_manager::IResponse* response);
112
113 void MarkProjectInDB(bool successfulDownload);
114
115 void ReportProgress();
116
117 bool InProgress() const;
118 void RequestsThread();
119
120 void SetState(State state);
121
122 void CleanupOrphanBlocks();
123
124 const std::string mSnapshotDBName;
127 const std::string mPath;
129
130 std::vector<std::string> mAttachedDBNames;
131
132 std::atomic<State> mState { State::Downloading };
133
134 using Clock = std::chrono::steady_clock;
135 using TimePoint = Clock::time_point;
136
137 TimePoint mStartTime { Clock::now() };
139
140 std::thread mRequestsThread;
141 std::mutex mRequestsMutex;
142 std::condition_variable mRequestsCV;
143
144 std::vector<std::pair<std::string, SuccessHandler>> mRequests;
145
146 int mRequestsInProgress { 0 };
147 size_t mNextRequestIndex { 0 };
148
149 std::mutex mResponsesMutex;
150 std::vector<std::shared_ptr<audacity::network_manager::IResponse>>
152 std::condition_variable mResponsesEmptyCV;
153
154 std::atomic<int64_t> mDownloadedBlocks { 0 };
155 std::atomic<int64_t> mCopiedBlocks { 0 };
156 std::atomic<int64_t> mDownloadedBytes { 0 };
157
158 int64_t mMissingBlocks { 0 };
159
160 std::optional<std::future<bool>> mCopyBlocksFuture;
161
162 std::atomic<bool> mProjectDownloaded { false };
163
164 bool mNothingToDo { false };
165 const bool mDownloadDetached { false };
166
167}; // class RemoteProjectSnapshot
168} // namespace audacity::cloud::audiocom::sync
struct State mState
RemoteProjectSnapshot & operator=(const RemoteProjectSnapshot &)=delete
RemoteProjectSnapshot(RemoteProjectSnapshot &&)=delete
RemoteProjectSnapshot & operator=(RemoteProjectSnapshot &&)=delete
std::vector< std::shared_ptr< audacity::network_manager::IResponse > > mResponses
std::function< void(audacity::network_manager::ResponsePtr)> SuccessHandler
std::vector< std::pair< std::string, SuccessHandler > > mRequests
RemoteProjectSnapshot(const RemoteProjectSnapshot &)=delete
Interface, that provides access to the data from the HTTP response.
Definition: IResponse.h:113
void Sync(const wxString &string)
Definition: Journal.cpp:321
std::function< void(RemoteProjectSnapshotState)> RemoteProjectSnapshotStateCallback
std::shared_ptr< IResponse > ResponsePtr
STL namespace.
Definition: Dither.cpp:67