Boost.UI
User Interface Boost library
interprocess.cpp

Boost.Interprocess usage example

// Copyright (c) 2017 Kolya Kosenko
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// Provide data exchange between processes using Boost.Interprocess.
#include <boost/config.hpp>
#if !defined(BOOST_NO_CXX11_HDR_CHRONO)
#include <chrono>
namespace chrono_ns = std::chrono;
#else
#define BOOST_UI_USE_CHRONO
#include <boost/chrono.hpp>
namespace chrono_ns = boost::chrono;
#endif
#include <boost/ui.hpp>
#include <boost/interprocess/sync/named_mutex.hpp>
#include <boost/interprocess/ipc/message_queue.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <sstream>
namespace ui = boost::ui;
class interprocess_dialog : public ui::dialog
{
typedef interprocess_dialog this_type;
public:
interprocess_dialog();
private:
void on_lock();
void on_unlock();
void on_timeout();
boost::interprocess::named_mutex m_mutex;
ui::label m_lock_label;
void on_send();
void on_recieve();
boost::interprocess::message_queue m_mq;
};
#define MAX_MSG_SIZE 30
static const char g_mutex_name[] = "Boost.Interprocess.Mutex";
static const char g_message_queue_name[] = "Boost.Interprocess.MessageQueue";
interprocess_dialog::interprocess_dialog() :
ui::dialog("Boost.Interprocess + Boost.UI example"),
m_mutex(boost::interprocess::open_or_create, g_mutex_name),
m_mq(boost::interprocess::open_or_create, g_message_queue_name,
10, MAX_MSG_SIZE)
{
ui::vbox(*this)
<< ( ui::hbox()
<< ui::label(*this, "Mutex:")
.tooltip(g_mutex_name)
<< ui::button(*this, "&Lock")
.on_press(&this_type::on_lock, this)
<< ui::button(*this, "&Unlock")
.on_press(&this_type::on_unlock, this)
<< m_lock_label.create(*this)
)
<< ( ui::hbox()
<< ui::label(*this, "Message queue:")
.tooltip(g_message_queue_name)
<< ui::button(*this, "&Send current date and time")
.on_press(&this_type::on_send, this)
<< ui::button(*this, "&Recieve all messages")
.on_press(&this_type::on_recieve, this)
)
;
}
void interprocess_dialog::on_lock()
{
if ( !m_mutex.try_lock() )
ui::error_dialog("Mutex is already locked");
}
void interprocess_dialog::on_unlock()
{
m_mutex.unlock();
}
{
const bool was_locked = !m_mutex.try_lock();
if ( !was_locked )
m_mutex.unlock();
m_lock_label.text(was_locked ? "Locked" : "Unlocked");
ui::on_timeout(chrono_ns::milliseconds(100), &this_type::on_timeout, this);
}
void interprocess_dialog::on_send()
{
char buffer[MAX_MSG_SIZE];
strncpy(buffer, boost::posix_time::to_iso_extended_string(
boost::posix_time::second_clock::local_time()).c_str(), MAX_MSG_SIZE);
m_mq.send(&buffer, sizeof buffer, 0);
}
void interprocess_dialog::on_recieve()
{
std::ostringstream ss;
int i = 0;
for ( ;; i++ )
{
char buffer[MAX_MSG_SIZE];
boost::interprocess::message_queue::size_type recieved_size;
unsigned priority;
if ( !m_mq.try_receive(&buffer, sizeof buffer, recieved_size, priority) )
break;
else
ss << i << ") \"" << buffer << "\"" << std::endl;
}
if ( i == 0 )
ss << "There is no new messages";
ui::info_dialog(ss.str(), "Messages");
}
int ui_main()
{
interprocess_dialog().show_modal();
boost::interprocess::named_mutex::remove(g_mutex_name);
boost::interprocess::message_queue::remove(g_message_queue_name);
return 0;
}
int main(int argc, char* argv[])
{
return ui::entry(&ui_main, argc, argv);
}
boost::ui::on_timeout
void on_timeout(const std::chrono::duration< Rep, Period > &d, F &&f, Args &&... args)
Calls function one time after the specified time duration.
Definition: application.hpp:105
boost::ui
Boost.UI library namespace.
Definition: window.hpp:20
boost
Boost C++ libraries namespace.
Definition: window.hpp:19
boost::ui::info_dialog
BOOST_UI_DECL void info_dialog(const uistring &message, const uistring &title=ascii("Information"))
Display information in application-modal dialog.
std::endl
boost::ui::uiostringstream & endl(boost::ui::uiostringstream &os)
Manipulator that inserts newline character into stream.
Definition: stream.hpp:188
boost::ui::entry
int entry(int(*ui_main)(int, char *[]), int argc, char *argv[])
UI application entry.
ui.hpp
Master Boost.UI file.
boost::ui::error_dialog
BOOST_UI_DECL void error_dialog(const uistring &message, const uistring &title=ascii("Error"))
Display error in application-modal dialog.