Changeset 10325 in project


Ignore:
Timestamp:
04/03/08 09:21:43 (12 years ago)
Author:
svnwiki
Message:

Changes applied for ecloud (72.208.150.200) through svnwiki:

dbus:enable-polling-thread! etc.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/dbus

    r10323 r10325  
    101101dbus:register-method : provide a handler to be called when the given method-name is received and matches the given context.
    102102
     103dbus:enable-polling-thread! : enable or disable the polling thread, and set the polling interval in seconds
     104
     105dbus:poll-for-message : check once whether any incoming DBus message is waiting, and if so, dispatch it to the appropriate callback (which you have previously registered using dbus:register-method).  Returns #t if it received a message, #f if not.
     106
    103107=== Examples
    104108
     
    120124That example called a method but it did not expect any return values.
    121125
    122 Now suppose you want to simulate the car, so you can use the above example to control your own car rather than the QT one:
     126Now suppose you want to simulate the car, so you can use the above example to control your own car rather than the QT one.  And suppose you want to poll for messages manually rather than via the default SRFI-18 polling thread:
    123127
    124128<enscript highlight=scheme>
    125129(use dbus)
     130
    126131(define (turn-right) (printf "car is turning to the right~%"))
    127132(define (turn-left) (printf "car is turning to the left~%"))
     133
    128134(define rc-car-context (dbus:make-context
    129         service: 'com.trolltech.CarExample
    130         path: '/Car
    131         interface: 'com.trolltech.Examples.CarInterface ))
     135        service: 'com.trolltech.CarExample
     136        path: '/Car
     137        interface: 'com.trolltech.Examples.CarInterface ))
     138
     139(dbus:enable-polling-thread enable: #f)
     140
    132141(dbus:register-method rc-car-context "turnRight" turn-right)
    133142(dbus:register-method rc-car-context "turnLeft" turn-left)
     143
     144(let loop ()
     145        (dbus:poll-for-message)
     146        (loop))
    134147</enscript>
    135148
    136149dbus:register-method starts a polling loop the first time that it is called.  So you can then run the program above which does dbus:send, and you will see the appropriate printf statement execute asynchronously when the message is received.  However the polling thread is subject to the usual limitations of Chicken threads: if there is any blocking I/O, which is waiting for something, then all threads are blocked.  That means you should not use the readline egg for example, because the polling thread will be blocked between each time that you type something and hit Enter.
    137150
    138 If the polling thread doesn't work, and you would prefer to poll manually, the dbus:poll-for-message procedure is exported for that purpose, however there needs to be some mechanism to prevent the creation of the polling thread; that is not done yet.
     151If the polling thread doesn't work, and you would prefer to poll manually, you can call (dbus:enable-polling-thread! enable: #f) to stop the thread (or to prevent it from being started when you register a new method), and then call dbus:poll-for-message periodically.  Both of those functions can take an optional bus: parameter (which defaults to dbus:session-bus, naturally).  dbus:poll-for-message can also take an optional timeout: parameter (which is 0 by default, so that it is a non-blocking call).
    139152
    140153==== Examples based on the DBus Tutorial
Note: See TracChangeset for help on using the changeset viewer.