Notify file manager when file starts to download.

GitOrigin-RevId: ee21c170dcd7098b734d31fb170e99053f64a139
This commit is contained in:
levlam 2018-01-09 14:57:11 +03:00
parent 0f07341e91
commit ed54c461ae
6 changed files with 50 additions and 0 deletions

View File

@ -177,6 +177,8 @@ Result<std::pair<NetQueryPtr, bool>> FileDownloader::start_part(Part part, int32
auto size = get_part_size();
CHECK(part.size <= size);
callback_->on_start_download();
NetQueryPtr net_query;
if (!use_cdn_) {
net_query = G()->net_query_creator().create(

View File

@ -28,6 +28,7 @@ class FileDownloader : public FileLoader {
public:
class Callback : public FileLoader::Callback {
public:
virtual void on_start_download() = 0;
virtual void on_partial_download(const PartialLocalFileLocation &partial_local, int64 ready_size) = 0;
virtual void on_ok(const FullLocalFileLocation &full_local, int64 size) = 0;
virtual void on_error(Status status) = 0;

View File

@ -152,6 +152,17 @@ void FileLoadManager::close() {
loop();
}
void FileLoadManager::on_start_download() {
auto node_id = get_link_token();
auto node = nodes_container_.get(node_id);
if (node == nullptr) {
return;
}
if (!stop_flag_) {
send_closure(callback_, &Callback::on_start_download, node->query_id_);
}
}
void FileLoadManager::on_partial_download(const PartialLocalFileLocation &partial_local, int64 ready_size) {
auto node_id = get_link_token();
auto node = nodes_container_.get(node_id);
@ -162,6 +173,7 @@ void FileLoadManager::on_partial_download(const PartialLocalFileLocation &partia
send_closure(callback_, &Callback::on_partial_download, node->query_id_, partial_local, ready_size);
}
}
void FileLoadManager::on_partial_upload(const PartialRemoteFileLocation &partial_remote, int64 ready_size) {
auto node_id = get_link_token();
auto node = nodes_container_.get(node_id);
@ -172,6 +184,7 @@ void FileLoadManager::on_partial_upload(const PartialRemoteFileLocation &partial
send_closure(callback_, &Callback::on_partial_upload, node->query_id_, partial_remote, ready_size);
}
}
void FileLoadManager::on_ok_download(const FullLocalFileLocation &local, int64 size) {
auto node_id = get_link_token();
auto node = nodes_container_.get(node_id);
@ -184,6 +197,7 @@ void FileLoadManager::on_ok_download(const FullLocalFileLocation &local, int64 s
close_node(node_id);
loop();
}
void FileLoadManager::on_ok_upload(FileType file_type, const PartialRemoteFileLocation &remote, int64 size) {
auto node_id = get_link_token();
auto node = nodes_container_.get(node_id);
@ -196,6 +210,7 @@ void FileLoadManager::on_ok_upload(FileType file_type, const PartialRemoteFileLo
close_node(node_id);
loop();
}
void FileLoadManager::on_ok_upload_full(const FullRemoteFileLocation &remote) {
auto node_id = get_link_token();
auto node = nodes_container_.get(node_id);
@ -208,10 +223,12 @@ void FileLoadManager::on_ok_upload_full(const FullRemoteFileLocation &remote) {
close_node(node_id);
loop();
}
void FileLoadManager::on_error(Status status) {
auto node_id = get_link_token();
on_error_impl(node_id, std::move(status));
}
void FileLoadManager::on_error_impl(NodeId node_id, Status status) {
auto node = nodes_container_.get(node_id);
if (node == nullptr) {

View File

@ -33,6 +33,7 @@ class FileLoadManager final : public Actor {
Callback(const Callback &) = delete;
Callback &operator=(const Callback &) = delete;
~Callback() override = default;
virtual void on_start_download(QueryId id) = 0;
virtual void on_partial_download(QueryId id, const PartialLocalFileLocation &partial_local, int64 ready_size) = 0;
virtual void on_partial_upload(QueryId id, const PartialRemoteFileLocation &partial_remote, int64 ready_size) = 0;
virtual void on_upload_ok(QueryId id, FileType file_type, const PartialRemoteFileLocation &remtoe, int64 size) = 0;
@ -80,6 +81,7 @@ class FileLoadManager final : public Actor {
void close_node(NodeId node_id);
void on_start_download();
void on_partial_download(const PartialLocalFileLocation &partial_local, int64 ready_size);
void on_partial_upload(const PartialRemoteFileLocation &partial_remote, int64 ready_size);
void on_ok_download(const FullLocalFileLocation &local, int64 size);
@ -96,6 +98,9 @@ class FileLoadManager final : public Actor {
private:
ActorShared<FileLoadManager> actor_id_;
void on_start_download() override {
send_closure(actor_id_, &FileLoadManager::on_start_download);
}
void on_partial_download(const PartialLocalFileLocation &partial_local, int64 ready_size) override {
send_closure(actor_id_, &FileLoadManager::on_partial_download, partial_local, ready_size);
}

View File

@ -802,6 +802,7 @@ void FileManager::cancel_download(FileNode *node) {
}
send_closure(file_load_manager_, &FileLoadManager::cancel, node->download_id_);
node->download_id_ = 0;
node->is_download_started_ = false;
node->set_download_priority(0);
}
@ -911,8 +912,10 @@ Result<FileId> FileManager::merge(FileId x_file_id, FileId y_file_id, bool no_sy
cancel_download(node);
node->set_local_location(other_node->local_, other_node->local_ready_size_);
node->download_id_ = other_node->download_id_;
node->is_download_started_ |= other_node->is_download_started_;
node->set_download_priority(other_node->download_priority_);
other_node->download_id_ = 0;
other_node->is_download_started_ = false;
other_node->download_priority_ = 0;
//cancel_generate(node);
@ -1245,6 +1248,7 @@ bool FileManager::set_content(FileId file_id, BufferSlice bytes) {
QueryId id = queries_container_.create(Query{file_id, Query::SetContent});
node->download_id_ = id;
node->is_download_started_ = true;
send_closure(file_load_manager_, &FileLoadManager::from_bytes, id, node->remote_.full().type_, std::move(bytes),
node->name_);
return true;
@ -1387,6 +1391,7 @@ void FileManager::run_download(FileNode *node) {
auto file_id = node->file_ids_.back();
QueryId id = queries_container_.create(Query{file_id, Query::Download});
node->download_id_ = id;
node->is_download_started_ = false;
send_closure(file_load_manager_, &FileLoadManager::download, id, node->remote_.full(), node->local_, node->size_,
node->name_, node->encryption_key_, priority);
}
@ -1927,6 +1932,23 @@ FileManager::FileNodeId FileManager::next_file_node_id() {
return res;
}
void FileManager::on_start_download(QueryId query_id) {
auto query = queries_container_.get(query_id);
CHECK(query != nullptr);
auto file_id = query->file_id_;
auto *file_node = get_file_node(file_id);
if (file_node == nullptr) {
return;
}
if (file_node->download_id_ != query_id) {
return;
}
LOG(DEBUG) << "Start to download part of file " << file_id;
file_node->is_download_started_ = true;
}
void FileManager::on_partial_download(QueryId query_id, const PartialLocalFileLocation &partial_local,
int64 ready_size) {
auto query = queries_container_.get(query_id);
@ -2203,6 +2225,7 @@ std::pair<FileManager::Query, bool> FileManager::finish_query(QueryId query_id)
}
if (node->download_id_ == query_id) {
node->download_id_ = 0;
node->is_download_started_ = false;
node->set_download_priority(0);
was_active = true;
}

View File

@ -86,6 +86,7 @@ class FileNode {
FileLoadManager::QueryId download_id_ = 0;
int32 download_priority_ = 0;
int64 remote_ready_size_ = 0;
bool is_download_started_ = false;
GenerateFileLocation generate_;
FileLoadManager::QueryId generate_id_ = 0;
@ -370,6 +371,7 @@ class FileManager : public FileLoadManager::Callback {
void run_download(FileNode *node);
void run_generate(FileNode *node);
void on_start_download(QueryId query_id) override;
void on_partial_download(QueryId query_id, const PartialLocalFileLocation &partial_local, int64 ready_size) override;
void on_partial_upload(QueryId query_id, const PartialRemoteFileLocation &partial_remote, int64 ready_size) override;
void on_download_ok(QueryId query_id, const FullLocalFileLocation &local, int64 size) override;