libzypp  17.31.31
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>
19 #include <zypp-proto/media/provider.pb.h>
22 #include <zypp-media/ng/HeaderValueMap>
23 #include <zypp-media/MediaException>
24 #include <zypp-media/Mount>
25 
26 #include <string_view>
27 #include <deque>
28 
29 namespace zyppng::worker {
30 
31  using WorkerCaps = zypp::proto::Capabilities;
32  using Message = zypp::proto::Envelope;
34 
35  struct AuthInfo
36  {
37  std::string username;
38  std::string password;
39  int64_t last_auth_timestamp = 0;
40  std::map<std::string, std::string> extraKeys = {};
41  };
42 
44  {
45  public:
47  };
48 
51 
52  class ProvideWorkerItem : public zyppng::Base
53  {
54  public:
55  enum State {
59  };
60 
61  ProvideWorkerItem( ProvideMessage &&spec ) : _spec( std::move(spec) ) { }
62 
65  };
66 
67  class ProvideWorker : public Base
68  {
69  public:
70 
72  ONLY_NEW_PROVIDES, // provide is called only when new provide requests are added to the queue
73  QUEUE_NOT_EMTPY // provide is called continiously until the queue is empty
74  };
75 
76  ProvideWorker( std::string_view workerName );
77  virtual ~ProvideWorker();
78 
79  RpcMessageStream::Ptr messageStream() const;
80 
81  expected<void> run ( int recv = STDIN_FILENO, int send = STDOUT_FILENO );
82 
83  std::deque<ProvideWorkerItemRef> &requestQueue();
87  virtual void immediateShutdown (){};
88 
97  };
98  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 = {} );
99 
107  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 = {} );
108 
111 
112  protected:
113  virtual void initLog();
114  virtual expected<WorkerCaps> initialize ( const Configuration &conf ) = 0;
115 
119  virtual void provide ( ) = 0;
120  virtual void cancel ( const std::deque<ProvideWorkerItemRef>::iterator &request ) = 0;
121 
128  virtual ProvideWorkerItemRef makeItem (ProvideMessage &&spec );
129 
139  void provideStart ( const uint32_t id, const zypp::Url &url, const zypp::Pathname &localFile, const zypp::Pathname &stagingFile = {} );
140 
145  void provideSuccess (const uint32_t id, bool cacheHit, const zypp::Pathname &localFile, const HeaderValueMap extra = {} );
146 
152  void provideFailed ( const uint32_t id, const uint code, const std::string &reason, const bool transient, const HeaderValueMap extra = {} );
153 
159  void provideFailed ( const uint32_t id, const uint code, const bool transient, const zypp::Exception &e );
160 
164  void attachSuccess ( const uint32_t id );
165 
169  void detachSuccess ( const uint32_t id );
170 
175  void redirect ( const uint32_t id, const zypp::Url &url, const zypp::Pathname &newPath );
176 
180  AsyncDataSource &controlIO ();
181 
182 
183  private:
184  expected<void> executeHandshake ();
185  void maybeDelayedShutdown ();
186  void messageLoop ( Timer & );
187  void readFdClosed ( uint, AsyncDataSource::ChannelCloseReason );
188  void writeFdClosed ( AsyncDataSource::ChannelCloseReason );
189  void messageReceived ();
190  void onInvalidMessageReceived ( );
191  void invalidMessageReceived ( std::exception_ptr p );
192  void handleSingleMessage (const ProvideMessage &provide );
193  void pushSingleMessage ( const RpcMessage &msg );
194  expected<ProvideMessage> sendAndWaitForResponse ( const ProvideMessage &request, const std::vector<uint> &responseCodes );
195  expected<ProvideMessage> parseReceivedMessage( const RpcMessage &m );
196 
197  private:
199  bool _inControllerRequest = false; //< Used to signalize that we are currently in a blocking controller callback
200  bool _isRunning = false;
201  std::string_view _workerName;
202  EventLoop::Ptr _loop = EventLoop::create();
203  Timer::Ptr _msgAvail = Timer::create();
204  Timer::Ptr _delayedShutdown = Timer::create();
205  AsyncDataSource::Ptr _controlIO;
206  RpcMessageStream::Ptr _stream;
208 
209  std::exception_ptr _fatalError; //< Error that caused the eventloop to stop
210 
211  std::deque<ProvideMessage> _pendingMessages;
212  std::deque<ProvideWorkerItemRef> _pendingProvides;
213  };
214 }
215 
216 
217 #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
zypp::proto::Envelope Message
Definition: provideworker.h:32
zypp::proto::Capabilities WorkerCaps
Definition: provideworker.h:31
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 attachSuccess(const uint32_t id)
void provideStart(const uint32_t id, const zypp::Url &url, const zypp::Pathname &localFile, const zypp::Pathname &stagingFile={})
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)
expected< ProvideMessage > sendAndWaitForResponse(const ProvideMessage &request, const std::vector< uint > &responseCodes)
void setProvNotificationMode(const ProvideNotificatioMode &provNotificationMode)
virtual void immediateShutdown()
Definition: provideworker.h:87
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:61
RpcMessageStream::Ptr messageStream() const
std::deque< ProvideWorkerItemRef > & requestQueue()
std::map< std::string, std::string > extraKeys
Definition: provideworker.h:40
Base class for Exception.
Definition: Exception.h:145
void handleSingleMessage(const ProvideMessage &provide)
ZYPP_FWD_DECL_TYPE_WITH_REFS(DeviceDriver)
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)
zypp::proto::Configuration Configuration
Definition: provideworker.h:33
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