libzypp  17.32.5
provideworker.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
9 
10 #ifndef ZYPP_MEDIA_PROVIDE_WORKER_H_INCLUDED
11 #define ZYPP_MEDIA_PROVIDE_WORKER_H_INCLUDED
12 
13 #include <zypp-core/zyppng/base/Base>
14 #include <zypp-core/zyppng/base/EventLoop>
15 #include <zypp-core/zyppng/base/Timer>
16 #include <zypp-core/zyppng/io/AsyncDataSource>
17 #include <zypp-core/zyppng/rpc/MessageStream>
18 #include <zypp-core/zyppng/pipelines/Expected>
21 #include <zypp-media/ng/HeaderValueMap>
22 #include <zypp-media/MediaException>
23 #include <zypp-media/Mount>
24 
25 #include <string_view>
26 #include <deque>
27 
28 namespace zyppng::worker {
29 
33 
34  struct AuthInfo
35  {
36  std::string username;
37  std::string password;
38  int64_t last_auth_timestamp = 0;
39  std::map<std::string, std::string> extraKeys = {};
40  };
41 
43  {
44  public:
46  };
47 
50 
52  {
53  public:
54  enum State {
58  };
59 
60  ProvideWorkerItem( ProvideMessage &&spec ) : _spec( std::move(spec) ) { }
61 
64  };
65 
66  class ProvideWorker : public Base
67  {
68  public:
69 
71  ONLY_NEW_PROVIDES, // provide is called only when new provide requests are added to the queue
72  QUEUE_NOT_EMTPY // provide is called continiously until the queue is empty
73  };
74 
75  ProvideWorker( std::string_view workerName );
76  virtual ~ProvideWorker();
77 
79 
80  expected<void> run ( int recv = STDIN_FILENO, int send = STDOUT_FILENO );
81 
82  std::deque<ProvideWorkerItemRef> &requestQueue();
86  virtual void immediateShutdown (){};
87 
96  };
97  MediaChangeRes requestMediaChange ( const uint32_t id, const std::string &label, const int32_t mediaNr, const std::vector<std::string> &devices, const std::optional<std::string> &desc = {} );
98 
106  expected<AuthInfo> requireAuthorization ( const uint32_t id, const zypp::Url &url, const std::string &lastTriedUsername = "", const int64_t lastTimestamp = -1, const std::map<std::string, std::string> &extraFields = {} );
107 
110 
111  protected:
112  virtual void initLog();
113  virtual expected<WorkerCaps> initialize ( const Configuration &conf ) = 0;
114 
118  virtual void provide ( ) = 0;
119  virtual void cancel ( const std::deque<ProvideWorkerItemRef>::iterator &request ) = 0;
120 
127  virtual ProvideWorkerItemRef makeItem (ProvideMessage &&spec );
128 
138  void provideStart ( const uint32_t id, const zypp::Url &url, const zypp::Pathname &localFile, const zypp::Pathname &stagingFile = {} );
139 
144  void provideSuccess (const uint32_t id, bool cacheHit, const zypp::Pathname &localFile, const HeaderValueMap extra = {} );
145 
151  void provideFailed ( const uint32_t id, const uint code, const std::string &reason, const bool transient, const HeaderValueMap extra = {} );
152 
158  void provideFailed ( const uint32_t id, const uint code, const bool transient, const zypp::Exception &e );
159 
163  void attachSuccess ( const uint32_t id, const std::optional<std::string> &localMountPoint = {} );
164 
168  void detachSuccess ( const uint32_t id );
169 
174  void redirect ( const uint32_t id, const zypp::Url &url, const zypp::Pathname &newPath );
175 
179  AsyncDataSource &controlIO ();
180 
181 
182  private:
183  expected<void> executeHandshake ();
184  void maybeDelayedShutdown ();
185  void messageLoop ( Timer & );
188  void messageReceived ();
189  void onInvalidMessageReceived ( );
190  void invalidMessageReceived ( std::exception_ptr p );
191  void handleSingleMessage (const ProvideMessage &provide );
192  void pushSingleMessage ( const RpcMessage &msg );
193  expected<ProvideMessage> sendAndWaitForResponse ( const ProvideMessage &request, const std::vector<uint> &responseCodes );
194  expected<ProvideMessage> parseReceivedMessage( const RpcMessage &m );
195 
196  private:
198  bool _inControllerRequest = false; //< Used to signalize that we are currently in a blocking controller callback
199  bool _isRunning = false;
200  std::string_view _workerName;
207 
208  std::exception_ptr _fatalError; //< Error that caused the eventloop to stop
209 
210  std::deque<ProvideMessage> _pendingMessages;
211  std::deque<ProvideWorkerItemRef> _pendingProvides;
212  };
213 }
214 
215 
216 #endif
ProvideNotificatioMode provNotificationMode() const
ProvideNotificatioMode _provNotificationMode
void pushSingleMessage(const RpcMessage &msg)
std::exception_ptr _fatalError
virtual void cancel(const std::deque< ProvideWorkerItemRef >::iterator &request)=0
expected< ProvideMessage > parseReceivedMessage(const RpcMessage &m)
expected< void > run(int recv=STDIN_FILENO, int send=STDOUT_FILENO)
Definition: Arch.h:363
RpcMessageStream::Ptr _stream
void readFdClosed(uint, AsyncDataSource::ChannelCloseReason)
void provideStart(const uint32_t id, const zypp::Url &url, const zypp::Pathname &localFile, const zypp::Pathname &stagingFile={})
EventLoopRef Ptr
Definition: eventloop.h:47
void provideFailed(const uint32_t id, const uint code, const std::string &reason, const bool transient, const HeaderValueMap extra={})
MediaChangeRes requestMediaChange(const uint32_t id, const std::string &label, const int32_t mediaNr, const std::vector< std::string > &devices, const std::optional< std::string > &desc={})
void invalidMessageReceived(std::exception_ptr p)
ProviderConfiguration _workerConf
expected< ProvideMessage > sendAndWaitForResponse(const ProvideMessage &request, const std::vector< uint > &responseCodes)
void setProvNotificationMode(const ProvideNotificatioMode &provNotificationMode)
virtual void immediateShutdown()
Definition: provideworker.h:86
zyppng::WorkerCaps WorkerCaps
Definition: provideworker.h:30
ProvideWorker(std::string_view workerName)
Just inherits Exception to separate media exceptions.
virtual ProvideWorkerItemRef makeItem(ProvideMessage &&spec)
AsyncDataSource & controlIO()
void redirect(const uint32_t id, const zypp::Url &url, const zypp::Pathname &newPath)
expected< void > executeHandshake()
ProvideWorkerItem(ProvideMessage &&spec)
Definition: provideworker.h:60
static Ptr create()
RpcMessageStream::Ptr messageStream() const
std::deque< ProvideWorkerItemRef > & requestQueue()
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
std::map< std::string, std::string > extraKeys
Definition: provideworker.h:39
void attachSuccess(const uint32_t id, const std::optional< std::string > &localMountPoint={})
Base class for Exception.
Definition: Exception.h:146
void handleSingleMessage(const ProvideMessage &provide)
ZYPP_FWD_DECL_TYPE_WITH_REFS(DeviceDriver)
zyppng::ProviderConfiguration Configuration
Definition: provideworker.h:32
AsyncDataSource::Ptr _controlIO
void detachSuccess(const uint32_t id)
expected< AuthInfo > requireAuthorization(const uint32_t id, const zypp::Url &url, const std::string &lastTriedUsername="", const int64_t lastTimestamp=-1, const std::map< std::string, std::string > &extraFields={})
void writeFdClosed(AsyncDataSource::ChannelCloseReason)
std::deque< ProvideWorkerItemRef > _pendingProvides
void provideSuccess(const uint32_t id, bool cacheHit, const zypp::Pathname &localFile, const HeaderValueMap extra={})
std::deque< ProvideMessage > _pendingMessages
Url manipulation class.
Definition: Url.h:91
virtual expected< WorkerCaps > initialize(const Configuration &conf)=0
std::shared_ptr< AsyncDataSource > Ptr
std::shared_ptr< RpcMessageStream > Ptr