Audacity 3.2.0
MissingBlocksUploader.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 MissingBlocksUploader.h
7
8 Dmitry Vedenko
9
10**********************************************************************/
11#pragma once
12
13#include <array>
14#include <atomic>
15#include <condition_variable>
16#include <cstdint>
17#include <mutex>
18#include <thread>
19
20#include <functional>
21
22#include "CloudSyncDTO.h"
23#include "NetworkUtils.h"
24
27
29{
30class IResponse;
31}
32
33class AudacityProject;
34
36{
37class ServiceConfig;
38} // namespace audacity::cloud::audiocom
39
41{
43
45{
46 int64_t TotalBlocks = 0;
47 int64_t UploadedBlocks = 0;
48 int64_t FailedBlocks = 0;
49
50 std::vector<ResponseResult> UploadErrors;
51};
52
53struct BlockUploadTask final
54{
57};
58
59using MissingBlocksUploadProgressCallback = std::function<void(
61 ResponseResult blockResponseResult)>;
62
65 public std::enable_shared_from_this<MissingBlocksUploader>
66{
67 struct Tag final
68 {
69 };
70
71public:
72 static constexpr auto NUM_PRODUCERS = 3;
73 static constexpr auto NUM_UPLOADERS = 6;
74 static constexpr auto RING_BUFFER_SIZE = 16;
75
76 MissingBlocksUploader(Tag, const ServiceConfig& serviceConfig);
77
78 static std::shared_ptr<MissingBlocksUploader> Create(
79 CancellationContextPtr cancellationContex, const ServiceConfig& serviceConfig,
80 std::vector<BlockUploadTask> uploadTasks,
82
84
85private:
86 struct ProducedItem final
87 {
89 std::vector<uint8_t> CompressedData;
90 };
91
92 void Start(
93 CancellationContextPtr cancellationContex,
94 std::vector<BlockUploadTask> uploadTasks,
96
97 void Cancel() override;
98
100 void ConsumeBlock(ProducedItem item);
101
104
105 void ConfirmBlock(BlockUploadTask task);
106 void HandleFailedBlock(const ResponseResult& result, BlockUploadTask task);
107
108 void ProducerThread();
109 void ConsumerThread();
110
112
113 std::vector<BlockUploadTask> mUploadTasks;
115
116 std::atomic_bool mIsRunning { true };
117
119 std::thread mConsumerThread;
120
121 std::mutex mBlocksMutex;
123
124 std::mutex mUploadsMutex;
125 std::condition_variable mUploadsNotFull;
126 size_t mConcurrentUploads { 0 };
127
129
130 std::condition_variable mRingBufferNotEmpty;
131 std::condition_variable mRingBufferNotFull;
132
133 std::array<ProducedItem, RING_BUFFER_SIZE> mRingBuffer;
136
139
141};
142} // namespace audacity::cloud::audiocom::sync
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:90
Configuration for the audio.com.
Definition: ServiceConfig.h:23
void HandleFailedBlock(const ResponseResult &result, BlockUploadTask task)
std::array< ProducedItem, RING_BUFFER_SIZE > mRingBuffer
MissingBlocksUploader(Tag, const ServiceConfig &serviceConfig)
static std::shared_ptr< MissingBlocksUploader > Create(CancellationContextPtr cancellationContex, const ServiceConfig &serviceConfig, std::vector< BlockUploadTask > uploadTasks, MissingBlocksUploadProgressCallback progress)
void Start(CancellationContextPtr cancellationContex, std::vector< BlockUploadTask > uploadTasks, MissingBlocksUploadProgressCallback progress)
std::function< void(const MissingBlocksUploadProgress &, const LockedBlock &, ResponseResult blockResponseResult)> MissingBlocksUploadProgressCallback
std::shared_ptr< CancellationContext > CancellationContextPtr