util_ipc_reliable_mq_writer.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #if !defined(BOOST_LOG_WITHOUT_IPC)
  8. #include <iostream>
  9. #include <string>
  10. #include <boost/log/utility/ipc/reliable_message_queue.hpp>
  11. #include <boost/log/utility/ipc/object_name.hpp>
  12. #include <boost/log/utility/open_mode.hpp>
  13. namespace logging = boost::log;
  14. namespace keywords = boost::log::keywords;
  15. //[ example_util_ipc_reliable_mq_writer
  16. int main()
  17. {
  18. typedef logging::ipc::reliable_message_queue queue_t;
  19. // Create a message_queue_type object that is associated with the interprocess
  20. // message queue named "ipc_message_queue".
  21. queue_t queue
  22. (
  23. keywords::name = logging::ipc::object_name(logging::ipc::object_name::user, "ipc_message_queue"),
  24. keywords::open_mode = logging::open_mode::open_or_create, /*< create the queue, if not yet created >*/
  25. keywords::capacity = 256, /*< if the queue has to be created, allocate 256 blocks... >*/
  26. keywords::block_size = 1024, /*< ... of 1 KiB each for messages >*/
  27. keywords::overflow_policy = queue_t::fail_on_overflow /*< if the queue is full, return error to the writer >*/
  28. );
  29. // Send a message through the queue
  30. std::string message = "Hello, Viewer!";
  31. queue_t::operation_result result = queue.send(message.data(), static_cast< queue_t::size_type >(message.size()));
  32. //<-
  33. #if !defined(BOOST_NO_CXX11_SCOPED_ENUMS)
  34. //->
  35. // See if the message was sent
  36. switch (result)
  37. {
  38. case queue_t::operation_result::succeeded:
  39. std::cout << "Message sent successfully" << std::endl;
  40. break;
  41. case queue_t::operation_result::no_space:
  42. std::cout << "Message could not be sent because the queue is full" << std::endl;
  43. break;
  44. case queue_t::operation_result::aborted:
  45. // This can happen is overflow_policy is block_on_overflow
  46. std::cout << "Message sending operation has been interrupted" << std::endl;
  47. break;
  48. }
  49. //<-
  50. #else // !defined(BOOST_NO_CXX11_SCOPED_ENUMS)
  51. // 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.
  52. switch (result)
  53. {
  54. case queue_t::succeeded:
  55. std::cout << "Message sent successfully" << std::endl;
  56. break;
  57. case queue_t::no_space:
  58. std::cout << "Message could not be sent because the queue is full" << std::endl;
  59. break;
  60. case queue_t::aborted:
  61. // This can happen is overflow_policy is block_on_overflow
  62. std::cout << "Message sending operation has been interrupted" << std::endl;
  63. break;
  64. }
  65. #endif // !defined(BOOST_NO_CXX11_SCOPED_ENUMS)
  66. //->
  67. return 0;
  68. }
  69. //]
  70. #else // !defined(BOOST_LOG_WITHOUT_IPC)
  71. int main()
  72. {
  73. return 0;
  74. }
  75. #endif // !defined(BOOST_LOG_WITHOUT_IPC)