a3c2a31f833a73c3aebb53885aa7e34a5bab8137
[platform/upstream/dbus.git] / mono / Service.cs
1 namespace DBus
2 {
3   using System;
4   using System.Runtime.InteropServices;
5   using System.Diagnostics;
6   using System.Collections;
7   using System.Reflection;
8   using System.Reflection.Emit;
9   
10   public class Service
11   {
12     private Connection connection;
13     private string name;
14     private bool local = false;
15     private Hashtable registeredHandlers = new Hashtable();
16     internal ModuleBuilder module = null;
17
18     internal Service(string name, Connection connection)
19     {
20       this.name = name;
21       this.connection = connection;
22     }
23
24     public Service(Connection connection, string name)
25     {
26       Error error = new Error();
27       error.Init();
28       
29       // This isn't used for now
30       uint flags = 0;
31
32       if (dbus_bus_acquire_service(connection.RawConnection, name, flags, ref error) == -1) {
33         throw new DBusException(error);
34       }
35
36       this.connection = connection;
37       this.name = name;
38       this.local = true;
39     }
40
41     public static bool Exists(Connection connection, string name)
42     {
43       Error error = new Error();
44       error.Init();
45       
46       if (dbus_bus_service_exists(connection.RawConnection, 
47                                   name, 
48                                   ref error)) {
49         return true;
50       } else {
51         if (error.IsSet) {
52           throw new DBusException(error);
53         }
54         return false;
55       }
56     }
57
58     public static Service Get(Connection connection, string name)
59     {
60       if (Exists(connection, name)) {
61         return new Service(name, connection);
62       } else {
63         throw new ApplicationException("Service '" + name + "' does not exist.");
64       }
65     }
66
67     public void RegisterObject(object handledObject, 
68                                string pathName) 
69     {
70       Handler handler = new Handler(handledObject, 
71                                             pathName, 
72                                             this);
73       registeredHandlers.Add(handledObject, handler);
74     }
75
76     internal Handler GetHandler(object handledObject) 
77     {
78       return (Handler) registeredHandlers[handledObject];
79     }
80
81     public object GetObject(Type type, string pathName)
82     {
83       ProxyBuilder builder = new ProxyBuilder(this, type, pathName);
84       object proxy = builder.GetProxy();
85       return proxy;
86     }
87
88     public string Name
89     {
90       get
91         {
92           return this.name;
93         }
94     }
95
96     public Connection Connection 
97     {
98       get
99         {
100           return connection;
101         }
102       
103       set 
104         {
105           this.connection = value;
106         }
107     }
108
109     [DllImport ("dbus-1")]
110     private extern static int dbus_bus_acquire_service (IntPtr rawConnection, string serviceName, uint flags, ref Error error);
111
112     [DllImport ("dbus-1")]
113     private extern static bool dbus_bus_service_exists (IntPtr rawConnection, string serviceName, ref Error error);    
114   }
115 }