libzypp  17.32.5
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 
28 namespace zypp::proto
29 {
30  class Envelope;
31 }
32 
33 namespace zyppng {
34 
35  class RpcMessageStream;
36 
38  {
39  public:
40  InvalidMessageReceivedException( const std::string &msg = {});
41  };
42 
47  class RpcBaseType {
48  public:
49  RpcBaseType() = default;
50  virtual ~RpcBaseType() = default;
51  RpcBaseType(const RpcBaseType &) = default;
52  RpcBaseType(RpcBaseType &&) = default;
53  RpcBaseType &operator=(const RpcBaseType &) = default;
54  RpcBaseType &operator=(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;
73  RpcMessage &operator=(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>
100  expected<void> deserializeMessageInto( const RpcMessage &message, T &target )
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  }
107  return expected<void>::success();
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  {
123  RpcMessage env;
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>
200  }
201 
207 
212 
213  template<class T>
214  static expected< T > parseMessage ( const RpcMessage &m ) {
215  return rpc::deserializeMessage<T>(m);
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 
243 namespace 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 
260 
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
virtual const std::string & typeName() const =0
std::deque< RpcMessage > _messages
Signal< void()> _sigInvalidMessageReceived
Namespace intended to collect all environment variables we use.
Definition: Env.h:22
uint32_t HeaderSizeType
Definition: rpc.h:17
zyppng::rpc::HeaderSizeType _pendingMessageSize
static Ptr create(IODevice::Ptr iostr)
zypp::RWCOW_pointer< zypp::proto::Envelope > _data
Definition: messagestream.h:84
RpcMessage serializeIntoMessage(const T &data)
virtual ~RpcBaseType()=default
RpcMessage & operator=(const RpcMessage &)=default
std::string serialize() const
String related utilities and Regular expression matching.
SignalProxy< void()> sigInvalidMessageReceived()
virtual void serializeInto(std::string &str) const =0
#define ZYPP_EXCPT_PTR(EXCPT)
Drops a logline and returns Exception as a std::exception_ptr.
Definition: Exception.h:433
SignalProxy< void()> sigMessageReceived()
bool sendMessage(const RpcMessage &env)
#define ERR
Definition: Logger.h:98
static expected< void > parseMessageInto(const RpcMessage &m, T &target)
typename enable_if< B, T >::type enable_if_t
Definition: TypeTraits.h:42
Convenient building of std::string via std::ostringstream Basically a std::ostringstream autoconverti...
Definition: String.h:211
void set_value(std::string name)
void set_messagetypename(std::string name)
std::optional< RpcMessage > nextMessageWait(const std::string &msgName="")
const std::string & messageTypeName()
Definition: messagestream.h:94
The Timer class provides repetitive and single-shot timers.
Definition: timer.h:44
const std::string & messagetypename() const
virtual bool deserialize(const std::string &data)=0
std::shared_ptr< IODevice > Ptr
Definition: iodevice.h:44
static expected< T > parseMessage(const RpcMessage &m)
static expected success(ConsParams &&...params)
Definition: expected.h:115
std::shared_ptr< Timer > Ptr
Definition: timer.h:51
static std::shared_ptr< Timer > create()
Creates a new Timer object, the timer is not started at this point.
Definition: timer.cc:52
const std::string & value() const
Base class for Exception.
Definition: Exception.h:146
std::optional< RpcMessage > nextMessage(const std::string &msgName="")
expected< void > deserializeMessageInto(const RpcMessage &message, T &target)
RpcMessageStream(IODevice::Ptr iostr)
RpcBaseType & operator=(const RpcBaseType &)=default
std::enable_if_t< !std::is_same_v< T, RpcMessage >, bool > sendMessage(const T &m)
void timeout(const zyppng::Timer &)
Signal< void()> _sigNextMessage
InvalidMessageReceivedException(const std::string &msg={})
Easy-to use interface to the ZYPP dependency resolver.
Definition: Application.cc:19
expected< T > deserializeMessage(const RpcMessage &message)
virtual std::string serialize() const
std::shared_ptr< Base > Ptr
Definition: base.h:65
std::shared_ptr< RpcMessageStream > Ptr
const std::string & msg() const
Return the message string provided to the ctor.
Definition: Exception.h:196