server.cpp

Go to the documentation of this file.
00001 /*
00002  *
00003  *  D-Bus++ - C++ bindings for D-Bus
00004  *
00005  *  Copyright (C) 2005-2007  Paolo Durante <shackan@gmail.com>
00006  *
00007  *
00008  *  This library is free software; you can redistribute it and/or
00009  *  modify it under the terms of the GNU Lesser General Public
00010  *  License as published by the Free Software Foundation; either
00011  *  version 2.1 of the License, or (at your option) any later version.
00012  *
00013  *  This library is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  *  Lesser General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU Lesser General Public
00019  *  License along with this library; if not, write to the Free Software
00020  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  */
00023 
00024 #ifdef HAVE_CONFIG_H
00025 #include <config.h>
00026 #endif
00027 
00028 #include <dbus-c++/debug.h>
00029 #include <dbus-c++/server.h>
00030 
00031 #include "internalerror.h"
00032 #include "server_p.h"
00033 #include "connection_p.h"
00034 #include "dispatcher_p.h"
00035 
00036 using namespace DBus;
00037 
00038 Server::Private::Private(DBusServer *s)
00039   : server(s)
00040 {
00041 }
00042 
00043 Server::Private::~Private()
00044 {
00045 }
00046 
00047 void Server::Private::on_new_conn_cb(DBusServer *server, DBusConnection *conn, void *data)
00048 {
00049   Server *s = static_cast<Server *>(data);
00050 
00051   Connection nc(new Connection::Private(conn, s->_pvt.get()));
00052 
00053   s->_pvt->connections.push_back(nc);
00054 
00055   s->on_new_connection(nc);
00056 
00057   debug_log("incoming connection 0x%08x", conn);
00058 }
00059 
00060 Server::Server(const char *address)
00061 {
00062   InternalError e;
00063   DBusServer *server = dbus_server_listen(address, e);
00064 
00065   if (e) throw Error(e);
00066 
00067   debug_log("server 0x%08x listening on %s", server, address);
00068 
00069   _pvt = new Private(server);
00070 
00071   dbus_server_set_new_connection_function(_pvt->server, Private::on_new_conn_cb, this, NULL);
00072 
00073   setup(default_dispatcher);
00074 }
00075 /*
00076 Server::Server(const Server &s)
00077 : _pvt(s._pvt)
00078 {
00079   dbus_server_ref(_pvt->server);
00080 }
00081 */
00082 Server::~Server()
00083 {
00084   dbus_server_unref(_pvt->server);
00085 }
00086 
00087 Dispatcher *Server::setup(Dispatcher *dispatcher)
00088 {
00089   debug_log("registering stubs for server %p", _pvt->server);
00090 
00091   Dispatcher *prev = _pvt->dispatcher;
00092 
00093   dbus_server_set_watch_functions(
00094     _pvt->server,
00095     Dispatcher::Private::on_add_watch,
00096     Dispatcher::Private::on_rem_watch,
00097     Dispatcher::Private::on_toggle_watch,
00098     dispatcher,
00099     0
00100   );
00101 
00102   dbus_server_set_timeout_functions(
00103     _pvt->server,
00104     Dispatcher::Private::on_add_timeout,
00105     Dispatcher::Private::on_rem_timeout,
00106     Dispatcher::Private::on_toggle_timeout,
00107     dispatcher,
00108     0
00109   );
00110 
00111   _pvt->dispatcher = dispatcher;
00112 
00113   return prev;
00114 }
00115 
00116 bool Server::operator == (const Server &s) const
00117 {
00118   return _pvt->server == s._pvt->server;
00119 }
00120 
00121 bool Server::listening() const
00122 {
00123   return dbus_server_get_is_connected(_pvt->server);
00124 }
00125 void Server::disconnect()
00126 {
00127   dbus_server_disconnect(_pvt->server);
00128 }
00129