IPC with Qt

9,170 views
8,880 views

Published on

Inter-Process Communication with Qt. Presented at Qt Developer Days 2007.

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
9,170
On SlideShare
0
From Embeds
0
Number of Embeds
66
Actions
Shares
0
Downloads
154
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

IPC with Qt

  1. 1. IPC with Qt
  2. 2. About me  Marius Bugge Monsen  Senior Software Engineer  5 years for TT  Msc. comp. sci. NTNU  Projects  Itemviews  QtDBus  WebKit 2
  3. 3. Contents  What is Inter­Process Communication ?  What is D­Bus ?  What is QtDBus ?  Exposing Interfaces  Calling Interfaces  Messages  More IPC 3
  4. 4.  What is Inter­Process Communication ?  What is D­Bus ?  What is QtDBus ?  Exposing Interfaces  Calling Interfaces  Messages  More IPC 4
  5. 5. What is Inter­Process Communication? I am a  process! So am I! Process Process 5
  6. 6. What Is Inter­Process Communication? Exchanging information between processes. 6
  7. 7. What Is Inter­Process Communication?  Techniques  remote procedure calls (RPC)  message passing  synchronization  shared memory 7
  8. 8. What Is Inter­Process Communication?  Implementations  CORBA  SOAP / XML­RPC  DCOM  DCOP  D­Bus  ... 8
  9. 9.  What is Inter­Process Communication ?  What is D­Bus ?  What is QtDBus ?  Exposing Interfaces  Calling Interfaces  Messages  More IPC 9
  10. 10. What is D­Bus ? A Message Bus System 10
  11. 11. What is D­Bus? Message Bus Daemon Process Process Process 11
  12. 12. What is D­Bus ?  Protocol  Library  Message bus framework 12
  13. 13. D­Bus  General one­to­one message passing framework  Single computer  Computer­to­computer through unencrypted TCP/IP  13
  14. 14. D­Bus  Portable to any *NIX system  Windows port in progress  Standard IPC mechanism for Gnome and KDE 14
  15. 15. What is D­Bus ?  System Bus  system­wide services  Session Bus  per­user­login­session services 15
  16. 16. Exposing Functionality  Service  Objects  Interfaces  Methods  Signals  Properties 16
  17. 17. Exposing Functionality  Service  Represent a process connected to a bus.  Has a unique (base) name chosen by the bus.  Uses a “:n.nnn” naming scheme (e.g. :1.32).  Has a well­known name chosen by the process.  Uses “reverse domain name” naming scheme (e.g.  com.trolltech.appname).  Multiple services per process 17
  18. 18. Exposing Functionality  Object  Multiple objects per service.  Specified by a path.  /path/to/ObjectName  The path structure is arbitrary.  Provides namespaces for objects. 18
  19. 19. Exposing Functionality  Interface  Multiple interfaces per object.  Dot­separated name.  com.trolltech.Assistant.HelpViewer  Defines a set of methods, signals and properties.  names  parameters  return values  Can be described by XML. 19
  20. 20. Exposing Functionality  Methods  A method can be called by sending a message to the process  exposing the functionality (the caller does not have to register  a service).  Many­to­one.  Signals  A signal is emitted by the process which is exposing the  interface and is available to any application on the same bus.  One­to­many. 20
  21. 21. Exposing Functionality  Properties  org.freedesktop.DBus.Properties  The Properties interface that provides Get and Set methods. 21
  22. 22. Exposing Functionality  Address  Service  Object  Interface  Method or Signal 22
  23. 23. D­Bus Address com.trolltech.Assistant   /Assistant   com.trolltech.Assistant.HelpViewer   showLink service object path interface method 23
  24. 24. Activation  The Bus can start clients automatically  by explicitly requesting the Bus to activate the client.  by invoking a method on an object belonging to the client. 24
  25. 25. Introspection  Objects may provide the introspection interface  org.freedesktop.DBus.Introspectable  The Introspect method returns an XML description. 25
  26. 26.  What is Inter­Process Communication ?  What is D­Bus ?  What is QtDBus ?  Exposing Interfaces  Calling Interfaces  Messages  More IPC 26
  27. 27. What is QtDBus ?  Object­oriented Qt API for D­Bus.  Maps Qt properties, signals and slots to D­Bus.  Maps D­Bus properties, signals and methods to Qt. 27
  28. 28. QtDBus Tools  qdbus  Command­line D­Bus explorer.  qdbusviewer  Graphical D­Bus explorer.  28
  29. 29. QtDBus Tools  qdbusxml2cpp  Compiles XML interface description to an interface or adaptor  class.  qdbuscpp2xml  Compiles an interface or adaptor class to an XML interface  description file. 29
  30. 30.  What is Inter­Process Communication ?  What is D­Bus ?  What is QtDBus ?  Exposing Interfaces  Calling Interfaces  Messages  More IPC 30
  31. 31. QtDBus Approaches  High­level  Interface representations generated from XML descriptions  Mid­level  Direct calls to a generic interface class  Low­level  Constructing and sending messages 31
  32. 32. Connecting To D­Bus  QDBusConnection  Represents a connection to the D­Bus bus daemon.  QDBusConnection::BusType  QDBusConnection::SessionBus  QDBusConnection::SystemBus  QDBusConnection::ActivationBus 32
  33. 33. Connect To The SessionBus int main(int argc, char *argv[]) {         QApplication app(argc, argv);         ...         QDBusConnection connection = QDBusConnection::sessionBus();         ...         return app.exec(); } 33
  34. 34. Register The Service int main(int argc, char *argv[]) {         QApplication app(argc, argv);         ...         QDBusConnection connection = QDBusConnection::sessionBus();         connection.registerService("com.trolltech.LineEdit");         ...         return app.exec(); } 34
  35. 35. Exposing Functionality  Expose QObject slots directly  Expose interfaces  doesn't have to map directly to any object 35
  36. 36. Exposing All Slots int main(int argc, char *argv[]) {         QApplication app(argc, argv);         QLineEdit le;         QDBusConnection connection = QDBusConnection::sessionBus();         connection.registerService("com.trolltech.LineEdit");         connection.registerObject("/LineEdit", &le,                                                        QDBusConnection::ExportAllSlots);         le.show();         return app.exec(); } 36
  37. 37. Exposing Interfaces  Adaptor Class  Special class attached to a QObject  Exposes an interface to the bus  Lightweight  Inherits QDBusAbstractAdaptor 37
  38. 38. Exposing Interfaces Adaptor Interface QObject Adaptor Interface Message Bus Adaptor Interface 38
  39. 39. Exposing An Interface int main(int argc, char *argv[]) {         QApplication app(argc, argv);         QLineEdit le;         new LineEditAdaptor(&le);         QDBusConnection connection = QDBusConnection::sessionBus();         connection.registerService("com.trolltech.LineEdit");         connection.registerObject("/LineEdit", &le,                                                      QDBusConnection::ExportAdaptors);         le.show();         return app.exec(); } 39
  40. 40. Exposing Methods class LineEditAdaptor : public QDBusAbstractAdaptor {         Q_OBJECT         Q_CLASSINFO("D­Bus Interface", "com.trolltech.LineEdit.Test")         LineEditAdaptor(QLineEdit *w) : QDBusAbstractAdaptor(w), le(w) {}         ~LineEditAdaptor() {} public slots:         inline void show() { le­>show(); }         inline void hide() { le­>hide(); } private:         QLineEdit *le; }; 40
  41. 41. Exposing Properties class LineEditAdaptor : public QDBusAbstractAdaptor {         Q_OBJECT         Q_CLASSINFO("D­Bus Interface", "com.trolltech.LineEdit.Test")         Q_PROPERTY(QString text READ text WRITE setText) public:         LineEditAdaptor(QLineEdit *w) : QDBusAbstractAdaptor(w), le(w) {}         ~LineEditAdaptor() {}        inline QString text() const { return le­>text(); }        inline void setText(const QString &text) { le­>setText(text); }        ... }; 41
  42. 42. Exposing Signals class LineEditAdaptor : public QDBusAbstractAdaptor {         Q_OBJECT         Q_CLASSINFO("D­Bus Interface", "com.trolltech.LineEdit.Test")         Q_PROPERTY(QString text READ text WRITE setText) public:         LineEditAdaptor(QLineEdit *w) : QDBusAbstractAdaptor(w), le(w)         {                 QDBusAbstractAdaptor::setAutoRelaySignals(true);         }         ... signals:         void textChanged(const QString &text);         ... }; 42
  43. 43. XML Interface Description <node name="/com/trollech/LineEdit"> <interface name="com.trolltech.LineEdit.Test"> <property name="text" type="s" access="readwrite" /> <method name="show"/> <method name="hide"/> <signal name="textChanged"> <arg type="s" name="text" /> </signal> </interface> </node> 43
  44. 44. XML To Adaptor test.xml qdbusxml2cpp ­a adaptor test.xml adaptor.h adaptor.cpp 44
  45. 45. XML To Proxy test.xml qdbusxml2cpp ­p proxy test.xml proxy.h proxy.cpp 45
  46. 46. Using Interfaces Interface Proxy Interface Message Bus Proxy Interface Proxy 46
  47. 47.  What is Inter­Process Communication ?  What is D­Bus ?  What is QtDBus ?  Exposing Interfaces  Calling Interfaces  Messages  More IPC 47
  48. 48. Using Interfaces  Proxy Class  Represents a D­Bus interface  Represented as a normal QObject  signals  slots  properties  Inherits QDBusAbstractInterface 48
  49. 49. The Generated Proxy         com::trolltech::LineEdit::Test *le =                 new com::trolltech::LineEdit::Test(         // interface                         "com.trolltech.LineEdit",                   // service                         "/LineEdit",                                         // object                         QDBusConnection::sessionBus());  // bus         QComboBox box;         box.setEditable(true);         box.setEditText(le­>isValid() ? le­>text() : "empty");         QObject::connect(le, SIGNAL(textChanged(const QString&)),                                        &box, SLOT(setEditText(const QString&))); 49
  50. 50. QDBusInterface  Generic accessor class  used to place calls to remote interfaces  requires org.freedesktop.DBus.Introspectable 50
  51. 51. Calling Methods int main() {         QDBusInterface iface("com.trolltech.LineEdit",                                            "/LineEdit",                                            "com.trolltech.LineEdit.Test",                                            QDBusConnection::sessionBus());         iface.call("hide");         return 0; } 51
  52. 52. Arguments And Return Values int main() {         QString service = "com.trolltech.Calculator";         QString path = "/Calculator";         QString interface = "com.trolltech.Calculator.Computation";         QDBusConnection bus = QDBusConnection::sessionBus();         QDBusInterface iface(service, path, interface, bus);         QDBusMessage result = iface.call("multiply", 4, 3);         QList<QVariant> values = result.arguments();         ...         return 0; } 52
  53. 53. Properties int main() {         QString service = "com.trolltech.Calculator";         QString path = "/Calculator";         QString interface = "com.trolltech.Calculator.Computation";         QDBusConnection bus = QDBusConnection::sessionBus();         QDBusInterface iface(service, path, interface, bus);         QVariant text = iface.property(“text”);         return 0; } 53
  54. 54. Signals And Slots int main() {         QString service = "com.trolltech.Calculator";         QString path = "/Calculator";         QString interface = "com.trolltech.Calculator.Computation";         QDBusConnection bus = QDBusConnection::sessionBus();                  QDBusInterface iface(service, path, interface, bus);         QObject::connect(&iface, SIGNAL(textChanged(const QString&)),                                        &receiver, SLOT(textChanged(const QString&)));         return 0; } 54
  55. 55.  What is Inter­Process Communication ?  What is D­Bus ?  What is QtDBus ?  Exposing Interfaces  Calling Interfaces  Messages  More IPC 55
  56. 56. QDBusMessage  Represents D­Bus message.  MessageType  MethodCallMessage  SignalMessage  ReplyMessage  ErrorMessage  InvalidMessage 56
  57. 57. Creating A Message  Static functions  createError(...)  createMethodCall(...)  createSignal(...)  Non­static functions  createErrorReply(...)  createReply(...) 57
  58. 58. Sending A Message int main() {         QDBusMessage msg = QDBusMessage::createMethodCall(                                                       “com.trolltech.Calculator”,                      // service                                                       “/Calculator”,                                          // object                                                       “com.trolltech.Calculator.Computation”, // iface                                                       “multiply”);                                              // method         QList<QVariant> args;                 args << 4 << 5;         msg.setArguments(args);         QDBusMessage reply = QDBusConnection::sessionBus().call(msg);         ...         return 0; } 58
  59. 59. Synchronous vs. Asynchronous  QDBus::CallMode  NoBlock  Block  BlockWithGui  AutoDetect 59
  60. 60. Sending A Message Asynchronously int main() {         ...         QDBusMessage msg = QDBusMessage::createMethodCall(                                                       “com.trolltech.LineEdit”,                     // service                                                       “/LineEdit”,                                         // object                                                       “com.trolltech.LineEdit.Test”,             // interface                                                       “hide”);                                               // method         QDBusConnection::sessionBus().send(msg);         ...         return 0; } 60
  61. 61. QtDBus Approaches  High­level  XML Interface Description  Adaptor class  Proxy class  Mid­level  QDBusInterface  Low­level  QDBusMessage 61
  62. 62.  What is Inter­Process Communication ?  What is D­Bus ?  What is QtDBus ?  Exposing Interfaces  Calling Interfaces  Messages  More IPC 62
  63. 63. More IPC  Techniques  remote procedure calls (RPC)  message passing  synchronization  shared memory 63
  64. 64. More Qt IPC (Qt 4.4)  Synchronization  QSystemSemaphore  Shared Memory  QSharedMemory  Local Sockets  QLocalSocket  QLocalServer 64
  65. 65. Resources http://doc.trolltech.com/4.3/intro­to­dbus.html http://www.freedesktop.org/wiki/Software/dbus http://sourceforge.net/projects/windbus 65

×