leopoodle
leopoodle

Reputation: 2482

Use DBus daemon to exchange message from C application to python application

Hi I am pretty new to DBus daemon and would like to accomplish simple message exchange between C application and python application, which are running on custom linux-like environment. My understanding is

  1. First, start dbus-daemon from init file
  2. Register the C application and the python application to the D-BUS
  3. Send message (it's just a simple string) on the bus between these 2 applications.

My questions are regarding (2) and (3) above. How can C application and python application register to the same bus?

Also, what APIs need to be invoked to send string messages between these 2 applications?

Upvotes: 1

Views: 1354

Answers (2)

Cadu
Cadu

Reputation: 195

[You asked for simple message passing]

Do you really need DBus?

If you're asking for simple message passing between a C app and Python, why not use a message-passing library like Rabbit/ZeroMQ? Those already solve all the issues related to passing/receiving messages.

And, if you want to keep dependencies to a minimum, you could use a UNIX socket or even some simple TCP/UDP datagrams.

EDIT: Since I'm trying to convince you to research ZeroMQ as your IPC platform and how simple it is, here's a sample C "Client" sending a complete datagram to the server, which replies back.

ZeroMQ Client Example in C:

// Hello World client
#include <zmq.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>

int main ( void )
{
    printf ( "Connecting to hello world server…\n" );
    void *context   = zmq_ctx_new ();
    void *requester = zmq_socket ( context, ZMQ_REQ );
    zmq_connect ( requester, "tcp://localhost:5555" );

    int request_nbr;
    for ( request_nbr = 0; request_nbr != 10; request_nbr++ ) {
        char buffer [10];
        printf ( "Sending Hello %d…\n", request_nbr );
        zmq_send ( requester, "Hello", 5, 0 );
        zmq_recv ( requester, buffer, 10, 0 );
        printf ( "Received World %d\n", request_nbr );
    }
    zmq_close (requester);
    zmq_ctx_destroy (context);
    return 0;
}

And the server is just as simple:

ZeroMQ Server Example in C

// Hello World server

#include <zmq.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>

int main ( void )
{
    // Socket to talk to clients
    void *context   = zmq_ctx_new ();
    void *responder = zmq_socket ( context, ZMQ_REP );
    int rc = zmq_bind ( responder, "tcp://*:5555" );
    assert ( rc == 0 );

    while ( 1 ) {
        char buffer [10];
        zmq_recv ( responder, buffer, 10, 0 );
        printf ( "Received Hello\n" );
        sleep ( 1 );                                // Do some 'work'
        zmq_send ( responder, "World", 5, 0 );
    }
    return 0;
}

Upvotes: 1

LEW21
LEW21

Reputation: 180

There are two default buses on each Linux system - the system bus (shared for all users, made for system services) and the session bus. All DBus libraries let you trivially connect to one of those buses.

Then, in the receiver process, you need to take ownership of a bus name (a string that will let other processes find you), and register an object providing some methods. And then you will be able to call those methods from another process.

The best DBus API for C is https://developer.gnome.org/gio/stable/GDBusConnection.html and the best API for Python is https://github.com/LEW21/pydbus

Upvotes: 0

Related Questions