4 using System.Runtime.InteropServices;
5 using System.Diagnostics;
6 using System.Reflection;
7 using System.Collections;
11 private string[] path = null;
12 private string pathName = null;
13 private Introspector introspector = null;
14 private object handledObject = null;
15 private DBusObjectPathVTable vTable;
16 private Connection connection;
17 private Service service;
18 private DBusHandleMessageFunction filterCalled;
20 internal delegate void DBusObjectPathUnregisterFunction(IntPtr rawConnection,
23 internal delegate int DBusObjectPathMessageFunction(IntPtr rawConnection,
27 internal delegate int DBusHandleMessageFunction(IntPtr rawConnection,
39 [StructLayout (LayoutKind.Sequential)]
40 private struct DBusObjectPathVTable
42 public DBusObjectPathUnregisterFunction unregisterFunction;
43 public DBusObjectPathMessageFunction messageFunction;
44 public IntPtr padding1;
45 public IntPtr padding2;
46 public IntPtr padding3;
47 public IntPtr padding4;
49 public DBusObjectPathVTable(DBusObjectPathUnregisterFunction unregisterFunction,
50 DBusObjectPathMessageFunction messageFunction)
52 this.unregisterFunction = unregisterFunction;
53 this.messageFunction = messageFunction;
54 this.padding1 = IntPtr.Zero;
55 this.padding2 = IntPtr.Zero;
56 this.padding3 = IntPtr.Zero;
57 this.padding4 = IntPtr.Zero;
63 if (Connection != null && Connection.RawConnection != IntPtr.Zero && path != null) {
64 dbus_connection_unregister_object_path(Connection.RawConnection,
69 public Handler(object handledObject,
74 Connection = service.Connection;
75 HandledObject = handledObject;
77 // Strip the leading / off if there is one and get the path as an array
78 pathName = pathName.TrimStart('/');
79 this.path = pathName.Split('/');
80 this.pathName = "/" + pathName;
82 // Create the vTable and register the path
83 vTable = new DBusObjectPathVTable(new DBusObjectPathUnregisterFunction(Unregister_Called),
84 new DBusObjectPathMessageFunction(Message_Called));
86 if (!dbus_connection_register_object_path(Connection.RawConnection,
90 throw new OutOfMemoryException();
92 // Setup the filter function
93 this.filterCalled = new DBusHandleMessageFunction(Filter_Called);
94 if (!dbus_connection_add_filter(Connection.RawConnection,
98 throw new OutOfMemoryException();
101 public object HandledObject
104 return this.handledObject;
108 this.handledObject = value;
112 // Register the methods
113 this.introspector = Introspector.GetIntrospector(value.GetType());
117 public int Filter_Called(IntPtr rawConnection,
121 Message message = Message.Wrap(rawMessage, Service);
123 if (message.Type == Message.MessageType.Signal) {
124 Signal signal = (Signal) message;
125 } else if (message.Type == Message.MessageType.MethodCall) {
126 MethodCall methodCall = (MethodCall) message;
129 return (int) Result.NotYetHandled;
132 public void Unregister_Called(IntPtr rawConnection,
135 System.Console.WriteLine("FIXME: Unregister called.");
138 private int Message_Called(IntPtr rawConnection,
142 Message message = Message.Wrap(rawMessage, Service);
144 switch (message.Type) {
145 case Message.MessageType.Signal:
146 System.Console.WriteLine("FIXME: Signal called.");
148 case Message.MessageType.MethodCall:
149 return (int) HandleMethod((MethodCall) message);
152 return (int) Result.NotYetHandled;
155 private Result HandleMethod(MethodCall methodCall)
157 methodCall.Service = service;
159 InterfaceProxy interfaceProxy = this.introspector.GetInterface(methodCall.InterfaceName);
160 if (interfaceProxy == null || !interfaceProxy.HasMethod(methodCall.Key)) {
161 // No such interface here.
162 return Result.NotYetHandled;
165 MethodInfo method = interfaceProxy.GetMethod(methodCall.Key);
167 // Now call the method. FIXME: Error handling
168 object [] args = methodCall.Arguments.GetParameters(method);
169 object retVal = method.Invoke(this.handledObject, args);
171 // Create the reply and send it
172 MethodReturn methodReturn = new MethodReturn(methodCall);
173 methodReturn.Arguments.AppendResults(method, retVal, args);
176 return Result.Handled;
179 internal string[] Path
187 public string PathName
195 internal Connection Connection
204 this.connection = value;
208 public Service Service
217 this.service = value;
221 [DllImport ("dbus-1")]
222 private extern static bool dbus_connection_register_object_path (IntPtr rawConnection, string[] path, ref DBusObjectPathVTable vTable, IntPtr userData);
224 [DllImport ("dbus-1")]
225 private extern static void dbus_connection_unregister_object_path (IntPtr rawConnection, string[] path);
227 [DllImport ("dbus-1")]
228 private extern static bool dbus_connection_add_filter(IntPtr rawConnection,
229 DBusHandleMessageFunction filter,