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