Switch to structs for thin wrappers.

This commit is contained in:
Tristan Hume 2015-05-01 16:59:40 -04:00
parent e370a57a42
commit 5905d50e1a
6 changed files with 38 additions and 21 deletions

View file

@ -5,7 +5,7 @@
"copyright": "Copyright © 2015, Tristan Hume",
"license": "MIT",
"authors": ["Tristan Hume"],
"lflags": ["-ldbus-1"],
"lflags": ["-ldbus-1"],
"dependencies": {
"dunit": "~>1.0.10"
}

View file

@ -6,11 +6,11 @@ void main()
{
Connection conn = connectToBus();
for(int i = 0; i < 50; i++) {
Message msg = new Message("ca.thume.transience","/ca/thume/transience/screensurface",
Message msg = Message("ca.thume.transience","/ca/thume/transience/screensurface",
"ca.thume.transience.screensurface","testDot");
conn.sendBlocking(msg);
}
Message msg2 = new Message("ca.thume.transience","/ca/thume/transience/screensurface",
Message msg2 = Message("ca.thume.transience","/ca/thume/transience/screensurface",
"ca.thume.transience.screensurface","testPing");
Message res = conn.sendWithReplyBlocking(msg2,3000);
int result = res.read!int();

View file

@ -76,7 +76,7 @@ void readIterTuple(Tup)(DBusMessageIter *iter, ref Tup tuple) if(isTuple!Tup &&
unittest {
import dunit.toolkit;
import ddbus.thin;
Message msg = new Message("org.example.wow","/wut","org.test.iface","meth");
Message msg = Message("org.example.wow","/wut","org.test.iface","meth");
bool[] emptyB;
auto args = tuple(5,true,"wow",[6,5],tuple(6.2,4,[["lol"]],emptyB));
msg.build(args.expand);

View file

@ -72,8 +72,8 @@ class MessageRouter {
unittest {
import dunit.toolkit;
auto msg = new Message("org.example.test", "/test","org.example.testing","testMethod");
auto patt= new MessagePattern(msg);
auto msg = Message("org.example.test", "/test","org.example.testing","testMethod");
auto patt= MessagePattern(msg);
patt.assertEqual(patt);
patt.sender.assertNull();
patt.path.assertEqual("/test");

View file

@ -14,14 +14,14 @@ class PathIface {
}
Ret call(Ret, Args...)(string meth, Args args) if(allCanDBus!Args && canDBus!Ret) {
Message msg = new Message(dbus_message_new_method_call(dest,path,iface,meth.toStringz()));
Message msg = Message(dbus_message_new_method_call(dest,path,iface,meth.toStringz()));
msg.build(args);
Message ret = conn.sendWithReplyBlocking(msg);
return ret.read!Ret();
}
Message opDispatch(string meth, Args...)(Args args) {
Message msg = new Message(dbus_message_new_method_call(dest,path,iface,meth.toStringz()));
Message msg = Message(dbus_message_new_method_call(dest,path,iface,meth.toStringz()));
msg.build(args);
return conn.sendWithReplyBlocking(msg);
}

View file

@ -1,3 +1,4 @@
/// Thin OO wrapper around DBus types
module ddbus.thin;
import ddbus.c_lib;
@ -30,7 +31,9 @@ enum MessageType {
Call, Return, Error, Signal
}
class Message {
struct Message {
DBusMessage *msg;
this(string dest, string path, string iface, string method) {
msg = dbus_message_new_method_call(dest.toStringz(), path.toStringz(), iface.toStringz(), method.toStringz());
}
@ -39,6 +42,10 @@ class Message {
msg = m;
}
this(this) {
dbus_message_ref(msg);
}
~this() {
dbus_message_unref(msg);
}
@ -71,7 +78,7 @@ class Message {
}
Message createReturn() {
return new Message(dbus_message_new_method_return(msg));
return Message(dbus_message_new_method_return(msg));
}
MessageType type() {
@ -109,22 +116,32 @@ class Message {
assert(cStr != null);
return cStr.fromStringz().assumeUnique();
}
DBusMessage *msg;
}
unittest {
import dunit.toolkit;
auto msg = new Message("org.example.test", "/test","org.example.testing","testMethod");
auto msg = Message("org.example.test", "/test","org.example.testing","testMethod");
msg.path().assertEqual("/test");
}
class Connection {
struct Connection {
DBusConnection *conn;
this(DBusConnection *connection) {
conn = connection;
}
this(this) {
dbus_connection_ref(conn);
}
~this() {
dbus_connection_unref(conn);
}
void close() {
dbus_connection_close(conn);
}
void send(Message msg) {
dbus_connection_send(conn,msg.msg, null);
}
@ -135,20 +152,20 @@ class Connection {
}
Message sendWithReplyBlocking(Message msg, int timeout = 100) {
DBusMessage *dbusMsg = msg.msg;
dbus_message_ref(dbusMsg);
DBusMessage *reply = wrapErrors((err) {
return dbus_connection_send_with_reply_and_block(conn,msg.msg,timeout,err);
auto ret = dbus_connection_send_with_reply_and_block(conn,dbusMsg,timeout,err);
dbus_message_unref(dbusMsg);
return ret;
});
return new Message(reply);
}
~this() {
dbus_connection_unref(conn);
return Message(reply);
}
}
Connection connectToBus(DBusBusType bus = DBusBusType.DBUS_BUS_SESSION) {
DBusConnection *conn = wrapErrors((err) { return dbus_bus_get(bus,err); });
return new Connection(conn);
return Connection(conn);
}
unittest {