libzypp 17.37.10

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/zyppng/base/Base>
20#include <zypp-core/zyppng/core/String>
21#include <zypp-core/zyppng/base/Signals>
22#include <zypp-core/zyppng/base/Timer>
23#include <zypp-core/zyppng/io/IODevice>
24#include <zypp-core/zyppng/pipelines/expected.h>
25
26#include <zypp-core/rpc/PluginFrame.h>
27
28#include <deque>
29#include <optional>
30
31 namespace 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
82 template <typename T>
83 inline zypp::PluginFrame prepareFrame() {
84 return zypp::PluginFrame ( std::string( T::typeName.data(), T::typeName.length() ) );
85 }
86
87 }
88
94 class ZYPP_API StompFrameStream : public zyppng::Base
95 {
96 public:
97
98 using Ptr = StompFrameStreamRef;
99
105 static Ptr create( IODevice::Ptr iostr ) {
106 return Ptr( new StompFrameStream( std::move(iostr) ) );
107 }
108
114 std::optional<zypp::PluginFrame> nextMessage ( const std::string &msgName = "" );
115
124 std::optional<zypp::PluginFrame> nextMessageWait ( const std::string &msgName = "" );
125
130 bool sendFrame ( const zypp::PluginFrame &message );
131
132 template <typename T>
133 bool sendMessage ( const T &message )
134 {
135 if constexpr ( std::is_same_v<T, zypp::PluginFrame> ) {
136 return sendFrame( message );
137 } else {
138 const auto &msg = rpc::toStompMessage(message);
139 if ( !msg ) {
140 ERR << "Failed to serialize message" << std::endl;
141 return false;
142 }
143 return sendFrame( *msg );
144 }
145 }
146
147 template<class T>
148 static expected< T > parseMessage ( const zypp::PluginFrame &m ) {
149 return rpc::fromStompMessage<T>(m);
150 }
151
156 void readAllMessages ();
157
162 SignalProxy<void()> sigMessageReceived ();
163
167 SignalProxy<void()> sigInvalidMessageReceived ();
168
169 private:
170 StompFrameStream( IODevice::Ptr iostr );
171 bool readNextMessage ();
172 void timeout( const zyppng::Timer &);
173
174 enum ParserState {
175 ReceiveCommand,
176 ReceiveHeaders,
177 ReceiveBody,
178 ParseError
179 } _parserState = ReceiveCommand;
180
181 std::optional<zypp::PluginFrame> _pendingMessage;
182 std::optional<int64_t> _pendingBodyLen;
183
184 IODevice::Ptr _ioDev;
185 Timer::Ptr _nextMessageTimer = Timer::create();
186 std::deque<zypp::PluginFrame> _messages;
187 Signal<void()> _sigNextMessage;
188 Signal<void()> _sigInvalidMessageReceived;
189
190 };
191}
192
193#endif // ZYPP_CORE_ZYPPNG_RPC_STOMPFRAMESTREAM_H_INCLUDED
PluginFrame.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:60
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:95
zyppng::StompFrameStream::_pendingBodyLen
std::optional< int64_t > _pendingBodyLen
Definition stompframestream.h:182
zyppng::StompFrameStream::_sigNextMessage
Signal< void()> _sigNextMessage
Definition stompframestream.h:187
zyppng::StompFrameStream::_messages
std::deque< zypp::PluginFrame > _messages
Definition stompframestream.h:186
zyppng::StompFrameStream::StompFrameStream
StompFrameStream(IODevice::Ptr iostr)
Definition stompframestream.cc:27
zyppng::StompFrameStream::_ioDev
IODevice::Ptr _ioDev
Definition stompframestream.h:184
zyppng::StompFrameStream::ParserState
ParserState
Definition stompframestream.h:174
zyppng::StompFrameStream::ReceiveBody
@ ReceiveBody
Definition stompframestream.h:177
zyppng::StompFrameStream::ReceiveHeaders
@ ReceiveHeaders
Definition stompframestream.h:176
zyppng::StompFrameStream::ParseError
@ ParseError
Definition stompframestream.h:178
zyppng::StompFrameStream::ReceiveCommand
@ ReceiveCommand
Definition stompframestream.h:175
zyppng::StompFrameStream::Ptr
StompFrameStreamRef Ptr
Definition stompframestream.h:98
zyppng::StompFrameStream::_pendingMessage
std::optional< zypp::PluginFrame > _pendingMessage
Definition stompframestream.h:181
zyppng::StompFrameStream::create
static Ptr create(IODevice::Ptr iostr)
Definition stompframestream.h:105
zyppng::StompFrameStream::_sigInvalidMessageReceived
Signal< void()> _sigInvalidMessageReceived
Definition stompframestream.h:188
zyppng::StompFrameStream::_nextMessageTimer
Timer::Ptr _nextMessageTimer
Definition stompframestream.h:185
zyppng::StompFrameStream::parseMessage
static expected< T > parseMessage(const zypp::PluginFrame &m)
Definition stompframestream.h:148
zyppng::StompFrameStream::sendFrame
bool sendFrame(const zypp::PluginFrame &message)
Definition stompframestream.cc:292
zyppng::StompFrameStream::sendMessage
bool sendMessage(const T &message)
Definition stompframestream.h:133
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:27
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_armv7hnl Arch_armv7nhl ZYPP_API
Definition Arch.h:247
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()
Definition stompframestream.h:83
zyppng::str::safe_strtonum
std::optional< T > safe_strtonum(const std::string_view &val)
Definition string.h:23
zyppng
Definition MediaNetworkRequestExecutor.h:16
TriBool.h
ZYPP_THROW
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition Exception.h:459
ERR
#define ERR
Definition Logger.h:102
ZYPP_FWD_DECL_TYPE_WITH_REFS
#define ZYPP_FWD_DECL_TYPE_WITH_REFS(T)
Definition zyppglobal.h:126