blob: b905291710c54da6a4d0229a13572c43a5e3c28b [file] [log] [blame]
Emeric Vigier2f625822012-08-06 11:09:52 -04001/*
2 *
3 * D-Bus++ - C++ bindings for D-Bus
4 *
5 * Copyright (C) 2005-2007 Paolo Durante <shackan@gmail.com>
6 *
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24#ifdef HAVE_CONFIG_H
25#include <config.h>
26#endif
27
28#include <dbus-c++/debug.h>
29#include <dbus-c++/server.h>
30
31#include "internalerror.h"
32#include "server_p.h"
33#include "connection_p.h"
34#include "dispatcher_p.h"
35
36using namespace DBus;
37
38Server::Private::Private(DBusServer *s)
39 : server(s)
40{
41}
42
43Server::Private::~Private()
44{
45}
46
47void Server::Private::on_new_conn_cb(DBusServer *server, DBusConnection *conn, void *data)
48{
49 Server *s = static_cast<Server *>(data);
50
51 Connection nc(new Connection::Private(conn, s->_pvt.get()));
52
53 s->_pvt->connections.push_back(nc);
54
55 s->on_new_connection(nc);
56
57 debug_log("incoming connection 0x%08x", conn);
58}
59
60Server::Server(const char *address)
61{
62 InternalError e;
63 DBusServer *server = dbus_server_listen(address, e);
64
65 if (e) throw Error(e);
66
67 debug_log("server 0x%08x listening on %s", server, address);
68
69 _pvt = new Private(server);
70
71 dbus_server_set_new_connection_function(_pvt->server, Private::on_new_conn_cb, this, NULL);
72
73 setup(default_dispatcher);
74}
75/*
76Server::Server(const Server &s)
77: _pvt(s._pvt)
78{
79 dbus_server_ref(_pvt->server);
80}
81*/
82Server::~Server()
83{
84 dbus_server_unref(_pvt->server);
85}
86
87Dispatcher *Server::setup(Dispatcher *dispatcher)
88{
89 debug_log("registering stubs for server %p", _pvt->server);
90
91 Dispatcher *prev = _pvt->dispatcher;
92
93 dbus_server_set_watch_functions(
94 _pvt->server,
95 Dispatcher::Private::on_add_watch,
96 Dispatcher::Private::on_rem_watch,
97 Dispatcher::Private::on_toggle_watch,
98 dispatcher,
99 0
100 );
101
102 dbus_server_set_timeout_functions(
103 _pvt->server,
104 Dispatcher::Private::on_add_timeout,
105 Dispatcher::Private::on_rem_timeout,
106 Dispatcher::Private::on_toggle_timeout,
107 dispatcher,
108 0
109 );
110
111 _pvt->dispatcher = dispatcher;
112
113 return prev;
114}
115
116bool Server::operator == (const Server &s) const
117{
118 return _pvt->server == s._pvt->server;
119}
120
121bool Server::listening() const
122{
123 return dbus_server_get_is_connected(_pvt->server);
124}
125void Server::disconnect()
126{
127 dbus_server_disconnect(_pvt->server);
128}
129