libzypp 17.34.1
messagestream.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_MESSAGESTREAM_H_INCLUDED
16#define ZYPP_CORE_ZYPPNG_RPC_MESSAGESTREAM_H_INCLUDED
17
18#include <zypp-core/zyppng/base/Base>
19#include <zypp-core/zyppng/base/Signals>
20#include <zypp-core/zyppng/base/Timer>
21#include <zypp-core/zyppng/io/IODevice>
24
25#include <deque>
26#include <optional>
27
28namespace zypp::proto
29{
30 class Envelope;
31}
32
33namespace zyppng {
34
35 class RpcMessageStream;
36
38 {
39 public:
40 InvalidMessageReceivedException( const std::string &msg = {});
41 };
42
48 public:
49 RpcBaseType() = default;
50 virtual ~RpcBaseType() = default;
51 RpcBaseType(const RpcBaseType &) = default;
52 RpcBaseType(RpcBaseType &&) = default;
53 RpcBaseType &operator=(const RpcBaseType &) = default;
55
56 virtual const std::string &typeName() const = 0;
57 virtual bool deserialize( const std::string &data ) = 0;
58 virtual void serializeInto( std::string &str ) const = 0;
59 virtual std::string serialize() const;
60 };
61
62
63 class RpcMessage {
64
65 public:
66 friend class RpcMessageStream;
67 RpcMessage( );
68 RpcMessage( zypp::proto::Envelope data );
69
70 RpcMessage(const RpcMessage &) = default;
71 RpcMessage(RpcMessage &&) = default;
72 RpcMessage &operator=(const RpcMessage &) = default;
74
75 void set_messagetypename( std::string name );
76 const std::string &messagetypename() const;
77
78 void set_value( std::string name );
79 const std::string &value() const;
80
81 std::string serialize() const;
82
83 public:
85 };
86
87 namespace rpc {
93 template <typename T>
94 const std::string & messageTypeName() {
95 static std::string name = T().GetTypeName();
96 return name;
97 }
98
99 template <typename T>
101 {
102 if ( !target.deserialize( message.value() ) ) {
103 const std::string &msg = zypp::str::Str() << "Failed to parse " << message.messagetypename() << " message.";
104 ERR << msg << std::endl ;
106 }
108 }
109
110 template <typename T>
112 {
113 T target;
114 auto res = deserializeMessageInto (message, target);
115 if ( !res )
116 return expected<T>::error( res.error() );
117 return expected<T>::success(std::move(target));
118 }
119
120 template <typename T>
122 {
124 env.set_messagetypename( data.typeName() );
125 env.set_value( data.serialize() );
126 return env;
127 }
128
129 }
130
150 {
151 public:
152
153 using Ptr = std::shared_ptr<RpcMessageStream>;
154
160 static Ptr create( IODevice::Ptr iostr ) {
161 return Ptr( new RpcMessageStream( std::move(iostr) ) );
162 }
163
169 std::optional<RpcMessage> nextMessage ( const std::string &msgName = "" );
170
179 std::optional<RpcMessage> nextMessageWait ( const std::string &msgName = "" );
180
185 bool sendMessage ( const RpcMessage &env );
186
191 void readAllMessages ();
192
197 template <typename T>
201
207
212
213 template<class T>
216 }
217
218 template<class T>
219 static expected< void > parseMessageInto ( const RpcMessage &m, T &target ) {
220 if ( !target.ParseFromString( m.value() ) ) {
221 const std::string &msg = zypp::str::Str() << "Failed to parse " << m.messagetypename() << " message.";
222 ERR << msg << std::endl ;
224 }
225 return expected<void>::success( );
226 }
227
228 private:
230 bool readNextMessage ();
231 void timeout( const zyppng::Timer &);
232
236 std::deque<RpcMessage> _messages;
239
240 };
241}
242
243namespace zypp {
244 template<> zypp::proto::Envelope* rwcowClone<zypp::proto::Envelope>( const zypp::proto::Envelope * rhs );
245}
246
251#define ZYPP_RPCBASE \
252 public: \
253 static const std::string &staticTypeName(); \
254 const std::string &typeName() const override; \
255 bool deserialize(const std::string &data) override; \
256 void serializeInto(std::string &str) const override; \
257 std::string serialize( ) const override; \
258 private: \
259
266#define ZYPP_IMPL_RPCBASE(Class, ImplClass, implVar) \
267 const std::string &Class::staticTypeName() \
268 { \
269 return rpc::messageTypeName<ImplClass>(); \
270 } \
271 \
272 const std::string &Class::typeName() const \
273 { \
274 return staticTypeName(); \
275 } \
276 \
277 bool Class::deserialize(const std::string &data) \
278 { \
279 return implVar->ParseFromString( data ); \
280 } \
281 \
282 void Class::serializeInto(std::string &str) const \
283 { \
284 implVar->SerializeToString( &str ); \
285 } \
286 \
287 std::string Class::serialize( ) const \
288 { \
289 return implVar->SerializeAsString( ); \
290 }
291
292
293
294#endif // ZYPP_CORE_ZYPPNG_RPC_MESSAGESTREAM_H_INCLUDED
Base class for Exception.
Definition Exception.h:147
const std::string & msg() const
Return the message string provided to the ctor.
Definition Exception.h:196
std::shared_ptr< IODevice > Ptr
Definition iodevice.h:44
InvalidMessageReceivedException(const std::string &msg={})
virtual std::string serialize() const
virtual bool deserialize(const std::string &data)=0
RpcBaseType & operator=(RpcBaseType &&)=default
virtual ~RpcBaseType()=default
virtual const std::string & typeName() const =0
RpcBaseType(RpcBaseType &&)=default
RpcBaseType(const RpcBaseType &)=default
virtual void serializeInto(std::string &str) const =0
RpcBaseType & operator=(const RpcBaseType &)=default
Signal< void()> _sigInvalidMessageReceived
SignalProxy< void()> sigMessageReceived()
Signal< void()> _sigNextMessage
std::shared_ptr< RpcMessageStream > Ptr
zyppng::rpc::HeaderSizeType _pendingMessageSize
std::optional< RpcMessage > nextMessage(const std::string &msgName="")
std::enable_if_t< !std::is_same_v< T, RpcMessage >, bool > sendMessage(const T &m)
static expected< void > parseMessageInto(const RpcMessage &m, T &target)
RpcMessageStream(IODevice::Ptr iostr)
static expected< T > parseMessage(const RpcMessage &m)
std::deque< RpcMessage > _messages
bool sendMessage(const RpcMessage &env)
SignalProxy< void()> sigInvalidMessageReceived()
static Ptr create(IODevice::Ptr iostr)
void timeout(const zyppng::Timer &)
std::optional< RpcMessage > nextMessageWait(const std::string &msgName="")
zypp::RWCOW_pointer< zypp::proto::Envelope > _data
void set_value(std::string name)
RpcMessage(const RpcMessage &)=default
void set_messagetypename(std::string name)
std::string serialize() const
RpcMessage(RpcMessage &&)=default
RpcMessage & operator=(const RpcMessage &)=default
RpcMessage & operator=(RpcMessage &&)=default
const std::string & value() const
const std::string & messagetypename() const
The Timer class provides repetitive and single-shot timers.
Definition timer.h:45
static std::shared_ptr< Timer > create()
Creates a new Timer object, the timer is not started at this point.
Definition timer.cc:52
std::shared_ptr< Timer > Ptr
Definition timer.h:51
static expected success(ConsParams &&...params)
Definition expected.h:115
typename enable_if< B, T >::type enable_if_t
Definition TypeTraits.h:42
String related utilities and Regular expression matching.
Namespace intended to collect all environment variables we use.
Definition Env.h:23
Easy-to use interface to the ZYPP dependency resolver.
zypp::proto::Envelope * rwcowClone< zypp::proto::Envelope >(const zypp::proto::Envelope *rhs)
expected< void > deserializeMessageInto(const RpcMessage &message, T &target)
const std::string & messageTypeName()
expected< T > deserializeMessage(const RpcMessage &message)
RpcMessage serializeIntoMessage(const T &data)
uint32_t HeaderSizeType
Definition rpc.h:17
RW_pointer supporting 'copy on write' functionality.
Definition PtrTypes.h:469
Convenient building of std::string via std::ostringstream Basically a std::ostringstream autoconverti...
Definition String.h:212
#define ZYPP_EXCPT_PTR(EXCPT)
Drops a logline and returns Exception as a std::exception_ptr.
Definition Exception.h:433
#define ERR
Definition Logger.h:100