2015-04-30 23:22:08 +00:00
|
|
|
module ddbus.router;
|
|
|
|
|
|
|
|
import ddbus.thin;
|
2015-05-02 00:36:06 +00:00
|
|
|
import ddbus.c_lib;
|
2015-05-02 01:38:41 +00:00
|
|
|
import ddbus.util;
|
2015-04-30 23:22:08 +00:00
|
|
|
import std.string;
|
|
|
|
import std.typecons;
|
2015-05-02 00:36:06 +00:00
|
|
|
import core.memory;
|
2015-05-02 01:38:41 +00:00
|
|
|
import std.array;
|
|
|
|
import std.algorithm;
|
2015-05-03 01:36:11 +00:00
|
|
|
import std.format;
|
2015-04-30 23:22:08 +00:00
|
|
|
|
|
|
|
struct MessagePattern {
|
|
|
|
string path;
|
|
|
|
string iface;
|
|
|
|
string method;
|
2015-05-02 00:53:39 +00:00
|
|
|
bool signal;
|
2015-04-30 23:22:08 +00:00
|
|
|
|
|
|
|
this(Message msg) {
|
|
|
|
path = msg.path();
|
|
|
|
iface = msg.iface();
|
|
|
|
method = msg.member();
|
2015-05-02 00:53:39 +00:00
|
|
|
signal = (msg.type() == MessageType.Signal);
|
2015-04-30 23:22:08 +00:00
|
|
|
}
|
|
|
|
|
2015-05-02 00:53:39 +00:00
|
|
|
this(string path, string iface, string method, bool signal = false) {
|
2015-05-02 00:36:06 +00:00
|
|
|
this.path = path;
|
|
|
|
this.iface = iface;
|
|
|
|
this.method = method;
|
2015-05-02 00:53:39 +00:00
|
|
|
this.signal = signal;
|
2015-05-02 00:36:06 +00:00
|
|
|
}
|
|
|
|
|
2015-04-30 23:22:08 +00:00
|
|
|
size_t toHash() const @safe nothrow {
|
|
|
|
size_t hash = 0;
|
|
|
|
auto stringHash = &(typeid(path).getHash);
|
|
|
|
hash += stringHash(&path);
|
|
|
|
hash += stringHash(&iface);
|
|
|
|
hash += stringHash(&method);
|
2015-05-02 00:53:39 +00:00
|
|
|
hash += (signal?1:0);
|
2015-04-30 23:22:08 +00:00
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool opEquals(ref const this s) const @safe pure nothrow {
|
2015-05-02 00:53:39 +00:00
|
|
|
return (path == s.path) && (iface == s.iface) && (method == s.method) && (signal == s.signal);
|
2015-04-30 23:22:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-03 01:36:11 +00:00
|
|
|
unittest {
|
|
|
|
import dunit.toolkit;
|
|
|
|
auto msg = Message("org.example.test", "/test","org.example.testing","testMethod");
|
|
|
|
auto patt= new MessagePattern(msg);
|
|
|
|
patt.assertEqual(patt);
|
|
|
|
patt.signal.assertFalse();
|
|
|
|
patt.path.assertEqual("/test");
|
|
|
|
}
|
|
|
|
|
2015-05-02 01:38:41 +00:00
|
|
|
struct MessageHandler {
|
2015-04-30 23:22:08 +00:00
|
|
|
alias HandlerFunc = void delegate(Message call, Connection conn);
|
2015-05-02 01:38:41 +00:00
|
|
|
HandlerFunc func;
|
|
|
|
string[] argSig;
|
|
|
|
string[] retSig;
|
|
|
|
}
|
|
|
|
|
|
|
|
class MessageRouter {
|
|
|
|
MessageHandler[MessagePattern] callTable;
|
2015-04-30 23:22:08 +00:00
|
|
|
|
2015-05-02 00:36:06 +00:00
|
|
|
bool handle(Message msg, Connection conn) {
|
2015-04-30 23:22:08 +00:00
|
|
|
MessageType type = msg.type();
|
|
|
|
if(type != MessageType.Call && type != MessageType.Signal)
|
|
|
|
return false;
|
|
|
|
auto pattern = MessagePattern(msg);
|
2015-05-02 00:53:39 +00:00
|
|
|
// import std.stdio; debug writeln("Handling ", pattern);
|
2015-05-03 01:36:11 +00:00
|
|
|
|
|
|
|
if(pattern.iface == "org.freedesktop.DBus.Introspectable" &&
|
|
|
|
pattern.method == "Introspect" && !pattern.signal) {
|
2015-05-04 00:52:55 +00:00
|
|
|
handleIntrospect(pattern.path, msg, conn);
|
2015-05-03 01:36:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
2015-12-12 19:17:08 +00:00
|
|
|
|
2015-05-02 01:38:41 +00:00
|
|
|
MessageHandler* handler = (pattern in callTable);
|
2015-04-30 23:22:08 +00:00
|
|
|
if(handler is null) return false;
|
2015-05-02 01:38:41 +00:00
|
|
|
|
|
|
|
// Check for matching argument types
|
|
|
|
version(DDBusNoChecking) {
|
2015-12-12 19:17:08 +00:00
|
|
|
|
2015-05-02 01:38:41 +00:00
|
|
|
} else {
|
|
|
|
if(!equal(join(handler.argSig), msg.signature())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handler.func(msg,conn);
|
2015-04-30 23:22:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-02 00:36:06 +00:00
|
|
|
void setHandler(Ret, Args...)(MessagePattern patt, Ret delegate(Args) handler) {
|
2015-04-30 23:22:08 +00:00
|
|
|
void handlerWrapper(Message call, Connection conn) {
|
|
|
|
Tuple!Args args = call.readTuple!(Tuple!Args)();
|
|
|
|
auto retMsg = call.createReturn();
|
|
|
|
static if(!is(Ret == void)) {
|
2015-05-02 00:53:39 +00:00
|
|
|
Ret ret = handler(args.expand);
|
2017-04-22 19:05:19 +00:00
|
|
|
static if (is(Ret == Tuple!T, T...))
|
|
|
|
retMsg.build!T(ret.expand);
|
|
|
|
else
|
|
|
|
retMsg.build(ret);
|
2015-05-02 00:53:39 +00:00
|
|
|
} else {
|
|
|
|
handler(args.expand);
|
2015-04-30 23:22:08 +00:00
|
|
|
}
|
2015-05-02 00:53:39 +00:00
|
|
|
if(!patt.signal)
|
|
|
|
conn.send(retMsg);
|
2015-04-30 23:22:08 +00:00
|
|
|
}
|
2015-05-02 01:38:41 +00:00
|
|
|
static string[] args = typeSigArr!Args;
|
|
|
|
static if(is(Ret==void)) {
|
|
|
|
static string[] ret = [];
|
|
|
|
} else {
|
2017-04-22 18:40:21 +00:00
|
|
|
static string[] ret = typeSigReturn!Ret;
|
2015-05-02 01:38:41 +00:00
|
|
|
}
|
|
|
|
MessageHandler handleStruct = {func: &handlerWrapper, argSig: args, retSig: ret};
|
|
|
|
callTable[patt] = handleStruct;
|
2015-04-30 23:22:08 +00:00
|
|
|
}
|
2015-05-03 01:36:11 +00:00
|
|
|
|
2015-05-04 00:52:55 +00:00
|
|
|
static string introspectHeader = `<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
|
2015-05-03 01:36:11 +00:00
|
|
|
<node name="%s">`;
|
|
|
|
|
|
|
|
string introspectXML(string path) {
|
|
|
|
auto methods = callTable.byKey().filter!(a => (a.path == path) && !a.signal)().array()
|
|
|
|
// .schwartzSort!((a) => a.iface, "a<b")();
|
2015-12-12 19:17:08 +00:00
|
|
|
.sort!((a,b) => a.iface < b.iface)();
|
|
|
|
auto ifaces = methods.groupBy();
|
2015-05-03 01:36:11 +00:00
|
|
|
auto app = appender!string;
|
|
|
|
formattedWrite(app,introspectHeader,path);
|
|
|
|
foreach(iface; ifaces) {
|
|
|
|
formattedWrite(app,`<interface name="%s">`,iface.front.iface);
|
2015-05-04 02:07:49 +00:00
|
|
|
foreach(methodPatt; iface.array()) {
|
2015-05-03 01:36:11 +00:00
|
|
|
formattedWrite(app,`<method name="%s">`,methodPatt.method);
|
|
|
|
auto handler = callTable[methodPatt];
|
|
|
|
foreach(arg; handler.argSig) {
|
|
|
|
formattedWrite(app,`<arg type="%s" direction="in"/>`,arg);
|
|
|
|
}
|
|
|
|
foreach(arg; handler.retSig) {
|
|
|
|
formattedWrite(app,`<arg type="%s" direction="out"/>`,arg);
|
|
|
|
}
|
|
|
|
app.put("</method>");
|
|
|
|
}
|
|
|
|
app.put("</interface>");
|
|
|
|
}
|
|
|
|
|
2015-05-04 00:52:55 +00:00
|
|
|
string childPath = path;
|
|
|
|
if(!childPath.endsWith("/")) {
|
|
|
|
childPath ~= "/";
|
|
|
|
}
|
2015-05-03 01:36:11 +00:00
|
|
|
auto children = callTable.byKey().filter!(a => (a.path.startsWith(childPath)) && !a.signal)()
|
|
|
|
.map!((s) => s.path.chompPrefix(childPath))
|
2015-05-04 00:52:55 +00:00
|
|
|
.map!((s) => s.splitter('/').front)
|
2015-05-04 02:07:49 +00:00
|
|
|
.array().sort().uniq();
|
2015-05-03 01:36:11 +00:00
|
|
|
foreach(child; children) {
|
|
|
|
formattedWrite(app,`<node name="%s"/>`,child);
|
|
|
|
}
|
2015-12-12 19:17:08 +00:00
|
|
|
|
2015-05-03 01:36:11 +00:00
|
|
|
app.put("</node>");
|
|
|
|
return app.data;
|
|
|
|
}
|
|
|
|
|
2015-05-04 00:52:55 +00:00
|
|
|
void handleIntrospect(string path, Message call, Connection conn) {
|
|
|
|
auto retMsg = call.createReturn();
|
|
|
|
retMsg.build(introspectXML(path));
|
|
|
|
conn.sendBlocking(retMsg);
|
2015-05-03 01:36:11 +00:00
|
|
|
}
|
2015-04-30 23:22:08 +00:00
|
|
|
}
|
|
|
|
|
2015-05-02 00:36:06 +00:00
|
|
|
extern(C) private DBusHandlerResult filterFunc(DBusConnection *dConn, DBusMessage *dMsg, void *routerP) {
|
|
|
|
MessageRouter router = cast(MessageRouter)routerP;
|
|
|
|
dbus_message_ref(dMsg);
|
|
|
|
Message msg = Message(dMsg);
|
|
|
|
dbus_connection_ref(dConn);
|
|
|
|
Connection conn = Connection(dConn);
|
|
|
|
bool handled = router.handle(msg, conn);
|
|
|
|
if(handled) {
|
|
|
|
return DBusHandlerResult.DBUS_HANDLER_RESULT_HANDLED;
|
|
|
|
} else {
|
|
|
|
return DBusHandlerResult.DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern(C) private void unrootUserData(void *userdata) {
|
|
|
|
GC.removeRoot(userdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerRouter(Connection conn, MessageRouter router) {
|
|
|
|
void *routerP = cast(void*)router;
|
|
|
|
GC.addRoot(routerP);
|
|
|
|
dbus_connection_add_filter(conn.conn, &filterFunc, routerP, &unrootUserData);
|
|
|
|
}
|
|
|
|
|
2015-05-03 01:36:11 +00:00
|
|
|
unittest{
|
2015-04-30 23:22:08 +00:00
|
|
|
import dunit.toolkit;
|
2017-08-19 12:50:45 +00:00
|
|
|
|
|
|
|
import std.typecons : BitFlags;
|
|
|
|
import std.variant : Algebraic;
|
|
|
|
|
2015-05-03 01:36:11 +00:00
|
|
|
auto router = new MessageRouter();
|
|
|
|
// set up test messages
|
|
|
|
MessagePattern patt = MessagePattern("/root","ca.thume.test","test");
|
|
|
|
router.setHandler!(int,int)(patt,(int p) {return 6;});
|
|
|
|
patt = MessagePattern("/root","ca.thume.tester","lolwut");
|
|
|
|
router.setHandler!(void,int,string)(patt,(int p, string p2) {});
|
|
|
|
patt = MessagePattern("/root/wat","ca.thume.tester","lolwut");
|
|
|
|
router.setHandler!(int,int)(patt,(int p) {return 6;});
|
2017-04-23 14:12:52 +00:00
|
|
|
patt = MessagePattern("/root/bar","ca.thume.tester","lolwut");
|
|
|
|
router.setHandler!(Variant!DBusAny,int)(patt,(int p) {return variant(DBusAny(p));});
|
2017-04-22 18:40:21 +00:00
|
|
|
patt = MessagePattern("/root/foo","ca.thume.tester","lolwut");
|
2017-04-23 13:19:15 +00:00
|
|
|
router.setHandler!(Tuple!(string,string,int),int,Variant!DBusAny)(patt,(int p, Variant!DBusAny any) {Tuple!(string,string,int) ret; ret[0] = "a"; ret[1] = "b"; ret[2] = p; return ret;});
|
2015-05-04 02:07:49 +00:00
|
|
|
patt = MessagePattern("/troll","ca.thume.tester","wow");
|
|
|
|
router.setHandler!(void)(patt,{return;});
|
2015-05-03 01:36:11 +00:00
|
|
|
|
2017-08-19 12:50:45 +00:00
|
|
|
patt = MessagePattern("/root/fancy","ca.thume.tester","crazyTest");
|
|
|
|
enum F : ushort { a = 1, b = 8, c = 16 }
|
|
|
|
struct S { byte b; ulong ul; F f; }
|
|
|
|
router.setHandler!(int)(patt, (Algebraic!(ushort, BitFlags!F, S) v) {
|
|
|
|
if (v.type is typeid(ushort) || v.type is typeid(BitFlags!F)) {
|
|
|
|
return v.coerce!int;
|
|
|
|
} else if (v.type is typeid(S)) {
|
|
|
|
auto s = v.get!S;
|
|
|
|
final switch (s.f) {
|
|
|
|
case F.a: return s.b;
|
|
|
|
case F.b: return cast(int) s.ul;
|
|
|
|
case F.c: return cast(int) s.ul + s.b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(false);
|
|
|
|
});
|
|
|
|
|
2017-04-23 13:19:15 +00:00
|
|
|
static assert(!__traits(compiles, {
|
|
|
|
patt = MessagePattern("/root/bar","ca.thume.tester","lolwut");
|
|
|
|
router.setHandler!(void, DBusAny)(patt,(DBusAny wrongUsage){return;});
|
|
|
|
}));
|
|
|
|
|
2015-05-04 02:07:49 +00:00
|
|
|
// TODO: these tests rely on nondeterministic hash map ordering
|
2015-05-04 00:52:55 +00:00
|
|
|
static string introspectResult = `<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
|
2017-08-19 12:50:45 +00:00
|
|
|
<node name="/root"><interface name="ca.thume.test"><method name="test"><arg type="i" direction="in"/><arg type="i" direction="out"/></method></interface><interface name="ca.thume.tester"><method name="lolwut"><arg type="i" direction="in"/><arg type="s" direction="in"/></method></interface><node name="bar"/><node name="fancy"/><node name="foo"/><node name="wat"/></node>`;
|
2015-05-04 00:52:55 +00:00
|
|
|
router.introspectXML("/root").assertEqual(introspectResult);
|
2017-04-22 18:40:21 +00:00
|
|
|
static string introspectResult2 = `<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
|
2017-04-23 13:19:15 +00:00
|
|
|
<node name="/root/foo"><interface name="ca.thume.tester"><method name="lolwut"><arg type="i" direction="in"/><arg type="v" direction="in"/><arg type="s" direction="out"/><arg type="s" direction="out"/><arg type="i" direction="out"/></method></interface></node>`;
|
2017-04-22 18:40:21 +00:00
|
|
|
router.introspectXML("/root/foo").assertEqual(introspectResult2);
|
2017-08-19 12:50:45 +00:00
|
|
|
static string introspectResult3 = `<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
|
|
|
|
<node name="/root/fancy"><interface name="ca.thume.tester"><method name="crazyTest"><arg type="v" direction="in"/><arg type="i" direction="out"/></method></interface></node>`;
|
|
|
|
router.introspectXML("/root/fancy").assertEqual(introspectResult3);
|
2015-05-04 02:07:49 +00:00
|
|
|
router.introspectXML("/").assertEndsWith(`<node name="/"><node name="root"/><node name="troll"/></node>`);
|
2015-04-30 23:22:08 +00:00
|
|
|
}
|