Type checking and groundwork for introspection.

This commit is contained in:
Tristan Hume 2015-05-01 21:38:41 -04:00
parent 602b3958fd
commit 2af2db0020
3 changed files with 43 additions and 5 deletions

View file

@ -2,6 +2,10 @@ task :testCall do
sh "dbus-send --type=method_call --print-reply --dest=ca.thume.ddbus.test /root ca.thume.test.test int32:5" sh "dbus-send --type=method_call --print-reply --dest=ca.thume.ddbus.test /root ca.thume.test.test int32:5"
end end
task :badCall do
sh "dbus-send --type=method_call --print-reply --dest=ca.thume.ddbus.test /root ca.thume.test.test double:5.5"
end
task :testSignal do task :testSignal do
sh "dbus-send --dest=ca.thume.ddbus.test /signaler ca.thume.test.signal int32:9" sh "dbus-send --dest=ca.thume.ddbus.test /signaler ca.thume.test.signal int32:9"
end end

View file

@ -2,9 +2,12 @@ module ddbus.router;
import ddbus.thin; import ddbus.thin;
import ddbus.c_lib; import ddbus.c_lib;
import ddbus.util;
import std.string; import std.string;
import std.typecons; import std.typecons;
import core.memory; import core.memory;
import std.array;
import std.algorithm;
struct MessagePattern { struct MessagePattern {
string path; string path;
@ -41,9 +44,15 @@ struct MessagePattern {
} }
} }
class MessageRouter { struct MessageHandler {
alias HandlerFunc = void delegate(Message call, Connection conn); alias HandlerFunc = void delegate(Message call, Connection conn);
HandlerFunc[MessagePattern] callTable; HandlerFunc func;
string[] argSig;
string[] retSig;
}
class MessageRouter {
MessageHandler[MessagePattern] callTable;
bool handle(Message msg, Connection conn) { bool handle(Message msg, Connection conn) {
MessageType type = msg.type(); MessageType type = msg.type();
@ -51,9 +60,19 @@ class MessageRouter {
return false; return false;
auto pattern = MessagePattern(msg); auto pattern = MessagePattern(msg);
// import std.stdio; debug writeln("Handling ", pattern); // import std.stdio; debug writeln("Handling ", pattern);
HandlerFunc* handler = (pattern in callTable); MessageHandler* handler = (pattern in callTable);
if(handler is null) return false; if(handler is null) return false;
(*handler)(msg,conn);
// Check for matching argument types
version(DDBusNoChecking) {
} else {
if(!equal(join(handler.argSig), msg.signature())) {
return false;
}
}
handler.func(msg,conn);
return true; return true;
} }
@ -70,7 +89,14 @@ class MessageRouter {
if(!patt.signal) if(!patt.signal)
conn.send(retMsg); conn.send(retMsg);
} }
callTable[patt] = &handlerWrapper; static string[] args = typeSigArr!Args;
static if(is(Ret==void)) {
static string[] ret = [];
} else {
static string[] ret = [typeSig!Ret];
}
MessageHandler handleStruct = {func: &handlerWrapper, argSig: args, retSig: ret};
callTable[patt] = handleStruct;
} }
} }

View file

@ -86,6 +86,14 @@ string typeSigAll(TS...)() if(allCanDBus!TS) {
return sig; return sig;
} }
string[] typeSigArr(TS...)() if(allCanDBus!TS) {
string[] sig = [];
foreach(i,T; TS) {
sig ~= typeSig!T();
}
return sig;
}
int typeCode(T)() if(canDBus!T) { int typeCode(T)() if(canDBus!T) {
string sig = typeSig!T(); string sig = typeSig!T();
return sig[0]; return sig[0];