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", "copyright": "Copyright © 2015, Tristan Hume",
"license": "MIT", "license": "MIT",
"authors": ["Tristan Hume"], "authors": ["Tristan Hume"],
"lflags": ["-ldbus-1"], "lflags": ["-ldbus-1"],
"dependencies": { "dependencies": {
"dunit": "~>1.0.10" "dunit": "~>1.0.10"
} }

View file

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

View file

@ -76,7 +76,7 @@ void readIterTuple(Tup)(DBusMessageIter *iter, ref Tup tuple) if(isTuple!Tup &&
unittest { unittest {
import dunit.toolkit; import dunit.toolkit;
import ddbus.thin; 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; bool[] emptyB;
auto args = tuple(5,true,"wow",[6,5],tuple(6.2,4,[["lol"]],emptyB)); auto args = tuple(5,true,"wow",[6,5],tuple(6.2,4,[["lol"]],emptyB));
msg.build(args.expand); msg.build(args.expand);

View file

@ -72,8 +72,8 @@ class MessageRouter {
unittest { unittest {
import dunit.toolkit; 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");
auto patt= new MessagePattern(msg); auto patt= MessagePattern(msg);
patt.assertEqual(patt); patt.assertEqual(patt);
patt.sender.assertNull(); patt.sender.assertNull();
patt.path.assertEqual("/test"); 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) { 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); msg.build(args);
Message ret = conn.sendWithReplyBlocking(msg); Message ret = conn.sendWithReplyBlocking(msg);
return ret.read!Ret(); return ret.read!Ret();
} }
Message opDispatch(string meth, Args...)(Args args) { 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); msg.build(args);
return conn.sendWithReplyBlocking(msg); return conn.sendWithReplyBlocking(msg);
} }

View file

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