1 module ddbus.simple;
2 
3 import ddbus.thin;
4 import ddbus.util;
5 import ddbus.c_lib;
6 import ddbus.router;
7 import std.string;
8 import std.traits;
9 
10 class PathIface {
11   this(Connection conn, string dest, string path, string iface) {
12     this.conn = conn;
13     this.dest = dest.toStringz();
14     this.path = path.toStringz();
15     this.iface = iface.toStringz();
16   }
17 
18   Ret call(Ret, Args...)(string meth, Args args) if(allCanDBus!Args && canDBus!Ret) {
19     Message msg = Message(dbus_message_new_method_call(dest,path,iface,meth.toStringz()));
20     msg.build(args);
21     Message ret = conn.sendWithReplyBlocking(msg);
22     return ret.read!Ret();
23   }
24 
25   Message opDispatch(string meth, Args...)(Args args) {
26     Message msg = Message(dbus_message_new_method_call(dest,path,iface,meth.toStringz()));
27     msg.build(args);
28     return conn.sendWithReplyBlocking(msg);
29   }
30 
31   Connection conn;
32   const(char)* dest;
33   const(char)* path;
34   const(char)* iface;
35 }
36 
37 unittest {
38   import dunit.toolkit;
39   Connection conn = connectToBus();
40   PathIface obj = new PathIface(conn, "org.freedesktop.DBus","/org/freedesktop/DBus",
41                                 "org.freedesktop.DBus");
42   auto names = obj.GetNameOwner("org.freedesktop.DBus").to!string();
43   names.assertEqual("org.freedesktop.DBus");
44   obj.call!string("GetNameOwner","org.freedesktop.DBus").assertEqual("org.freedesktop.DBus");
45 }
46 
47 enum SignalMethod;
48 
49 /**
50    Registers all *possible* methods of an object in a router.
51    It will not register methods that use types that ddbus can't handle.
52 
53    The implementation is rather hacky and uses the compiles trait to check for things
54    working so if some methods randomly don't seem to be added, you should probably use
55    setHandler on the router directly. It is also not efficient and creates a closure for every method.
56 
57    TODO: replace this with something that generates a wrapper class who's methods take and return messages
58    and basically do what MessageRouter.setHandler does but avoiding duplication. Then this DBusWrapper!Class
59    could be instantiated with any object efficiently and placed in the router table with minimal duplication.
60  */
61 void registerMethods(T : Object)(MessageRouter router, string path, string iface, T obj) {
62   MessagePattern patt = MessagePattern(path,iface,"",false);
63   foreach(member; __traits(allMembers, T)) {
64     static if (__traits(compiles, __traits(getOverloads, obj, member))
65                && __traits(getOverloads, obj, member).length > 0
66                && __traits(compiles, router.setHandler(patt, &__traits(getOverloads,obj,member)[0]))) {
67       patt.method = member;
68       patt.signal = hasUDA!(__traits(getOverloads,obj,member)[0], SignalMethod);
69       router.setHandler(patt, &__traits(getOverloads,obj,member)[0]);
70     }
71   }
72 }
73 
74 unittest {
75   import dunit.toolkit;
76   class Tester {
77     int lol(int x, string s, string[string] map, Variant!DBusAny any) {return 5;}
78     void wat() {}
79     @SignalMethod
80     void signalRecv() {}
81   }
82   auto o = new Tester;
83   auto router = new MessageRouter;
84   registerMethods(router, "/","ca.thume.test",o);
85   MessagePattern patt = MessagePattern("/","ca.thume.test","wat");
86   router.callTable.assertHasKey(patt);
87   patt.method = "signalRecv";
88   patt.signal = true;
89   router.callTable.assertHasKey(patt);
90   patt.method = "lol";
91   patt.signal = false;
92   router.callTable.assertHasKey(patt);
93   auto res = router.callTable[patt];
94   res.argSig.assertEqual(["i","s","a{ss}","v"]);
95   res.retSig.assertEqual(["i"]);
96 }