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