<div dir="ltr"><div><div><div><div><div><div>Hi all,<br><br></div>I've been playing around with Dbus and java for a while and I almost got what I wanted, but eventually got stuck anew.<br></div>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.<br>
<br></div>The dbus signals in which I'm concretely interested are DeviceAdded and DeviceRemoved: <br>[dbus-monitor output]<br><br>signal sender=:1.45 -> dest=(null destination) serial=186 path=/org/freedesktop/UDisks; interface=org.freedesktop.UDisks; member=DeviceAdded<br>
object path "/org/freedesktop/UDisks/devices/sdb"<br><br>signal sender=:1.45 -> dest=(null destination) serial=230 path=/org/freedesktop/UDisks; interface=org.freedesktop.UDisks; member=DeviceRemoved<br> object path "/org/freedesktop/UDisks/devices/sdb"<br>
<br></div>To accomplish this task in Java I created the following classes [1] and [2]:<br><br></div>[1] org.freedesktop.UDisks.java<br></div>/** Created with 'createinterface' utility on org.freedesktop.UDisks system bus interface**/<br>
<br>package org.freedesktop;<br><br>import java.util.List;<br>import org.freedesktop.dbus.DBusInterface;<br>import org.freedesktop.dbus.DBusSignal;<br>import org.freedesktop.dbus.UInt32;<br>import org.freedesktop.dbus.UInt64;<br>
import org.freedesktop.dbus.exceptions.DBusException;<br><br><div><div><div>public interface UDisks extends DBusInterface<br>{<br> /** ... **/<br> <br> public static class DeviceRemoved extends DBusSignal<br> {<br>
public final DBusInterface a;<br> public DeviceRemoved(String path, DBusInterface a) throws DBusException<br> {<br> super(path, a);<br> this.a = a;<br> }<br> }<br> <br> public static class DeviceAdded extends DBusSignal<br>
{<br> public final DBusInterface a;<br> public DeviceAdded(String path, DBusInterface a) throws DBusException<br> {<br> super(path, a);<br> this.a = a;<br> }<br> }<br><br> public void Uninhibit(String cookie);<br>
public String Inhibit();<br> public DBusInterface LinuxMdCreate(List<DBusInterface> components, String level, UInt64 stripe_size, String name, List<String> options);<br> ...<br></div><div>/** ... Remaining code removed for the sake of post length**/<br>
</div><div>}<br><br><br></div><div>[2] org.freedesktop.ListenHWDBusSignal.java<br></div><div><br>package org.freedesktop;<br><br>import org.freedesktop.dbus.DBusConnection;<br>import org.freedesktop.dbus.DBusSigHandler;<br>
import org.freedesktop.dbus.exceptions.DBusException;<br><br>public class ListenHWDBusSignal {<br><br> public static void main(String[] args) throws DBusException, ParseException {<br><br> System.out.println("Creating Connection"); <br>
DBusConnection conn=DBusConnection.getConnection(DBusConnection.SYSTEM);<br> <br> conn.addSigHandler(UDisks.DeviceAdded.class,new DBusSigHandler<UDisks.DeviceAdded>() {<br> @Override<br>
public void handle(<br> UDisks.DeviceAdded added) { <br> System.out.println("Device added!");<br> }<br> }); <br> <br> conn.addSigHandler(UDisks.DeviceRemoved.class,new DBusSigHandler<UDisks.DeviceRemoved>() {<br>
@Override<br> public void handle(<br> UDisks.DeviceRemoved removed) { <br> System.out.println("Device removed!");<br> }<br> }); <br>
System.out.println("Waiting for signals..."); <br> }<br><br>}<br><br><br></div><div>All right, I execute [2], all goes fine and the program waits for signals:<br><br>Creating Connection<br>Waiting for signals...<br>
<br></div><div>When I manually add a usb device, I got this line:<br>Device added!<br></div><div>but when I remove manually the usb, and I mean not safely-removing it, just plugging it out, I got nothing.<br></div><div>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?<br>
<br></div><div>Any help would be really appreciated, thanks!<br><br></div><div>Mzt.<br></div><div><br><br></div></div></div></div>