libzypp  17.35.14
provide_p.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \----------------------------------------------------------------------/
9 *
10 * This file contains private API, this might break at any time between releases.
11 * You have been warned!
12 *
13 */
14 #ifndef ZYPP_MEDIA_PRIVATE_PROVIDE_P_H_INCLUDED
15 #define ZYPP_MEDIA_PRIVATE_PROVIDE_P_H_INCLUDED
16 
17 #include "providefwd_p.h"
18 #include "providequeue_p.h"
19 #include "attachedmediainfo_p.h"
20 
21 #include <zypp-media/auth/CredentialManager>
22 #include <zypp-media/ng/Provide>
23 #include <zypp-media/ng/ProvideItem>
24 #include <zypp-media/ng/ProvideSpec>
26 #include <zypp-core/zyppng/base/Timer>
27 #include <zypp-core/ManagedFile.h>
28 
29 namespace zyppng {
30 
31  namespace constants {
32  constexpr std::string_view DEFAULT_PROVIDE_WORKER_PATH = ZYPP_WORKER_PATH;
33  constexpr std::string_view ATTACHED_MEDIA_SUFFIX = "-media";
34  constexpr auto DEFAULT_ACTIVE_CONN_PER_HOST = 5; //< how many simultanious connections to the same host are allowed
35  constexpr auto DEFAULT_ACTIVE_CONN = 10; //< how many simultanious connections are allowed
36  constexpr auto DEFAULT_MAX_DYNAMIC_WORKERS = 20;
37  constexpr auto DEFAULT_CPU_WORKERS = 4;
38  }
39 
40  class ProvideQueue;
41  class ProvidePrivate : public BasePrivate
42  {
44  public:
45  ProvidePrivate( zypp::Pathname &&workDir, Provide &pub );
46 
48  {
55  };
56 
57  void schedule( ScheduleReason reason );
58 
59  bool queueRequest ( ProvideRequestRef req );
60  bool dequeueRequest( ProvideRequestRef req, std::exception_ptr error );
61  void queueItem ( ProvideItemRef item );
62  void dequeueItem ( ProvideItem *item );
63 
64  std::string nextMediaId () const;
65  AttachedMediaInfo_Ptr addMedium ( AttachedMediaInfo_Ptr &&medium );
66 
67  std::string effectiveScheme ( const std::string &scheme ) const;
68 
69  void onPulseTimeout ( Timer & );
70  void onQueueIdle ();
71  void onItemStateChanged ( ProvideItem &item );
72  expected<ProvideQueue::Config> schemeConfig(const std::string &scheme);
73 
74  std::optional<zypp::ManagedFile> addToFileCache ( const zypp::Pathname &downloadedFile );
75  bool isInCache ( const zypp::Pathname &downloadedFile ) const;
76 
77  bool isRunning() const;
78 
79  const zypp::Pathname &workerPath() const;
80  const std::string queueName( ProvideQueue &q ) const;
81 
82  std::vector<AttachedMediaInfo_Ptr> &attachedMediaInfos();
83 
84  std::list<ProvideItemRef> &items();
85 
87 
88  std::vector<zypp::Url> sanitizeUrls ( const std::vector<zypp::Url> &urls );
89 
90  ProvideStatusRef log () {
91  return _log;
92  }
93 
94  uint32_t nextRequestId();
95 
97  Signal< std::optional<zypp::media::AuthData> ( const zypp::Url &reqUrl, const std::string &triedUsername, const std::map<std::string, std::string> &extraValues ) > _sigAuthRequired;
98 
99  protected:
100  void doSchedule (Timer &);
101 
102  //@TODO should we make those configurable?
103  std::unordered_map< std::string, std::string > _workerAlias {
104  {"ftp" ,"http"},
105  {"tftp" ,"http"},
106  {"https","http"},
107  {"cifs" ,"smb" },
108  {"nfs4" ,"nfs" },
109  {"cd" ,"disc"},
110  {"dvd" ,"disc"},
111  {"file" ,"dir" },
112  {"hd" ,"disk"}
113  };
114 
115  bool _isRunning = false;
116  bool _isScheduling = false;
118  Timer::Ptr _scheduleTrigger = Timer::create(); //< instead of constantly calling schedule we set a trigger event so it runs as soon as event loop is on again
120 
121  std::list< ProvideItemRef > _items; //< The list of running provide Items, each of them can spawn multiple requests
122  uint32_t _nextRequestId = 0; //< The next request ID , we use controller wide unique IDs instead of worker locals IDs , its easier to track
123 
124  struct QueueItem {
125  std::string _schemeName;
126  std::deque<ProvideRequestRef> _requests;
127  };
128  std::deque<QueueItem> _queues; //< List of request queues for the workers, grouped by scheme. We use a deque and not a map because of possible changes to the list of queues during scheduling
129 
130 
131  std::vector< AttachedMediaInfo_Ptr > _attachedMediaInfos; //< List of currently attached medias
132 
133  std::unordered_map< std::string, ProvideQueueRef > _workerQueues;
134  std::unordered_map< std::string, ProvideQueue::Config > _schemeConfigs;
135 
136  struct FileCacheItem {
138  std::optional<std::chrono::steady_clock::time_point> _deathTimer; // timepoint where this item was seen first without a refcount
139  };
140  std::unordered_map< std::string, FileCacheItem > _fileCache;
141 
144 
145  ProvideStatusRef _log;
147  };
148 }
149 
150 #endif
constexpr auto DEFAULT_MAX_DYNAMIC_WORKERS
Definition: provide_p.h:36
std::optional< zypp::ManagedFile > addToFileCache(const zypp::Pathname &downloadedFile)
Definition: provide.cc:759
bool queueRequest(ProvideRequestRef req)
Definition: provide.cc:821
ProvideStatusRef _log
Definition: provide_p.h:145
Signal< Provide::MediaChangeAction(const std::string &, const std::string &, const int32_t, const std::vector< std::string > &, const std::optional< std::string > &) > _sigMediaChange
Definition: provide_p.h:96
std::list< ProvideItemRef > _items
Definition: provide_p.h:121
Signal< std::optional< zypp::media::AuthData > const zypp::Url &reqUrl, const std::string &triedUsername, const std::map< std::string, std::string > &extraValues) > _sigAuthRequired
Definition: provide_p.h:97
Signal< void()> _sigIdle
Definition: provide_p.h:146
const zypp::Pathname & workerPath() const
Definition: provide.cc:859
std::string nextMediaId() const
Definition: provide.cc:803
std::unordered_map< std::string, ProvideQueue::Config > _schemeConfigs
Definition: provide_p.h:134
std::optional< std::chrono::steady_clock::time_point > _deathTimer
Definition: provide_p.h:138
uint32_t nextRequestId()
Definition: provide.cc:946
zypp::Pathname _workerPath
Definition: provide_p.h:142
Timer::Ptr _scheduleTrigger
Definition: provide_p.h:118
std::vector< AttachedMediaInfo_Ptr > & attachedMediaInfos()
Definition: provide.cc:739
bool isInCache(const zypp::Pathname &downloadedFile) const
Definition: provide.cc:779
expected< ProvideQueue::Config > schemeConfig(const std::string &scheme)
Definition: provide.cc:744
ProvidePrivate(zypp::Pathname &&workDir, Provide &pub)
Definition: provide.cc:21
std::deque< QueueItem > _queues
Definition: provide_p.h:128
void onItemStateChanged(ProvideItem &item)
Definition: provide.cc:928
std::list< ProvideItemRef > & items()
Definition: provide.cc:699
void onPulseTimeout(Timer &)
Definition: provide.cc:887
bool isRunning() const
Definition: provide.cc:873
The Timer class provides repetitive and single-shot timers.
Definition: timer.h:44
ProvideStatusRef log()
Definition: provide_p.h:90
std::vector< AttachedMediaInfo_Ptr > _attachedMediaInfos
Definition: provide_p.h:131
void schedule(ScheduleReason reason)
Definition: provide.cc:38
zypp::Pathname _workDir
Definition: provide_p.h:119
constexpr auto DEFAULT_CPU_WORKERS
Definition: provide_p.h:37
constexpr auto DEFAULT_ACTIVE_CONN_PER_HOST
Definition: provide_p.h:34
const std::string queueName(ProvideQueue &q) const
Definition: provide.cc:864
void dequeueItem(ProvideItem *item)
Definition: provide.cc:791
zypp::media::CredManagerOptions _credManagerOptions
Definition: provide_p.h:143
std::shared_ptr< Timer > Ptr
Definition: timer.h:51
static std::shared_ptr< Timer > create()
Creates a new Timer object, the timer is not started at this point.
Definition: timer.cc:52
constexpr std::string_view DEFAULT_PROVIDE_WORKER_PATH
Definition: provide_p.h:32
std::unordered_map< std::string, FileCacheItem > _fileCache
Definition: provide_p.h:140
bool dequeueRequest(ProvideRequestRef req, std::exception_ptr error)
Definition: provide.cc:837
constexpr auto DEFAULT_ACTIVE_CONN
Definition: provide_p.h:35
void queueItem(ProvideItemRef item)
Definition: provide.cc:785
zypp::media::CredManagerOptions & credManagerOptions()
Definition: provide.cc:704
std::unordered_map< std::string, std::string > _workerAlias
Definition: provide_p.h:103
#define ZYPP_DECLARE_PUBLIC(Class)
Definition: zyppglobal.h:98
Timer::Ptr _pulseTimer
Definition: provide_p.h:117
AttachedMediaInfo_Ptr addMedium(AttachedMediaInfo_Ptr &&medium)
Definition: provide.cc:809
void doSchedule(Timer &)
Definition: provide.cc:70
std::unordered_map< std::string, ProvideQueueRef > _workerQueues
Definition: provide_p.h:133
std::string effectiveScheme(const std::string &scheme) const
Definition: provide.cc:878
std::deque< ProvideRequestRef > _requests
Definition: provide_p.h:126
constexpr std::string_view ATTACHED_MEDIA_SUFFIX
Definition: provide_p.h:33
std::vector< zypp::Url > sanitizeUrls(const std::vector< zypp::Url > &urls)
Definition: provide.cc:709
Url manipulation class.
Definition: Url.h:91