libzypp: zypp/media/detail/OptionalDownloadProgressReport.cc Source File

libzypp 17.38.5

OptionalDownloadProgressReport.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
9
10#include "OptionalDownloadProgressReport.h"
11
12 namespace internal {
13
14 using namespace zypp;
15
16 OptionalDownloadProgressReport::OptionalDownloadProgressReport(bool isOptional)
17 : _oldRec { Distributor::instance().getReceiver() }
18 , _isOptional { isOptional }
19 { connect(); }
20
21 OptionalDownloadProgressReport::~OptionalDownloadProgressReport() {
22 if (_oldRec)
23 Distributor::instance().setReceiver(*_oldRec);
24 else
25 Distributor::instance().noReceiver();
26 }
27
28 void OptionalDownloadProgressReport::reportbegin()
29 { if ( _oldRec ) _oldRec->reportbegin(); }
30
31 void OptionalDownloadProgressReport::reportend()
32 { if ( _oldRec ) _oldRec->reportend(); }
33
34 void OptionalDownloadProgressReport::report(const UserData &userData_r)
35 { if ( _oldRec ) _oldRec->report( userData_r ); }
36
37 void OptionalDownloadProgressReport::start(const Url &file_r, Pathname localfile_r)
38 {
39 if ( not _oldRec ) return;
40 if ( _isOptional ) {
41 // delay start until first data are received.
42 _startFile = file_r;
43 _startLocalfile = std::move(localfile_r);
44 return;
45 }
46 _oldRec->start( file_r, localfile_r );
47 }
48
49 bool OptionalDownloadProgressReport::progress(int value_r, const Url &file_r, double dbps_avg_r, double dbps_current_r)
50 {
51 if ( not _oldRec ) return true;
52 if ( notStarted() ) {
53 if ( not ( value_r || dbps_avg_r || dbps_current_r ) )
54 return true;
55 sendStart();
56 }
57
58 //static constexpr std::chrono::milliseconds minfequency { 1000 }; only needed if we'd avoid sending reports without change
59 static constexpr std::chrono::milliseconds maxfequency { 100 };
60 TimePoint now { TimePoint::clock::now() };
61 TimePoint::duration elapsed { now - _lastProgressSent };
62 if ( elapsed < maxfequency )
63 return true; // continue
64 _lastProgressSent = now;
65 return _oldRec->progress( value_r, file_r, dbps_avg_r, dbps_current_r );
66 }
67
68 media::DownloadProgressReport::Action OptionalDownloadProgressReport::problem(const Url &file_r, Error error_r, const std::string &description_r)
69 {
70 if ( not _oldRec || notStarted() ) return ABORT;
71 return _oldRec->problem( file_r, error_r, description_r );
72 }
73
74 void OptionalDownloadProgressReport::finish(const Url &file_r, Error error_r, const std::string &reason_r)
75 {
76 if ( not _oldRec || notStarted() ) return;
77 _oldRec->finish( file_r, error_r, reason_r );
78 }
79
80 bool OptionalDownloadProgressReport::notStarted() const
81 { return _isOptional; }
82
83 void OptionalDownloadProgressReport::sendStart()
84 {
85 if ( _isOptional ) {
86 // we know _oldRec is valid...
87 _oldRec->start( _startFile, std::move(_startLocalfile) );
88 _isOptional = false;
89 }
90 }
91
92}
OptionalDownloadProgressReport.h
zypp::Url
Url manipulation class.
Definition Url.h:93
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:48
internal
Definition OptionalDownloadProgressReport.cc:12
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
internal::OptionalDownloadProgressReport::notStarted
bool notStarted() const
Definition OptionalDownloadProgressReport.cc:80
internal::OptionalDownloadProgressReport::~OptionalDownloadProgressReport
~OptionalDownloadProgressReport() override
Definition OptionalDownloadProgressReport.cc:21
internal::OptionalDownloadProgressReport::report
void report(const UserData &userData_r=UserData()) override
The most generic way of sending/receiving data.
Definition OptionalDownloadProgressReport.cc:34
internal::OptionalDownloadProgressReport::_startFile
zypp::Url _startFile
Definition OptionalDownloadProgressReport.h:57
internal::OptionalDownloadProgressReport::_startLocalfile
zypp::Pathname _startLocalfile
Definition OptionalDownloadProgressReport.h:58
internal::OptionalDownloadProgressReport::start
void start(const zypp::Url &file_r, zypp::Pathname localfile_r) override
Definition OptionalDownloadProgressReport.cc:37
internal::OptionalDownloadProgressReport::reportbegin
void reportbegin() override
Definition OptionalDownloadProgressReport.cc:28
internal::OptionalDownloadProgressReport::finish
void finish(const zypp::Url &file_r, Error error_r, const std::string &reason_r) override
Definition OptionalDownloadProgressReport.cc:74
internal::OptionalDownloadProgressReport::reportend
void reportend() override
Definition OptionalDownloadProgressReport.cc:31
internal::OptionalDownloadProgressReport::problem
Action problem(const zypp::Url &file_r, Error error_r, const std::string &description_r) override
Definition OptionalDownloadProgressReport.cc:68
internal::OptionalDownloadProgressReport::OptionalDownloadProgressReport
OptionalDownloadProgressReport(bool isOptional=false)
Definition OptionalDownloadProgressReport.cc:16
internal::OptionalDownloadProgressReport::TimePoint
std::chrono::steady_clock::time_point TimePoint
Definition OptionalDownloadProgressReport.h:23
internal::OptionalDownloadProgressReport::_oldRec
Receiver *const _oldRec
Definition OptionalDownloadProgressReport.h:55
internal::OptionalDownloadProgressReport::progress
bool progress(int value_r, const zypp::Url &file_r, double dbps_avg_r=-1, double dbps_current_r=-1) override
Download progress.
Definition OptionalDownloadProgressReport.cc:49
internal::OptionalDownloadProgressReport::_isOptional
bool _isOptional
Definition OptionalDownloadProgressReport.h:56
internal::OptionalDownloadProgressReport::sendStart
void sendStart()
Definition OptionalDownloadProgressReport.cc:83
internal::OptionalDownloadProgressReport::_lastProgressSent
TimePoint _lastProgressSent
Definition OptionalDownloadProgressReport.h:59
zypp::callback::DistributeReport::noReceiver
void noReceiver()
Definition Callback.h:219
zypp::callback::DistributeReport::instance
static DistributeReport & instance()
Definition Callback.h:204
zypp::callback::DistributeReport::setReceiver
void setReceiver(Receiver &rec_r)
Definition Callback.h:213
zypp::callback::ReceiveReport< zypp::media::DownloadProgressReport >::connect
void connect()
Definition Callback.h:183
zypp::callback::ReceiveReport< zypp::media::DownloadProgressReport >::Distributor
DistributeReport< zypp::media::DownloadProgressReport > Distributor
Definition Callback.h:172
zypp::callback::ReportBase::UserData
callback::UserData UserData
Definition Callback.h:151
zypp::media::DownloadProgressReport::Action
Action
Definition ZYppCallbacks.h:364
zypp::media::DownloadProgressReport::ABORT
@ ABORT
Definition ZYppCallbacks.h:365
zypp::media::DownloadProgressReport::Error
Error
Definition ZYppCallbacks.h:370