Home · All Classes · Grouped Classes · Annotated · Functions

Tutorial: Qtopia IPC Server

Files:

The Qtopia IPC Tutorial demonstrates the use of QtopiaIpcAdaptor, which is the fundamental class of the Qtopia IPC system. This example demonstrates the basics of sending and receiving messages between different applications. For the other part of this tutorial, see Tutorial: Qtopia IPC Client.

The cannonserver application starts and begins listening on an IPC channel QPE/CannonExample. When it receives a cannon shoot order, it randomly decides whether the cannon has hit something or missed completely. It then broadcasts the results of the shot on the same channel and exits.

CannonListener Class Definition

    class CannonListener : public QtopiaIpcAdaptor
    {
        Q_OBJECT

    public:
        CannonListener(QObject *parent = 0);

    public slots:
        void shootCannon(int);

    signals:
        void missed();
        void hit();
    };

The CannonListener class inherits QtopiaIpcAdaptor which is used to send and receive messages between Qtopia based applications. There are two signals that CannonListener class emits, and a one slot. When a message of type shootCannon() arrives, the shootCannon slot will be called in CannonListener automatically. Similarly, when the CannonListener emits a signal, a message with the same signature will be sent over the IPC channel.

CannonListener Class Implementation

The constructor contains a single call to publishAll(). This tells the Qtopia IPC system to make the necessary connections between the IPC messages and slots in the CannonListener class. In this example we want all messages with the same signature as the public slots to be hooked up automatically. All signals sent by this class will also be automatically broadcast to the remote clients whenever CannonListener emits the defined signal.

    CannonListener::CannonListener(QObject *parent)
        : QtopiaIpcAdaptor("QPE/CannonExample", parent)
    {
        publishAll(QtopiaIpcAdaptor::SignalsAndSlots);
    }

The shootCannon() slot will be automatically called whenever a message with the same signature arrives. The power parameter will hold the message arguments. We roll a simple dice to figure out whether the cannon hit or missed and emit a corresponding signal. This will result in the Qtopia IPC system broadcasting and IPC message with the same signature. Finally we delete the CannonListener object by using deleteLater(). This will result in the application terminating.

    void CannonListener::shootCannon(int power)
    {
        int pwr = power % 100;
        int roll = qrand() % 100;

        if (pwr >= roll)
            emit hit();
        else
            emit missed();

        deleteLater();
    }

Building the Cannon Listener application.

To install and run the Cannon Listener application, carry out the following steps.

  1. Create a new directory (e.g. $HOME/src/cannonserver) and copy all the example files to that directory.
        mkdir $HOME/src/cannonserver
        cd $HOME/src/cannonserver
        cp -r <Qtopia-source-directory>/examples/ipc/cannonserver/* .
        chmod +w *
  2. Build the new application.
        export QPEDIR=<Qtopia-build-directory>
        $QPEDIR/bin/qtopiamake
        make install
  3. Run Qtopia.
        $QPEDIR/bin/runqtopia
  4. Run the cannonserver application, you can then run the cannonclient application.


Copyright © 2008 Nokia Trademarks
Qtopia 4.3.3