From 516349d755abcb33ebf09680624d68dc182c4579 Mon Sep 17 00:00:00 2001 From: Alp Toker Date: Fri, 29 Sep 2006 10:38:45 +0100 Subject: [PATCH] Major API restructuring, introducing the Bus class --- examples/Test.cs | 2 +- examples/TestExceptions.cs | 27 ++---- examples/TestExport.cs | 30 ++----- examples/TestNotifications.cs | 16 +--- examples/TestSample.cs | 16 +--- src/Bus.cs | 151 ++++++++++++++++++++++++++++++++++ src/Connection.cs | 15 ++++ src/DBus.cs | 2 +- src/DProxy.cs | 18 +--- src/Makefile | 2 +- tools/Monitor.cs | 2 +- 11 files changed, 191 insertions(+), 90 deletions(-) create mode 100644 src/Bus.cs diff --git a/examples/Test.cs b/examples/Test.cs index 243ec40..41aabb9 100644 --- a/examples/Test.cs +++ b/examples/Test.cs @@ -15,7 +15,7 @@ public static void Main () ObjectPath opath = new ObjectPath ("/org/freedesktop/DBus"); string name = "org.freedesktop.DBus"; - Bus bus = conn.GetObject (name, opath); + IBus bus = conn.GetObject (name, opath); bus.NameAcquired += delegate (string acquired_name) { Console.WriteLine ("NameAcquired: " + acquired_name); diff --git a/examples/TestExceptions.cs b/examples/TestExceptions.cs index a646faf..c48ab8e 100644 --- a/examples/TestExceptions.cs +++ b/examples/TestExceptions.cs @@ -11,46 +11,31 @@ public class ManagedDBusTestExceptions { public static void Main () { - Connection conn = Connection.Open (Address.SessionBus); + Bus bus = Bus.SessionBus; - //begin ugly bits - ObjectPath opath = new ObjectPath ("/org/freedesktop/DBus"); - string name = "org.freedesktop.DBus"; - - Bus bus = conn.GetObject (name, opath); - - bus.NameAcquired += delegate (string acquired_name) { - Console.WriteLine ("NameAcquired: " + acquired_name); - }; - - string myName = bus.Hello (); - Console.WriteLine ("myName: " + myName); - - - ObjectPath myOpath = new ObjectPath ("/org/ndesk/testexceptions"); string myNameReq = "org.ndesk.testexceptions"; + ObjectPath myOpath = new ObjectPath ("/org/ndesk/testexceptions"); DemoObject demo; if (bus.NameHasOwner (myNameReq)) { - demo = conn.GetObject (myNameReq, myOpath); + demo = bus.GetObject (myNameReq, myOpath); } else { NameReply nameReply = bus.RequestName (myNameReq, NameFlag.None); Console.WriteLine ("nameReply: " + nameReply); demo = new DemoObject (); - conn.Register (myNameReq, myOpath, demo); + bus.Register (myNameReq, myOpath, demo); while (true) - conn.Iterate (); + bus.Iterate (); } - //end ugly bits Console.WriteLine (); //org.freedesktop.DBus.Error.InvalidArgs: Requested bus name "" is not valid try { - bus.RequestName ("", NameFlag.None); + bus.RequestName (""); } catch (Exception e) { Console.WriteLine (e); } diff --git a/examples/TestExport.cs b/examples/TestExport.cs index 9a15495..68666f2 100644 --- a/examples/TestExport.cs +++ b/examples/TestExport.cs @@ -11,21 +11,7 @@ public class ManagedDBusTestExport { public static void Main () { - Connection conn = Connection.Open (Address.SessionBus); - - //begin ugly bits - ObjectPath opath = new ObjectPath ("/org/freedesktop/DBus"); - string name = "org.freedesktop.DBus"; - - Bus bus = conn.GetObject (name, opath); - - bus.NameAcquired += delegate (string acquired_name) { - Console.WriteLine ("NameAcquired: " + acquired_name); - }; - - string myName = bus.Hello (); - Console.WriteLine ("myName: " + myName); - + Bus bus = Bus.SessionBus; ObjectPath myOpath = new ObjectPath ("/org/ndesk/test"); string myNameReq = "org.ndesk.test"; @@ -33,17 +19,17 @@ public static void Main () DemoObject demo; if (bus.NameHasOwner (myNameReq)) { - demo = conn.GetObject (myNameReq, myOpath); + demo = bus.GetObject (myNameReq, myOpath); } else { - NameReply nameReply = bus.RequestName (myNameReq, NameFlag.None); + NameReply nameReply = bus.RequestName (myNameReq); Console.WriteLine ("nameReply: " + nameReply); demo = new DemoObject (); - conn.Register (myNameReq, myOpath, demo); + bus.Register (myNameReq, myOpath, demo); while (true) - conn.Iterate (); + bus.Iterate (); } //end ugly bits @@ -100,19 +86,19 @@ public static void Main () demo.SomeEvent += delegate (string arg1, object arg2, double arg3, MyTuple mt) {Console.WriteLine ("SomeEvent handler two: " + arg1 + ", " + arg2 + ", " + arg3 + ", " + mt.A + ", " + mt.B);}; demo.FireOffSomeEvent (); //handle the raised signal - conn.Iterate (); + bus.Iterate (); Console.WriteLine (); demo.SomeEvent += HandleSomeEventA; demo.FireOffSomeEvent (); //handle the raised signal - conn.Iterate (); + bus.Iterate (); Console.WriteLine (); demo.SomeEvent -= HandleSomeEventA; demo.FireOffSomeEvent (); //handle the raised signal - conn.Iterate (); + bus.Iterate (); Console.WriteLine (); { diff --git a/examples/TestNotifications.cs b/examples/TestNotifications.cs index c909942..4c426a4 100644 --- a/examples/TestNotifications.cs +++ b/examples/TestNotifications.cs @@ -15,21 +15,9 @@ public class ManagedDBusTestNotifications { public static void Main () { - Connection conn = Connection.Open (Address.SessionBus); + Bus bus = Bus.SessionBus; - ObjectPath opath = new ObjectPath ("/org/freedesktop/DBus"); - string name = "org.freedesktop.DBus"; - - Bus bus = conn.GetObject (name, opath); - - bus.NameAcquired += delegate (string acquired_name) { - Console.WriteLine ("NameAcquired: " + acquired_name); - }; - - string myName = bus.Hello (); - Console.WriteLine ("myName: " + myName); - - Notifications nf = conn.GetObject ("org.freedesktop.Notifications", new ObjectPath ("/org/freedesktop/Notifications")); + Notifications nf = bus.GetObject ("org.freedesktop.Notifications", new ObjectPath ("/org/freedesktop/Notifications")); Console.WriteLine (); Console.WriteLine ("Capabilities:"); diff --git a/examples/TestSample.cs b/examples/TestSample.cs index a4f5a69..4a822da 100644 --- a/examples/TestSample.cs +++ b/examples/TestSample.cs @@ -11,21 +11,9 @@ public class ManagedDBusTestSample { public static void Main () { - Connection conn = Connection.Open (Address.SessionBus); + Bus bus = Bus.SessionBus; - ObjectPath opath = new ObjectPath ("/org/freedesktop/DBus"); - string name = "org.freedesktop.DBus"; - - Bus bus = conn.GetObject (name, opath); - - bus.NameAcquired += delegate (string acquired_name) { - Console.WriteLine ("NameAcquired: " + acquired_name); - }; - - string myName = bus.Hello (); - Console.WriteLine ("myName: " + myName); - - SampleInterface sample = conn.GetObject ("org.designfu.SampleService", new ObjectPath ("/SomeObject")); + SampleInterface sample = bus.GetObject ("org.designfu.SampleService", new ObjectPath ("/SomeObject")); Console.WriteLine (); string xmlData = sample.Introspect (); diff --git a/src/Bus.cs b/src/Bus.cs new file mode 100644 index 0000000..490aeac --- /dev/null +++ b/src/Bus.cs @@ -0,0 +1,151 @@ +// Copyright 2006 Alp Toker +// This software is made available under the MIT License +// See COPYING for details + +using System; +using System.Collections.Generic; +using org.freedesktop.DBus; + +namespace NDesk.DBus +{ + public class Bus : Connection + { + public static Bus SystemBus + { + get { + return Bus.Open (Address.SystemBus); + } + } + + public static Bus SessionBus + { + get { + return Bus.Open (Address.SessionBus); + } + } + + //TODO: parsing of starter bus type, or maybe do this another way + public static Bus Starter + { + get { + return Bus.Open (Address.Starter); + } + } + + //public static readonly Bus SessionBus = null; + + //TODO: use the guid, not the whole address string + //TODO: consider what happens when a connection has been closed + protected static Dictionary buses = new Dictionary (); + + //public static Connection Open (string address) + public static new Bus Open (string address) + { + if (buses.ContainsKey (address)) + return buses[address]; + + Bus bus = new Bus (address); + buses[address] = bus; + + return bus; + } + + //protected org.freedesktop.DBus.Bus bus_proxy; + protected string unique_name; + protected IBus bus; + + static readonly string DBusName = "org.freedesktop.DBus"; + static readonly ObjectPath DBusPath = new ObjectPath ("/org/freedesktop/DBus"); + + public Bus (string address) : base (address) + { + bus = GetObject (DBusName, DBusPath); + /* + bus.NameAcquired += delegate (string acquired_name) { + Console.WriteLine ("NameAcquired: " + acquired_name); + }; + */ + Register (); + Iterate (); + } + + protected void Register () + { + unique_name = bus.Hello (); + } + + public string UniqueName + { + get { + return unique_name; + } set { + unique_name = value; + } + } + + public ulong GetUnixUser (string name) + { + return bus.GetConnectionUnixUser (name); + } + + public NameReply RequestName (string name) + { + return RequestName (name, NameFlag.None); + } + + public NameReply RequestName (string name, NameFlag flags) + { + return bus.RequestName (name, flags); + } + + public ReleaseNameReply ReleaseName (string name) + { + return bus.ReleaseName (name); + } + + public bool NameHasOwner (string name) + { + return bus.NameHasOwner (name); + } + + public StartReply StartServiceByName (string name) + { + return StartServiceByName (name); + } + + public StartReply StartServiceByName (string name, uint flags) + { + return bus.StartServiceByName (name, flags); + } + + public override void AddMatch (string rule) + { + bus.AddMatch (rule); + Iterate (); + } + + public override void RemoveMatch (string rule) + { + bus.RemoveMatch (rule); + Iterate (); + } + + /* + protected abstract string Hello (); + + protected abstract uint GetConnectionUnixUser (string connection_name); + + public abstract NameReply RequestName (string name, NameFlag flags); + + public abstract ReleaseNameReply ReleaseName (string name); + + public abstract bool NameHasOwner (string name); + + public abstract StartReply StartServiceByName (string name, uint flags); + + public abstract void AddMatch (string rule); + + public abstract void RemoveMatch (string rule); + */ + } +} diff --git a/src/Connection.cs b/src/Connection.cs index c0c3f4f..d8eac3e 100644 --- a/src/Connection.cs +++ b/src/Connection.cs @@ -40,6 +40,12 @@ public Connection () { } + public Connection (string address) + { + OpenPrivate (address); + Authenticate (); + } + protected bool isConnected = false; public bool IsConnected { @@ -779,6 +785,15 @@ public void InvokeSignal (string bus_name, string object_path, MethodInfo mi, st Send (signal.message); } + //these look out of place, but are useful + public virtual void AddMatch (string rule) + { + } + + public virtual void RemoveMatch (string rule) + { + } + static Connection () { if (BitConverter.IsLittleEndian) diff --git a/src/DBus.cs b/src/DBus.cs index 4c1f8d8..816ec24 100644 --- a/src/DBus.cs +++ b/src/DBus.cs @@ -86,7 +86,7 @@ public interface Properties } [Interface ("org.freedesktop.DBus")] - public interface Bus : Introspectable + public interface IBus : Introspectable { NameReply RequestName (string name, NameFlag flags); ReleaseNameReply ReleaseName (string name); diff --git a/src/DProxy.cs b/src/DProxy.cs index b1b7ce6..d9552a8 100644 --- a/src/DProxy.cs +++ b/src/DProxy.cs @@ -49,13 +49,7 @@ public override IMessage Invoke (IMessage msg) conn.Handlers[matchRule] = Delegate.Combine (conn.Handlers[matchRule], dlg); else { conn.Handlers[matchRule] = dlg; - - //inelegant - if (bus_name != "org.freedesktop.DBus" || object_path.Value != "/org/freedesktop/DBus" || ename != "NameAcquired") { - org.freedesktop.DBus.Bus bus = conn.GetObject ("org.freedesktop.DBus", new ObjectPath ("/org/freedesktop/DBus")); - bus.AddMatch (matchRule); - conn.Iterate (); - } + conn.AddMatch (matchRule); } return (IMethodReturnMessage) newRet; @@ -71,14 +65,8 @@ public override IMessage Invoke (IMessage msg) conn.Handlers[matchRule] = Delegate.Remove (conn.Handlers[matchRule], dlg); - if (conn.Handlers[matchRule] == null) { - //inelegant - if (bus_name != "org.freedesktop.DBus" || object_path.Value != "/org/freedesktop/DBus" || ename != "NameAcquired") { - org.freedesktop.DBus.Bus bus = conn.GetObject ("org.freedesktop.DBus", new ObjectPath ("/org/freedesktop/DBus")); - bus.RemoveMatch (matchRule); - conn.Iterate (); - } - } + if (conn.Handlers[matchRule] == null) + conn.RemoveMatch (matchRule); return (IMethodReturnMessage) newRet; } diff --git a/src/Makefile b/src/Makefile index 1b9a546..efc0ef4 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,6 +1,6 @@ all: NDesk.DBus.dll -BUS_SOURCES=Address.cs AssemblyInfo.cs Connection.cs Authentication.cs Protocol.cs Message.cs MessageFilter.cs MessageReader.cs MessageWriter.cs Server.cs Transport.cs Wrapper.cs +BUS_SOURCES=Address.cs AssemblyInfo.cs Bus.cs Connection.cs Authentication.cs Protocol.cs Message.cs MessageFilter.cs MessageReader.cs MessageWriter.cs Server.cs Transport.cs Wrapper.cs #UNIX_SOURCES=UnixMonoTransport.cs UNIX_SOURCES=UnixNativeTransport.cs CLR_SOURCES=DBus.cs Introspection.cs DProxy.cs Signature.cs diff --git a/tools/Monitor.cs b/tools/Monitor.cs index 9037d64..161b6ba 100644 --- a/tools/Monitor.cs +++ b/tools/Monitor.cs @@ -36,7 +36,7 @@ public static void Main (string[] args) ObjectPath opath = new ObjectPath ("/org/freedesktop/DBus"); string name = "org.freedesktop.DBus"; - Bus bus = conn.GetObject (name, opath); + IBus bus = conn.GetObject (name, opath); bus.NameAcquired += delegate (string acquired_name) { Console.WriteLine ("NameAcquired: " + acquired_name);