1 /*
2 * Copyright Andrey Semashev 2016.
3 * Distributed under the Boost Software License, Version 1.0.
4 * (See accompanying file LICENSE_1_0.txt or copy at
5 * http://www.boost.org/LICENSE_1_0.txt)
6 */
7
8 #if !defined(BOOST_LOG_WITHOUT_IPC)
9
10 #include <iostream>
11 #include <string>
12 #include <boost/log/utility/ipc/reliable_message_queue.hpp>
13 #include <boost/log/utility/ipc/object_name.hpp>
14 #include <boost/log/utility/open_mode.hpp>
15
16 namespace logging = boost::log;
17 namespace keywords = boost::log::keywords;
18
19 //[ example_util_ipc_reliable_mq_writer
main()20 int main()
21 {
22 typedef logging::ipc::reliable_message_queue queue_t;
23
24 // Create a message_queue_type object that is associated with the interprocess
25 // message queue named "ipc_message_queue".
26 queue_t queue
27 (
28 keywords::name = logging::ipc::object_name(logging::ipc::object_name::user, "ipc_message_queue"),
29 keywords::open_mode = logging::open_mode::open_or_create, /*< create the queue, if not yet created >*/
30 keywords::capacity = 256, /*< if the queue has to be created, allocate 256 blocks... >*/
31 keywords::block_size = 1024, /*< ... of 1 KiB each for messages >*/
32 keywords::overflow_policy = queue_t::fail_on_overflow /*< if the queue is full, return error to the writer >*/
33 );
34
35 // Send a message through the queue
36 std::string message = "Hello, Viewer!";
37 queue_t::operation_result result = queue.send(message.data(), static_cast< queue_t::size_type >(message.size()));
38
39 //<-
40 #if !defined(BOOST_NO_CXX11_SCOPED_ENUMS)
41 //->
42 // See if the message was sent
43 switch (result)
44 {
45 case queue_t::operation_result::succeeded:
46 std::cout << "Message sent successfully" << std::endl;
47 break;
48
49 case queue_t::operation_result::no_space:
50 std::cout << "Message could not be sent because the queue is full" << std::endl;
51 break;
52
53 case queue_t::operation_result::aborted:
54 // This can happen is overflow_policy is block_on_overflow
55 std::cout << "Message sending operation has been interrupted" << std::endl;
56 break;
57 }
58 //<-
59 #else // !defined(BOOST_NO_CXX11_SCOPED_ENUMS)
60
61 // Strict C++03 compilers do not allow to use enum type as a scope for its values. Otherwise, the code is the same as above.
62 switch (result)
63 {
64 case queue_t::succeeded:
65 std::cout << "Message sent successfully" << std::endl;
66 break;
67
68 case queue_t::no_space:
69 std::cout << "Message could not be sent because the queue is full" << std::endl;
70 break;
71
72 case queue_t::aborted:
73 // This can happen is overflow_policy is block_on_overflow
74 std::cout << "Message sending operation has been interrupted" << std::endl;
75 break;
76 }
77
78 #endif // !defined(BOOST_NO_CXX11_SCOPED_ENUMS)
79 //->
80
81 return 0;
82 }
83 //]
84
85 #else // !defined(BOOST_LOG_WITHOUT_IPC)
86
main()87 int main()
88 {
89 return 0;
90 }
91
92 #endif // !defined(BOOST_LOG_WITHOUT_IPC)
93