WvStreams
wvdbusmsg.h
1 /* -*- Mode: C++ -*-
2  * Worldvisions Weaver Software:
3  * Copyright (C) 2004-2006 Net Integration Technologies, Inc.
4  *
5  * Pathfinder Software:
6  * Copyright (C) 2007, Carillon Information Security Inc.
7  *
8  * This library is licensed under the LGPL, please read LICENSE for details.
9  *
10  * WvDBusMsg is intended to be an easy-to-use abstraction over the low-level
11  * D-Bus DBusMessage structure. It represents a message being passed around on
12  * the bus.
13  */
14 #ifndef __WVDBUSMSG_H
15 #define __WVDBUSMSG_H
16 
17 #include "wvstringlist.h"
18 #include "wvbuf.h"
19 #include <stdint.h>
20 
21 struct DBusMessageIter;
22 struct DBusMessage;
23 
24 class WvDBusMsg;
25 class WvDBusConn;
26 
27 
28 class WvDBusMsg
29 {
30 public:
36  WvDBusMsg(WvStringParm busname, WvStringParm objectname,
37  WvStringParm ifc, WvStringParm method);
38 
42  WvDBusMsg(WvDBusMsg &_msg);
43 
48  WvDBusMsg(DBusMessage *_msg);
49 
50  virtual ~WvDBusMsg();
51 
52  operator DBusMessage* () const;
53 
61  static WvDBusMsg *demarshal(WvBuf &buf);
62 
71  static size_t demarshal_bytes_needed(WvBuf &buf);
72 
80  void marshal(WvBuf &buf);
81 
82  WvString get_sender() const;
83  WvString get_dest() const;
84  WvString get_path() const;
85  WvString get_interface() const;
86  WvString get_member() const;
87  WvString get_error() const;
88  uint32_t get_serial() const;
89  uint32_t get_replyserial() const;
90  bool is_reply() const;
91  operator WvString() const;
92 
93  void get_arglist(WvStringList &list) const;
94  WvString get_argstr() const;
95 
100  WvDBusMsg &append(const char *s);
101  WvDBusMsg &append(bool b);
102  WvDBusMsg &append(signed char c);
103  WvDBusMsg &append(unsigned char c);
104  WvDBusMsg &append(int16_t i);
105  WvDBusMsg &append(uint16_t i);
106  WvDBusMsg &append(int32_t i);
107  WvDBusMsg &append(uint32_t i);
108  WvDBusMsg &append(int64_t i);
109  WvDBusMsg &append(uint64_t i);
110  WvDBusMsg &append(double d);
111 
115  WvDBusMsg &variant_start(WvStringParm element_type);
116 
121 
127  WvDBusMsg &struct_start(WvStringParm element_type);
128 
133 
138  WvDBusMsg &array_start(WvStringParm element_type);
139 
143  WvDBusMsg &array_end();
144 
153  WvDBusMsg &varray_start(WvStringParm element_type);
154 
159 
163  WvDBusMsg reply();
164 
168  bool iserror() const;
169 
174  void send(WvDBusConn &conn);
175 
176  class Iter
177  {
178  public:
179  DBusMessageIter *const first, *const it;
180  mutable WvString s;
181  bool rewound;
182 
183  Iter(const WvDBusMsg &_msg);
184  Iter(const WvDBusMsg::Iter &_it);
185  Iter(const DBusMessageIter &_first);
186  ~Iter();
187 
192  void rewind();
193 
198  int type() const;
199 
207  Iter open() const;
208 
215  bool next();
216 
223  { next(); return *this; }
224 
228  bool cur() const;
229 
233  void get_all(WvStringList &list);
234 
239  WvString get_all();
240 
244  WvString get_str() const;
245 
250  int64_t get_int() const;
251  operator int64_t() const { return get_int(); }
252  operator int32_t() const { return get_int(); }
253  operator int16_t() const { return get_int(); }
254  operator int8_t() const { return get_int(); }
255  operator bool() const { return get_int() != 0; }
256 
261  uint64_t get_uint() const;
262  operator uint64_t() const { return get_uint(); }
263  operator uint32_t() const { return get_uint(); }
264  operator uint16_t() const { return get_uint(); }
265  operator uint8_t() const { return get_uint(); }
266 
271  double get_double() const;
272  operator double() const { return get_double(); }
273  operator float() const { return get_double(); }
274 
279  WvString *ptr() const;
280  operator WvString() const { return *ptr(); }
281 
282  WvIterStuff(WvString);
283  };
284 
285 protected:
286  mutable DBusMessage *msg;
288 };
289 
290 
291 class WvDBusSignal : public WvDBusMsg
292 {
293 public:
294  WvDBusSignal(WvStringParm objectname, WvStringParm ifc,
295  WvStringParm name);
296 };
297 
298 
299 class WvDBusError : public WvDBusMsg
300 {
301  DBusMessage *setup1(WvDBusMsg &in_reply_to,
302  WvStringParm errname, WvStringParm message);
303  void setup2();
304 public:
305  WvDBusError(WvDBusMsg &in_reply_to,
306  WvStringParm errname, WvStringParm message)
307  : WvDBusMsg(setup1(in_reply_to, errname, message))
308  {
309  setup2();
310  }
311 
312  WvDBusError(WvDBusMsg &in_reply_to,
313  WvStringParm errname, WVSTRING_FORMAT_DECL)
314  : WvDBusMsg(setup1(in_reply_to, errname,
315  WvString(WVSTRING_FORMAT_CALL)))
316  {
317  setup2();
318  }
319 };
320 
321 #endif // __WVDBUSMSG_H
WvDBusMsg::send
void send(WvDBusConn &conn)
A shortcut for sending this message on the given connection.
Definition: wvdbusmsg.cc:641
WvDBusMsg::demarshal
static WvDBusMsg * demarshal(WvBuf &buf)
Demarshals a new WvDBusMsg from a buffer containing its binary DBus protocol representation.
Definition: wvdbusmarshal.cc:27
WvDBusMsg::Iter::cur
bool cur() const
Returns: true if the current link is valid.
Definition: wvdbusmsg.cc:96
WvDBusMsg::iserror
bool iserror() const
Return true if this message is an error response.
Definition: wvdbusmsg.cc:635
WvDBusMsg::Iter::next
bool next()
Moves the iterator along the list to point to the next element.
Definition: wvdbusmsg.cc:71
WvDBusMsg::array_start
WvDBusMsg & array_start(WvStringParm element_type)
Start an array.
Definition: wvdbusmsg.cc:597
WvDBusMsg::Iter::getnext
Iter & getnext()
Same as next(), but returns *this instead so you can convert the new item to the right value type.
Definition: wvdbusmsg.h:222
WvDBusError
Definition: wvdbusmsg.h:299
WvDBusMsg::append
WvDBusMsg & append(const char *s)
The following methods are designed to allow appending various arguments to the message.
Definition: wvdbusmsg.cc:461
WvDBusSignal
Definition: wvdbusmsg.h:291
WvDBusMsg::reply
WvDBusMsg reply()
Generate a message that will be a reply to this one.
Definition: wvdbusmsg.cc:629
WvDBusMsg::Iter::rewind
void rewind()
Rewinds the iterator to make it point to an imaginary element preceeding the first element of the lis...
Definition: wvdbusmsg.cc:65
WvDBusMsg::array_end
WvDBusMsg & array_end()
End an array started with array_start().
Definition: wvdbusmsg.cc:608
WvDBusMsg
Definition: wvdbusmsg.h:28
WvDBusMsg::marshal
void marshal(WvBuf &buf)
Locks this message, encodes it in DBus binary protocol format, and adds it to the given buffer.
Definition: wvdbusmarshal.cc:83
WvString
WvString is an implementation of a simple and efficient printable-string class.
Definition: wvstring.h:329
WvDBusMsg::Iter::ptr
WvString * ptr() const
Returns a pointer to the WvString at the iterator's current location.
Definition: wvdbusmsg.cc:306
WvDBusMsg::varray_end
WvDBusMsg & varray_end()
End an array started with array_start().
Definition: wvdbusmsg.cc:621
WvDBusMsg::Iter
Definition: wvdbusmsg.h:176
WvDBusMsg::Iter::get_uint
uint64_t get_uint() const
Get the current element as a uint64_t (possible for all integer types)
Definition: wvdbusmsg.cc:207
WvDBusMsg::Iter::get_int
int64_t get_int() const
Get the current element as an int64_t (possible for all integer types)
Definition: wvdbusmsg.cc:160
WvBufBase< unsigned char >
Specialization of WvBufBase for unsigned char type buffers intended for use with raw memory buffers.
Definition: wvbuf.h:22
WvDBusMsg::struct_end
WvDBusMsg & struct_end()
End a struct started with struct_start().
Definition: wvdbusmsg.cc:591
WvDBusMsg::Iter::get_all
WvString get_all()
Return a WvString representation of all elements in a single string.
Definition: wvdbusmsg.cc:112
WvDBusMsg::variant_end
WvDBusMsg & variant_end()
End a variant.
Definition: wvdbusmsg.cc:564
WvDBusMsg::WvDBusMsg
WvDBusMsg(WvStringParm busname, WvStringParm objectname, WvStringParm ifc, WvStringParm method)
Constructs a new WvDBus message.
Definition: wvdbusmsg.cc:323
WvDBusMsg::varray_start
WvDBusMsg & varray_start(WvStringParm element_type)
Start a variant-array.
Definition: wvdbusmsg.cc:614
WvDBusMsg::Iter::open
Iter open() const
Returns a sub-iterator for walking through recursive types, such as arrays, structs,...
Definition: wvdbusmsg.cc:88
WvDBusMsg::struct_start
WvDBusMsg & struct_start(WvStringParm element_type)
Start a struct.
Definition: wvdbusmsg.cc:580
WvDBusMsg::variant_start
WvDBusMsg & variant_start(WvStringParm element_type)
Start a variant.
Definition: wvdbusmsg.cc:553
WvDBusMsg::Iter::get_double
double get_double() const
Get the current element as a double (possible for all integer and floating point types)
Definition: wvdbusmsg.cc:254
WvDBusMsg::Iter::get_str
WvString get_str() const
Get the current element as a string (possible for all types).
Definition: wvdbusmsg.cc:120
WvStringList
This is a WvList of WvStrings, and is a really handy way to parse strings.
Definition: wvstringlist.h:27
WvDBusMsg::Iter::type
int type() const
Returns the data type of the current element.
Definition: wvdbusmsg.cc:82
WvList< DBusMessageIter >
WvDBusMsg::demarshal_bytes_needed
static size_t demarshal_bytes_needed(WvBuf &buf)
Given a buffer containing what might be the header of a DBus message, checks how many bytes need to b...
Definition: wvdbusmarshal.cc:70
WvDBusConn
Definition: wvdbusconn.h:65