Missing org.freedesktop.NetworkManager signals (DBus 2.8 and Java)

Lambrecht Jürgen <[email protected]>
Newsgroups gmane.comp.freedesktop.dbus
Message-ID <[email protected]>
Hi,

I am trying to receive NetworkManager signals over Dbus with a Java 
program.
But I never receive any signal.
Although, when I start in a shell:
   dbus-monitor --system 
"type='signal',sender='org.freedesktop.NetworkManager',interface='org.freedesktop.NetworkManager'"
I do receive the signals.

I am new to Dbus (and to java), and I used code I found on this list:
ref.: http://lists.freedesktop.org/archives/dbus/2013-September/015781.html
     Missing org.freedesktop.UDisks signals (DBus 2.7 and Java)
(I also used example code from Rm5248 - thanks both)

I also changed DBusInterface to Path in the signals as proposed by Matt 
in that email thread.
But no help: I receive no signal.

I compile with Eclipse using libraries dbus-2.8.jar, hexdump-0.2.jar, 
unix-0.5.jar, JRE System Library [JavaSE-1.7].
I run the program on my laptop (LMDE, Debian) with java version 
"1.7.0_21" and NetworkManager version 0.9.8.0. (both from Eclipse and 
from the command line)
I also test on an embedded PC with Debian server (stable v7.6) with java 
version "1.7.0_65" and NetworkManager 0.9.10.0. (with only root user)

The code is in attach. I generated NetworkManager.java with
   CreateInterface -y -f org.freedesktop.NetworkManager 
/org/freedesktop/NetworkManager

Any help would be really appreciated, thanks!
Kind regards,
Jürgen

-- 
Jürgen Lambrecht
R&D Associate
Mobile: +32 499 644 531
Twitter: JGRLambrecht
Tel: +32 (0)51 303045    Fax: +32 (0)51 310670
http://www.televic-rail.com
Televic Rail NV - Leo Bekaertlaan 1 - 8870 Izegem - Belgium
Company number 0825.539.581 - RPR Kortrijk

_______________________________________________
dbus mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dbus
Dbus.java (text/x-java, 7.6 KB)
package dbus;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.freedesktop.DBus.Introspectable;
//import org.freedesktop.dbus.*;
//import org.freedesktop.dbus.exceptions.DBusException;
import org.freedesktop.*;
import org.freedesktop.dbus.*;
import org.freedesktop.dbus.exceptions.*;


public class Dbus {
    private DBusConnection conn = null;
    private DBus.Properties props = null;
    private Handler handler = null;

	public Dbus() {
		this.handler = new Handler();
        try {
            this.conn = DBusConnection.getConnection(DBusConnection.SYSTEM);
            this.props = this.conn.getRemoteObject("org.freedesktop.NetworkManager",
                    "/org/freedesktop/NetworkManager", DBus.Properties.class);
        } catch (DBusException ex) {
            ex.printStackTrace();
        }
//		DBusConnection conn = DBusConnection.getConnection(DBusConnection.SYSTEM);
//		DBus.Properties props = conn.getRemoteObject(
//				"org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager", DBus.Properties.class);
	}
	
	public void Test() {
		System.out.println("dbus network monitor test program");
//		this.Rm5248Test();
		this.ActiveConnections();
//        this.NMJavaDBusTest();
        this.NMproperties();
//        this.conn.disconnect(); //JL why needed?, if program stops automatic; is there a destructor?		
	}

	public void testJava() {
//		Runtime rt = getRuntime();
		System.out.println("Runtime max memory: " + (Runtime.getRuntime().maxMemory() / (1024*1024)) + "MB");
		System.out.println("Runtime total memory: " + (Runtime.getRuntime().totalMemory() / (1024*1024)) + "MB");
		System.out.println("Runtime free memory: " + (Runtime.getRuntime().freeMemory() / (1024*1024)) + "MB");

		Date myDate = new Date();
		System.out.println("The date is " + myDate);
		System.out.println("end <syso Ctrl-space> from http://group/Pages/RD/Java/Java_starter.aspx");
	}
   private void ActiveConnections() {
	   try {
//	       List<Path> activeConns = this.props.Get("org.freedesktop.NetworkManager", "ActiveConnections");
//	       Iterator<Path> i = activeConns.iterator();
//	       Path path = null;
//	       while (i.hasNext()) {
//	           path = i.next();
//	           System.out.println(path);
//	           this.getActiveConnection(path.toString());
//	       }
            List<Object> activeConns = this.props.Get("org.freedesktop.NetworkManager", "ActiveConnections");
            Iterator<Object> i = activeConns.iterator();
            String path = null;
            while (i.hasNext()) {
                path = i.next().toString();
                System.out.println(path);
                this.getActiveConnection(path);
            }
//        } catch (DBusException ex) {
//            ex.printStackTrace();
        } catch (DBusExecutionException ex) {
            ex.printStackTrace();
        }
    }
	private void getActiveConnection(String activeConnPath) {
        try {
            DBus.Properties props = this.conn.getRemoteObject("org.freedesktop.NetworkManager", activeConnPath,
                DBus.Properties.class);
            System.out.println(props.Get("org.freedesktop.NetworkManager.Connection.Active", "Connection"));
//            System.out.println(props.Get("org.freedesktop.NetworkManager.Connection.Active", "ServiceName"));
            System.out.println(props.Get("org.freedesktop.NetworkManager.Connection.Active", "SpecificObject"));
            System.out.println((UInt32) props.Get("org.freedesktop.NetworkManager.Connection.Active", "State"));
        } catch (DBusException ex) {
        	ex.printStackTrace();
        } catch (DBusExecutionException ex) {
            ex.printStackTrace();
        }
    }

	private void NMJavaDBusTest() {
		try {
			Introspectable intro = (Introspectable) this.conn.getRemoteObject("org.freedesktop.NetworkManager", 
										"/org/freedesktop/NetworkManager", Introspectable.class);
			
			String data = intro.Introspect(); 
			//service 'org.freedesktop.nm_dispatcher'
			//conn.callMethodAsync("/org/freedesktop/NetworkManager/Devices/1", "Get", arg1, arg2); 
			
			System.out.println(data);

		} catch (DBusException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 		
		//DBusInterfaceName dbi;
	}

	private void Rm5248Test() {
		try {
			DBusConnection conn = DBusConnection.getConnection(DBusConnection.SESSION);
			ReceiveInterface recvInterface = (ReceiveInterface)conn.getRemoteObject("com.rm5248", "/", ReceiveInterface.class);
			int returnValue = recvInterface.echoMessage("Hello rm5248 from Televic!");
			System.out.println(returnValue);
			returnValue = recvInterface.echoAndAdd("Hello rm5248, do the math for me!", 11, 14);
			System.out.println(returnValue);
		} catch (DBusException e) {
			e.printStackTrace();
		}
	}
		
	public void EnableWireless() {
		this.props.Set("org.freedesktop.NetworkManager", "WirelessEnabled", true);
		System.out.println("wireless enabled");
	}

	private void NMproperties() {
		UInt32 u32_dState = this.props.Get("org.freedesktop.NetworkManager", "State");
		System.out.println("state is " + u32_dState);
		String s_dVersion = this.props.Get("org.freedesktop.NetworkManager", "Version");
		System.out.println("version is " + s_dVersion);
		Boolean b_dWireless = this.props.Get("org.freedesktop.NetworkManager", "WirelessEnabled");
		if (b_dWireless) {
			System.out.println("Wireless is enabled");
		} else {
			System.out.println("Wireless is disabled");
		}
	}
	
	public void NMsignalListen() {
		try {
//			DBusConnection conn = DBusConnection.getConnection(DBusConnection.SYSTEM);
////			conn.requestBusName("dbus");
//			//Anonymous inner class, because I don't feel like making a new class which implements DBusSigHandler
			this.conn.addSigHandler(NetworkManager.DeviceAdded.class, new DBusSigHandler<NetworkManager.DeviceAdded>(){

				@Override
				public void handle(NetworkManager.DeviceAdded sig) {
//					System.out.println(sig.a);
					System.out.println("device added");
				}
				
			});
		} catch (DBusException e) {
			e.printStackTrace();
		}
		
		try {
//			DBusConnection conn = DBusConnection.getConnection(DBusConnection.SYSTEM);
//			//Anonymous inner class, because I don't feel like making a new class which implements DBusSigHandler
			this.conn.addSigHandler(NetworkManager.DeviceRemoved.class, new DBusSigHandler<NetworkManager.DeviceRemoved>() {
				@Override
				public void handle(NetworkManager.DeviceRemoved sig) {
//					System.out.println(sig.a);			
					System.out.println("device removed");
				}
				
			});
		} catch (DBusException e) {
			e.printStackTrace();
		}
		
		try {
			this.conn.addSigHandler(NetworkManager.StateChanged.class, handler);
		} catch (DBusException e) {
			e.printStackTrace();
		}
		System.out.println("Waiting for signals...");
		//JL: doesn't work, so I will try using a path instead of a DBusInterface
		
		//org.freedesktop.DBus.Properties.Get(org.freedesktop.NetworkManager, ActiveConnections);
		//int speed = conn.getRemoteObject(org.freedesktop.NetworkManager.Device.Wired, Speed);
		
		//DBus is from (3) where a new interface "DBus" is defined
//		public class Handler extends DBusSigHandler<DBus.NameAcquired>  
//		{  
//		   public void handle(DBus.NameAcquired sig)  
//		   {  
//		         ...  
//		   }  
//		} 
		
		//NetworkManager nm = (NetworkManager) conn.getRemoteObject("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager/", NetworkManager.class);
				//org.freedesktop.DBus.Introspectable.Introspect
				//org.freedesktop.DBus.Properties.Get
		
		
	}
//public String toString(Dbus db){}
}
Handler.java (text/x-java, 304 B)
package dbus;


import org.freedesktop.dbus.DBusSigHandler;

public class Handler implements DBusSigHandler<NetworkManager.StateChanged>  
{  
   public void handle(NetworkManager.StateChanged sig)  
   {  
		System.out.println("state changed to:");
		System.out.println(sig.a);			
   }  
}
Main.java (text/x-java, 354 B)
package dbus;

public class Main {

	public static void main(String[] args) {
		System.out.println("Number of args: " + args.length);
		
		for (int i = 0; i < args.length; i++) {
			System.out.println(args[i] );
		}
		
		Dbus dbus = new Dbus();
//		dbus.testJava();
		dbus.EnableWireless();
		dbus.Test();
		dbus.NMsignalListen();
	}
}
NetworkManager.java (text/x-java, 2.5 KB)
package dbus;
import java.util.List;
import java.util.Map;

import org.freedesktop.dbus.DBusInterface;
import org.freedesktop.dbus.DBusSignal;
import org.freedesktop.dbus.Path;
import org.freedesktop.dbus.UInt32;
import org.freedesktop.dbus.Variant;
import org.freedesktop.dbus.exceptions.DBusException;

public interface NetworkManager extends DBusInterface
{
   public static class DeviceRemoved extends DBusSignal
   {
//      public final DBusInterface a;
//      public DeviceRemoved(String path, DBusInterface a) throws DBusException
	  public final Path a;
      public DeviceRemoved(String path, Path 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
      public final Path a;
      public DeviceAdded(String path, Path a) throws DBusException
      {
         super(path, a);
         this.a = a;
      }
   }
   public static class PropertiesChanged extends DBusSignal
   {
      public final Map<String,Variant> a;
      public PropertiesChanged(String path, Map<String,Variant> a) throws DBusException
      {
         super(path, a);
         this.a = a;
      }
   }
   public static class StateChanged extends DBusSignal
   {
      public final UInt32 a;
      public StateChanged(String path, UInt32 a) throws DBusException
      {
         super(path, a);
         this.a = a;
      }
   }
   public static class CheckPermissions extends DBusSignal
   {
      public CheckPermissions(String path) throws DBusException
      {
         super(path);
      }
   }

  public UInt32 state();
  public Pair<String, String> GetLogging();
  public void SetLogging(String level, String domains);
  public Map<String,String> GetPermissions();
  public void Enable(boolean enable);
  public void Sleep(boolean sleep);
  public void DeactivateConnection(DBusInterface active_connection);
  public Pair<DBusInterface, DBusInterface> AddAndActivateConnection(Map<String,Map<String,Variant>> connection, DBusInterface device, DBusInterface specific_object);
  public DBusInterface ActivateConnection(DBusInterface connection, DBusInterface device, DBusInterface specific_object);
  //last argument can be of type Path (http://marc.info/?l=freedesktop-dbus&m=136723927504376&w=2)
  public DBusInterface GetDeviceByIpIface(String iface);
  public List<DBusInterface> GetDevices();

}
Pair.java (text/x-java, 332 B)
package dbus;
import org.freedesktop.dbus.Position;
import org.freedesktop.dbus.Tuple;
/** Just a typed container class */
public final class Pair <A,B> extends Tuple
{
   @Position(0)
   public final A a;
   @Position(1)
   public final B b;
   public Pair(A a, B b)
   {
      this.a = a;
      this.b = b;
   }
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.