| 1 |
// Copyright 2006 Alp Toker <[email protected]> |
| 2 |
// This software is made available under the MIT License |
| 3 |
// See COPYING for details |
| 4 |
|
| 5 |
using System; |
| 6 |
using System.Reflection; |
| 7 |
using System.Reflection.Emit; |
| 8 |
|
| 9 |
using org.freedesktop.DBus; |
| 10 |
|
| 11 |
namespace NDesk.DBus |
| 12 |
{ |
| 13 |
//TODO: perhaps ExportObject should not derive from BusObject |
| 14 |
internal class ExportObject : BusObject //, Peer |
| 15 |
{ |
| 16 |
public readonly object obj; |
| 17 |
|
| 18 |
public ExportObject (Connection conn, ObjectPath object_path, object obj) : base (conn, null, object_path) |
| 19 |
{ |
| 20 |
this.obj = obj; |
| 21 |
} |
| 22 |
|
| 23 |
//maybe add checks to make sure this is not called more than once |
| 24 |
//it's a bit silly as a property |
| 25 |
public bool Registered |
| 26 |
{ |
| 27 |
set { |
| 28 |
Type type = obj.GetType (); |
| 29 |
|
| 30 |
foreach (MemberInfo mi in Mapper.GetPublicMembers (type)) { |
| 31 |
EventInfo ei = mi as EventInfo; |
| 32 |
|
| 33 |
if (ei == null) |
| 34 |
continue; |
| 35 |
|
| 36 |
Delegate dlg = GetHookupDelegate (ei); |
| 37 |
|
| 38 |
if (value) |
| 39 |
ei.AddEventHandler (obj, dlg); |
| 40 |
else |
| 41 |
ei.RemoveEventHandler (obj, dlg); |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
public void HandleMethodCall (MethodCall method_call) |
| 47 |
{ |
| 48 |
Type type = obj.GetType (); |
| 49 |
//object retObj = type.InvokeMember (msg.Member, BindingFlags.InvokeMethod, null, obj, MessageHelper.GetDynamicValues (msg)); |
| 50 |
|
| 51 |
//TODO: there is no member name mapping for properties etc. yet |
| 52 |
MethodInfo mi = Mapper.GetMethod (type, method_call); |
| 53 |
|
| 54 |
if (mi == null) { |
| 55 |
conn.MaybeSendUnknownMethodError (method_call); |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
object retObj = null; |
| 60 |
object[] parmValues = MessageHelper.GetDynamicValues (method_call.message, mi.GetParameters ()); |
| 61 |
|
| 62 |
try { |
| 63 |
retObj = mi.Invoke (obj, parmValues); |
| 64 |
} catch (TargetInvocationException e) { |
| 65 |
if (!method_call.message.ReplyExpected) |
| 66 |
return; |
| 67 |
|
| 68 |
Exception ie = e.InnerException; |
| 69 |
//TODO: complete exception sending support |
| 70 |
|
| 71 |
Error error = new Error (Mapper.GetInterfaceName (ie.GetType ()), method_call.message.Header.Serial); |
| 72 |
error.message.Signature = new Signature (DType.String); |
| 73 |
|
| 74 |
MessageWriter writer = new MessageWriter (Connection.NativeEndianness); |
| 75 |
writer.connection = conn; |
| 76 |
writer.Write (ie.Message); |
| 77 |
error.message.Body = writer.ToArray (); |
| 78 |
|
| 79 |
//TODO: we should be more strict here, but this fallback was added as a quick fix for p2p |
| 80 |
if (method_call.Sender != null) |
| 81 |
error.message.Header.Fields[FieldCode.Destination] = method_call.Sender; |
| 82 |
|
| 83 |
conn.Send (error.message); |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
if (method_call.message.ReplyExpected) { |
| 88 |
Message reply = MessageHelper.ConstructDynamicReply (method_call, mi, retObj, parmValues); |
| 89 |
conn.Send (reply); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/* |
| 94 |
public void Ping () |
| 95 |
{ |
| 96 |
} |
| 97 |
|
| 98 |
public string GetMachineId () |
| 99 |
{ |
| 100 |
//TODO: implement this |
| 101 |
return String.Empty; |
| 102 |
} |
| 103 |
*/ |
| 104 |
} |
| 105 |
} |