Imported Upstream version 17.22.1
[platform/upstream/libzypp.git] / zypp / zyppng / media / network / networkrequestdispatcher.h
1 #ifndef ZYPP_NG_MEDIA_CURL_CURL_H_INCLUDED
2 #define ZYPP_NG_MEDIA_CURL_CURL_H_INCLUDED
3
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>
8 #include <vector>
9
10 #include <zypp/zyppng/media/network/networkrequesterror.h>
11
12 namespace zyppng {
13
14   class NetworkRequestDispatcherPrivate;
15   class NetworkRequest;
16
17   /*!
18    * The NetworkRequestDispatcher class is used to run multiple NetworkRequest instances
19    * at the same time, making full use of the event loop.
20    *
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.
25    *
26    * \code
27    * zyppng::EventDispatcher::Ptr loop = zyppng::EventDispatcher::createMain();
28    * zyppng::NetworkRequestDispatcher downloader;
29    *
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");
32    *
33    * std::shared_ptr<zyppng::NetworkRequest> req = std::make_shared<zyppng::NetworkRequest>( zypp::Url(url), target );
34    *
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;
38    *   return 1;
39    * }
40    *
41    * req->setDigest( dig );
42    * req->setExpectedChecksum( hexstr2bytes("11822f1421ae50fb1a07f72220b79000") );
43    *
44    * req->sigStarted().connect( [] ( const zyppng::NetworkRequest &r ){
45    *   std::cout << r.url() << " started downloading " << std::endl;
46    * });
47    *
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;
51    *   } else {
52    *     std::cout << r.url() << " finished downloading " << std::endl;
53    *   }
54    * });
55    *
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;
62    * });
63    *
64    * downloader.enqueue( req );
65    *
66    * downloader.sigQueueFinished().connect( [ &loop ]( const zyppng::NetworkRequestDispatcher & ) {
67    *   loop->quit();
68    * });
69    *
70    * downloader.run( );
71    * loop->run();
72    * \code
73    *
74    * \sa zyppng::Downloader
75    */
76   class LIBZYPP_NG_EXPORT NetworkRequestDispatcher : public Base
77   {
78     ZYPP_DECLARE_PRIVATE(NetworkRequestDispatcher)
79     public:
80
81       using Ptr = std::shared_ptr<NetworkRequestDispatcher>;
82       using WeakPtr = std::weak_ptr<NetworkRequestDispatcher>;
83
84       NetworkRequestDispatcher ( );
85
86       /*!
87        * Returns true if the protocol used in the Url scheme is supported by
88        * the dispatcher backend
89        */
90       static bool supportsProtocol ( const Url &url );
91
92       /*!
93        * Change the number of the concurrently started requests, the default is 10.
94        */
95       void setMaximumConcurrentConnections (size_t maxConn );
96
97       /*!
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
100        */
101       void enqueue ( const std::shared_ptr<NetworkRequest> &req );
102
103       /*!
104        * Cancels the request \a req setting the error description to \a reason.
105        */
106       void cancel  ( NetworkRequest &req , std::string reason = std::string() );
107
108       /*!
109        * Cancels the request \a req setting the error to \a err.
110        */
111       void cancel  ( NetworkRequest &req , const NetworkRequestError &err );
112
113       /*!
114        * Start dispatching requests, this needs to be done explicitely before any request can be executed.
115        */
116       void run ( );
117
118       /*!
119        * Returns the last encountered error in a request.
120        */
121       const NetworkRequestError &lastError() const;
122
123       /*!
124        * Signal is emitted when a download is started
125        */
126       SignalProxy<void ( NetworkRequestDispatcher &, NetworkRequest & )> sigDownloadStarted();
127
128       /*!
129        * Emitted when a request was finished, check the request to see if it was successful
130        */
131       SignalProxy<void ( NetworkRequestDispatcher &, NetworkRequest & )> sigDownloadFinished();
132
133       /*!
134        * Emitted when the internal request queue is completely processed and all requests are finished.
135        */
136       SignalProxy<void ( NetworkRequestDispatcher & )> sigQueueFinished ();
137
138       /*!
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.
141        */
142       SignalProxy<void ( NetworkRequestDispatcher & )> sigError ();
143    };
144 }
145
146
147 #endif