00001 namespace DBus
00002 {
00003 using System;
00004 using System.Runtime.InteropServices;
00005 using System.Diagnostics;
00006 using System.Collections;
00007 using System.Threading;
00008 using System.Reflection;
00009 using System.Reflection.Emit;
00010
00011 public class Service
00012 {
00013 private Connection connection;
00014 private string name;
00015 private bool local = false;
00016 private Hashtable registeredHandlers = new Hashtable();
00017 private DBusHandleMessageFunction filterCalled;
00018 public delegate void SignalCalledHandler(Signal signal);
00019 public event SignalCalledHandler SignalCalled;
00020 private static AssemblyBuilder proxyAssembly;
00021 private ModuleBuilder module = null;
00022
00023 internal Service(string name, Connection connection)
00024 {
00025 this.name = name;
00026 this.connection = connection;
00027 AddFilter();
00028 }
00029
00030 public Service(Connection connection, string name)
00031 {
00032 Error error = new Error();
00033 error.Init();
00034
00035
00036 uint flags = 0;
00037
00038 if (dbus_bus_acquire_service(connection.RawConnection, name, flags, ref error) == -1) {
00039 throw new DBusException(error);
00040 }
00041
00042 this.connection = connection;
00043 this.name = name;
00044 this.local = true;
00045 }
00046
00047 public static bool Exists(Connection connection, string name)
00048 {
00049 Error error = new Error();
00050 error.Init();
00051
00052 if (dbus_bus_service_exists(connection.RawConnection,
00053 name,
00054 ref error)) {
00055 return true;
00056 } else {
00057 if (error.IsSet) {
00058 throw new DBusException(error);
00059 }
00060 return false;
00061 }
00062 }
00063
00064 public static Service Get(Connection connection, string name)
00065 {
00066 if (Exists(connection, name)) {
00067 return new Service(name, connection);
00068 } else {
00069 throw new ApplicationException("Service '" + name + "' does not exist.");
00070 }
00071 }
00072
00073 public void UnregisterObject(object handledObject)
00074 {
00075 registeredHandlers.Remove(handledObject);
00076 }
00077
00078 public void RegisterObject(object handledObject,
00079 string pathName)
00080 {
00081 Handler handler = new Handler(handledObject, pathName, this);
00082 registeredHandlers.Add(handledObject, handler);
00083 }
00084
00085 internal Handler GetHandler(object handledObject)
00086 {
00087 if (!registeredHandlers.Contains(handledObject)) {
00088 throw new ArgumentException("No handler registered for object: " + handledObject);
00089 }
00090
00091 return (Handler) registeredHandlers[handledObject];
00092 }
00093
00094 public object GetObject(Type type, string pathName)
00095 {
00096 ProxyBuilder builder = new ProxyBuilder(this, type, pathName);
00097 object proxy = builder.GetProxy();
00098 return proxy;
00099 }
00100
00101 private void AddFilter()
00102 {
00103
00104 this.filterCalled = new DBusHandleMessageFunction(Service_FilterCalled);
00105 Connection.AddFilter (this.filterCalled);
00106
00107 Connection.AddMatch ("type='signal'");
00108 }
00109
00110 private int Service_FilterCalled(IntPtr rawConnection,
00111 IntPtr rawMessage,
00112 IntPtr userData)
00113 {
00114 Message message = Message.Wrap(rawMessage, this);
00115
00116 if (message.Type == Message.MessageType.Signal) {
00117
00118 Signal signal = (Signal) message;
00119 if (SignalCalled != null) {
00120 Message.Push (message);
00121 SignalCalled(signal);
00122 Message.Pop ();
00123 }
00124 }
00125
00126 message.Dispose ();
00127
00128 return (int) Result.NotYetHandled;
00129 }
00130
00131 public string Name
00132 {
00133 get
00134 {
00135 return this.name;
00136 }
00137 }
00138
00139 public Connection Connection
00140 {
00141 get
00142 {
00143 return connection;
00144 }
00145
00146 set
00147 {
00148 this.connection = value;
00149 }
00150 }
00151
00152 internal AssemblyBuilder ProxyAssembly
00153 {
00154 get {
00155 if (proxyAssembly == null){
00156 AssemblyName assemblyName = new AssemblyName();
00157 assemblyName.Name = "DBusProxy";
00158 proxyAssembly = Thread.GetDomain().DefineDynamicAssembly(assemblyName,
00159 AssemblyBuilderAccess.RunAndSave);
00160 }
00161
00162 return proxyAssembly;
00163 }
00164 }
00165
00166 internal ModuleBuilder Module
00167 {
00168 get {
00169 if (this.module == null) {
00170 this.module = ProxyAssembly.DefineDynamicModule (Name, Name + ".proxy.dll", true);
00171 }
00172
00173 return this.module;
00174 }
00175 }
00176
00177 [DllImport("dbus-1")]
00178 private extern static int dbus_bus_acquire_service(IntPtr rawConnection,
00179 string serviceName,
00180 uint flags, ref Error error);
00181
00182 [DllImport("dbus-1")]
00183 private extern static bool dbus_bus_service_exists(IntPtr rawConnection,
00184 string serviceName,
00185 ref Error error);
00186
00187
00188 }
00189 }