14 Examples

As an example here are a complete set of interfaces for the bluemon24 daemon, which communicates over D-Bus. These interfaces were all created by querying introspection data over the bus.


package cx.ath.matthew.bluemon;  
import org.freedesktop.dbus.DBusInterface;  
import org.freedesktop.dbus.UInt32;  
public interface Bluemon extends DBusInterface  
{  
  public Triplet<String, Boolean, UInt32>  
  Status(String address);  
}


Figure 21: cx/ath/matthew/bluemon/Bluemon.java


package cx.ath.matthew.bluemon;  
import org.freedesktop.dbus.DBusInterface;  
import org.freedesktop.dbus.DBusSignal;  
import org.freedesktop.dbus.exceptions.DBusException;  
public interface ProximitySignal extends DBusInterface  
{  
   public static class Connect extends DBusSignal  
   {  
      public final String address;  
      public Connect(String path, String address)  
                                 throws DBusException  
      {  
         super(path, address);  
         this.address = address;  
      }  
   }  
   public static class Disconnect extends DBusSignal  
   {  
      public final String address;  
      public Disconnect(String path, String address)  
                                 throws DBusException  
      {  
         super(path, address);  
         this.address = address;  
      }  
   }  
}


Figure 22: cx/ath/matthew/bluemon/ProximitySignal.java


package cx.ath.matthew.bluemon;  
import org.freedesktop.dbus.Tuple;  
/** Just a typed container class */  
public final class Triplet <A,B,C> extends Tuple  
{  
   public final A a;  
   public final B b;  
   public final C c;  
   public Triplet(A a, B b, C c)  
   {  
      super(a, b, c);  
      this.a = a;  
      this.b = b;  
      this.c = c;  
   }  
}


Figure 23: cx/ath/matthew/bluemon/Triplet.java


package cx.ath.matthew.bluemon;  
import org.freedesktop.dbus.DBusConnection;  
import org.freedesktop.dbus.DBusSigHandler;  
import org.freedesktop.dbus.DBusSignal;  
import org.freedesktop.dbus.UInt32;  
import org.freedesktop.dbus.exceptions.DBusException;  
 
public class Query {  
   public static void main(String[] args) {  
      String btid;  
      Triplet<String, Boolean, UInt32> rv = null;  
 
      if (0 == args.length) btid = "";  
      else btid = args[0];  
 
      DBusConnection conn = null;  
      try {  
         conn = DBusConnection.getConnection(DBusConnection.SYSTEM);  
      } catch (DBusException De) {  
         System.exit(1);  
      }  
      Bluemon b = (Bluemon) conn.getRemoteObject(  
            "cx.ath.matthew.bluemon.server",  
            "/cx/ath/matthew/bluemon/Bluemon", Bluemon.class);  
      try {  
         rv = b.Status(btid);  
      } catch (RuntimeException Re) {  
         System.exit(1);  
      }  
      String address = rv.a;  
      boolean status = rv.b;  
      int level = rv.c.intValue();  
 
      if (status)  
         System.out.println("Device "+address+  
                            " connected with level "+level);  
      else  
         System.out.println("Device "+address+" not connected");  
      conn.disconnect();  
   }  
}


Figure 24: cx/ath/matthew/bluemon/Query.java


/* cx/ath/matthew/bluemon/Client.java */  
package cx.ath.matthew.bluemon;  
 
import org.freedesktop.dbus.DBusConnection;  
import org.freedesktop.dbus.DBusSigHandler;  
import org.freedesktop.dbus.DBusSignal;  
import org.freedesktop.dbus.exceptions.DBusException;  
 
public class Client implements DBusSigHandler  
{  
   public void handle(DBusSignal s)  
   {  
      if (s instanceof ProximitySignal.Connect)  
         System.out.println("Got a connect for "  
               +((ProximitySignal.Connect) s).address);  
      else if (s instanceof ProximitySignal.Disconnect)  
         System.out.println("Got a disconnect for "  
               +((ProximitySignal.Disconnect) s).address);  
   }  
   public static void main(String[] args)  
   {  
      System.out.println("Creating Connection");  
      DBusConnection conn = null;  
      try {  
         conn = DBusConnection  
                  .getConnection(DBusConnection.SYSTEM);  
      } catch (DBusException DBe) {  
         System.out.println("Could not connect to bus");  
         System.exit(1);  
      }  
 
      try {  
         conn.addSigHandler(ProximitySignal.Connect.class,  
         new Client());  
         conn.addSigHandler(ProximitySignal.Disconnect.class,  
         new Client());  
      } catch (DBusException DBe) {  
         conn.disconnect();  
         System.exit(1);  
      }  
   }  
}


Figure 25: cx/ath/matthew/bluemon/Client.java