libzypp 17.37.10

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>
18#include <zypp-media/ng/provide-configvars.h>
19#include <zypp-media/ng/private/providemessage_p.h>
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
29 using WorkerCaps = zyppng::WorkerCaps;
30 using Message = zyppng::ProvideMessage;
31 using Configuration = zyppng::ProviderConfiguration;
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
41 class ZYPP_API RequestCancelException : public zypp::media::MediaException
42 {
43 public:
44 RequestCancelException();
45 };
46
47 ZYPP_FWD_DECL_TYPE_WITH_REFS (ProvideWorker);
48 ZYPP_FWD_DECL_TYPE_WITH_REFS (ProvideWorkerItem);
49
50 class ProvideWorkerItem : public zyppng::Base
51 {
52 public:
53 enum State {
54 Pending,
55 Running,
56 Finished
57 };
58
59 ProvideWorkerItem( ProvideMessage &&spec ) : _spec( std::move(spec) ) { }
60
61 State _state = Pending;
62 ProvideMessage _spec;
63 };
64
65 class ProvideWorker : public Base
66 {
67 public:
68
69 enum ProvideNotificatioMode {
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
91 enum MediaChangeRes {
92 SUCCESS,
93 ABORT,
94 SKIP
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
107 ProvideNotificatioMode provNotificationMode() const;
108 void setProvNotificationMode(const ProvideNotificatioMode &provNotificationMode);
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 & );
185 void readFdClosed ( uint, AsyncDataSource::ChannelCloseReason );
186 void writeFdClosed ( AsyncDataSource::ChannelCloseReason );
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:
196 ProvideNotificatioMode _provNotificationMode = QUEUE_NOT_EMTPY;
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;
200 EventLoop::Ptr _loop = EventLoop::create();
201 Timer::Ptr _msgAvail = Timer::create();
202 Timer::Ptr _delayedShutdown = Timer::create();
203 AsyncDataSource::Ptr _controlIO;
204 StompFrameStreamRef _stream;
205 ProviderConfiguration _workerConf;
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
zypp::Exception
Base class for Exception.
Definition Exception.h:153
zypp::PluginFrame
Command frame for communication with PluginScript.
Definition PluginFrame.h:42
zypp::Url
Url manipulation class.
Definition Url.h:93
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:47
zypp::media::MediaException
Just inherits Exception to separate media exceptions.
Definition mediaexception.h:37
zyppng::AsyncDataSource
Definition asyncdatasource.h:12
zyppng::AsyncDataSource::ChannelCloseReason
ChannelCloseReason
Definition asyncdatasource.h:16
zyppng::AsyncDataSource::Ptr
std::shared_ptr< AsyncDataSource > Ptr
Definition asyncdatasource.h:23
zyppng::Base
Definition base.h:60
zyppng::Base::Base
Base()
Definition base.cc:20
zyppng::EventLoop::Ptr
EventLoopRef Ptr
Definition eventloop.h:47
zyppng::EventLoop::create
static Ptr create()
Definition eventloop_glib.cc:24
zyppng::HeaderValueMap
Definition headervaluemap.h:67
zyppng::ProvideMessage
Definition providemessage_p.h:521
zyppng::ProvideMessage::Code
MessageCodes Code
Definition providemessage_p.h:523
zyppng::ProviderConfiguration
Definition providemessage_p.h:366
zyppng::Timer
The Timer class provides repetitive and single-shot timers.
Definition timer.h:45
zyppng::Timer::create
static std::shared_ptr< Timer > create()
Creates a new Timer object, the timer is not started at this point.
Definition timer.cc:52
zyppng::Timer::Ptr
std::shared_ptr< Timer > Ptr
Definition timer.h:51
zyppng::WorkerCaps
Definition providemessage_p.h:376
zyppng::expected
Definition expected.h:27
zyppng::worker::ProvideWorkerItem
Definition provideworker.h:51
zyppng::worker::ProvideWorkerItem::_spec
ProvideMessage _spec
Definition provideworker.h:62
zyppng::worker::ProvideWorkerItem::ProvideWorkerItem
ProvideWorkerItem(ProvideMessage &&spec)
Definition provideworker.h:59
zyppng::worker::ProvideWorkerItem::_state
State _state
Definition provideworker.h:61
zyppng::worker::ProvideWorkerItem::State
State
Definition provideworker.h:53
zyppng::worker::ProvideWorkerItem::Pending
@ Pending
Definition provideworker.h:54
zyppng::worker::ProvideWorkerItem::Finished
@ Finished
Definition provideworker.h:56
zyppng::worker::ProvideWorkerItem::Running
@ Running
Definition provideworker.h:55
zyppng::worker::ProvideWorker
Definition provideworker.h:66
zyppng::worker::ProvideWorker::_workerName
std::string_view _workerName
Definition provideworker.h:199
zyppng::worker::ProvideWorker::detachSuccess
void detachSuccess(const uint32_t id)
Definition provideworker.cc:187
zyppng::worker::ProvideWorker::immediateShutdown
virtual void immediateShutdown()
Definition provideworker.h:85
zyppng::worker::ProvideWorker::_pendingProvides
std::deque< ProvideWorkerItemRef > _pendingProvides
Definition provideworker.h:210
zyppng::worker::ProvideWorker::handleSingleMessage
void handleSingleMessage(const ProvideMessage &provide)
Definition provideworker.cc:398
zyppng::worker::ProvideWorker::requestMediaChange
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={})
Definition provideworker.cc:238
zyppng::worker::ProvideWorker::controlIO
AsyncDataSource & controlIO()
Definition provideworker.cc:284
zyppng::worker::ProvideWorker::ProvideNotificatioMode
ProvideNotificatioMode
Definition provideworker.h:69
zyppng::worker::ProvideWorker::QUEUE_NOT_EMTPY
@ QUEUE_NOT_EMTPY
Definition provideworker.h:71
zyppng::worker::ProvideWorker::ONLY_NEW_PROVIDES
@ ONLY_NEW_PROVIDES
Definition provideworker.h:70
zyppng::worker::ProvideWorker::messageLoop
void messageLoop(Timer &)
Definition provideworker.cc:328
zyppng::worker::ProvideWorker::attachSuccess
void attachSuccess(const uint32_t id, const std::optional< std::string > &localMountPoint={})
Definition provideworker.cc:177
zyppng::worker::ProvideWorker::_loop
EventLoop::Ptr _loop
Definition provideworker.h:200
zyppng::worker::ProvideWorker::ProvideWorker
ProvideWorker(std::string_view workerName)
Definition provideworker.cc:37
zyppng::worker::ProvideWorker::executeHandshake
expected< void > executeHandshake()
Definition provideworker.cc:289
zyppng::worker::ProvideWorker::_controlIO
AsyncDataSource::Ptr _controlIO
Definition provideworker.h:203
zyppng::worker::ProvideWorker::onInvalidMessageReceived
void onInvalidMessageReceived()
Definition provideworker.cc:382
zyppng::worker::ProvideWorker::provideStart
void provideStart(const uint32_t id, const zypp::Url &url, const zypp::Pathname &localFile, const zypp::Pathname &stagingFile={})
Definition provideworker.cc:125
zyppng::worker::ProvideWorker::messageStream
StompFrameStreamRef messageStream() const
Definition provideworker.cc:58
zyppng::worker::ProvideWorker::cancel
virtual void cancel(const std::deque< ProvideWorkerItemRef >::iterator &request)=0
zyppng::worker::ProvideWorker::_pendingMessages
std::deque< ProvideMessage > _pendingMessages
Definition provideworker.h:209
zyppng::worker::ProvideWorker::writeFdClosed
void writeFdClosed(AsyncDataSource::ChannelCloseReason)
Definition provideworker.cc:367
zyppng::worker::ProvideWorker::MediaChangeRes
MediaChangeRes
Definition provideworker.h:91
zyppng::worker::ProvideWorker::ABORT
@ ABORT
Definition provideworker.h:93
zyppng::worker::ProvideWorker::SKIP
@ SKIP
Definition provideworker.h:94
zyppng::worker::ProvideWorker::SUCCESS
@ SUCCESS
Definition provideworker.h:92
zyppng::worker::ProvideWorker::~ProvideWorker
virtual ~ProvideWorker()
Definition provideworker.cc:55
zyppng::worker::ProvideWorker::pushSingleMessage
void pushSingleMessage(const zypp::PluginFrame &msg)
Definition provideworker.cc:431
zyppng::worker::ProvideWorker::parseReceivedMessage
expected< ProvideMessage > parseReceivedMessage(const zypp::PluginFrame &m)
Definition provideworker.cc:459
zyppng::worker::ProvideWorker::_delayedShutdown
Timer::Ptr _delayedShutdown
Definition provideworker.h:202
zyppng::worker::ProvideWorker::messageReceived
void messageReceived()
Definition provideworker.cc:373
zyppng::worker::ProvideWorker::maybeDelayedShutdown
void maybeDelayedShutdown()
Definition provideworker.cc:350
zyppng::worker::ProvideWorker::_isRunning
bool _isRunning
Definition provideworker.h:198
zyppng::worker::ProvideWorker::provideSuccess
void provideSuccess(const uint32_t id, bool cacheHit, const zypp::Pathname &localFile, const HeaderValueMap extra={})
Definition provideworker.cc:136
zyppng::worker::ProvideWorker::provNotificationMode
ProvideNotificatioMode provNotificationMode() const
Definition provideworker.cc:106
zyppng::worker::ProvideWorker::initLog
virtual void initLog()
Definition provideworker.cc:114
zyppng::worker::ProvideWorker::run
expected< void > run(int recv=STDIN_FILENO, int send=STDOUT_FILENO)
Definition provideworker.cc:63
zyppng::worker::ProvideWorker::makeItem
virtual ProvideWorkerItemRef makeItem(ProvideMessage &&spec)
Definition provideworker.cc:120
zyppng::worker::ProvideWorker::_msgAvail
Timer::Ptr _msgAvail
Definition provideworker.h:201
zyppng::worker::ProvideWorker::setProvNotificationMode
void setProvNotificationMode(const ProvideNotificatioMode &provNotificationMode)
Definition provideworker.cc:110
zyppng::worker::ProvideWorker::_workerConf
ProviderConfiguration _workerConf
Definition provideworker.h:205
zyppng::worker::ProvideWorker::provide
virtual void provide()=0
zyppng::worker::ProvideWorker::provideFailed
void provideFailed(const uint32_t id, const ProvideMessage::Code code, const std::string &reason, const bool transient, const HeaderValueMap extra={})
Definition provideworker.cc:149
zyppng::worker::ProvideWorker::initialize
virtual expected< WorkerCaps > initialize(const Configuration &conf)=0
zyppng::worker::ProvideWorker::readFdClosed
void readFdClosed(uint, AsyncDataSource::ChannelCloseReason)
Definition provideworker.cc:361
zyppng::worker::ProvideWorker::_stream
StompFrameStreamRef _stream
Definition provideworker.h:204
zyppng::worker::ProvideWorker::requestQueue
std::deque< ProvideWorkerItemRef > & requestQueue()
Definition provideworker.cc:101
zyppng::worker::ProvideWorker::_provNotificationMode
ProvideNotificatioMode _provNotificationMode
Definition provideworker.h:196
zyppng::worker::ProvideWorker::_inControllerRequest
bool _inControllerRequest
Definition provideworker.h:197
zyppng::worker::ProvideWorker::requireAuthorization
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={})
Definition provideworker.cc:256
zyppng::worker::ProvideWorker::invalidMessageReceived
void invalidMessageReceived(std::exception_ptr p)
Definition provideworker.cc:387
zyppng::worker::ProvideWorker::redirect
void redirect(const uint32_t id, const zypp::Url &url, const zypp::Pathname &newPath)
zyppng::worker::ProvideWorker::sendAndWaitForResponse
expected< ProvideMessage > sendAndWaitForResponse(const ProvideMessage &request, const std::vector< uint > &responseCodes)
Definition provideworker.cc:195
zyppng::worker::ProvideWorker::_fatalError
std::exception_ptr _fatalError
Definition provideworker.h:207
zyppng::worker::RequestCancelException::RequestCancelException
RequestCancelException()
Definition provideworker.cc:34
std
Definition Arch.h:364
zypp::url
Url details namespace.
Definition UrlBase.cc:58
zypp::ZYPP_API
const Arch Arch_armv7hnl Arch_armv7nhl ZYPP_API
Definition Arch.h:247
zyppng::worker
Definition devicedriver.cc:23
zyppng::worker::Configuration
zyppng::ProviderConfiguration Configuration
Definition provideworker.h:31
zyppng::worker::Message
zyppng::ProvideMessage Message
Definition provideworker.h:30
zyppng::worker::WorkerCaps
zyppng::WorkerCaps WorkerCaps
Definition provideworker.h:29
provide-configvars.h
providemessage_p.h
zyppng::worker::AuthInfo
Definition provideworker.h:34
zyppng::worker::AuthInfo::username
std::string username
Definition provideworker.h:35
zyppng::worker::AuthInfo::last_auth_timestamp
int64_t last_auth_timestamp
Definition provideworker.h:37
zyppng::worker::AuthInfo::extraKeys
std::map< std::string, std::string > extraKeys
Definition provideworker.h:38
zyppng::worker::AuthInfo::password
std::string password
Definition provideworker.h:36
ZYPP_FWD_DECL_TYPE_WITH_REFS
#define ZYPP_FWD_DECL_TYPE_WITH_REFS(T)
Definition zyppglobal.h:126