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{
53 struct Tag
54 {
55 };
56
57public:
59 Tag, ProjectInfo projectInfo, SnapshotInfo snapshotInfo, std::string path,
60 RemoteProjectSnapshotStateCallback callback, bool downloadDetached);
61
63
68
69 static std::shared_ptr<RemoteProjectSnapshot> Sync(
70 ProjectInfo projectInfo, SnapshotInfo snapshotInfo, std::string path,
71 RemoteProjectSnapshotStateCallback callback, bool downloadDetached);
72
73 void Cancel();
74
75 TransferStats GetTransferStats() const;
76
77 std::string_view GetProjectId() const;
78
79private:
80 enum class State
81 {
82 Downloading,
83 Cancelled,
84 Failed,
86 };
87
89 std::function<void(audacity::network_manager::ResponsePtr)>;
90
91 std::string AttachOriginalDB();
92
93 void SetupBlocksCopy(
94 const std::string& dbName, std::unordered_set<std::string> blocks);
95
96 std::unordered_set<std::string>
97 CalculateKnownBlocks(const std::string& attachedDbName) const;
98
99 void DoCancel();
100
101 void
102 DownloadBlob(std::string url, SuccessHandler onSuccess, int retries = 3);
103
104 void
105 OnProjectBlobDownloaded(audacity::network_manager::ResponsePtr response);
106 void OnBlockDownloaded(
107 std::string blockHash, audacity::network_manager::ResponsePtr response);
108
109 void OnFailure(ResponseResult result);
110 void RemoveResponse(audacity::network_manager::IResponse* response);
111
112 void MarkProjectInDB(bool successfulDownload);
113
114 void ReportProgress();
115
116 bool InProgress() const;
117 void RequestsThread();
118
119 void SetState(State state);
120
121 void CleanupOrphanBlocks();
122
123 const std::string mSnapshotDBName;
126 const std::string mPath;
128
129 std::vector<std::string> mAttachedDBNames;
130
131 std::atomic<State> mState { State::Downloading };
132
133 using Clock = std::chrono::steady_clock;
134 using TimePoint = Clock::time_point;
135
136 TimePoint mStartTime { Clock::now() };
138
139 std::thread mRequestsThread;
140 std::mutex mRequestsMutex;
141 std::condition_variable mRequestsCV;
142
143 std::vector<std::pair<std::string, SuccessHandler>> mRequests;
144
145 int mRequestsInProgress { 0 };
146 size_t mNextRequestIndex { 0 };
147
148 std::mutex mResponsesMutex;
149 std::vector<std::shared_ptr<audacity::network_manager::IResponse>>
151 std::condition_variable mResponsesEmptyCV;
152
153 std::atomic<int64_t> mDownloadedBlocks { 0 };
154 std::atomic<int64_t> mCopiedBlocks { 0 };
155 std::atomic<int64_t> mDownloadedBytes { 0 };
156
157 int64_t mMissingBlocks { 0 };
158
159 std::optional<std::future<bool>> mCopyBlocksFuture;
160
161 std::atomic<bool> mProjectDownloaded { false };
162
163 bool mNothingToDo { false };
164 const bool mDownloadDetached { false };
165
166}; // class RemoteProjectSnapshot
167} // 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