Commit graph

60 commits

Author SHA1 Message Date
Zhu, Yongsheng
3da11167c0 GDBus C++ binding: changes according to gdbus changes
Add one callback for each interface(DBusObjectHelper):
Each DBusObjectHelper sub-class could register its own
callback function via this parameter. The callback is
eventually called before each method function is called.
2010-01-26 16:17:08 +08:00
Zhu, Yongsheng
283e6ba46c gdbus: add interface-level callback function
A new function pointer 'callback' is added in the InterfaceData.
The callback function is per-interface. It is set together
with the registration of interface. It provides a mechanism to let
gdbus users do something before the registered methods are called.
It must be called *before* any method of an interface is called if set.
The reason is that the method might un-register the interface so
InterfaceData and its user data might be destroyed.
The user data transferred to the callback function is the 'user_data'
member in the InterfaceData set by users of gdbus.
The main purpose of this callback is that it is wanted to track
whether the dbus server is still in use.
2010-01-26 15:21:05 +08:00
Zhu, Yongsheng
6e9b26a212 DBus server: implement InfoRequest and InfoResponse (MB#6376)
Implement the info exchange mechanism when the dbus server wants
any information that only dbus clients can provide.

The dbus server uses the class 'InfoReq' to handle the requested
information from it and get response from dbus clients. The main
processing logic is referred to dbus api document.

To allow multiple info requests, the DBusServer uses a map to manage
all pending info requests and dispatches responses to the corresponding
info request. Also DBusServer.createInfoReq() is provided to create
info requests.

For InfoRequest callers, A series of methods in 'InfoReq' are provided:
check - check whether the required response is gotten
cancel - cancel the info request
wait - wait until timeout, abort or suspend
getResponse - get the response result provided by dbus clients.

All methods here are asynchronous except 'wait', which waits for
response explicitly.
2009-12-25 13:08:12 +08:00
Patrick Ohly
ff145efc1c gdbus: fixed g_dbus_create_error_valist() error handling
Out-of-memory errors were not handled correctly:
- The code using realloc() might have leaked a string.
- A static pointer was used to avoid further memory
  allocation errors, but the caller then would have
  freed it => try strdup() and check result in caller
  instead.
2009-12-09 12:19:44 +01:00
Patrick Ohly
3fc19c58d1 exporting of local changes in gdbus
Importing was already possible, improved README about that.
Exporting is added in this commit.
2009-12-09 11:53:02 +01:00
Patrick Ohly
950fa6857e gdbus: allow using private connection
This change was necessary because it turned out that using
g_dbus_setup_bus() and later dbus_g_bus_get() leads to problems
(assertion about watch data on Moblin 2.1, CRITICAL warning
and possibly other issues on Debian Lenny).

It seems that sharing a DBusConnection between different layers on top
of libdbus is either not supported or incorrectly implemented, at
least in glib-dbus.

The problem was found in SyncEvolution when using a libecal/ebook
which call D-Bus under the hood (Moblin Bugzilla #8460). SyncEvolution
has no control over those calls, therefore making the connection used
by the syncevo-dbus-server private was the easier alternative.
2009-12-04 12:47:47 +01:00
Patrick Ohly
123333d676 gdbus-cxx: header file must be distributed
"make distcheck" failed because it didn't include the new
C++ binding header files. Fixed by including them in the
files which need to be installed in /usr/include, just like
the rest of gdbus does it.
2009-11-30 19:41:00 +01:00
Chen Congwu
aaa6fc915d gdbus: fix a compile error
Need to include stdint for uint
2009-11-16 23:09:24 +01:00
Zhu, Yongsheng
912ddf031d syncevo-dbus-server: add specific exceptions (MB#6548)
Define a DBusCxxException class in gdbus cxx bridge.
It only has 2 virtual functions: getName, getMessage,
which provide the information necessary to generate
a D-Bus error.

Then define a class DBusSyncException deriving from
DBusCxxException and SyncEvo::Exception.
Also 3 classes NoSuchConfig, NoSuchSource,
InvalidCall are based on DBusSyncException.
Turn some errors into these 3 specific errors.

Define a macro to let user have an opportunity to write
its own exception handling function. The SyncEvoHandleException()
logs the error via Exception::handle() and then overrides
the conversion of all exceptions so that they result in
an "org.syncevolution.Exception", unless specified otherwise
when throwing a DBusCXXException or dbus_error.
2009-11-10 13:39:14 +01:00
Patrick Ohly
bb73c2745b gdbus C++: demonstrate how to bind methods in private base classes
Passing the "this" pointer to DBusObjectHelper::add() does not
work in this case because it cannot downcast to the required
base class. However, the caller can do that, which solves the
problem.
2009-10-14 12:28:02 +02:00
Patrick Ohly
fce06ca799 gdbus C++ example: added static and const function callback
The const method does not work at the moment and therefore
is commented out. The underlying problem is that the DBusObjectHelper::add()
call must be able to determine the method pointer type without the
const qualifier. All attempts to write a template function where
"M" is a normal function pointer and "method" a const member pointer
failed with compiler errors or didn't have the desired effect.
2009-10-14 12:28:01 +02:00
Patrick Ohly
7f81e1593a gdbus C++: simplified method declaration
It is no longer necessary to duplicate the list of function parameters
when declaring a D-Bus methods. Instead methods are added to a
DBusObjectHelper instance with the minimum amount of information:
- optional this pointer for methods
- the address of the method, global or static function
- the name of the D-Bus method

The DBusHelper::add() call determines the signature based on the
function pointer. It would have been nice to pass a boost::bind()
result and that way get rid of the special case of method pointers,
but it seems to be impossible to determine the type of all parameters
in the functor returned by boost::bind().

The maximum number of parameters of methods, including the "this"
pointer, is now 9, the maximum supported by boost::bind().

Internally DBusHelper maintains a DBusMethodTable with one pointer to
a boost::function instance for each method. This depends on the new
libgdbus per-method callback data.

Signals are also added with add() and likewise maintained inside the
DBusObjectHelper. This avoids duplicating the signal name.

All strings passed to DBusObjectHelper are copied and freed when no
longer needed.

The old handling of asynchronous method implementations via dedicated
method callbacks was replaced with code that detects when a method ask
for a Result* parameter. This is done via dbus_traits for those
parameter types, similar to the way how Watches were handled. The
compiler can detect this situation at compile time, which should
remove the useless post-method handling of the return values from the
generated code.

The main advantage is that the exponential duplication of code for the
various combinations of n arguments and m asynchronous result values
could be removed. It is nicer for users, too, because the _ASYNC flag
no longer has to be specified manually when registering the method.
2009-10-14 12:28:01 +02:00
Patrick Ohly
92a1dd2f39 gdbus: per-method callback data
The traditional usage of method callbacks is that all callbacks
share the same global data pointer. When composing a D-Bus object
from several independent modules this may be too limited, for
example when using C++.

This patch introduces G_DBUS_METHOD_FLAG_METHOD_DATA and new
fields at the end of GDBusMethodTable. Old code works as before.
New code which sets the flag is passed the new "method_data"
value as its "user_data" pointer.

This implies that the GDBusMethodTable has to be built up dynamically.
It is still owned and managed by the caller of libgdbus. To
simplify this, a new "destroy" callback can be stored inside the
GDBusMethodTable.
2009-10-14 12:28:01 +02:00
Patrick Ohly
d413d1d425 gdbus: add _NONE = 0 enum value for C++
In C++, 0 is not automatically casted into an enum. This patch
adds enum names for the empty bit field, which makes C++ code
using gdbus a bit easier to read.
2009-10-14 12:28:01 +02:00
Patrick Ohly
7b5ae309bf D-Bus C++: added support for structs
Members of structs must be read- and writeable
because the marshaling code accesses them directly.
The dbus_traits then can be defined by deriving
from dbus_struct_traits with template parameters
which describe the struct members.
2009-10-07 16:11:33 +02:00
Patrick Ohly
ecaa53241b D-Bus C++: added support for in-place passing of basic arrays
With V being a basic type, "std::pair<size_t, const V *> &" can
be used to pass an array. The caller owns the array, so this
can be used for method parameters, reply parameters and signals,
but not for return values.

dbus_traits<V> must provide a dbus_type, which is used to
extract and encode the data efficiently via dbus_message_iter_get_fixed_array()
and dbus_message_iter_append_fixed_array().
2009-10-07 16:11:20 +02:00
Patrick Ohly
940383f0b4 D-Bus C++: let app use char and unsigned char for D-BUS BYTE
Strictly speaking, an app has to use uint8_t when it wants
to exchange BYTEs with D-Bus. The extended binding maps both
int8_t and uint8_t to BYTE, so the app can use char and unsigned
char.
2009-10-07 16:11:20 +02:00
Patrick Ohly
b435d87392 D-Bus C++: added the possibility to pass Caller ID string and a watch
Use
    const Caller_t &caller
to get a caller ID string. Caller_t is a class which mimicks a
string, which is necessary to distinguish this parameter
from a std::string parameter passed via D-Bus.

Use
    const boost::shared_ptr<Watch> &watch
to be called with an active watch on the caller.
The callback invoked by the Watch must be set
with setCallback().
2009-10-07 16:11:20 +02:00
Patrick Ohly
0fb5794a21 D-Bus C++: C++ helper classes for libdbus/libgdbus
C++ classes and templates provided by header files simplify
programming in C++ with libdbus and libgdbus. libdbus items are
tracked with Boost shared pointers. Errors are turned into
exceptions. The binding to C++ is done via templates which provide the
C-style callbacks and declarations expected by libgdbus and map to
standard C++ types and STL data structures. Neither code generator nor
library code is required.

Synchronous and asynchronous method implementation are supported.
Asynchronous result deliver is done via a callback object with a
C++ signature that matches the values which have to be returned.

Due to the lack of variadic templates, lots of similar templates have
to be written to cover methods with varying combinations of arguments
and return codes. Not particularly elegant, but the alternative would
be to introduce a code generator, which has its own
drawbacks. Currently up to 10 items per method are supported, where
the number of items include the optional return value, arguments and
retvals.

Setting a Watch (base class) notifies the implementor of an when his
caller disconnects.  This can be used to abort a long-running method
when the consumer of the result goes away.

Callers are identified by their unique Bus ID, represented as Caller_t,
an alias for a plain std::string.

Objects are represented by their path with a DBusObject_t, again a
plain std::string. By default, each path comes with one interface
(DBusObject). It is possible to instantiate multiple DBusObjects with
the same path, which appears on D-Bus as a single object with multiple
interfaces.

The DBusObjectHelper simplifies object handling. It stores the
necessary information and connection reference so that the object can
be unregistered when the DBusObjectHelper is destroyed. The intended
usage is that classes own instances of DBusObjectHelper and activate
those with a method table that points to class methods.

Signals are handled as instances of EmitSignalX templates where X
stands for the number of parameters, pretty much like ResultX
callbacks are handled. The function call operator emits the signal,
with the auxiliary information (D-Bus signal name) set when
instantiating the EmitSignalX object as part of a class. The D-Bus
connection and interface is provided at the time of the signal
emission by the parent DBusObject.

The table entry for the signal is created by a static member function
of the template, which allows building a static table as required by
gdbus. Note that the signal name must be passed into that
function. This slight duplication is necessary because the template
cannot be parameterized with a string constant. Turning
makeSignalEntry() into a normal member function would make it
difficult to build the signal table.

DBusErrorCXX is a helper class for DBusError which automatically
initializes the struct, can be used to check for an error and throws
an exception for it.
2009-10-07 16:11:20 +02:00
Patrick Ohly
914dc8d15e gdbus: fixed segfault in watch disconnect function
If the apps callback function removes the watch that
triggered it, then disconnect_function() used a dangling
data pointer to retrieve the id (first problem) and
g_dbus_remove_watch() used a -1 connection_slot (second
problem, only occurs when the current watch was the
last one).

Fixed by storing the ID in a temporary variable and
adding a connection_slot check to g_dbus_remove_watch(),
similar to the one which was already in g_dbus_remove_all_watches().
2009-10-07 16:11:19 +02:00
Patrick Ohly
962599e5f8 gdbus: allow registering multiple interfaces per object
Apparently this was part of the design (there was a refcount
in ObjectData and a list of interfaces), but the implementation
didn't really use much of that.

Added a check whether there is already an object registered under
a path and reuse that object when adding further interfaces. Only
remove the object when its last interface is gone.
2009-10-07 16:11:19 +02:00
Patrick Ohly
50c7d83636 g_dbus_unregister_object(): fix same invalid memory reuse as for watches
g_dbus_unregister_object() has the same logical flaw as g_dbus_remove_watch():
it left a dangling pointer to its data in the connection slot. This pointer
was found when the slot was reused in the following call sequence:
g_dbus_register_interface()
g_dbus_unregister_interface()
g_dbus_register_interface()

The result was a segfault.
2009-10-07 16:11:19 +02:00
Patrick Ohly
5b71583cd1 g_dbus_create_error(): implemented support for detailed error description
The API had printf-style formatting for a detailed error description,
but that information was ignored. Added a sprintf variant which
allocates the resulting string dynamically, based on vsnprintf().
2009-10-07 16:11:19 +02:00
Patrick Ohly
7e558b23a2 gdbus: compile the gdbus utility library
The library is compiled and installed as a syncevolution utility
library (in other words, as lib/syncevolution/libgdbus), so that
it never conflicts with a copy of the code in another project.
2009-10-07 16:11:02 +02:00
Patrick Ohly
83bf7442d3 watch: call dbus_bus_remove_match() for each dbus_bus_add_match()
Without the remove call the matches would accumulate over time,
which can't be good for the dbus-server.

libgdbus commit ID:
d0084fd7b13b7f4665c223877f77576b08624dbf
2009-10-07 16:07:50 +02:00
Patrick Ohly
b71e1a6fbd fix for ConnectionData handling
g_dbus_remove_watch() decremented the slot ref count and freed
the ConnectionData associated with it if the ref count reached
zero. This left a dangling pointer to the ConnectionData instance
in the slot, which was found and used once the slot got recycled.

The new approach is to clear the data pointer in the slot if
its last user (watch or handler) is gone.

libgdbus commit ID:
3bb0e1e01230f50f66b3004ca385d73c49bff2c6
2009-10-07 16:07:50 +02:00
Marcel Holtmann
55a400b646 Extended GDBusWatchFunction parameter list
libgdbus commit ID:
0ed91c956fb6cf3d94eac17e778c63d5867bcb96
2009-10-07 16:07:49 +02:00
Marcel Holtmann
a8eff07719 Convert all documentation to gtk-doc style
libgdbus commit ID:
a6267460b97efd0ec421e42b88b9ca81a5c9baeb
2009-10-07 16:07:49 +02:00
Marcel Holtmann
53462914bc Protect the interface list with a static mutex
libgdbus commit ID:
9e96781f0e9495ac8efe3b81df968d4ac505e4e8
2009-10-07 16:07:49 +02:00
Marcel Holtmann
8ed69fa134 Use a GSource for message dispatching
libgdbus commit ID:
5b48fc1b816042a9ba8df93f4463fc70f8f259c6
2009-10-07 16:07:49 +02:00
Marcel Holtmann
60ec6188ee Don't add an extra newline in the debug messages
libgdbus commit ID:
8673867875ccc57712ca8bd0aeacc5f43156a32e
2009-10-07 16:07:49 +02:00
Marcel Holtmann
3320a13ea9 Fix typo in debug statement
libgdbus commit ID:
40bc7e6c459548abff92303b770999d2c5e82904
2009-10-07 16:07:49 +02:00
Marcel Holtmann
8d86a083c2 Add watch helper for receiving signals
libgdbus commit ID:
944ca9edbf23fd8d1b5eef4125f18bf26d7257dd
2009-10-07 16:07:48 +02:00
Marcel Holtmann
cf5698f3e7 Use automatic object registration only
libgdbus commit ID:
b0b19a4da72972887580597aadb0b4c86e64134a
2009-10-07 16:07:48 +02:00
Marcel Holtmann
47809b8836 Use common GDBusWatchFunction prototype
libgdbus commit ID:
6605699e3a1ff8275dcc359da7aa73e9d36996f2
2009-10-07 16:07:48 +02:00
Marcel Holtmann
0031027d25 Remove return parameter from disconnect watch callback
libgdbus commit ID:
3dff292ec3e3f9a614e23c8801bccd76e8a052b3
2009-10-07 16:07:48 +02:00
Marcel Holtmann
bd0305aff4 Add more flexible error handling helpers
libgdbus commit ID:
475a7f3b15bc2446f76383e81ed1621ecd6b47e5
2009-10-07 16:07:48 +02:00
Marcel Holtmann
d860fdbec2 Add support for per interface user data
libgdbus commit ID:
5aa0ef05fd3bedef99e1a95ede14e29593cddcdc
2009-10-07 16:07:48 +02:00
Marcel Holtmann
81a28ef5af Add helper functions for creating errors and replies
libgdbus commit ID:
67e0538719f9509ac14843195515e93202f278fe
2009-10-07 16:07:47 +02:00
Marcel Holtmann
2af84d7bbf Add helper functions for sending errors and replies
libgdbus commit ID:
354a36005f34dfab3c23f6a26270e5e266f92475
2009-10-07 16:07:47 +02:00
Marcel Holtmann
874f948b27 Protect the object list with a static mutex
libgdbus commit ID:
b2ee63cb570dd6d8ea58abe9ed3403091cfc389a
2009-10-07 16:07:47 +02:00
Marcel Holtmann
8f16323bfb Update copyright information
libgdbus commit ID:
b0972117a2588631286c677791652b924eaa42ac
2009-10-07 16:07:47 +02:00
Marcel Holtmann
c8d91e35bb Remove option for using dbus-glib's mainloop integration
libgdbus commit ID:
d718c7a6cdef1b2c1345ffda1810fb6e4b4a8573
2009-10-07 16:07:47 +02:00
Marcel Holtmann
816208b434 Add additional checks for error handling
libgdbus commit ID:
17ba2b7bead1c2c37dc33187bdacbaa7bac78b96
2009-10-07 16:07:47 +02:00
Marcel Holtmann
397ba9d4b9 Add option to use dbus-glib's mainloop integration
libgdbus commit ID:
fb91bf75e2704a39f334a403b18cd5a6059fd47b
2009-10-07 16:07:47 +02:00
Marcel Holtmann
12b610ed5e Add error parameter to setup functions
libgdbus commit ID:
159897795a8ec83a03dd2370e2dd800d89d73ddb
2009-10-07 16:07:46 +02:00
Marcel Holtmann
264d47f595 Add annotation for asynchronous methods
libgdbus commit ID:
7fd09a4ca7a95027ff4b877e82049b6f913c1e78
2009-10-07 16:07:46 +02:00
Marcel Holtmann
cbcaa2da7d Add error handling if bus is not available
libgdbus commit ID:
50a20744b2c96f554ad0b7f405c6d0e656aec8fc
2009-10-07 16:07:46 +02:00
Marcel Holtmann
11ab7f4e03 Add function for requesting additional bus names
libgdbus commit ID:
bbd311b2e965c5544980c5f27bd6c4174c0df051
2009-10-07 16:07:46 +02:00
Marcel Holtmann
a9041ca056 Fix naming of object flags
libgdbus commit ID:
69b72cb3af989e75476429fd7b158ea974719fb4
2009-10-07 16:07:46 +02:00