如何使用java接收dbus信号'org.gtk.Private.RemoteVolumeMonitor.DriveConnected'?

Posted

技术标签:

【中文标题】如何使用java接收dbus信号\'org.gtk.Private.RemoteVolumeMonitor.DriveConnected\'?【英文标题】:How to receive dbus signal 'org.gtk.Private.RemoteVolumeMonitor.DriveConnected' using java?如何使用java接收dbus信号'org.gtk.Private.RemoteVolumeMonitor.DriveConnected'? 【发布时间】:2021-05-23 09:08:51 【问题描述】:

我已经为获取 dbus 信号而苦苦挣扎了一段时间。我正在使用带有https://github.com/hypfvieh/dbus-java 3.2.4 版的java 1.8。目标只是让我的代码得到提及:

package pl.thetosters.szafagra.dbus;

import org.freedesktop.dbus.connections.impl.DBusConnection;
import org.freedesktop.dbus.exceptions.DBusException;
import org.freedesktop.dbus.interfaces.DBusSigHandler;
import org.gtk.Private.RemoteVolumeMonitor;

public class DBusSupport 
  private DBusConnection conn;

  public DBusSupport() 
    try 
      conn = DBusConnection.getConnection(DBusConnection.DBusBusType.SESSION);
      conn.addSigHandler(RemoteVolumeMonitor.DriveConnected.class, new DBusSigHandler<RemoteVolumeMonitor.DriveConnected>() 
        @Override
        public void handle(RemoteVolumeMonitor.DriveConnected driveConnected) 
          System.out.println("------OK");
        
      );
     catch (DBusException e) 
      e.printStackTrace();
    
  

package org.gtk.Private;

import org.freedesktop.dbus.exceptions.DBusException;
import org.freedesktop.dbus.interfaces.DBusInterface;
import org.freedesktop.dbus.messages.DBusSignal;

public class RemoteVolumeMonitor implements DBusInterface 

  public class DriveConnected extends DBusSignal 
    public DriveConnected(String path, String s, Object arg) throws DBusException 
      super(path, s, arg);
    
  

  @Override
  public boolean isRemote() 
    return false;
  

  @Override
  public String getObjectPath() 
    return null;
  

当我插入 USB 笔时,我得到了响应,我在控制台上看到以下内容:

2021-02-20 16:31:15 DEBUG InputStreamMessageReader:179 - => DBusSignal [clazz=null]
2021-02-20 16:31:15 DEBUG DBusConnection:857 - Handling incoming signal: DBusSignal [clazz=null]
2021-02-20 16:31:15 DEBUG DBusSignal:196 - Converting signal to type: class org.gtk.Private.RemoteVolumeMonitor$DriveConnected
2021-02-20 16:31:15 WARN  DBusConnection:912 - Exception while running signal handler 'pl.thetosters.szafagra.dbus.DBusSupport$1@5383c3b0' for signal 'DBusSignal [clazz=class org.gtk.Private.RemoteVolumeMonitor$DriveConnected]':
org.freedesktop.dbus.exceptions.DBusException: java.lang.IllegalArgumentException: argument type mismatch
    at org.freedesktop.dbus.messages.DBusSignal.createReal(DBusSignal.java:247)
    at org.freedesktop.dbus.connections.AbstractConnection$3.run(AbstractConnection.java:903)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalArgumentException: argument type mismatch
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.freedesktop.dbus.messages.DBusSignal.createReal(DBusSignal.java:240)
    ... 4 more
2021-02-20 16:31:15 DEBUG Error:143 - Creating message with serial 4
2021-02-20 16:31:15 DEBUG Error:848 - Appending sig: yyyy data: [108, 3, 0, 1]
2021-02-20 16:31:15 DEBUG Error:848 - Appending sig: ua(yv) data: [4, [[4, [s, org.freedesktop.dbus.exceptions.DBusExecutionException]], [5, [u, 835]], [6, [s, :1.33]], [8, [g, s]]]]
2021-02-20 16:31:15 DEBUG Error:848 - Appending sig: s data: [Error handling signal org.gtk.Private.RemoteVolumeMonitor.DriveConnected: java.lang.IllegalArgumentException: argument type mismatch]
2021-02-20 16:31:15 DEBUG OutputStreamMessageWriter:34 - <= Error(0,4)  Error Name=>org.freedesktop.dbus.exceptions.DBusExecutionException, Reply Serial=>835, Destination=>:1.33, Signature=>s   Error handling signal org.gtk.Private.RemoteVolumeMonitor.DriveConnected: java.lang.IllegalArgumentException: argument type mismatch 

我不知道我做错了什么。我试图将 DriveConnected 构造函数更改为几个无效的变体。如果它不同,那么这个我得到了另一个错误:

DBusSignal:226 - Could not find suitable constructor for class org.gtk.Private.RemoteVolumeMonitor$DriveConnected with argument-types: [class java.lang.String, class java.lang.String, class [Ljava.lang.Object;]

所以我猜签名没问题(因为参数与我在运行 dbus-monitor 时在控制台上看到的相匹配)。我做错了什么?

编辑: 来自 dbus-monitor 的日志显示 MountAdded 消息

signal time=1613978013.675766 sender=:1.33 -> destination=(null destination) serial=980 path=/org/gtk/Private/RemoteVolumeMonitor; interface=org.gtk.Private.RemoteVolumeMonitor; member=MountAdded
   string "org.gtk.vfs.UDisks2VolumeMonitor"
   string "0x7f645c015c80"
   struct 
      string "0x7f645c015c80"
      string "3,9 GB Volume"
      string ". GThemedIcon drive-removable-media-usb drive-removable-media drive-removable drive"
      string ". GThemedIcon drive-removable-media-usb-symbolic drive-removable-media-symbolic drive-removable-symbolic drive-symbolic drive-removable-media-usb drive-removable-media drive-removable drive"
      string ""
      string "file:///media/bar/6A90-7B00"
      boolean true
      string "0x5620577283d0"
      array [
      ]
      string "gvfs.time_detected_usec.1613978013673952"
      array [
      ]
   

【问题讨论】:

【参考方案1】:

我认为在参数的反序列化过程中存在问题。也许您在另一端以错误的格式发送信号。 实际上,在这一边,您正在等待“DriveConnected "pippo" Object”的信号,这可能是一个问题,因为 DBUS 无法反序列化。

第一次尝试尝试在构造函数上删除对象,并在客户端检查您是否发送 DriveConnected /from_path_1 "pippo"

【讨论】:

此信号由系统应用发送。当您运行 dbus-monitor --session --monitor 然后插入 USB 笔时,您将看到此信号已发出。我无法更改此信号的格式。 你能把插入usb pen后在dbus-monitor上看到的信息贴出来吗? 我编辑了初始问题以添加此日志。作为评论太长了。 我确认它无法将结构转换为对象。您必须创建一个类,该类将包含您在 dbus-monitor 上看到的结构内的所有字段。 DBus 仅适用于具有良好序列化的回调。创建 USBInfo 类后,您应该在 DriveConnected 构造函数签名中提供该类,而不是 Object arg 你应该创建你的结构对象,如下所示 ==> dbus.freedesktop.org/doc/dbus-java/dbus-java/…

以上是关于如何使用java接收dbus信号'org.gtk.Private.RemoteVolumeMonitor.DriveConnected'?的主要内容,如果未能解决你的问题,请参考以下文章

用python接收dbus信号

Python DBus 移除信号匹配(移除信号接收器)

如何使用 gdbus-codegen 使用 org.gtk.GDBus.C.ForceGVariant 注释 xml 文件

Open*** v3 Dbus 客户端未接收到所有信号

dbus 信号中收到的消息与发送的数据不匹配

UDisks2 的 Dbus 自省不完整?