2016-07-08 11 views
0

私はpydbusで作業していますが、すでにセッションバス(「クライアント側」)で信号を聞くのに成功しています。私は、プログラムがどこに(例えば、FSにファイルを書き込むときなど)アクションがトリガーされるたびに信号を送信するサーバー側を書きたいと思います。私は実際にそのGitHubでその例を得ていません。彼らはクライアントが呼び出すことができる方法の束を持っている基本的なサーバを書く方法を示しているだけです(しかし、私が欲しいものです)。pydbusライブラリを使用してセッションバス経由で信号を送信する

FYI、クライアント側は非常に簡単で、以下のようになります。

my_callback
# Subscribe to DBus dump signal 
session_bus = SessionBus() 
session_bus.subscribe(iface='my.iface', 
         signal='my_signal_name', 
         object='/my/object', 
         signal_fired=my_callback) 

# Create and run main loop 
loop = GObject.MainLoop() 
loop.run() 

(この場合はmy_signal_name)聞いイベントポップ毎回呼び出されるメソッドの助けを

Thxをです。

答えて

0

信号を担当するクラスは、genericモジュールにあります。それはdocumentated十分になります。

Static signal object 

You're expected to set it as a class property:: 

    class A: 
     SomethingHappened = signal() 

Declared this way, it can be used on class instances 
to connect signal observers:: 

    a = A() 
    a.SomethingHappened.connect(func) 

and emit the signal:: 

    a.SomethingHappened() 

You may pass any parameters to the emiting function 
- they will be forwarded to all subscribed callbacks. 

信号を使用していますexample in the tutorialもあります。最後の行とプロパティSomePropertyに注意してください。 setterでpythonのプロパティSomePropertyが変更された場合、信号はself.PropertiesChanged("net.lew21.pydbus.TutorialExample", {"SomeProperty": self.SomeProperty}, [])で送出されます。

from pydbus.generic import signal 

class Example(object): 
    """ 
    <node> 
     <interface name='net.lew21.pydbus.TutorialExample'> 
     <method name='EchoString'> 
      <arg type='s' name='a' direction='in'/> 
      <arg type='s' name='response' direction='out'/> 
     </method> 
     <property name="SomeProperty" type="s" access="readwrite"> 
      <annotation name="org.freedesktop.DBus.Property.EmitsChangedSignal" value="true"/> 
     </property> 
     </interface> 
    </node> 
    """ 

    def EchoString(self, s): 
    """returns whatever is passed to it""" 
    return s 

    def __init__(self): 
    self._someProperty = "initial value" 

    @property 
    def SomeProperty(self): 
    return self._someProperty 

    @SomeProperty.setter 
    def SomeProperty(self, value): 
    self._someProperty = value 
    self.PropertiesChanged("net.lew21.pydbus.TutorialExample", {"SomeProperty": self.SomeProperty}, []) 

    PropertiesChanged = signal() 

notification_server exampleもありますが、シグナルは使用されますが、呼び出されません。

0

次のpython3/pydbusプログラムは毎秒ランダムな整数をセッションD-Busに公開します。

#!/usr/bin/env python3 
#! 
from pydbus.generic import signal 
from pydbus import SessionBus 
from gi.repository import GLib 
import random 
loop = GLib.MainLoop() 
interface_name = "org.example.project_1.server_1" 
bus = SessionBus() 


class Server_XML(object): 
    """ 
    Server_XML definition. 
    Emit/Publish a signal that is a random integer every second 
    type='i' for integer. 
    """ 
    dbus = """ 
    <node> 
     <interface name="org.example.project_1.server_1"> 
      <signal name="app_1_signal"> 
       <arg type='i'/> 
      </signal> 
     </interface> 
    </node> 
    """ 
    app_1_signal = signal() 

def repeating_timer(): 
    """Generate random integer between 0 and 100 and emit over Session D-Bus 
    return True to keep the GLib timer calling this function once a second.""" 
    random_integer = random.randint(0,100) 
    print(random_integer) 
    emit.app_1_signal(random_integer) 
    return True 


if __name__=="__main__": 
    # Setup server to emit signals over the DBus 
    emit = Server_XML() 
    bus.publish(interface_name, emit) 
    # Setup repeating timer 
    GLib.timeout_add_seconds(interval=1, function=repeating_timer) 
    # Run loop with graceful ctrl C exiting. 
    try: 
     loop.run() 
    except KeyboardInterrupt as e: 
     loop.quit() 
     print("\nExit by Control C") 

他のLinuxコンソール端末を開いて、整数がセッション・バス上で公開されていることを確認するためにgdbusユーティリティを使用します。たとえば...

$ gdbus monitor --session --dest org.example.project_1.server_1 
Monitoring signals from all objects owned by org.example.project_1.server_1 
The name org.example.project_1.server_1 is owned by :1.618 
/org/example/project_1/server_1: org.example.project_1.server_1.app_1_signal (36,) 
/org/example/project_1/server_1: org.example.project_1.server_1.app_1_signal (37,) 
/org/example/project_1/server_1: org.example.project_1.server_1.app_1_signal (25,) 

次のpython3/pydbusクライアントプログラムは、セッションD-バス上で公開されているランダムな整数に加入している...

#!/usr/bin/env python3 
#! 
from pydbus.generic import signal 
from pydbus import SessionBus 
from gi.repository import GLib 
loop = GLib.MainLoop() 
dbus_filter = "/org/example/project_1/server_1" 
bus = SessionBus() 


def cb_server_signal_emission(*args): 
    """ 
    Callback on emitting signal from server 
    """ 
    print("Message: ", args) 
    print("Data: ", str(args[4][0])) 

if __name__=="__main__": 
    # Subscribe to bus to monitor for server signal emissions 
    bus.subscribe(object = dbus_filter, 
        signal_fired = cb_server_signal_emission)  
    loop.run() 

コンソール出力このクライアントプログラムからなりますこれと似ている...

$ python3 client_pydbus.py 
Message: (':1.621', '/org/example/project_1/server_1', 'org.example.project_1.server_1', 'app_1_signal', (72,)) 
Data: 72 
Message: (':1.621', '/org/example/project_1/server_1', 'org.example.project_1.server_1', 'app_1_signal', (46,)) 
Data: 46 
Message: (':1.621', '/org/example/project_1/server_1', 'org.example.project_1.server_1', 'app_1_signal', (47,)) 
Data: 47 
関連する問題