libzypp 17.31.23
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
29namespace zyppng::worker {
30
31 using WorkerCaps = zypp::proto::Capabilities;
32 using Message = zypp::proto::Envelope;
33 using Configuration = zypp::proto::Configuration;
34
35 struct AuthInfo
36 {
37 std::string username;
38 std::string password;
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
96 SKIP
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 ();
191 void invalidMessageReceived ( std::exception_ptr p );
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
Base class for Exception.
Definition: Exception.h:146
Url manipulation class.
Definition: Url.h:92
Just inherits Exception to separate media exceptions.
ProvideWorkerItem(ProvideMessage &&spec)
Definition: provideworker.h:61
void detachSuccess(const uint32_t id)
virtual void immediateShutdown()
Definition: provideworker.h:87
std::deque< ProvideWorkerItemRef > _pendingProvides
void handleSingleMessage(const ProvideMessage &provide)
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={})
AsyncDataSource & controlIO()
expected< void > executeHandshake()
AsyncDataSource::Ptr _controlIO
void provideStart(const uint32_t id, const zypp::Url &url, const zypp::Pathname &localFile, const zypp::Pathname &stagingFile={})
virtual void cancel(const std::deque< ProvideWorkerItemRef >::iterator &request)=0
std::deque< ProvideMessage > _pendingMessages
void writeFdClosed(AsyncDataSource::ChannelCloseReason)
void attachSuccess(const uint32_t id)
void provideSuccess(const uint32_t id, bool cacheHit, const zypp::Pathname &localFile, const HeaderValueMap extra={})
RpcMessageStream::Ptr messageStream() const
RpcMessageStream::Ptr _stream
ProvideNotificatioMode provNotificationMode() const
expected< void > run(int recv=STDIN_FILENO, int send=STDOUT_FILENO)
virtual ProvideWorkerItemRef makeItem(ProvideMessage &&spec)
void setProvNotificationMode(const ProvideNotificatioMode &provNotificationMode)
virtual expected< WorkerCaps > initialize(const Configuration &conf)=0
void readFdClosed(uint, AsyncDataSource::ChannelCloseReason)
void pushSingleMessage(const RpcMessage &msg)
void provideFailed(const uint32_t id, const uint code, const std::string &reason, const bool transient, const HeaderValueMap extra={})
std::deque< ProvideWorkerItemRef > & requestQueue()
ProvideNotificatioMode _provNotificationMode
expected< ProvideMessage > parseReceivedMessage(const RpcMessage &m)
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 invalidMessageReceived(std::exception_ptr p)
void redirect(const uint32_t id, const zypp::Url &url, const zypp::Pathname &newPath)
expected< ProvideMessage > sendAndWaitForResponse(const ProvideMessage &request, const std::vector< uint > &responseCodes)
std::exception_ptr _fatalError
Definition: Arch.h:361
ZYPP_FWD_DECL_TYPE_WITH_REFS(DeviceDriver)
zypp::proto::Envelope Message
Definition: provideworker.h:32
zypp::proto::Configuration Configuration
Definition: provideworker.h:33
zypp::proto::Capabilities WorkerCaps
Definition: provideworker.h:31
std::map< std::string, std::string > extraKeys
Definition: provideworker.h:40