~thomas-voss/dbus-cpp/add_support_for_get_connection_app_armor_security_context

« back to all changes in this revision

Viewing changes to src/core/dbus/message_impl.h

  • Committer: Tarmac
  • Author(s): thomas-voss, Thomas Voß
  • Date: 2013-12-12 13:25:20 UTC
  • mfrom: (11.2.42 dbus-cpp-refactoring)
  • Revision ID: tarmac-20131212132520-gi9pig97oo57tke5
 * Add introspect function to autogenerated protocol header files.
 * Add a code generator for protocol header files.
 * Split up files and move the boost::asio executor implementation into src.
 * Force definitions inline.

Approved by Timo Jyrinki, PS Jenkins bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2012 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU Lesser General Public License version 3,
 
6
 * as published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 
17
 */
 
18
#ifndef CORE_DBUS_MESSAGE_H_
 
19
#define CORE_DBUS_MESSAGE_H_
 
20
 
 
21
#include <core/dbus/argument_type.h>
 
22
#include <core/dbus/visibility.h>
 
23
 
 
24
#include <core/dbus/types/any.h>
 
25
#include <core/dbus/types/object_path.h>
 
26
#include <core/dbus/types/signature.h>
 
27
#include <core/dbus/types/unix_fd.h>
 
28
#include <core/dbus/types/variant.h>
 
29
 
 
30
#include <dbus/dbus.h>
 
31
 
 
32
#include <exception>
 
33
#include <map>
 
34
#include <memory>
 
35
#include <ostream>
 
36
#include <stdexcept>
 
37
 
 
38
namespace core
 
39
{
 
40
namespace dbus
 
41
{
 
42
template<typename T> struct Codec;
 
43
class Error;
 
44
namespace impl
 
45
{
 
46
/**
 
47
 * @brief The Message class wraps a raw DBus message
 
48
 */
 
49
class Message : public std::enable_shared_from_this<Message>
 
50
{
 
51
public:
 
52
    typedef std::shared_ptr<Message> Ptr;
 
53
 
 
54
    /**
 
55
     * @brief The Type enum models the type of the message.
 
56
     */
 
57
    enum class Type : int
 
58
    {
 
59
        invalid = DBUS_MESSAGE_TYPE_INVALID, ///< Invalid message type
 
60
        signal = DBUS_MESSAGE_TYPE_SIGNAL, ///< A signal message
 
61
        method_call = DBUS_MESSAGE_TYPE_METHOD_CALL, ///< A method-call message
 
62
        method_return = DBUS_MESSAGE_TYPE_METHOD_RETURN, ///< A method-return message
 
63
        error = DBUS_MESSAGE_TYPE_ERROR ///< An error message.
 
64
    };
 
65
 
 
66
    /**
 
67
     * @brief The Reader class allows type-safe reading of arguments from a message.
 
68
     */
 
69
    class Reader
 
70
    {
 
71
    public:
 
72
        ~Reader();
 
73
 
 
74
        Reader(const Reader&) = delete;
 
75
        Reader& operator=(const Reader&) = delete;
 
76
 
 
77
        Reader(Reader&&);
 
78
        Reader& operator=(Reader&&);
 
79
 
 
80
        /**
 
81
          * @brief Returns the current type.
 
82
          *
 
83
          */
 
84
        ArgumentType type() const;
 
85
 
 
86
        /**
 
87
          * @brief Advances this view into the message.
 
88
          */
 
89
        void pop();
 
90
 
 
91
        /**
 
92
         * @brief Reads a byte from the underlying message.
 
93
         */
 
94
        std::int8_t pop_byte();
 
95
 
 
96
        /**
 
97
         * @brief Reads a boolean from the underlying message.
 
98
         */
 
99
        bool pop_boolean();
 
100
 
 
101
        /**
 
102
         * @brief Reads an int16 from the underlying message.
 
103
         */
 
104
        std::int16_t pop_int16();
 
105
 
 
106
        /**
 
107
         * @brief Reads a uint16 from the underlying message.
 
108
         */
 
109
        std::uint16_t pop_uint16();
 
110
 
 
111
        /**
 
112
         * @brief Reads an int32 from the underlying message.
 
113
         */
 
114
        std::int32_t pop_int32();
 
115
 
 
116
        /**
 
117
         * @brief Reads a uint32 from the underlying message.
 
118
         */
 
119
        std::uint32_t pop_uint32();
 
120
 
 
121
        /**
 
122
         * @brief Reads an int64 from the underlying message.
 
123
         */
 
124
        std::int64_t pop_int64();
 
125
 
 
126
        /**
 
127
         * @brief Reads a uint64 from the underlying message.
 
128
         */
 
129
        std::uint64_t pop_uint64();
 
130
 
 
131
        /**
 
132
         * @brief Reads a floating point value from the underlying message.
 
133
         */
 
134
        double pop_floating_point();
 
135
 
 
136
        /**
 
137
         * @brief Reads a string from the underlying message.
 
138
         */
 
139
        const char* pop_string();
 
140
 
 
141
        /**
 
142
         * @brief Reads an object_path from the underlying message.
 
143
         */
 
144
        types::ObjectPath pop_object_path();
 
145
 
 
146
        /**
 
147
         * @brief Reads a signature from the underlying message.
 
148
         */
 
149
        types::Signature pop_signature();
 
150
 
 
151
        /**
 
152
         * @brief Reads a unix fd from the underlying message.
 
153
         */
 
154
        types::UnixFd pop_unix_fd();
 
155
 
 
156
        /**
 
157
         * @brief Prepares reading of an array from the underlying message.
 
158
         * @return A reader pointing to the array.
 
159
         */
 
160
        Reader pop_array();
 
161
 
 
162
        /**
 
163
         * @brief Prepares reading of a structure from the underlying message.
 
164
         * @return A reader pointing into the structure.
 
165
         */
 
166
        Reader pop_structure();
 
167
 
 
168
        /**
 
169
         * @brief Prepares reading of a variant from the underlying message.
 
170
         * @return A reader pointing into the variant.
 
171
         */
 
172
        Reader pop_variant();
 
173
 
 
174
        /**
 
175
         * @brief Prepares reading of a dict entry from the underlying message.
 
176
         * @return A reader pointing to the array.
 
177
         */
 
178
        Reader pop_dict_entry();
 
179
 
 
180
    private:
 
181
        friend struct Codec<types::Any>;
 
182
        friend class Message;
 
183
        explicit Reader(const std::shared_ptr<Message>& msg);
 
184
 
 
185
        const std::shared_ptr<Message>& access_message();
 
186
 
 
187
        struct Private;
 
188
        std::unique_ptr<Private> d;
 
189
    };
 
190
 
 
191
    /**
 
192
     * @brief The Writer class allows type-safe serialization of input arguments to a message.
 
193
     */
 
194
    class Writer
 
195
    {
 
196
    public:
 
197
        ~Writer();
 
198
        Writer(const Writer&) = delete;
 
199
        Writer& operator=(const Writer&) = delete;
 
200
 
 
201
        Writer(Writer&&);
 
202
        Writer& operator=(Writer&&);
 
203
 
 
204
        /**
 
205
         * @brief Writes a byte to the underlying message.
 
206
         */
 
207
        void push_byte(std::int8_t value);
 
208
 
 
209
        /**
 
210
         * @brief Writes a boolean to the underlying message.
 
211
         */
 
212
        void push_boolean(bool value);
 
213
 
 
214
        /**
 
215
         * @brief Writes an int16 to the underlying message.
 
216
         */
 
217
        void push_int16(std::int16_t value);
 
218
 
 
219
        /**
 
220
         * @brief Writes a uint16 to the underlying message.
 
221
         */
 
222
        void push_uint16(std::uint16_t value);
 
223
 
 
224
        /**
 
225
         * @brief Writes an int32 to the underlying message.
 
226
         */
 
227
        void push_int32(std::int32_t value);
 
228
 
 
229
        /**
 
230
         * @brief Writes a uint32 to the underlying message.
 
231
         */
 
232
        void push_uint32(std::uint32_t value);
 
233
 
 
234
        /**
 
235
         * @brief Writes an int64 to the underlying message.
 
236
         */
 
237
        void push_int64(std::int64_t value);
 
238
 
 
239
        /**
 
240
         * @brief Writes a uint64 to the underlying message.
 
241
         */
 
242
        void push_uint64(std::uint64_t value);
 
243
 
 
244
        /**
 
245
         * @brief Writes a floating point value to the underlying message.
 
246
         */
 
247
        void push_floating_point(double value);
 
248
 
 
249
        /**
 
250
         * @brief Writes a string to the underlying message.
 
251
         */
 
252
        void push_stringn(const char* value, std::size_t size);
 
253
 
 
254
        /**
 
255
         * @brief Writes an object_path to the underlying message.
 
256
         */
 
257
        void push_object_path(const types::ObjectPath& value);
 
258
 
 
259
        /**
 
260
         * @brief Writes a signature to the underlying message.
 
261
         */
 
262
        void push_signature(const types::Signature& value);
 
263
 
 
264
        /**
 
265
         * @brief Writes a unix fd to the underlying message.
 
266
         */
 
267
        void push_unix_fd(const types::UnixFd& value);
 
268
 
 
269
        /**
 
270
         * @brief Prepares writing of an array to the underlying message.
 
271
         * @param [in] signature The signature of the contained data type.
 
272
         */
 
273
        Writer open_array(const types::Signature& signature);
 
274
 
 
275
        /**
 
276
         * @brief Finalizes writing of an array to the underlying message.
 
277
         */
 
278
        void close_array(Writer writer);
 
279
 
 
280
        /**
 
281
         * @brief Prepares writing of a structure to the underlying message.
 
282
         */
 
283
        Writer open_structure();
 
284
 
 
285
        /**
 
286
         * @brief Finalizes writing of a structure to the underlying message.
 
287
         */
 
288
        void close_structure(Writer writer);
 
289
 
 
290
        /**
 
291
         * @brief Prepares writing of a variant to the underlying message.
 
292
         * @param [in] signature The signature of the contained data type.
 
293
         */
 
294
        Writer open_variant(const types::Signature& signature);
 
295
 
 
296
        /**
 
297
         * @brief Finalizes writing of a variant to the underlying message.
 
298
         */
 
299
        void close_variant(Writer writer);
 
300
 
 
301
        /**
 
302
         * @brief Prepares writing of a dict entry to the underlying message.
 
303
         */
 
304
        Writer open_dict_entry();
 
305
 
 
306
        /**
 
307
         * @brief Finalizes writing of a dict entry to the underlying message.
 
308
         */
 
309
        void close_dict_entry(Writer writer);
 
310
 
 
311
    private:
 
312
        friend class Message;
 
313
        explicit Writer(const std::shared_ptr<Message>& msg);
 
314
 
 
315
        struct Private;
 
316
        std::unique_ptr<Private> d;
 
317
    };
 
318
 
 
319
    /**
 
320
     * @brief make_method_call creates an instance of Message with type Type::method_call.
 
321
     * @param destination The name of the remote service to send the message to.
 
322
     * @param path The name of the remote object to send the message to.
 
323
     * @param interface The interface to route the message to.
 
324
     * @param method The actual method that should be invoked
 
325
     * @return An instance of message of type Type::method_call.
 
326
     * @throw std::runtime_error if any of the parameters violates the DBus specification.
 
327
     */
 
328
    static std::shared_ptr<Message> make_method_call(
 
329
        const std::string& destination,
 
330
        const types::ObjectPath& path,
 
331
        const std::string& interface,
 
332
        const std::string& method);
 
333
 
 
334
    /**
 
335
     * @brief make_method_return creates a message instance in response to a raw DBus message of type method-call.
 
336
     * @param msg The message to reply to, must not be null. Must be of type Type::method_call.
 
337
     * @return An instance of message of type Type::method_return.
 
338
     */
 
339
    static std::shared_ptr<Message> make_method_return(const Message::Ptr& msg);
 
340
 
 
341
    /**
 
342
     * @brief make_signal creates a message instance wrapping a signal emission.
 
343
     * @param path The path of the object emitting the signal.
 
344
     * @param interface The interface containing the signal.
 
345
     * @param signal The actual signal name.
 
346
     * @return An instance of message of type Type::signal.
 
347
     */
 
348
    static std::shared_ptr<Message> make_signal(
 
349
        const std::string& path,
 
350
        const std::string& interface,
 
351
        const std::string& signal);
 
352
 
 
353
    /**
 
354
     * @brief make_error creates an error message instance in response to a raw DBus message of type method-call.
 
355
     * @param in_reply_to The message to reply to, must not be null. Must be of type Type::method_call.
 
356
     * @param error_name The name of the error.
 
357
     * @param error_desc Human-readable description of the error.
 
358
     * @return An instance of message of type Type::error.
 
359
     */
 
360
    static std::shared_ptr<Message> make_error(
 
361
        const Message::Ptr& in_reply_to,
 
362
        const std::string& error_name,
 
363
        const std::string& error_desc);
 
364
 
 
365
    /**
 
366
     * @brief from_raw_message creates an instance of message from a raw message.
 
367
     * @param msg The message to wrap.
 
368
     * @return An instance of Message with a type corresponding to the type of the raw message.
 
369
     */
 
370
    static std::shared_ptr<Message> from_raw_message(DBusMessage* msg);
 
371
 
 
372
    ~Message();
 
373
 
 
374
    /**
 
375
     * @brief Queries the type of the message.
 
376
     */
 
377
    Type type() const;
 
378
 
 
379
    /**
 
380
     * @brief Checks if the message expects a reply, i.e., is of type Type::method_call.
 
381
     */
 
382
    bool expects_reply() const;
 
383
 
 
384
    /**
 
385
     * @brief Queries the path of the object that this message belongs to.
 
386
     */
 
387
    types::ObjectPath path() const;
 
388
 
 
389
    /**
 
390
     * @brief Queries the member name that this message corresponds to.
 
391
     */
 
392
    std::string member() const;
 
393
 
 
394
    /**
 
395
     * @brief Queries the type signature of this message.
 
396
     */
 
397
    std::string signature() const;
 
398
 
 
399
    /**
 
400
     * @brief Queries the interface name that this message corresponds to.
 
401
     */
 
402
    std::string interface() const;
 
403
 
 
404
    /**
 
405
     * @brief Queries the name of the destination that this message should go to.
 
406
     */
 
407
    std::string destination() const;
 
408
 
 
409
    /**
 
410
     * @brief Queries the name of the sender that this message originates from.
 
411
     */
 
412
    std::string sender() const;
 
413
 
 
414
    /**
 
415
      * @brief Extracts error information from the message.
 
416
      * @throw std::runtime_error if not an error message.
 
417
      */
 
418
    Error error() const;
 
419
 
 
420
    /**
 
421
     * @brief Creates a Reader instance to read from this message.
 
422
     */
 
423
    Reader reader();
 
424
 
 
425
    /**
 
426
     * @brief Creates a Writer instance to write to this message.
 
427
     */
 
428
    Writer writer();
 
429
 
 
430
    /**
 
431
     * @brief Extracts the raw DBus message contained within this instance. Use with care.
 
432
     */
 
433
    DBusMessage* get() const;
 
434
 
 
435
private:
 
436
    friend struct Codec<types::Any>;
 
437
    friend class MessageFactory;
 
438
 
 
439
    Message(
 
440
        DBusMessage* msg,
 
441
        bool ref_on_construction = false);
 
442
 
 
443
    std::shared_ptr<Message> clone();
 
444
 
 
445
    std::shared_ptr<DBusMessage> dbus_message;
 
446
};
 
447
}
 
448
}
 
449
}
 
450
 
 
451
#include "impl/message.h>
 
452
 
 
453
#endif // CORE_DBUS_MESSAGE_H_