Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Major API restructuring, introducing the Bus class
Browse files Browse the repository at this point in the history
  • Loading branch information
Alp Toker committed Sep 29, 2006
1 parent c30fa3c commit 516349d
Show file tree
Hide file tree
Showing 11 changed files with 191 additions and 90 deletions.
2 changes: 1 addition & 1 deletion examples/Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static void Main ()
ObjectPath opath = new ObjectPath ("/org/freedesktop/DBus");
string name = "org.freedesktop.DBus";

Bus bus = conn.GetObject<Bus> (name, opath);
IBus bus = conn.GetObject<IBus> (name, opath);

bus.NameAcquired += delegate (string acquired_name) {
Console.WriteLine ("NameAcquired: " + acquired_name);
Expand Down
27 changes: 6 additions & 21 deletions examples/TestExceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bus> (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<DemoObject> (myNameReq, myOpath);
demo = bus.GetObject<DemoObject> (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);
}
Expand Down
30 changes: 8 additions & 22 deletions examples/TestExport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,25 @@ 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<Bus> (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";

DemoObject demo;

if (bus.NameHasOwner (myNameReq)) {
demo = conn.GetObject<DemoObject> (myNameReq, myOpath);
demo = bus.GetObject<DemoObject> (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

Expand Down Expand Up @@ -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 ();
{
Expand Down
16 changes: 2 additions & 14 deletions examples/TestNotifications.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bus> (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<Notifications> ("org.freedesktop.Notifications", new ObjectPath ("/org/freedesktop/Notifications"));
Notifications nf = bus.GetObject<Notifications> ("org.freedesktop.Notifications", new ObjectPath ("/org/freedesktop/Notifications"));

Console.WriteLine ();
Console.WriteLine ("Capabilities:");
Expand Down
16 changes: 2 additions & 14 deletions examples/TestSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bus> (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<SampleInterface> ("org.designfu.SampleService", new ObjectPath ("/SomeObject"));
SampleInterface sample = bus.GetObject<SampleInterface> ("org.designfu.SampleService", new ObjectPath ("/SomeObject"));

Console.WriteLine ();
string xmlData = sample.Introspect ();
Expand Down
151 changes: 151 additions & 0 deletions src/Bus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// Copyright 2006 Alp Toker <alp@atoker.com>
// 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<string,Bus> buses = new Dictionary<string,Bus> ();

//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<IBus> (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);
*/
}
}
15 changes: 15 additions & 0 deletions src/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public Connection ()
{
}

public Connection (string address)
{
OpenPrivate (address);
Authenticate ();
}

protected bool isConnected = false;
public bool IsConnected
{
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/DBus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 3 additions & 15 deletions src/DProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.Bus> ("org.freedesktop.DBus", new ObjectPath ("/org/freedesktop/DBus"));
bus.AddMatch (matchRule);
conn.Iterate ();
}
conn.AddMatch (matchRule);
}

return (IMethodReturnMessage) newRet;
Expand All @@ -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.Bus> ("org.freedesktop.DBus", new ObjectPath ("/org/freedesktop/DBus"));
bus.RemoveMatch (matchRule);
conn.Iterate ();
}
}
if (conn.Handlers[matchRule] == null)
conn.RemoveMatch (matchRule);

return (IMethodReturnMessage) newRet;
}
Expand Down
Loading

0 comments on commit 516349d

Please sign in to comment.