IPC with Qt
Upcoming SlideShare
Loading in...5
×
 

IPC with Qt

on

  • 9,118 views

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

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

Statistics

Views

Total Views
9,118
Views on SlideShare
9,058
Embed Views
60

Actions

Likes
1
Downloads
134
Comments
0

2 Embeds 60

http://www.slideshare.net 59
http://www.docseek.net 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

IPC with Qt IPC with Qt Presentation Transcript

  • IPC with Qt
  • About me  Marius Bugge Monsen  Senior Software Engineer  5 years for TT  Msc. comp. sci. NTNU  Projects  Itemviews  QtDBus  WebKit 2
  • Contents  What is Inter­Process Communication ?  What is D­Bus ?  What is QtDBus ?  Exposing Interfaces  Calling Interfaces  Messages  More IPC 3
  •  What is Inter­Process Communication ?  What is D­Bus ?  What is QtDBus ?  Exposing Interfaces  Calling Interfaces  Messages  More IPC 4
  • What is Inter­Process Communication? I am a  process! So am I! Process Process 5
  • What Is Inter­Process Communication? Exchanging information between processes. 6
  • What Is Inter­Process Communication?  Techniques  remote procedure calls (RPC)  message passing  synchronization  shared memory 7
  • What Is Inter­Process Communication?  Implementations  CORBA  SOAP / XML­RPC  DCOM  DCOP  D­Bus  ... 8
  •  What is Inter­Process Communication ?  What is D­Bus ?  What is QtDBus ?  Exposing Interfaces  Calling Interfaces  Messages  More IPC 9
  • What is D­Bus ? A Message Bus System 10
  • What is D­Bus? Message Bus Daemon Process Process Process 11
  • What is D­Bus ?  Protocol  Library  Message bus framework 12
  • D­Bus  General one­to­one message passing framework  Single computer  Computer­to­computer through unencrypted TCP/IP  13
  • D­Bus  Portable to any *NIX system  Windows port in progress  Standard IPC mechanism for Gnome and KDE 14
  • What is D­Bus ?  System Bus  system­wide services  Session Bus  per­user­login­session services 15
  • Exposing Functionality  Service  Objects  Interfaces  Methods  Signals  Properties 16
  • 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
  • Exposing Functionality  Object  Multiple objects per service.  Specified by a path.  /path/to/ObjectName  The path structure is arbitrary.  Provides namespaces for objects. 18
  • 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
  • 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
  • Exposing Functionality  Properties  org.freedesktop.DBus.Properties  The Properties interface that provides Get and Set methods. 21
  • Exposing Functionality  Address  Service  Object  Interface  Method or Signal 22
  • D­Bus Address com.trolltech.Assistant   /Assistant   com.trolltech.Assistant.HelpViewer   showLink service object path interface method 23
  • 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
  • Introspection  Objects may provide the introspection interface  org.freedesktop.DBus.Introspectable  The Introspect method returns an XML description. 25
  •  What is Inter­Process Communication ?  What is D­Bus ?  What is QtDBus ?  Exposing Interfaces  Calling Interfaces  Messages  More IPC 26
  • 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
  • QtDBus Tools  qdbus  Command­line D­Bus explorer.  qdbusviewer  Graphical D­Bus explorer.  28
  • 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
  •  What is Inter­Process Communication ?  What is D­Bus ?  What is QtDBus ?  Exposing Interfaces  Calling Interfaces  Messages  More IPC 30
  • 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
  • Connecting To D­Bus  QDBusConnection  Represents a connection to the D­Bus bus daemon.  QDBusConnection::BusType  QDBusConnection::SessionBus  QDBusConnection::SystemBus  QDBusConnection::ActivationBus 32
  • Connect To The SessionBus int main(int argc, char *argv[]) {         QApplication app(argc, argv);         ...         QDBusConnection connection = QDBusConnection::sessionBus();         ...         return app.exec(); } 33
  • 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
  • Exposing Functionality  Expose QObject slots directly  Expose interfaces  doesn't have to map directly to any object 35
  • 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
  • Exposing Interfaces  Adaptor Class  Special class attached to a QObject  Exposes an interface to the bus  Lightweight  Inherits QDBusAbstractAdaptor 37
  • Exposing Interfaces Adaptor Interface QObject Adaptor Interface Message Bus Adaptor Interface 38
  • 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
  • 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
  • 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
  • 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
  • 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
  • XML To Adaptor test.xml qdbusxml2cpp ­a adaptor test.xml adaptor.h adaptor.cpp 44
  • XML To Proxy test.xml qdbusxml2cpp ­p proxy test.xml proxy.h proxy.cpp 45
  • Using Interfaces Interface Proxy Interface Message Bus Proxy Interface Proxy 46
  •  What is Inter­Process Communication ?  What is D­Bus ?  What is QtDBus ?  Exposing Interfaces  Calling Interfaces  Messages  More IPC 47
  • Using Interfaces  Proxy Class  Represents a D­Bus interface  Represented as a normal QObject  signals  slots  properties  Inherits QDBusAbstractInterface 48
  • 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
  • QDBusInterface  Generic accessor class  used to place calls to remote interfaces  requires org.freedesktop.DBus.Introspectable 50
  • Calling Methods int main() {         QDBusInterface iface("com.trolltech.LineEdit",                                            "/LineEdit",                                            "com.trolltech.LineEdit.Test",                                            QDBusConnection::sessionBus());         iface.call("hide");         return 0; } 51
  • 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
  • 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
  • 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
  •  What is Inter­Process Communication ?  What is D­Bus ?  What is QtDBus ?  Exposing Interfaces  Calling Interfaces  Messages  More IPC 55
  • QDBusMessage  Represents D­Bus message.  MessageType  MethodCallMessage  SignalMessage  ReplyMessage  ErrorMessage  InvalidMessage 56
  • Creating A Message  Static functions  createError(...)  createMethodCall(...)  createSignal(...)  Non­static functions  createErrorReply(...)  createReply(...) 57
  • 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
  • Synchronous vs. Asynchronous  QDBus::CallMode  NoBlock  Block  BlockWithGui  AutoDetect 59
  • 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
  • QtDBus Approaches  High­level  XML Interface Description  Adaptor class  Proxy class  Mid­level  QDBusInterface  Low­level  QDBusMessage 61
  •  What is Inter­Process Communication ?  What is D­Bus ?  What is QtDBus ?  Exposing Interfaces  Calling Interfaces  Messages  More IPC 62
  • More IPC  Techniques  remote procedure calls (RPC)  message passing  synchronization  shared memory 63
  • More Qt IPC (Qt 4.4)  Synchronization  QSystemSemaphore  Shared Memory  QSharedMemory  Local Sockets  QLocalSocket  QLocalServer 64
  • Resources http://doc.trolltech.com/4.3/intro­to­dbus.html http://www.freedesktop.org/wiki/Software/dbus http://sourceforge.net/projects/windbus 65