From 3d455880a95774aa6140eaf79e589761dabbad82 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Tue, 7 Oct 2008 01:09:30 -0400 Subject: [PATCH] Eliminated vxdbus.cs. The VxDbusInterfaceRouter stuff was much too complicated. Just do a series of string compares against the method name. This could still stand some cleaning up (eg. removing unnecessary 'out' parameters) but I think it's about clean enough for now. --- versaplexd/Makefile | 2 +- versaplexd/versaplexd.cs | 8 ++-- versaplexd/vxapi.cs | 108 +++++++++++++++++++++++++++++------------------ versaplexd/vxdbus.cs | 100 ------------------------------------------- 4 files changed, 72 insertions(+), 146 deletions(-) delete mode 100644 versaplexd/vxdbus.cs diff --git a/versaplexd/Makefile b/versaplexd/Makefile index 3d95cac..c77f213 100644 --- a/versaplexd/Makefile +++ b/versaplexd/Makefile @@ -15,7 +15,7 @@ t/all: all versaplexd.exe: versaplexd.cs \ ischemabackend.cs \ vxapi.cs \ - vxcolumninfo.cs vxdbus.cs vxdbusdatetime.cs vxdbschema.cs \ + vxcolumninfo.cs vxdbusdatetime.cs vxdbschema.cs \ vxexceptions.cs \ vxschema.cs vxschemachecksums.cs vxschemadiff.cs vxschemaerrors.cs \ vxsqlpool.cs wvdbusserver.cs \ diff --git a/versaplexd/versaplexd.cs b/versaplexd/versaplexd.cs index f38914c..7f45fd0 100644 --- a/versaplexd/versaplexd.cs +++ b/versaplexd/versaplexd.cs @@ -10,7 +10,7 @@ using Wv.NDesk.Options; public static class VersaMain { static WvLog log = new WvLog("Versaplex"); - static VxMethodCallRouter msgrouter = new VxMethodCallRouter(); + static VxDbusRouter msgrouter = new VxDbusRouter(); static WvDBusServer dbusserver; static Thread dbusserver_thread = null; static ManualResetEvent thread_ready = new ManualResetEvent(false); @@ -19,7 +19,7 @@ public static class VersaMain public static Connection conn; static Queue action_queue = new Queue(); - private static bool MessageReady(Connection conn, Message msg) + static bool MessageReady(Connection conn, Message msg) { // FIXME: This should really queue things to be run from the thread // pool and then the response would be sent back through the action @@ -33,7 +33,7 @@ public static class VersaMain { action_queue.Enqueue(() => { Message reply; - if (msgrouter.RouteMessage(conn, msg, out reply)) + if (msgrouter.route(conn, msg, out reply)) { if (reply == null) { // FIXME: Do something if this happens, maybe? @@ -113,8 +113,6 @@ public static class VersaMain StartDBusServerThread(listeners.ToArray()); - msgrouter.AddInterface(VxDbInterfaceRouter.Instance); - bool cfgfound = false; if (File.Exists(cfgfile)) diff --git a/versaplexd/vxapi.cs b/versaplexd/vxapi.cs index 6f55089..89f1d69 100644 --- a/versaplexd/vxapi.cs +++ b/versaplexd/vxapi.cs @@ -53,7 +53,7 @@ internal static class VxDb { object[][] data, byte[][] nulls) { MessageWriter writer = - VxDbInterfaceRouter.PrepareRecordsetWriter(colinfo, data, nulls); + VxDbusRouter.PrepareRecordsetWriter(colinfo, data, nulls); writer.Write(call.serial); new Signal(call.sender, call.path, "vx.db", "ChunkRecordsetSig", @@ -66,7 +66,7 @@ internal static class VxDb { internal static void ExecChunkRecordset(Connection conn, Message call, out Message reply) { - string connid = VxDbInterfaceRouter.GetClientId(call); + string connid = VxDbusRouter.GetClientId(call); if (connid == null) { @@ -256,7 +256,7 @@ internal static class VxDb { // Create reply, either with or with no data MessageWriter replywriter = - VxDbInterfaceRouter.PrepareRecordsetWriter(colinfo, + VxDbusRouter.PrepareRecordsetWriter(colinfo, rows.ToArray(), rownulls.ToArray()); reply = call.reply("a(issnny)vaay").write(replywriter); @@ -455,37 +455,65 @@ internal static class VxDb { } } -public class VxDbInterfaceRouter : VxInterfaceRouter +public class VxDbusRouter { - - static WvLog log = new WvLog("VxDbInterfaceRouter"); - static readonly VxDbInterfaceRouter instance; - public static VxDbInterfaceRouter Instance { - get { return instance; } - } - - static VxDbInterfaceRouter() { - instance = new VxDbInterfaceRouter(); + static WvLog log = new WvLog("VxDbusRouter"); + protected delegate + void MethodCallProcessor(Connection conn, Message call, + out Message reply); + + public VxDbusRouter() + { } - - private VxDbInterfaceRouter() : base("vx.db") + + public bool route(Connection conn, Message msg, out Message reply) { - methods.Add("Test", CallTest); - methods.Add("Quit", CallQuit); - methods.Add("ExecScalar", CallExecScalar); - methods.Add("ExecRecordset", CallExecRecordset); - methods.Add("ExecChunkRecordset", CallExecChunkRecordset); - methods.Add("GetSchemaChecksums", CallGetSchemaChecksums); - methods.Add("GetSchema", CallGetSchema); - methods.Add("PutSchema", CallPutSchema); - methods.Add("DropSchema", CallDropSchema); - methods.Add("GetSchemaData", CallGetSchemaData); - methods.Add("PutSchemaData", CallPutSchemaData); + MethodCallProcessor p; + + if (msg.ifc != "vx.db" || msg.path != "/db") + { + reply = null; + return false; + } + + if (msg.method == "Test") + p = CallTest; + else if (msg.method == "Quit") + p = CallQuit; + else if (msg.method == "ExecScalar") + p = CallExecScalar; + else if (msg.method == "ExecRecordset") + p = CallExecRecordset; + else if (msg.method == "ExecChunkRecordset") + p = CallExecChunkRecordset; + else if (msg.method == "GetSchemaChecksums") + p = CallGetSchemaChecksums; + else if (msg.method == "GetSchema") + p = CallGetSchema; + else if (msg.method == "PutSchema") + p = CallPutSchema; + else if (msg.method == "DropSchema") + p = CallDropSchema; + else if (msg.method == "GetSchemaData") + p = CallGetSchemaData; + else if (msg.method == "PutSchemaData") + p = CallPutSchemaData; + else + { + // FIXME: this should be done at a higher level somewhere + reply = msg.err_reply( + "org.freedesktop.DBus.Error.UnknownMethod", + "Method name {0} not found on interface {1}", + msg.method, msg.ifc); + return true; + } + ExecuteCall(p, conn, msg, out reply); + return true; } - protected override void ExecuteCall(MethodCallProcessor processor, - Connection conn, - Message call, out Message reply) + void ExecuteCall(MethodCallProcessor processor, + Connection conn, + Message call, out Message reply) { try { processor(conn, call, out reply); @@ -553,7 +581,7 @@ public class VxDbInterfaceRouter : VxInterfaceRouter return username; } - private static Message CreateUnknownMethodReply(Message call, + static Message CreateUnknownMethodReply(Message call, string methodname) { return call.err_reply("org.freedesktop.DBus.Error.UnknownMethod", @@ -561,7 +589,7 @@ public class VxDbInterfaceRouter : VxInterfaceRouter methodname, call.signature); } - private static void CallTest(Connection conn, + static void CallTest(Connection conn, Message call, out Message reply) { if (call.signature.ne()) { @@ -588,7 +616,7 @@ public class VxDbInterfaceRouter : VxInterfaceRouter reply = call.reply("a(issnny)vaay").write(writer); } - private static void CallQuit(Connection conn, + static void CallQuit(Connection conn, Message call, out Message reply) { // FIXME: Check permissions here @@ -598,7 +626,7 @@ public class VxDbInterfaceRouter : VxInterfaceRouter VersaMain.want_to_die = true; } - private static void CallExecScalar(Connection conn, + static void CallExecScalar(Connection conn, Message call, out Message reply) { if (call.signature != "s") { @@ -687,7 +715,7 @@ public class VxDbInterfaceRouter : VxInterfaceRouter reply = call.reply("a(issnny)vaay").write(writer); } - private static void CallExecChunkRecordset(Connection conn, + static void CallExecChunkRecordset(Connection conn, Message call, out Message reply) { // XXX: Stuff in this comment block shamelessly stolen from @@ -749,7 +777,7 @@ public class VxDbInterfaceRouter : VxInterfaceRouter return sig.ToString(); } - private static void CallGetSchemaChecksums(Connection conn, + static void CallGetSchemaChecksums(Connection conn, Message call, out Message reply) { if (call.signature.ne()) { @@ -778,7 +806,7 @@ public class VxDbInterfaceRouter : VxInterfaceRouter reply = call.reply(VxSchemaChecksums.GetDbusSignature()).write(writer); } - private static void CallGetSchema(Connection conn, + static void CallGetSchema(Connection conn, Message call, out Message reply) { if (call.signature != "as") { @@ -809,7 +837,7 @@ public class VxDbInterfaceRouter : VxInterfaceRouter reply = call.reply(VxSchema.GetDbusSignature()).write(writer); } - private static void CallDropSchema(Connection conn, + static void CallDropSchema(Connection conn, Message call, out Message reply) { if (call.signature != "as") { @@ -846,7 +874,7 @@ public class VxDbInterfaceRouter : VxInterfaceRouter } } - private static void CallPutSchema(Connection conn, + static void CallPutSchema(Connection conn, Message call, out Message reply) { if (call.signature != String.Format("{0}i", @@ -886,7 +914,7 @@ public class VxDbInterfaceRouter : VxInterfaceRouter } } - private static void CallGetSchemaData(Connection conn, + static void CallGetSchemaData(Connection conn, Message call, out Message reply) { if (call.signature != "ss") { @@ -918,7 +946,7 @@ public class VxDbInterfaceRouter : VxInterfaceRouter reply = call.reply("s").write(writer); } - private static void CallPutSchemaData(Connection conn, + static void CallPutSchemaData(Connection conn, Message call, out Message reply) { if (call.signature != "ss") { diff --git a/versaplexd/vxdbus.cs b/versaplexd/vxdbus.cs deleted file mode 100644 index 9a9f64f..0000000 --- a/versaplexd/vxdbus.cs +++ /dev/null @@ -1,100 +0,0 @@ -using System; -using System.Collections.Generic; -using Wv; -using Wv.Extensions; - -public class VxMethodCallRouter { - WvLog log = new WvLog("VxMethodCallRouter", WvLog.L.Debug3); - - private IDictionary interfaces - = new Dictionary(); - - public void AddInterface(VxInterfaceRouter ir) - { - log.print("Adding interface {0}\n", ir.Interface); - interfaces.Add(ir.Interface, ir); - } - - public void RemoveInterface(VxInterfaceRouter ir) - { - RemoveInterface(ir.Interface); - } - - public void RemoveInterface(string iface) - { - interfaces.Remove(iface); - } - - public bool RouteMessage(Connection conn, Message call, out Message reply) - { - if (call.type != MessageType.MethodCall) - throw new ArgumentException("Not a method call message"); - - reply = null; - - // FIXME: Dbus spec says that interface should be optional so it - // should search all of the interfaces for a matching method... - if (call.ifc.e()) - return false; // No interface; ignore it - - log.print("Router interface {0}\n", call.ifc); - - VxInterfaceRouter ir; - if (!interfaces.TryGetValue(call.ifc, out ir)) - return false; // Interface not found - - log.print("Passing to interface router\n"); - - return ir.RouteMessage(conn, call, out reply); - } -} - -public abstract class VxInterfaceRouter { - public readonly string Interface; - - protected VxInterfaceRouter(string iface) - { - Interface = iface; - } - - // Return value is the response - protected delegate - void MethodCallProcessor(Connection conn, Message call, - out Message reply); - - protected IDictionary methods - = new Dictionary(); - - public bool RouteMessage(Connection conn, Message call, out Message reply) - { - if (call.type != MessageType.MethodCall) - throw new ArgumentException("Not a method call message"); - - reply = null; - string method = call.method; - - MethodCallProcessor processor; - if (!methods.TryGetValue(method, out processor)) { - reply = call.err_reply( - "org.freedesktop.DBus.Error.UnknownMethod", - "Method name {0} not found on interface {1}", - method, Interface); - - return true; - } - - ExecuteCall(processor, conn, call, out reply); - - return true; - } - - protected virtual void ExecuteCall(MethodCallProcessor processor, - Connection conn, Message call, out Message reply) - { - try { - processor(conn, call, out reply); - } catch (Exception e) { - reply = call.err_reply("vx.db.exception", e.ToString()); - } - } -} -- 2.11.4.GIT