This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathBus.cs
165 lines (135 loc) · 3.41 KB
/
Bus.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// 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 DBus
{
public sealed class Bus : Connection
{
static readonly string DBusName = "org.freedesktop.DBus";
static readonly ObjectPath DBusPath = new ObjectPath ("/org/freedesktop/DBus");
static Dictionary<string,Bus> buses = new Dictionary<string,Bus> ();
static Bus starterBus = null;
static Bus systemBus = Address.StarterBusType=="system" ? Starter : (Address.System !=null ? Bus.Open (Address.System ) : null);
static Bus sessionBus = Address.StarterBusType=="session" ? Starter : (Address.Session != null ? Bus.Open (Address.Session) : null);
IBus bus;
string address;
string uniqueName;
public static Bus System
{
get {
return systemBus;
}
}
public static Bus Session
{
get {
return sessionBus;
}
}
public static Bus Starter
{
get {
if (starterBus == null) {
try {
starterBus = Bus.Open (Address.Starter);
} catch (Exception e) {
throw new Exception ("Unable to open the starter message bus.", e);
}
}
return starterBus;
}
}
public static new Bus Open (string address)
{
if (address == null)
throw new ArgumentNullException ("address");
Bus bus;
if (buses.TryGetValue (address, out bus))
return bus;
bus = new Bus (address);
buses[address] = bus;
return bus;
}
public Bus (string address) : base (address)
{
this.bus = GetObject<IBus> (DBusName, DBusPath);
this.address = address;
Register ();
}
//should this be public?
//as long as Bus subclasses Connection, having a Register with a completely different meaning is bad
void Register ()
{
if (uniqueName != null)
throw new Exception ("Bus already has a unique name");
uniqueName = bus.Hello ();
}
protected override void CloseInternal ()
{
/* In case the bus was opened with static method
* Open, clear it from buses dictionary
*/
if (buses.ContainsKey (address))
buses.Remove (address);
}
protected override bool CheckBusNameExists (string busName)
{
if (busName == DBusName)
return true;
return NameHasOwner (busName);
}
public ulong GetUnixUser (string name)
{
return bus.GetConnectionUnixUser (name);
}
public RequestNameReply RequestName (string name)
{
return RequestName (name, NameFlag.None);
}
public RequestNameReply 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, 0);
}
public StartReply StartServiceByName (string name, uint flags)
{
return bus.StartServiceByName (name, flags);
}
internal protected override void AddMatch (string rule)
{
bus.AddMatch (rule);
}
internal protected override void RemoveMatch (string rule)
{
bus.RemoveMatch (rule);
}
public string GetId ()
{
return bus.GetId ();
}
public string UniqueName
{
get {
return uniqueName;
} set {
if (uniqueName != null)
throw new Exception ("Unique name can only be set once");
uniqueName = value;
}
}
}
}