libzypp  17.35.15
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/pipelines/Expected>
20 #include <zypp-media/ng/HeaderValueMap>
21 #include <zypp-media/MediaException>
22 #include <zypp-media/Mount>
23 
24 #include <string_view>
25 #include <deque>
26 
27 namespace zyppng::worker {
28 
32 
33  struct AuthInfo
34  {
35  std::string username;
36  std::string password;
37  int64_t last_auth_timestamp = 0;
38  std::map<std::string, std::string> extraKeys = {};
39  };
40 
42  {
43  public:
45  };
46 
49 
51  {
52  public:
53  enum State {
57  };
58 
59  ProvideWorkerItem( ProvideMessage &&spec ) : _spec( std::move(spec) ) { }
60 
63  };
64 
65  class ProvideWorker : public Base
66  {
67  public:
68 
70  ONLY_NEW_PROVIDES, // provide is called only when new provide requests are added to the queue
71  QUEUE_NOT_EMTPY // provide is called continiously until the queue is empty
72  };
73 
74  ProvideWorker( std::string_view workerName );
75  virtual ~ProvideWorker();
76 
77  StompFrameStreamRef messageStream() const;
78 
79  expected<void> run ( int recv = STDIN_FILENO, int send = STDOUT_FILENO );
80 
81  std::deque<ProvideWorkerItemRef> &requestQueue();
85  virtual void immediateShutdown (){};
86 
95  };
96  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 = {} );
97 
105  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 = {} );
106 
109 
110  protected:
111  virtual void initLog();
112  virtual expected<WorkerCaps> initialize ( const Configuration &conf ) = 0;
113 
117  virtual void provide ( ) = 0;
118  virtual void cancel ( const std::deque<ProvideWorkerItemRef>::iterator &request ) = 0;
119 
126  virtual ProvideWorkerItemRef makeItem (ProvideMessage &&spec );
127 
137  void provideStart ( const uint32_t id, const zypp::Url &url, const zypp::Pathname &localFile, const zypp::Pathname &stagingFile = {} );
138 
143  void provideSuccess (const uint32_t id, bool cacheHit, const zypp::Pathname &localFile, const HeaderValueMap extra = {} );
144 
150  void provideFailed (const uint32_t id, const ProvideMessage::Code code, const std::string &reason, const bool transient, const HeaderValueMap extra = {} );
151 
157  void provideFailed (const uint32_t id, const ProvideMessage::Code code, const bool transient, const zypp::Exception &e );
158 
162  void attachSuccess ( const uint32_t id, const std::optional<std::string> &localMountPoint = {} );
163 
167  void detachSuccess ( const uint32_t id );
168 
173  void redirect ( const uint32_t id, const zypp::Url &url, const zypp::Pathname &newPath );
174 
178  AsyncDataSource &controlIO ();
179 
180 
181  private:
182  expected<void> executeHandshake ();
183  void maybeDelayedShutdown ();
184  void messageLoop ( Timer & );
187  void messageReceived ();
188  void onInvalidMessageReceived ( );
189  void invalidMessageReceived ( std::exception_ptr p );
190  void handleSingleMessage (const ProvideMessage &provide );
191  void pushSingleMessage (const zypp::PluginFrame &msg );
192  expected<ProvideMessage> sendAndWaitForResponse ( const ProvideMessage &request, const std::vector<uint> &responseCodes );
193  expected<ProvideMessage> parseReceivedMessage(const zypp::PluginFrame &m );
194 
195  private:
197  bool _inControllerRequest = false; //< Used to signalize that we are currently in a blocking controller callback
198  bool _isRunning = false;
199  std::string_view _workerName;
204  StompFrameStreamRef _stream;
206 
207  std::exception_ptr _fatalError; //< Error that caused the eventloop to stop
208 
209  std::deque<ProvideMessage> _pendingMessages;
210  std::deque<ProvideWorkerItemRef> _pendingProvides;
211  };
212 }
213 
214 
215 #endif
void pushSingleMessage(const zypp::PluginFrame &msg)
ProvideNotificatioMode provNotificationMode() const
ProvideNotificatioMode _provNotificationMode
std::exception_ptr _fatalError
virtual void cancel(const std::deque< ProvideWorkerItemRef >::iterator &request)=0
Command frame for communication with PluginScript.
Definition: PluginFrame.h:41
#define ZYPP_API
Definition: Globals.h:69
StompFrameStreamRef messageStream() const
expected< void > run(int recv=STDIN_FILENO, int send=STDOUT_FILENO)
Definition: Arch.h:363
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
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)
expected< ProvideMessage > parseReceivedMessage(const zypp::PluginFrame &m)
ProviderConfiguration _workerConf
expected< ProvideMessage > sendAndWaitForResponse(const ProvideMessage &request, const std::vector< uint > &responseCodes)
void setProvNotificationMode(const ProvideNotificatioMode &provNotificationMode)
virtual void immediateShutdown()
Definition: provideworker.h:85
zyppng::WorkerCaps WorkerCaps
Definition: provideworker.h:29
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:59
static Ptr create()
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:38
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:31
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 provideFailed(const uint32_t id, const ProvideMessage::Code code, const std::string &reason, const bool transient, const HeaderValueMap extra={})
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
StompFrameStreamRef _stream