Java Dbus - Problem with Deserialisation

Johannes Felten johannesfelten at googlemail.com
Wed Mar 5 09:37:16 PST 2008


Hi there,

I use dbus-java to communicate via DBus. This works fine as long as I stick
to primitve types natively supported by the java dbus lib such as String,
int, boolean and so on. However I try to send objects instances (of a class
called Metadata) via DBus, which have a String member and a
ArrayList<String> member. So I implemented the serialize and deserialize
methods according to the API and the tutorial which ships with the lib.
When trying to call a remote method which should deliver a Metadata object,
I receive the following exception:

Exception in thread "main"
org.freedesktop.dbus.exceptions.DBusExecutionException:
Wrong return type (not expecting Tuple)
    at org.freedesktop.dbus.RemoteInvocationHandler.convertRV(
RemoteInvocationHandler.java:65)
    at org.freedesktop.dbus.RemoteInvocationHandler.executeRemoteMethod(
RemoteInvocationHandler.java:138)
    at org.freedesktop.dbus.RemoteInvocationHandler.invoke(
RemoteInvocationHandler.java:187)
    at $Proxy1.pongMD(Unknown Source)
    at de.ichaus.entw.DBusTest.dBusClient.DBusClient.main(DBusClient.java
:65)

I put together a small testcase existing of a server and a client in case
anyone want to run it.

I thought it might be a problem with that List member in Metadata. Won't
work with Vector<String>, ArrayList<String> or Collection<String> either.
I tried both versions Version 2.4 and 1.13 of the library.

It would be great if anyone could suggest something I am missing out on or
got wrong. :)

Greetings Johannes Felten

CLASSES:

//------------------------------------------------------------------------------------------------------------------
package de.ichaus.entw.DBusTest.dBusServer;

import java.io.IOException;

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

public class DBusServer{

    private static DBusConnection conn = null;
    private static Pong pongObj = null;

    private static void connect(){
        try {
            conn = DBusConnection.getConnection(DBusConnection.SESSION);
            conn.requestBusName("de.ichaus.entw.DBusServerPingPong");
            conn.exportObject("/pongObj",pongObj);
        } catch (DBusException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        pongObj = new Pong();

        connect();
        System.out.print("Press ENTER to exit.");
        try {
            System.in.read();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally{
            conn.disconnect();
        }

    }

}

//------------------------------------------------------------------------------------------------------------------

package de.ichaus.entw.DBusTest.dBusServer;

import java.util.ArrayList;

import org.freedesktop.dbus.DBusInterface;
import org.freedesktop.dbus.Tuple;

import de.ichaus.entw.DBusTest.types.Metadata;

public class Pong implements DBusConnectorInterface {

    private Metadata pongMD;

    public Pong(){};

    public String pong() {
        return "pong";
    }

    public void makeMD(String mdStr){
        ArrayList<String> strLst = new ArrayList<String>(3);
        for (int i=0; i<3; i++)
            strLst.add(mdStr+"_"+i);
        pongMD = new Metadata(mdStr, strLst);
    }

    public Metadata pongMD(){
        return pongMD;
    }

    public boolean isRemote() {
        return false;
    }

}


//------------------------------------------------------------------------------------------------------------------

package de.ichaus.entw.DBusTest.dBusClient;

import java.util.Calendar;

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

import de.ichaus.entw.DBusTest.dBusServer.DBusConnectorInterface;
import de.ichaus.entw.DBusTest.dBusServer.Pong;

public class DBusClient {

    private static DBusConnection conn = null;
    private static DBusConnectorInterface pongObj = null;

    private static void connect(){
        try {
            conn = DBusConnection.getConnection(DBusConnection.SESSION);
            //pongObj = (DBusConnectorInterface)conn.getRemoteObject("
de.ichaus.entw.DBusServerPingPong", "/pongObj");
            //pongObj = (DBusConnectorInterface)conn.getRemoteObject("
de.ichaus.entw.DBusServerPingPong", "/pongObj",
de.ichaus.entw.DBusTest.dBusServer.DBusConnectorInterface.class);
            pongObj = (DBusConnectorInterface)conn.getRemoteObject("
de.ichaus.entw.DBusServerPingPong", "/pongObj", DBusConnectorInterface.class
);
        } catch (DBusException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static String getTimeStamp(){
        Calendar rightNow = Calendar.getInstance();
        return rightNow.get(Calendar.YEAR) + "-"
                + (rightNow.get(Calendar.MONTH) + 1) + "-"
                + rightNow.get(Calendar.DAY_OF_MONTH) + " "
                + rightNow.get(Calendar.HOUR_OF_DAY) + ":"
                + rightNow.get(Calendar.MINUTE) + ":"
                + rightNow.get(Calendar.SECOND) + "."
                + rightNow.get(Calendar.MILLISECOND);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("Starttime:    " + getTimeStamp());
        connect();
        System.out.println("Connected:    " + getTimeStamp());

        System.out.println("\n Play some ping pong...");
        for (int i = 0; i < 10; i++) {
            System.out.println("I said ping...");
            if (pongObj != null)
                System.out.println("Server said " + pongObj.pong());
            else
                System.out.println("Server said nothing...");
        }

        System.out.println("Endtime:      " + getTimeStamp());
        System.out.println(" Finished ping pong play.");

        System.out.println("\nPlay some Metadata ping pong...");
        for (int i = 0; i < 10; i++) {
            System.out.println("I said makeMD...");
            if (pongObj != null)
            {
                pongObj.makeMD("pong_"+i);
                System.out.println("Server said " + pongObj.pongMD());
            }
            else
                System.out.println("Server said nothing...");
        }
        System.out.println(" Finished Metadata ping pong.");

        if (conn != null)
            conn.disconnect();

        System.out.println("Disconnected: " + getTimeStamp());
    }
}

//------------------------------------------------------------------------------------------------------------------
package de.ichaus.entw.DBusTest.dBusServer;

import org.freedesktop.dbus.DBusInterface;

import de.ichaus.entw.DBusTest.types.Metadata;

public interface DBusConnectorInterface extends DBusInterface {

    public String pong();
    public void makeMD(String mdStr);
    public Metadata pongMD();

}

//------------------------------------------------------------------------------------------------------------------

package de.ichaus.entw.DBusTest.types;

import java.util.ArrayList;
import java.util.List;

import org.freedesktop.dbus.DBusSerializable;
import org.freedesktop.dbus.Position;
import org.freedesktop.dbus.exceptions.DBusException;

public class Metadata implements DBusSerializable{

    @Position(0)
    private String md5Sum = "";
    @Position(1)
    private List<String> someStrings;

    public Metadata(){}

    public Metadata(String md5, List<String> strings){
        md5Sum = md5;
        someStrings = strings;
    }

    public Object[] serialize() throws DBusException {
        return new Object[] {this.md5Sum, this.someStrings};
    }

    public void deserialize(String md5sum, List<String> someStrings){
        this.md5Sum = md5sum;
        this.someStrings = new ArrayList<String>(someStrings);
    }

}

//------------------------------------------------------------------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.freedesktop.org/archives/dbus/attachments/20080305/bf561fe5/attachment.htm 


More information about the dbus mailing list