D-BUS分析—4 实例分析

2009-01-05 at 11:39 am 张世长

代码都编译出来了,自然首先该做的是试着用用罗。下面就来看看编译的结果(DEBUG版):

dbus-1d.dll,test-thread-init.exe, test-spawn.exe, test-sleep-forever.exe,test-shell-service,exe, test-shell.exe,test-service.exe, test-segfault.exe, test-pending-call-dispatch.exe, test-names.exe, test-names1.exe, test-exit.exe, dbus-test,exe, dbus-send.exe, dbus-lauch.exe, dbus-daemon.exe, bus-test.exe.我们来分析一下其中一些重要的例子。

  1.  
    1. dbus-daemon     其实,dbus-daemon不是例子,他是dbus的一部分,属于构架中的第二层–守护进程。当应用程序连接dbus时,dbus会检测dbus-daemon进程是否存在,否则将自动启动进程。dbus-daemon就像一个消息的路由器,他的主要职责是对消息进行准确的派发,同时实现了应用程序一对多或多对一的发送。dbus的基础协议是一对一的。

           当然,dbus-daemon也可以事先通过手工启动,–session参数表示是一个会话层的通讯,会话总线,–system表示系统总线。其中很有用的参数是—introspect:通过这个参数可以查看总线对象支持的接口。消息总线除了根本的消息派发功能,本身也是一个应用,因此消息总线也提供了自己的接口,通过–introspect参数查看结果如下:

      <!DOCTYPE node PUBLIC “-//freedesktop//DTD D-BUS Object Introspection 1.0//EN”

      http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd”>

      <node>

        <interface name=”org.freedesktop.DBus.Introspectable”>

          <method name=”Introspect”>

            <arg name=”data” direction=”out” type=”s”/>

          </method>

        </interface>

        <interface name=”org.freedesktop.DBus”>

          <method name=”Hello”>

            <arg direction=”out” type=”s”/>

          </method>

          <method name=”RequestName”>

            <arg direction=”in” type=”s”/>

            <arg direction=”in” type=”u”/>

            <arg direction=”out” type=”u”/>

          </method>

          <method name=”ReleaseName”>

            <arg direction=”in” type=”s”/>

            <arg direction=”out” type=”u”/>

          </method>

          <method name=”StartServiceByName”>

            <arg direction=”in” type=”s”/>

            <arg direction=”in” type=”u”/>

            <arg direction=”out” type=”u”/>

          </method>

          <method name=”UpdateActivationEnvironment”>

            <arg direction=”in” type=”a{ss}”/>

          </method>

          <method name=”NameHasOwner”>

            <arg direction=”in” type=”s”/>

            <arg direction=”out” type=”b”/>

          </method>

          <method name=”ListNames”>

            <arg direction=”out” type=”as”/>

          </method>

          <method name=”ListActivatableNames”>

            <arg direction=”out” type=”as”/>

          </method>

          <method name=”AddMatch”>

            <arg direction=”in” type=”s”/>

          </method>

          <method name=”RemoveMatch”>

            <arg direction=”in” type=”s”/>

          </method>

          <method name=”GetNameOwner”>

            <arg direction=”in” type=”s”/>

            <arg direction=”out” type=”s”/>

          </method>

          <method name=”ListQueuedOwners”>

            <arg direction=”in” type=”s”/>

            <arg direction=”out” type=”as”/>

          </method>

          <method name=”GetConnectionUnixUser”>

            <arg direction=”in” type=”s”/>

            <arg direction=”out” type=”u”/>

          </method>

          <method name=”GetConnectionUnixProcessID”>

            <arg direction=”in” type=”s”/>

            <arg direction=”out” type=”u”/>

          </method>

          <method name=”GetAdtAuditSessionData”>

            <arg direction=”in” type=”s”/>

            <arg direction=”out” type=”ay”/>

          </method>

          <method name=”GetConnectionSELinuxSecurityContext”>

            <arg direction=”in” type=”s”/>

            <arg direction=”out” type=”ay”/>

          </method>

          <method name=”ReloadConfig”>

          </method>

          <method name=”GetId”>

            <arg direction=”out” type=”s”/>

          </method>

          <signal name=”NameOwnerChanged”>

            <arg type=”s”/>

            <arg type=”s”/>

            <arg type=”s”/>

          </signal>

          <signal name=”NameLost”>

            <arg type=”s”/>

          </signal>

          <signal name=”NameAcquired”>

            <arg type=”s”/>

          </signal>

        </interface>

      </node>

      从结果上看总线对象DBus支持两个接口org.freedesktop.DBus.Introspectable和org.freedesktop.DBus,org.freedesktop.DBus.Introspectable的用法我们在后面介绍,org.freedesktop.DBus接口有18个方法和3个信号。每个方法和信号的详细说明请参阅http://dbus.freedesktop.org/doc/dbus-specification.html

      值得一提的是其中的Hello方法:一个应用程序能发送消息给其他应用程序之前必须发送org.freedesktop.DBus.Hello消息给消息总线以活得唯一名,假如应用程序没带唯一名发送消息给其他应用程序 或消息总线,他将被断开和消息总线连接,但是这种断开不是我们期待的,也不是标准的断开方法。

      如果你在Linux系统中,有另外一个程序叫D-Feet,很有用,除了可以查看d-bus总线中提供的所有接口方法信号意外,还可以直接测试。可以运行单独某个方法。windows下好像还没有

    2. dbus-send这是一个发送消息的例子程序,功能还蛮强大的。比如我们通过dbus-send来调用上面提到的org.freedesktop.DBus.Introspectable接口的Introspect:  dbus-send –session –type=method_call –print-reply –dest=org.freedesktop.DBus / org.freedesktop.DBus.Introspectable.Introspect

      输出结果和上面得到的是一样的。

    3. dbus-monitor作用是监视总线。

      运行dbus-monitor,再运行dbus-send –session –type=method_call –print-reply –dest=org.freedesktop.DBus / org.freedesktop.DBus.Introspectable.Introspect

      我们可以看到在dbus-monitor窗口中显示:

      signal sender=org.freedesktop.DBus -> dest=(null destination) path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameOwnerChanged

         string “:1.9″

         string “”

         string “:1.9″

      method call sender=:1.9 -> dest=org.freedesktop.DBus path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=Hello

      method call sender=:1.9 -> dest=org.freedesktop.DBus path=/; interface=org.freedesktop.DBus.Introspectable; member=Introspect

      signal sender=org.freedesktop.DBus -> dest=(null destination) path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameOwnerChanged

         string “:1.9″

         string “:1.9″

         string “”

      从结果中非常容易就看出总线的执行情况。

    4. test-service这是一个提供服务的例子程序,提供的服务很简单。运行test-service后运行dbus-send –session –type=method_call –print-reply –dest=org.freedesktop.DBus / org.freedesktop.DBus.ListNames

      输出:

      method return sender=org.freedesktop.DBus -> dest=:1.67 reply_serial=2

         array [

            string "org.freedesktop.DBus"

            string ":1.67"

            string "org.freedesktop.DBus.TestSuiteEchoService"

            string ":1.12"

            string ":1.47"

         ]

      可以看出多了TestSuiteEchoService,正式由test-service提供的

    5. test- pending-call-dispatch这个例子是针对test-service提供的服务提供测试的。运行了100次的循环进行测试并记录实际花费时间。从两边的输出结构可以看出提供的服务
Technorati 标签: ,

Leave a Reply

You must be logged in to post a comment.