1 #ifndef ZYPP_NG_MEDIA_CURL_CURL_H_INCLUDED
2 #define ZYPP_NG_MEDIA_CURL_CURL_H_INCLUDED
4 #include <zypp/zyppng/base/zyppglobal.h>
5 #include <zypp/zyppng/base/Base>
6 #include <zypp/zyppng/base/signals.h>
7 #include <zypp/zyppng/core/Url>
10 #include <zypp/zyppng/media/network/networkrequesterror.h>
14 class NetworkRequestDispatcherPrivate;
18 * The NetworkRequestDispatcher class is used to run multiple NetworkRequest instances
19 * at the same time, making full use of the event loop.
21 * Dispatching is implemented using a internal priority queue, all requests in the
22 * queue are set to waiting. Once a request is dequeued it is initialized and started
23 * right away. Its possible to change the maximum number of concurrent connections to control
24 * the load on the network.
27 * zyppng::EventDispatcher::Ptr loop = zyppng::EventDispatcher::createMain();
28 * zyppng::NetworkRequestDispatcher downloader;
30 * zypp::Url url ( "https://download.opensuse.org/distribution/leap/15.0/repo/oss/x86_64/0ad-0.0.22-lp150.2.10.x86_64.rpm" );
31 * zypp::Pathname target("/tmp/0ad-0.0.22-lp150.2.10.x86_64.rpm");
33 * std::shared_ptr<zyppng::NetworkRequest> req = std::make_shared<zyppng::NetworkRequest>( zypp::Url(url), target );
35 * std::shared_ptr<zypp::Digest> dig = std::make_shared<zypp::Digest>();
36 * if ( !dig->create( zypp::Digest::md5() ) ) {
37 * std::cerr << "Unable to create Digest " << std::endl;
41 * req->setDigest( dig );
42 * req->setExpectedChecksum( hexstr2bytes("11822f1421ae50fb1a07f72220b79000") );
44 * req->sigStarted().connect( [] ( const zyppng::NetworkRequest &r ){
45 * std::cout << r.url() << " started downloading " << std::endl;
48 * req->sigFinished().connect( [] ( const zyppng::NetworkRequest &r, const zyppng::NetworkRequestError &err ){
49 * if ( err.isError() ) {
50 * std::cout << r.url() << " finsihed with err " << err.nativeErrorString() << " : "<< err.isError() << " : " << err.toString() << std::endl;
52 * std::cout << r.url() << " finished downloading " << std::endl;
56 * req->sigProgress().connect( [] ( const zyppng::NetworkRequest &r, off_t dltotal, off_t dlnow, off_t ultotal, off_t ulnow ){
57 * std::cout << r.url() << " at: " << std::endl
58 * << "dltotal: "<< dltotal<< std::endl
59 * << "dlnow: "<< dlnow<< std::endl
60 * << "ultotal: "<< ultotal<< std::endl
61 * << "ulnow: "<< ulnow<< std::endl;
64 * downloader.enqueue( req );
66 * downloader.sigQueueFinished().connect( [ &loop ]( const zyppng::NetworkRequestDispatcher & ) {
74 * \sa zyppng::Downloader
76 class LIBZYPP_NG_EXPORT NetworkRequestDispatcher : public Base
78 ZYPP_DECLARE_PRIVATE(NetworkRequestDispatcher)
81 using Ptr = std::shared_ptr<NetworkRequestDispatcher>;
82 using WeakPtr = std::weak_ptr<NetworkRequestDispatcher>;
84 NetworkRequestDispatcher ( );
87 * Returns true if the protocol used in the Url scheme is supported by
88 * the dispatcher backend
90 static bool supportsProtocol ( const Url &url );
93 * Change the number of the concurrently started requests, the default is 10.
95 void setMaximumConcurrentConnections (size_t maxConn );
98 * Enqueues a new \a request and puts it into the waiting queue. If the dispatcher
99 * is already running and has free capacatly the request might be started right away
101 void enqueue ( const std::shared_ptr<NetworkRequest> &req );
104 * Cancels the request \a req setting the error description to \a reason.
106 void cancel ( NetworkRequest &req , std::string reason = std::string() );
109 * Cancels the request \a req setting the error to \a err.
111 void cancel ( NetworkRequest &req , const NetworkRequestError &err );
114 * Start dispatching requests, this needs to be done explicitely before any request can be executed.
119 * Returns the last encountered error in a request.
121 const NetworkRequestError &lastError() const;
124 * Signal is emitted when a download is started
126 SignalProxy<void ( NetworkRequestDispatcher &, NetworkRequest & )> sigDownloadStarted();
129 * Emitted when a request was finished, check the request to see if it was successful
131 SignalProxy<void ( NetworkRequestDispatcher &, NetworkRequest & )> sigDownloadFinished();
134 * Emitted when the internal request queue is completely processed and all requests are finished.
136 SignalProxy<void ( NetworkRequestDispatcher & )> sigQueueFinished ();
139 * Emitted when there is a error in the backend the dispatcher can not recover from. All requests are cancelled
140 * use \a lastError to get more informations.
142 SignalProxy<void ( NetworkRequestDispatcher & )> sigError ();