DCOP is a simple IPC/RPC mechanism built to operate over sockets. Either unix domain sockets or TCP/IP sockets are supported. DCOP is built on top of the Inter Client Exchange (ICE) protocol, which comes standard as a part of X11R6 and later. It also depends on {\bf Qt}, but beyond that it does not require any other libraries. Because of this, it is extremely lightweight, enabling it to be linked into all {\bf KDE} applications with low overhead.\section{Model:}\label{index_model}
The model is simple. Each application using DCOP is a client. They communicate to each other through a DCOP server, which functions like a traffic director, dispatching messages/calls to the proper destinations. All clients are peers of each other.

Two types of actions are possible with DCOP: \char`\"{}send and forget\char`\"{} messages, which do not block, and \char`\"{}calls,\char`\"{} which block waiting for some data to be returned.

Any data that will be sent is serialized (also referred to as marshalling in CORBA speak) using the built-in {\bf QData\-Stream} operators available in all of the {\bf Qt} classes. This is fast and easy. In fact it's so little work that you can easily write the marshalling code by hand. In addition, there's a simple IDL-like compiler available (dcopidl and dcopidl2cpp) that generates stubs and skeletons for you. Using the dcopidl compiler has the additional benefit of type safety.

The manual method is covered first, followed by the automatic IDL method.\section{Establishing the Connection:}\label{index_establish}
{\bf KApplication} has gained a method called {\tt {\bf KApplication::dcop\-Client()}} which returns a pointer to a {\bf DCOPClient}{\rm (p.\,\pageref{classDCOPClient})} instance. The first time this method is called, the client class will be created. DCOPClients have unique identifiers attached to them which are based on what KApplication::name() returns. In fact, if there is only a single instance of the program running, the app\-Id will be equal to KApplication::name().

To actually enable DCOP communication to begin, you must use {\tt {\bf DCOPClient::attach()}{\rm (p.\,\pageref{classDCOPClient_a2})}}. This will attempt to attach to the DCOP server. If no server is found or there is any other type of error, {\bf DCOPClient::attach()}{\rm (p.\,\pageref{classDCOPClient_a2})} will return false. {\bf KApplication} will catch a dcop signal and display an appropriate error message box in that case.

After connecting with the server via {\bf DCOPClient::attach()}{\rm (p.\,\pageref{classDCOPClient_a2})}, you need to register this app\-Id with the server so it knows about you. Otherwise, you are communicating anonymously. Use the DCOPClient::register\-As(const QCString \&name) to do so. In the simple case:



\footnotesize\begin{verbatim}appId = client->registerAs(kapp->name());
\end{verbatim}
\normalsize


If you never retrieve the {\bf DCOPClient}{\rm (p.\,\pageref{classDCOPClient})} pointer from {\bf KApplication}, the object will not be created and thus there will be no memory overhead.

You may also detach from the server by calling {\bf DCOPClient::detach()}{\rm (p.\,\pageref{classDCOPClient_a4})}. If you wish to attach again you will need to re-register as well. If you only wish to change the ID under which you are registered, simply call {\bf DCOPClient::register\-As()}{\rm (p.\,\pageref{classDCOPClient_a11})} with the new name.

{\bf KUnique\-Application} automatically registers itself to DCOP. If you are using {\bf KUnique\-Application} you should not attach or register yourself, this is already done. The app\-Id is by definition equal to {\tt kapp-$>$name()}. You can retrieve the registered DCOP client by calling {\tt kapp-$>$dcop\-Client()}.\section{Sending Data to a Remote Application:}\label{index_sending_data}
To actually communicate, you have one of two choices. You may either call the \char`\"{}send\char`\"{} or the \char`\"{}call\char`\"{} method. Both methods require three identification parameters: an application identifier, a remote object, a remote function. Sending is asynchronous (i.e. it returns immediately) and may or may not result in your own application being sent a message at some point in the future. Then \char`\"{}send\char`\"{} requires one and \char`\"{}call\char`\"{} requires two data parameters.

The remote object must be specified as an object hierarchy. That is, if the toplevel object is called {\tt foo\-Object} and has the child {\tt bar\-Object}, you would reference this object as {\tt foo\-Object/bar\-Object}. Functions must be described by a full function signature. If the remote function is called {\tt do\-It}, and it takes an int, it would be described as {\tt do\-It(int)}. Please note that the return type is not specified here, as it is not part of the function signature (or at least the C++ understanding of a function signature). You will get the return type of a function back as an extra parameter to {\bf DCOPClient::call()}{\rm (p.\,\pageref{classDCOPClient_a20})}. See the section on call() for more details.

In order to actually get the data to the remote client, it must be \char`\"{}serialized\char`\"{} via a {\bf QData\-Stream} operating on a {\bf QByte\-Array}. This is how the data parameter is \char`\"{}built\char`\"{}. A few examples will make clear how this works.

Say you want to call {\tt do\-It} as described above, and not block (or wait for a response). You will not receive the return value of the remotely called function, but you will not hang while the RPC is processed either. The return value of {\bf DCOPClient::send()}{\rm (p.\,\pageref{classDCOPClient_a18})} indicates whether DCOP communication succeeded or not.



\footnotesize\begin{verbatim}QByteArray data;
QDataStream arg(data, IO_WriteOnly);
arg << 5;
if (!client->send("someAppId", "fooObject/barObject", "doIt(int)",
              data))
  qDebug("there was some error using DCOP.");
\end{verbatim}
\normalsize


OK, now let's say we wanted to get the data back from the remotely called function. You have to execute a {\bf DCOPClient::call()}{\rm (p.\,\pageref{classDCOPClient_a20})} instead of a {\bf DCOPClient::send()}{\rm (p.\,\pageref{classDCOPClient_a18})}. The returned value will then be available in the data parameter \char`\"{}reply\char`\"{}. The actual return value of call() is still whether or not DCOP communication was successful.



\footnotesize\begin{verbatim}QByteArray data, replyData;
QCString replyType;
QDataStream arg(data, IO_WriteOnly);
arg << 5;
if (!client->call("someAppId", "fooObject/barObject", "doIt(int)",
                  data, replyType, replyData))
  qDebug("there was some error using DCOP.");
else {
  QDataStream reply(replyData, IO_ReadOnly);
  if (replyType == "QString") {
    QString result;
    reply >> result;
    print("the result is: %s",result.latin1());
  } else
    qDebug("doIt returned an unexpected type of reply!");
}
\end{verbatim}
\normalsize
\section{Receiving Data via DCOP:}\label{index_receiving_data}
Currently the only real way to receive data from DCOP is to multiply inherit from the normal class that you are inheriting (usually some sort of {\bf QWidget} subclass or {\bf QObject}) as well as the {\bf DCOPObject}{\rm (p.\,\pageref{classDCOPObject})} class. {\bf DCOPObject}{\rm (p.\,\pageref{classDCOPObject})} provides one very important method: {\bf DCOPObject::process()}{\rm (p.\,\pageref{classDCOPObject_a6})}. This is a pure virtual method that you must implement in order to process DCOP messages that you receive. It takes a function signature, {\bf QByte\-Array} of parameters, and a reference to a {\bf QByte\-Array} for the reply data that you must fill in.

Think of {\bf DCOPObject::process()}{\rm (p.\,\pageref{classDCOPObject_a6})} as a sort of dispatch agent. In the future, there will probably be a precompiler for your sources to write this method for you. However, until that point you need to examine the incoming function signature and take action accordingly. Here is an example implementation.



\footnotesize\begin{verbatim}bool BarObject::process(const QCString &fun, const QByteArray &data,
                QCString &replyType, QByteArray &replyData)
{
  if (fun == "doIt(int)") {
    QDataStream arg(data, IO_ReadOnly);
    int i; // parameter
    arg >> i;
    QString result = self->doIt (i);
    QDataStream reply(replyData, IO_WriteOnly);
    reply << result;
    replyType = "QString";
    return true;
  } else {
    qDebug("unknown function call to BarObject::process()");
    return false;
  }
}
\end{verbatim}
\normalsize
\section{Receiving Calls and processing them:}\label{index_receiving_calls}
If your applications is able to process incoming function calls right away the above code is all you need. When your application needs to do more complex tasks you might want to do the processing out of 'process' function call and send the result back later when it becomes available.

For this you can ask your {\bf DCOPClient}{\rm (p.\,\pageref{classDCOPClient})} for a transaction\-Id. You can then return from the 'process' function and when the result is available finish the transaction. In the mean time your application can receive incoming DCOP function calls from other clients.

Such code could like this:



\footnotesize\begin{verbatim}bool BarObject::process(const QCString &fun, const QByteArray &data,
                QCString &, QByteArray &)
{
  if (fun == "doIt(int)") {
    QDataStream arg(data, IO_ReadOnly);
    int i; // parameter
    arg >> i;
    QString result = self->doIt(i);

    DCOPClientTransaction *myTransaction;
    myTransaction = kapp->dcopClient()->beginTransaction();

    // start processing...
    // Calls slotProcessingDone when finished.
    startProcessing( myTransaction, i);

    return true;
  } else {
    qDebug("unknown function call to BarObject::process()");
    return false;
  }
}

slotProcessingDone(DCOPClientTransaction *myTransaction, const QString &result)
{
    QCString replyType = "QString";
    QByteArray replyData;
    QDataStream reply(replyData, IO_WriteOnly);
    reply << result;
    kapp->dcopClient()->endTransaction( myTransaction, replyType, replyData );
}
\end{verbatim}
\normalsize
\section{Using the dcopidl compiler:}\label{index_dcopidl}
dcopidl makes setting up a DCOP server easy. Instead of having to implement the process() method and unmarshalling (retrieving from {\bf QByte\-Array}) parameters manually, you can let dcopidl create the necessary code on your behalf.

This also allows you to describe the interface for your class in a single, separate header file.

Writing an IDL file is very similar to writing a normal C++ header. An exception is the keyword 'ASYNC'. It indicates that a call to this function shall be processed asynchronously. For the C++ compiler, it expands to 'void'.

Example:



\footnotesize\begin{verbatim}#ifndef MY_INTERFACE_H
#define MY_INTERFACE_H

#include <dcopobject.h>

class MyInterface : virtual public DCOPObject
{
  K_DCOP

  k_dcop:

    virtual ASYNC myAsynchronousMethod(QString someParameter) = 0;
    virtual QRect mySynchronousMethod() = 0;
};

#endif
\end{verbatim}
\normalsize


As you can see, you're essentially declaring an abstract base class, which virtually inherits from {\bf DCOPObject}{\rm (p.\,\pageref{classDCOPObject})}.

If you're using the standard {\bf KDE} build scripts, then you can simply add this file (which you would call My\-Interface.h) to your sources directory. Then you edit your Makefile.am, adding 'My\-Interface.skel' to your SOURCES list and My\-Interface.h to include\_\-HEADERS.

The build scripts will use dcopidl to parse My\-Interface.h, converting it to an XML description in My\-Interface.kidl. Next, a file called My\-Interface\_\-skel.cpp will automatically be created, compiled and linked with your binary.

The next thing you have to do is to choose which of your classes will implement the interface described in My\-Interface.h. Alter the inheritance of this class such that it virtually inherits from My\-Interface. Then add declarations to your class interface similar to those on My\-Interface.h, but virtual, not pure virtual.

Example:



\footnotesize\begin{verbatim}class MyClass: public QObject, virtual public MyInterface
{
  Q_OBJECT

  public:
    MyClass();
    ~MyClass();

    ASYNC myAsynchronousMethod(QString someParameter);
    QRect mySynchronousMethod();
};
\end{verbatim}
\normalsize
 \begin{Desc}
\item[Note:]({\bf Qt} issue) Remember that if you are inheriting from {\bf QObject}, you must place it first in the list of inherited classes.\end{Desc}
In the implementation of your class' ctor, you must explicitly initialize those classes from which you are inheriting from. This is, of course, good practice, but it is essential here as you need to tell {\bf DCOPObject}{\rm (p.\,\pageref{classDCOPObject})} the name of the interface which your are implementing.

Example:



\footnotesize\begin{verbatim}MyClass::MyClass()
  : QObject(),
    DCOPObject("MyInterface")
{
  // whatever...
}
\end{verbatim}
\normalsize


Now you can simply implement the methods you have declared in your interface, exactly the same as you would normally.

Example:



\footnotesize\begin{verbatim}void MyClass::myAsynchronousMethod(QString someParameter)
{
  qDebug("myAsyncMethod called with param `" + someParameter + "'");
}
\end{verbatim}
\normalsize


It is not necessary (though very clean) to define an interface as an abstract class of its own, like we did in the example above. We could just as well have defined a k\_\-dcop section directly within My\-Class:



\footnotesize\begin{verbatim}class MyClass: public QObject, virtual public DCOPObject
{
  Q_OBJECT
  K_DCOP

  public:
    MyClass();
    ~MyClass();

  k_dcop:
    ASYNC myAsynchronousMethod(QString someParameter);
    QRect mySynchronousMethod();
};
\end{verbatim}
\normalsize


In addition to skeletons, dcopidl2cpp also generate stubs. Those make it easy to call a DCOP interface without doing the marshalling manually. To use a stub, add My\-Interface.stub to the SOURCES list of your Makefile.am. The stub class will then be called My\-Interface\_\-stub.\section{Inter-user communication:}\label{index_iuc}
Sometimes it might be interesting to use DCOP between processes belonging to different users, e.g. a frontend process running with the user's id, and a backend process running as root.

To do this, two steps have to be taken:

a) both processes need to talk to the same DCOP server b) the authentication must be ensured

For the first step, you simply pass the server address (as found in .DCOPserver) to the second process. For the authentication, you can use the ICEAUTHORITY environment variable to tell the second process where to find the authentication information. (Note that this implies that the second process is able to read the authentication file, so it will probably only work if the second process runs as root. If it should run as another user, a similar approach to what kdesu does with xauth must be taken. In fact, it would be a very good idea to add DCOP support to kdesu!)

For example

ICEAUTHORITY=$\sim$user/.ICEauthority kdesu root -c kcmroot -dcopserver `cat $\sim$user/.DCOPserver`

will, after kdesu got the root password, execute kcmroot as root, talking to the user's dcop server.

NOTE: DCOP communication is not encrypted, so please do not pass important information around this way.\section{DCOP Protocol description:}\label{index_protocol}
A DCOPSend message does not expect any reply. 

\footnotesize\begin{verbatim}data: << fromId << toId << objId << fun << dataSize + data[dataSize]
\end{verbatim}
\normalsize


A DCOPCall message can get a {\bf DCOPReply}{\rm (p.\,\pageref{classDCOPReply})}, a DCOPReply\-Failed or a DCOPReply\-Wait message in response. 

\footnotesize\begin{verbatim}data: << fromId << toId << objId << fun << dataSize + data[dataSize]
\end{verbatim}
\normalsize


{\bf DCOPReply}{\rm (p.\,\pageref{classDCOPReply})} is the successful reply to a DCOPCall message 

\footnotesize\begin{verbatim}data: << fromId << toId << replyType << replyDataSize + replyData[replyDataSize]
\end{verbatim}
\normalsize


DCOPReply\-Failed indicates failure of a DCOPCall message 

\footnotesize\begin{verbatim}data: << fromId << toId
\end{verbatim}
\normalsize


DCOPReply\-Wait indicates that a DCOPCall message is successfully being processed but that response will come later. 

\footnotesize\begin{verbatim}data: << fromId << toId << transactionId
\end{verbatim}
\normalsize


DCOPReply\-Delayed is the successful reply to a DCOPCall message after a DCOPReply\-Wait message. 

\footnotesize\begin{verbatim}data: << fromId << toId << transactionId << replyType << replyData
\end{verbatim}
\normalsize


DCOPFind is a message much like a \char`\"{}call\char`\"{} message. It can however be send to multiple objects within a client. If a function in a object that is being called returns a boolean with the value \char`\"{}true\char`\"{}, a {\bf DCOPReply}{\rm (p.\,\pageref{classDCOPReply})} will be send back containing the {\bf DCOPRef}{\rm (p.\,\pageref{classDCOPRef})} of the object who returned \char`\"{}true\char`\"{}.

All c-strings (from\-Id, to\-Id, obj\-Id, fun and reply\-Type), are marshalled with their respective length as 32 bit unsigned integer first: 

\footnotesize\begin{verbatim}data: length + string[length]
\end{verbatim}
\normalsize
 \begin{Desc}
\item[Note:]This happens automatically when using {\bf QCString} on a {\bf QData\-Stream}.\end{Desc}
\section{protection and reentrancy}\label{index_Deadlock}
When a DCOP call is made, the dcop client will be monitoring the dcop connection for the reply on the call. When an incoming call is received in this period, it will normally not be processed but queued until the outgoing call has been fully handled.

However, the above scenario would cause deadlock if the incoming call was directly or indirectly a result of the outgoing call and the reply on the outgoing call is waiting for the result of the incoming call. (E.g. a circular call such as client A calling client B, with client B calling client A)

To prevent deadlock in this case, DCOP has a call tracing mechanism that detects circular calls. When it detects an incoming circular call that would otherwise be queued and as a result cause deadlock, it will handle the incoming call immediately instead of queueing it. This means that the incoming call may be processed at a point in the code where an outgoing DCOP call is made. An application should be aware of this kind of reentrancy. A special case of this is when a DCOP client makes a call to itself, such calls are always handled directly.

Call tracing works by appending a key to each outgoing call. When a client receives an incoming call while waiting for a response on an outgoing call, it will check if the key of the incoming call is equal to the key used for the last outgoing call. If the keys are equal a circular call has been detected.

The key used by clients is 0 if they have not yet received any key. In this case the server will send them back a unique key that they should use in further calls. If a client makes an outgoing call in response to an incoming call it will use the key of the incoming call for the outgoing call instead of the key that was received from the server.

A key value of 1 has a special meaning and is used for non-call messages such as DCOPSend, DCOPReply\-Failed and DCOP signals.

A key value of 2 has a special meaning and is used for priority calls. When a dcop clien is in priority call mode, it will only handle incoming calls that have a key value of 2.

NOTE: If client A and client B would call each other simultaneously there is still a risk of deadlock because both calls would have unique keys and both clients would decide to queue the incoming call until they receive a response on their outgoing call.\section{DCOP Signals:}\label{index_dcop_signals}
Sometimes a component wants to send notifications via DCOP to other components but does not know which components will be interested in these notifications. One could use a broadcast in such a case but this is a very crude method. For a more sophisticated method DCOP signals have been invented.

DCOP signals are very similair to {\bf Qt} signals, there are some differences though. A DCOP signal can be connected to a DCOP function. Whenever the DCOP signal gets emitted, the DCOP functions to which the signal is connected are being called. DCOP signals are, just like {\bf Qt} signals, one way. They do not provide a return value. For declaration of dcop signals, the keyword {\tt k\_\-dcop\_\-signals} is provided. A declaration looks like this:



\footnotesize\begin{verbatim}class Example : virtual public DCOPClient
{
  K_DCOP

  k_dcop:
    // some ordinary dcop methods here
  ...
  k_dcop_signals:
    // our dcop signal
    void clientDied(pid_t pid);
  ...
}
\end{verbatim}
\normalsize


A DCOP signal originates from a DCOP Object/DCOP Client combination (sender). It can be connected to a function of another DCOP Object/DCOP Client combination (receiver).

\begin{Desc}
\item[Note:]There are two major differences between connections of {\bf Qt} signals and connections of DCOP signals. In DCOP, unlike {\bf Qt}, a signal connections can have an anonymous sender and, unlike {\bf Qt}, a DCOP signal connection can be non-volatile.\end{Desc}
With DCOP one can connect a signal without specifying the sending DCOP Object or DCOP Client. In that case signals from any DCOP Object and/or DCOP Client will be delivered. This allows the specification of certain events without tying oneself to a certain object that implementes the events.

Another DCOP feature are so called non-volatile connections. With {\bf Qt} signal connections, the connection gets deleted when either sender or receiver of the signal gets deleted. A volatile DCOP signal connection will behave the same. However, a non-volatile DCOP signal connection will not get deleted when the sending object gets deleted. Once a new object gets created with the same name as the original sending object, the connection will be restored. There is no difference between the two when the receiving object gets deleted, in that case the signal connection will always be deleted.

A receiver can create a non-volatile connection while the sender doesn't (yet) exist. An anonymous DCOP connection should always be non-volatile.

The following example shows how KLauncher emits a signal whenever it notices that an application that was started via KLauncher terminates:



\footnotesize\begin{verbatim}QByteArray params;
QDataStream stream(params, IO_WriteOnly);
stream << pid;
kapp->dcopClient()->emitDCOPSignal("clientDied(pid_t)", params);
\end{verbatim}
\normalsize


The task manager of the {\bf KDE} panel connects to this signal. It uses an anonymous connection (it doesn't require that the signal is being emitted by KLauncher) that is non-volatile:



\footnotesize\begin{verbatim}connectDCOPSignal(0, 0, "clientDied(pid_t)", "clientDied(pid_t)", false);
\end{verbatim}
\normalsize


It connects the client\-Died(pid\_\-t) signal to its own client\-Died(pid\_\-t) DCOP function. In this case the signal and the function to call have the same name. This isn't needed as long as the arguments of both signal and receiving function match. The receiving function may ignore one or more of the trailing arguments of the signal. E.g. it is allowed to connect the client\-Died(pid\_\-t) signal to a client\-Died(void) DCOP function.\section{Conclusion:}\label{index_conclusion}
Hopefully this document will get you well on your way into the world of inter-process communication with KDE! Please direct all comments and/or suggestions to the {\bf KDE} Core Developers List $<${\tt kde-core-devel@kde.org}$>$. 