Creating services in C

John Palmieri johnp at redhat.com
Tue Dec 23 11:41:05 PST 2008


While I won't do your homework for you (you will learn better if you figure it out yourself)  I will go over the differences between the bindings and the C API and point you to the correct API to use.  First the C API has a concept of this higher level object interface but has very few convenience functions that express this API in the way the bindings do.  This is because the lowlevel API is there to be small and flexible and relies on the bindings to provide the appropriate interface to fit into the particular language.  So step one is to understand that for the most part names, object paths and interfaces are all concepts though the bus does give names a concrete semantic and the specification spells out the rest.

In the C bindings you have the concept of a filter function which all messages flow through.  It is this filter function (think of it as a callback every time a message is sent to you from the bus), which you create a state machine which gives the object_path, interface and message type and message name meaning.  So the filter function gets called with the message as the argument.  You then check the various fields (object_path, interface, message type, and message name) to route the message to the correct handler (you can either handle the message in the filter function or like in any program hand it off to a helper function or object).  If it is a message your filter function is setup to handle you need to return the correct enum to say you handled it otherwise you need to send back that it hasn't been handled so other filter functions will get it, including the default one which sends back an error that the method can't be found.

Some other tidbits are that you need to implement all the standard interfaces such as Introspectable in order to be a good D-Bus citizen.  Read the Spec in its entirety.  The C API follows this.  Also look at the API docs http://dbus.freedesktop.org/doc/dbus/api/html/modules.html

Of interest to you are 

MessageBus API's:
dbus_bus_get
dbus_bus_request_name
dbus_bus_add_match

DBusConnection API's:
dbus_connection_send*
dbus_connection_add_filter
There is also a whole bunch of convenience functions for keeping track of object paths and such

DBusError API's:
(all of it)

DBusMessage API's:
(most of it)

Hope that helps.

----- "Søren Hauberg" <hauberg at gmail.com> wrote:

> Hi All,
>   I'm not quite sure where to ask, so if this is the wrong mailing
> list just tell me :-)
> Anyway, I'm working on the Octave interpreter (www.octave.org), and
> I'd like to have some way for external applications to run code in
> this interpreter. So, I figured D-Bus would be the tool for this. I
> find the documentation for the Python bindings fairly easy to read,
> but I can't seem to wrap my head around the C docs. So, I've written
> the program I need in Python, and it is:
> 
> == CODE ==
> class octave_server (dbus.service.Object):
>     @dbus.service.method ("org.octave.interface",
>                           in_signature="s", out_signature="")
>     def eval_when_idle (self, code):
>         print (code)
> 
>     @dbus.service.method ("org.octave.interface", in_signature="",
> out_signature="")
>     def exit (self):
>         print ("exiting per request")
>         mainloop.quit ()
> 
> 
> if __name__ == "__main__":
>     dbus.mainloop.glib.DBusGMainLoop (set_as_default=True)
> 
>     session_bus = dbus.SessionBus ()
>     name = dbus.service.BusName ("org.octave.interface", session_bus)
>     object = octave_server (session_bus,  "/octave")
> 
>     mainloop = gobject.MainLoop ()
>     mainloop.run ()
> == END CODE ==
> 
> Now, I need to port this code to C. Could anybody give me some
> pointers, on which functions to look into, etc. ?
> 
> Thanks,
> Søren
> _______________________________________________
> dbus mailing list
> dbus at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/dbus

-- 
--
John (J5) Palmieri
Software Engineer
Red Hat, Inc.


More information about the dbus mailing list