0

我一直在玩 Dbus 2.7 和 java,我几乎得到了我想要的东西,但最终又被卡住了。我需要的是一个 java 程序,它监听 SYSTEM 总线,特别是 org.freedesktop.UDisks 接口信号,以便检测何时从机器中添加或删除某些设备(通常是块设备)(对我来说这意味着“插入和拔出设备”)。请注意,我只需要检测这些信号,而不是与它们一起传播的信息。

我特别感兴趣的 dbus 信号是 DeviceAdded 和 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"

为了在 Java 中完成这项任务,我创建了以下类 [1] 和 [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...");
   }

}

好吧,我执行[2],一切正常,程序等待信号:

Creating Connection
Waiting for signals...

当我手动添加一个 USB 设备时,我得到这一行: 设备已添加! 但是当我手动移除 USB 时,我的意思是不安全地移除它,只是将其拔出,我什么也得不到。

我还使用 dbus-monitor 检查了总线信号,发现 DeviceRemoved 信号实际上是由 org.freedesktop.UDisks 发送到系统总线的,所以我认为 java 端存在我没有看到的问题。为什么我没有得到“设备已删除!”这一行?代码有问题吗?

任何帮助将不胜感激,谢谢!

4

0 回答 0