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 /** 48 Registers all *possible* methods of an object in a router. 49 It will not register methods that use types that ddbus can't handle. 50 51 The implementation is rather hacky and uses the compiles trait to check for things 52 working so if some methods randomly don't seem to be added, you should probably use 53 setHandler on the router directly. It is also not efficient and creates a closure for every method. 54 55 TODO: replace this with something that generates a wrapper class who's methods take and return messages 56 and basically do what MessageRouter.setHandler does but avoiding duplication. Then this DBusWrapper!Class 57 could be instantiated with any object efficiently and placed in the router table with minimal duplication. 58 */ 59 void registerMethods(T : Object)(MessageRouter router, string path, string iface, T obj) { 60 MessagePattern patt = MessagePattern(path,iface,"",false); 61 foreach(member; __traits(allMembers, T)) { 62 static if (__traits(compiles, __traits(getOverloads, obj, member)) 63 && __traits(getOverloads, obj, member).length > 0 64 && __traits(compiles, router.setHandler(patt, &__traits(getOverloads,obj,member)[0]))) { 65 patt.method = member; 66 router.setHandler(patt, &__traits(getOverloads,obj,member)[0]); 67 } 68 } 69 } 70 71 unittest { 72 import dunit.toolkit; 73 class Tester { 74 int lol(int x, string s) {return 5;} 75 void wat() {} 76 } 77 auto o = new Tester; 78 auto router = new MessageRouter; 79 registerMethods(router, "/","ca.thume.test",o); 80 MessagePattern patt = MessagePattern("/","ca.thume.test","wat"); 81 router.callTable.assertHasKey(patt); 82 patt.method = "lol"; 83 router.callTable.assertHasKey(patt); 84 auto res = router.callTable[patt]; 85 res.argSig.assertEqual(["i","s"]); 86 res.retSig.assertEqual(["i"]); 87 }