libzypp: zypp-core/ng/rpc/stompframestream.h Source File

libzypp 17.38.6

stompframestream.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8-----------------------------------------------------------------------/
9*
10* This file contains private API, this might break at any time between releases.
11* You have been warned!
12*
13*/
14
15#ifndef ZYPP_CORE_ZYPPNG_RPC_STOMPFRAMESTREAM_H_INCLUDED
16#define ZYPP_CORE_ZYPPNG_RPC_STOMPFRAMESTREAM_H_INCLUDED
17
18#include <zypp-core/TriBool.h>
19#include <zypp-core/ng/base/Base>
20#include <zypp-core/ng/core/String>
21#include <zypp-core/ng/base/Signals>
22#include <zypp-core/ng/base/Timer>
23#include <zypp-core/ng/io/IODevice>
24#include <zypp-core/ng/pipelines/expected.h>
25
26#include <zypp-core/rpc/PluginFrame.h>
27
28#include <deque>
29#include <optional>
30
31namespace zyppng {
32
33 ZYPP_FWD_DECL_TYPE_WITH_REFS (StompFrameStream);
34
35 class ZYPP_API InvalidMessageReceivedException : public zypp::Exception
36 {
37 public:
38 InvalidMessageReceivedException( const std::string &msg = {});
39 };
40
41 namespace rpc {
42
43 template <typename T>
44 expected<zypp::PluginFrame> toStompMessage( const T& msg ) {
45 return msg.toStompMessage();
46 }
47
48 template <typename T>
49 expected<T> fromStompMessage( const zypp::PluginFrame &message ) {
50 return T::fromStompMessage( message );
51 }
52
53 // Reads data from the stomp message and converts it to the target type
54 // used to read header values and values serialized into a terminated data field
55 template <typename T>
56 void parseDataIntoField( const std::string &headerVal, T &target )
57 {
58 if constexpr ( std::is_same_v<bool, T> ) {
59 const auto &triBool = zypp::str::strToTriBool ( headerVal );
60 if ( indeterminate(triBool) ) {
61 ZYPP_THROW ( zypp::PluginFrameException( "Invalid value for boolean field" ) );
62 }
63 target = bool(triBool);
64 } else if constexpr ( std::is_same_v<std::string, T> ) {
65 target = headerVal;
66 } else {
67 // numbers
68 auto val = zyppng::str::safe_strtonum<T> ( headerVal );
69 if ( !val )
70 ZYPP_THROW ( zypp::PluginFrameException( "Invalid value for numerical field" ) );
71 target = *val;
72 }
73 }
74
75 // queries a header value and calls parseDataIntoField on it
76 template <typename T>
77 void parseHeaderIntoField( const zypp::PluginFrame &msg, const std::string &name, T &target )
78 {
79 return parseDataIntoField ( msg.getHeader(name), target );
80 }
81
96 template <typename T>
97 inline zypp::PluginFrame prepareFrame() {
98 return zypp::PluginFrame ( std::string( T::typeName.data(), T::typeName.length() ) );
99 }
100
101 }
102
108 class ZYPP_API StompFrameStream : public zyppng::Base
109 {
110 public:
111
112 using Ptr = StompFrameStreamRef;
113
119 static Ptr create( IODevice::Ptr iostr ) {
120 return Ptr( new StompFrameStream( std::move(iostr) ) );
121 }
122
128 std::optional<zypp::PluginFrame> nextMessage ( const std::string &msgName = "" );
129
138 std::optional<zypp::PluginFrame> nextMessageWait ( const std::string &msgName = "" );
139
144 bool sendFrame ( const zypp::PluginFrame &message );
145
146 template <typename T>
147 bool sendMessage ( const T &message )
148 {
149 if constexpr ( std::is_same_v<T, zypp::PluginFrame> ) {
150 return sendFrame( message );
151 } else {
152 const auto &msg = rpc::toStompMessage(message);
153 if ( !msg ) {
154 ERR << "Failed to serialize message" << std::endl;
155 return false;
156 }
157 return sendFrame( *msg );
158 }
159 }
160
161 template<class T>
162 static expected< T > parseMessage ( const zypp::PluginFrame &m ) {
163 return rpc::fromStompMessage<T>(m);
164 }
165
170 void readAllMessages ();
171
176 SignalProxy<void()> sigMessageReceived ();
177
181 SignalProxy<void()> sigInvalidMessageReceived ();
182
183 private:
184 StompFrameStream( IODevice::Ptr iostr );
185 bool readNextMessage ();
186 void timeout( const zyppng::Timer &);
187
188 enum ParserState {
189 ReceiveCommand,
190 ReceiveHeaders,
191 ReceiveBody,
192 ParseError
193 } _parserState = ReceiveCommand;
194
195 std::optional<zypp::PluginFrame> _pendingMessage;
196 std::optional<int64_t> _pendingBodyLen;
197
198 IODevice::Ptr _ioDev;
199 Timer::Ptr _nextMessageTimer = Timer::create();
200 std::deque<zypp::PluginFrame> _messages;
201 Signal<void()> _sigNextMessage;
202 Signal<void()> _sigInvalidMessageReceived;
203
204 };
205}
206
207#endif // ZYPP_CORE_ZYPPNG_RPC_STOMPFRAMESTREAM_H_INCLUDED
ZYPP_THROW
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition Exception.h:459
ERR
#define ERR
Definition Logger.h:105
PluginFrame.h
TriBool.h
bool
zypp::Exception
Base class for Exception.
Definition Exception.h:153
zypp::Exception::msg
const std::string & msg() const
Return the message string provided to the ctor.
Definition Exception.h:206
zypp::PluginFrameException
Base class for PluginFrame Exception.
Definition PluginFrameException.h:28
zypp::PluginFrame
Command frame for communication with PluginScript.
Definition PluginFrame.h:42
zypp::PluginFrame::getHeader
const std::string & getHeader(const std::string &key_r) const
Return header value for key_r.
Definition PluginFrame.cc:469
zyppng::Base
Definition base.h:61
zyppng::IODevice::Ptr
std::shared_ptr< IODevice > Ptr
Definition iodevice.h:45
zyppng::InvalidMessageReceivedException::InvalidMessageReceivedException
InvalidMessageReceivedException(const std::string &msg={})
Definition stompframestream.cc:23
zyppng::SignalProxy
Definition signals.h:325
zyppng::Signal
Definition signals.h:151
zyppng::StompFrameStream
Definition stompframestream.h:109
zyppng::StompFrameStream::_pendingBodyLen
std::optional< int64_t > _pendingBodyLen
Definition stompframestream.h:196
zyppng::StompFrameStream::_sigNextMessage
Signal< void()> _sigNextMessage
Definition stompframestream.h:201
zyppng::StompFrameStream::_messages
std::deque< zypp::PluginFrame > _messages
Definition stompframestream.h:200
zyppng::StompFrameStream::StompFrameStream
StompFrameStream(IODevice::Ptr iostr)
Definition stompframestream.cc:27
zyppng::StompFrameStream::_ioDev
IODevice::Ptr _ioDev
Definition stompframestream.h:198
zyppng::StompFrameStream::ParserState
ParserState
Definition stompframestream.h:188
zyppng::StompFrameStream::ReceiveBody
@ ReceiveBody
Definition stompframestream.h:191
zyppng::StompFrameStream::ReceiveHeaders
@ ReceiveHeaders
Definition stompframestream.h:190
zyppng::StompFrameStream::ParseError
@ ParseError
Definition stompframestream.h:192
zyppng::StompFrameStream::ReceiveCommand
@ ReceiveCommand
Definition stompframestream.h:189
zyppng::StompFrameStream::Ptr
StompFrameStreamRef Ptr
Definition stompframestream.h:112
zyppng::StompFrameStream::_pendingMessage
std::optional< zypp::PluginFrame > _pendingMessage
Definition stompframestream.h:195
zyppng::StompFrameStream::create
static Ptr create(IODevice::Ptr iostr)
Definition stompframestream.h:119
zyppng::StompFrameStream::_sigInvalidMessageReceived
Signal< void()> _sigInvalidMessageReceived
Definition stompframestream.h:202
zyppng::StompFrameStream::_nextMessageTimer
Timer::Ptr _nextMessageTimer
Definition stompframestream.h:199
zyppng::StompFrameStream::parseMessage
static expected< T > parseMessage(const zypp::PluginFrame &m)
Definition stompframestream.h:162
zyppng::StompFrameStream::sendFrame
bool sendFrame(const zypp::PluginFrame &message)
Definition stompframestream.cc:292
zyppng::StompFrameStream::sendMessage
bool sendMessage(const T &message)
Definition stompframestream.h:147
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::expected
Definition expected.h:56
expected.h
zypp::iostr
Iostream related utilities.
Definition IOStream.cc:26
zypp::str::strToTriBool
TriBool strToTriBool(const C_Str &str)
Parse str into a bool if it's a legal true or false string; else indeterminate.
Definition String.cc:96
zypp::target
Definition KeyRing.h:325
zypp::ZYPP_API
const Arch Arch_empty ZYPP_API
relates: Arch This is an empty Arch represented by an empty string.
Definition Arch.h:173
zyppng::rpc
Definition stompframestream.h:41
zyppng::rpc::parseHeaderIntoField
void parseHeaderIntoField(const zypp::PluginFrame &msg, const std::string &name, T &target)
Definition stompframestream.h:77
zyppng::rpc::toStompMessage
expected< zypp::PluginFrame > toStompMessage(const T &msg)
Definition stompframestream.h:44
zyppng::rpc::fromStompMessage
expected< T > fromStompMessage(const zypp::PluginFrame &message)
Definition stompframestream.h:49
zyppng::rpc::parseDataIntoField
void parseDataIntoField(const std::string &headerVal, T &target)
Definition stompframestream.h:56
zyppng::rpc::prepareFrame
zypp::PluginFrame prepareFrame()
Constructs a zypp::PluginFrame based on the static type information of T.
Definition stompframestream.h:97
zyppng::str::safe_strtonum
std::optional< T > safe_strtonum(const std::string_view &val)
Definition string.h:23
zyppng
Definition MediaNetworkRequestExecutor.h:16
ZYPP_FWD_DECL_TYPE_WITH_REFS
#define ZYPP_FWD_DECL_TYPE_WITH_REFS(T)
Definition zyppglobal.h:119