1// Copyright (c) Benjamin Kietzman (github.com/bkietz)
2//
3// Distributed under the Boost Software License, Version 1.0. (See accompanying
4// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6#ifndef DBUS_CONNECTION_IPP
7#define DBUS_CONNECTION_IPP
8
9#include <dbus/dbus.h>
10#include <dbus/detail/watch_timeout.hpp>
11
12#include <boost/atomic.hpp>
13
14namespace dbus {
15namespace impl {
16
17class connection
18{
19public:
20  boost::atomic<bool> is_paused;
21  DBusConnection *conn;
22
23  connection()
24    : is_paused(true),
25      conn(NULL)
26  {
27  }
28
29  void open(boost::asio::io_service& io, int bus)
30  {
31    error e;
32    conn = dbus_bus_get_private((DBusBusType)bus, e);
33    e.throw_if_set();
34
35    dbus_connection_set_exit_on_disconnect(conn, false);
36
37    detail::set_watch_timeout_dispatch_functions(conn, io);
38  }
39
40  void open(boost::asio::io_service& io, const string& address)
41  {
42    error e;
43    conn = dbus_connection_open_private(address.c_str(), e);
44    e.throw_if_set();
45
46    dbus_bus_register(conn, e);
47    e.throw_if_set();
48
49    dbus_connection_set_exit_on_disconnect(conn, false);
50
51    detail::set_watch_timeout_dispatch_functions(conn, io);
52  }
53
54  ~connection()
55  {
56    dbus_connection_close(conn);
57    dbus_connection_unref(conn);
58  }
59
60  operator DBusConnection *()
61  {
62    return conn;
63  }
64  operator const DBusConnection *() const
65  {
66    return conn;
67  }
68
69  // begin asynchronous operation
70  //FIXME should not get io from an argument
71  void start(boost::asio::io_service& io)
72  {
73    bool old_value(true);
74    if(is_paused.compare_exchange_strong(old_value, false))
75    {
76      // If two threads call connection::async_send()
77      // simultaneously on a paused connection, then
78      // only one will pass the CAS instruction and
79      // only one dispatch_handler will be injected.
80      io.post(detail::dispatch_handler(io, conn));
81    }
82  }
83
84  void cancel(boost::asio::io_service& io)
85  {
86    bool old_value(false);
87    if(is_paused.compare_exchange_strong(old_value, true))
88    {
89      //TODO
90    }
91  }
92};
93
94} // namespace impl
95} // namespace dbus
96
97#endif // DBUS_CONNECTION_IPP
98