Missing org.freedesktop.UDisks signals (DBus 2.7 and Java)

mazzzta mazzzta mazzzta2 at gmail.com
Fri Sep 6 00:36:18 PDT 2013


Hi all,

I've been playing around with Dbus and java for a while and I almost got
what I wanted, but eventually got stuck anew.
What I need is a java program that listens to the SYSTEM bus, particularly
the org.freedesktop.UDisks interface signals, in order to detect when some
devices (most often block devices) are added or removed from the machine(
for me that means "plugged the device in and out"). Note again that I only
need to detect these signals, not the information that travels along with
it.

The dbus signals in which I'm concretely interested are DeviceAdded and
DeviceRemoved:
[dbus-monitor output]

signal sender=:1.45 -> dest=(null destination) serial=186
path=/org/freedesktop/UDisks; interface=org.freedesktop.UDisks;
member=DeviceAdded
   object path "/org/freedesktop/UDisks/devices/sdb"

signal sender=:1.45 -> dest=(null destination) serial=230
path=/org/freedesktop/UDisks; interface=org.freedesktop.UDisks;
member=DeviceRemoved
   object path "/org/freedesktop/UDisks/devices/sdb"

To accomplish this task in Java I created the following classes [1] and [2]:

[1] org.freedesktop.UDisks.java
/** Created with 'createinterface' utility on org.freedesktop.UDisks system
bus interface**/

package org.freedesktop;

import java.util.List;
import org.freedesktop.dbus.DBusInterface;
import org.freedesktop.dbus.DBusSignal;
import org.freedesktop.dbus.UInt32;
import org.freedesktop.dbus.UInt64;
import org.freedesktop.dbus.exceptions.DBusException;

public interface UDisks extends DBusInterface
{
  /** ... **/

   public static class DeviceRemoved extends DBusSignal
   {
      public final DBusInterface a;
      public DeviceRemoved(String path, DBusInterface a) throws
DBusException
      {
         super(path, a);
         this.a = a;
      }
   }

   public static class DeviceAdded extends DBusSignal
   {
      public final DBusInterface a;
      public DeviceAdded(String path, DBusInterface a) throws DBusException
      {
         super(path, a);
         this.a = a;
      }
   }

  public void Uninhibit(String cookie);
  public String Inhibit();
  public DBusInterface LinuxMdCreate(List<DBusInterface> components, String
level, UInt64 stripe_size, String name, List<String> options);
  ...
/** ... Remaining code removed for the sake of post length**/
}


[2] org.freedesktop.ListenHWDBusSignal.java

package org.freedesktop;

import org.freedesktop.dbus.DBusConnection;
import org.freedesktop.dbus.DBusSigHandler;
import org.freedesktop.dbus.exceptions.DBusException;

public class ListenHWDBusSignal {

    public static void main(String[] args) throws DBusException,
ParseException {

        System.out.println("Creating Connection");
        DBusConnection
conn=DBusConnection.getConnection(DBusConnection.SYSTEM);

        conn.addSigHandler(UDisks.DeviceAdded.class,new
DBusSigHandler<UDisks.DeviceAdded>() {
              @Override
              public void handle(
                UDisks.DeviceAdded added) {
                 System.out.println("Device added!");
              }
            });

        conn.addSigHandler(UDisks.DeviceRemoved.class,new
DBusSigHandler<UDisks.DeviceRemoved>() {
              @Override
              public void handle(
                UDisks.DeviceRemoved removed) {
                 System.out.println("Device removed!");
              }
            });
        System.out.println("Waiting for signals...");
    }

}


All right, I execute [2], all goes fine and the program waits for signals:

Creating Connection
Waiting for signals...

When I manually add a usb device, I got this line:
Device added!
but when I remove manually the usb, and I mean not safely-removing it, just
plugging it out, I got nothing.
I also checked the bus signals with dbus-monitor along and see the
DeviceRemoved signal is sent by org.freedesktop.UDisks, so I think there's
a problem on the java side that I do not see. Why I do not get the line
"Device removed!"? Is there something wrong in the code?

Any help would be really appreciated, thanks!

Mzt.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freedesktop.org/archives/dbus/attachments/20130906/64663a72/attachment.html>


More information about the dbus mailing list