hypfvieh / dbus-java

Improved version of java DBus library provided by freedesktop.org (https://dbus.freedesktop.org/doc/dbus-java/)
https://hypfvieh.github.io/dbus-java/
MIT License
180 stars 72 forks source link

How to connect remote DBus Session ? #217

Closed nihasmata closed 2 months ago

nihasmata commented 1 year ago

Thank you for the library,

I have a question let me ask. Which way should i follow to connect remote VLC player dbus via this library.

Normally i use the following command to connect VLC locally

dbus-send --type=method_call --dest=org.mpris.MediaPlayer2.vlc /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause

But i want to connect to the the VLC via this library. I configured session.conf on server side. My server ip is : 10.0.5.161 and port 65656

Thanks

hypfvieh commented 1 year ago

I don't understand what you are planning to do. Directly connecting dbus-java to VLC will not work, you always need a DBus server.

Based on the command line you posted, you need to create the interface classes for VLC using the code generator.

After that you can use dbus-java to connect to the DBus server. When using regular DBus server on unix socket you have to connect to the session bus of the user running VLC.

If you want to connect to a DBus server running on TCP you have to add the TCP transport to your classpath and open a TCP connection:

        String host = "tcp:host=localhost,port=12344";
        try (DBusConnection conn = DBusConnectionBuilder.forAddress(BusAddress.of(host)).build()) {
            // do something
        } catch (DBusException | IOException _exDbe) {
            _exDbe.printStackTrace();
        }
nihasmata commented 1 year ago

Thank you for the response,

I added the following configuration to the /etc/dbus-1/session.conf on my CentOS DBus server


<listen>tcp:host=localhost,bind=*,port=6667,family=ipv4</listen>
<listen>unix:tmpdir=/tmp</listen>

<auth>ANONYMOUS</auth>
<allow_anonymous/>

When i try to connect to the dbus server from the java client, i get the connection refused exception. Do you have any idea ?

Exception in thread "main" java.lang.RuntimeException: org.freedesktop.dbus.exceptions.DBusException: Failed to connect to bus: Connection refused
    at Main.createDBusConnection(Main.java:23)
    at Main.main(Main.java:9)
Caused by: org.freedesktop.dbus.exceptions.DBusException: Failed to connect to bus: Connection refused
    at org.freedesktop.dbus.connections.AbstractConnection.<init>(AbstractConnection.java:157)
    at org.freedesktop.dbus.connections.impl.DBusConnection.<init>(DBusConnection.java:85)
    at org.freedesktop.dbus.connections.impl.DBusConnectionBuilder.build(DBusConnectionBuilder.java:195)
    at Main.createDBusConnection(Main.java:15)
    ... 1 more
Caused by: java.net.ConnectException: Connection refused
    at java.base/sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
    at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:776)
    at java.base/sun.nio.ch.SocketAdaptor.connect(SocketAdaptor.java:120)
    at org.freedesktop.dbus.transport.tcp.TcpTransport.connectImpl(TcpTransport.java:62)
    at org.freedesktop.dbus.connections.transports.AbstractTransport.internalConnect(AbstractTransport.java:171)
    at org.freedesktop.dbus.connections.transports.AbstractTransport.connect(AbstractTransport.java:141)
    at org.freedesktop.dbus.connections.transports.TransportBuilder.build(TransportBuilder.java:339)
    at org.freedesktop.dbus.connections.AbstractConnection.<init>(AbstractConnection.java:151)
    ... 4 more
hypfvieh commented 1 year ago

No I don't have any idea. I have to admit I never tried running any DBus server on TCP allowing access from outside (only localhost).

I would suggest to check if the server is reachable using telnet. The error looks like the connection is denied on network level which means either no process is listening on that port or the firewall blocks the port.

nihasmata commented 1 year ago

Thank you for the answer, I just enabled the TCP connection. And call dbus method (without parameters) successfully like below

DBusConnection sessionConnection =  DBusConnectionBuilder.forAddress("tcp:host=10.0.5.161,port=2334").build();

Message methodCall = new MethodCall(null,"org.mpris.MediaPlayer2.vlc","/org/mpris/MediaPlayer2","org.mpris.MediaPlayer2.Player","Pause",(byte)0,null);
 sessionConnection.sendMessage(methodCall);

But i also have method Seek like below. It requires a paramater Offset. Where can i put this parameter in MethodCall ? Seek (x: Offset)

hypfvieh commented 1 year ago

I don't know why you didn't create the required interfaces and just use them instead of trying to solve this the hard way.

I added an example to the example project demonstrating how to control a running VLC instance using DBus on the session bus of the current user. You may adept that for your needs.

nihasmata commented 1 year ago

Thank you for the reply. Let me ask last question.

There is no Signal generated for Seeked of mpris in the example.

How to create Seeked signal of mpris ?

hypfvieh commented 1 year ago

You don't create the Seeked signal. Signals are usually never created manually.

Signals are used to asynchronously notify a DBus connected application about an action.

In this particular case, the Seeked signal will be created by VLC/mpris and will be send to DBus. If you want to do anything when this signal is received, you have to install a signal handler for it.

See the description of Seeked signal which describes when it will be created by VLC/mpris.

nihasmata commented 1 year ago

How to create a java class which emits this signal using java-dbus ?

hypfvieh commented 1 year ago

I don't know what you want to do. You said you want to control a VLC instance which provides mpris-API on DBus. In that case: you DON'T EMIT SIGNALS, VLC will do

It is completely useless to emit this signal because nobody wants to have it or will even care about it.

nihasmata commented 1 year ago

Sorry for misunderstanding. My aim is to be triggered in my java application when the vlc publish Seeked data to DBUS. As far as i understand , i should create a class which extends DBusSignal class and add this class to my Dbus connection with addSigHandler method. My question is how to create such a java signal class which gets the VLC Seeked data ?

hypfvieh commented 1 year ago

So you don't want to send/emit a signal, you want to receive and react to a signal. This requires you to create a proper signal handler and provide the proper signal class.

I updated the example to show how to do that.