• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • dcop
 

dcop

  • dcop
testdcop.cpp
1/*****************************************************************
2
3Copyright (c) 1999 Preston Brown <pbrown@kde.org>
4Copyright (c) 1999 Matthias Ettrich <ettrich@kde.org>
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in
14all copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23******************************************************************
24*/
25
26#include <testdcop.h>
27#include <tqtimer.h>
28
29#include <assert.h>
30
31DCOPClientTransaction *countDownAction = 0;
32int countDownCount = 0;
33
34DCOPClientTransaction *countDownAction2 = 0;
35int countDownCount2 = 0;
36DCOPClient *client = 0;
37
38bool MyDCOPObject::process(const TQCString &fun, const TQByteArray &data,
39 TQCString& replyType, TQByteArray &replyData)
40{
41 tqDebug("in MyDCOPObject::process, fun = %s", fun.data());
42
43 // note "fun" is normlized here (i.e. whitespace clean)
44 if (fun == "aFunction(TQString,int)") {
45 TQDataStream args(data, IO_ReadOnly);
46 TQString arg1;
47 int arg2;
48 args >> arg1 >> arg2;
49 function(arg1, arg2);
50 replyType = "void";
51 return true;
52 }
53 if (fun == "canLaunchRockets(TQRect)") {
54 TQDataStream args(data, IO_ReadOnly);
55 TQRect arg1;
56 args >> arg1;
57
58 printf("Rect x = %d, y = %d, w = %d, h = %d\n", arg1.x(), arg1.y(), arg1.width(), arg1.height());
59
60 replyType = "TQRect";
61 TQDataStream reply( replyData, IO_WriteOnly );
62 TQRect r(10,20,100,200);
63 reply << r;
64 return true;
65 }
66 if (fun == "isAliveSlot(int)") {
67
68 tqDebug("isAliveSlot(int)");
69 bool connectResult = client->disconnectDCOPSignal("", objId(), "", objId(), "" );
70 tqDebug("disconnectDCOPSignal returns %s", connectResult ? "true" : "false");
71 return true;
72 }
73 if (fun == "countDown()") {
74tqDebug("countDown() countDownAction = %p", countDownAction);
75 if (countDownAction2)
76 {
77 replyType = "TQString";
78 TQDataStream reply( replyData, IO_WriteOnly );
79 reply << TQString("Hey");
80 return true;
81 }
82
83 if (countDownAction == 0)
84 {
85 countDownCount = 10;
86 countDownAction = client->beginTransaction();
87 TQTimer::singleShot(1000, this, TQ_SLOT(slotTimeout()));
88 }
89 else
90 {
91 countDownCount2 = 10;
92 countDownAction2 = client->beginTransaction();
93 TQTimer::singleShot(1000, this, TQ_SLOT(slotTimeout2()));
94 }
95 return true;
96 }
97
98 return DCOPObject::process(fun, data, replyType, replyData);
99}
100
101void MyDCOPObject::slotTimeout()
102{
103 tqDebug("MyDCOPObject::slotTimeout() %d", countDownCount);
104 countDownCount--;
105 if (countDownCount == 0)
106 {
107 TQCString replyType = "TQString";
108 TQByteArray replyData;
109 TQDataStream reply( replyData, IO_WriteOnly );
110 reply << TQString("Hello World");
111 client->endTransaction(countDownAction, replyType, replyData);
112 countDownAction = 0;
113 }
114 else
115 {
116 TQTimer::singleShot(1000, this, TQ_SLOT(slotTimeout()));
117 }
118}
119
120void MyDCOPObject::slotTimeout2()
121{
122 tqDebug("MyDCOPObject::slotTimeout2() %d", countDownCount2);
123 countDownCount2--;
124 if (countDownCount2 == 0)
125 {
126 TQCString replyType = "TQString";
127 TQByteArray replyData;
128 TQDataStream reply( replyData, IO_WriteOnly );
129 reply << TQString("Hello World");
130 client->endTransaction(countDownAction2, replyType, replyData);
131 countDownAction2 = 0;
132 }
133 else
134 {
135 TQTimer::singleShot(1000, this, TQ_SLOT(slotTimeout2()));
136 }
137}
138
139QCStringList MyDCOPObject::functions()
140{
141 QCStringList result = DCOPObject::functions();
142 result << "TQRect canLaunchRockets(TQRect)";
143 return result;
144}
145
146TestObject::TestObject(const TQCString& app)
147 : m_app(app)
148{
149 TQTimer::singleShot(2500, this, TQ_SLOT(slotTimeout()));
150}
151
152void TestObject::slotTimeout()
153{
154 TQCString replyType;
155 TQByteArray data, reply;
156 tqWarning("#3 Calling countDown");
157
158 if (!client->call(m_app, "object1", "countDown()", data, replyType, reply))
159 tqDebug("#3 I couldn't call countDown");
160 else
161 tqDebug("#3 countDown() return type was '%s'", replyType.data() );
162
163}
164
165void TestObject::slotCallBack(int callId, const TQCString &replyType, const TQByteArray &replyData)
166{
167 tqWarning("Call Back! callId = %d", callId);
168 tqWarning("Type = %s", replyType.data());
169
170 TQDataStream args(replyData, IO_ReadOnly);
171 TQString arg1;
172 args >> arg1;
173
174 tqWarning("Value = %s", arg1.latin1());
175}
176
177#ifdef Q_OS_WIN
178# define main kdemain
179#endif
180
181int main(int argc, char **argv)
182{
183 TQApplication app(argc, argv, "testdcop");
184
185 TQCString replyType;
186 TQByteArray data, reply;
187 client = new DCOPClient();
188
189 if (argc == 2)
190 {
191 TQCString appId = argv[1];
192 TestObject obj(appId);
193 tqWarning("#1 Calling countDown");
194 int result = client->callAsync(appId, "object1", "countDown()", data, &obj, TQ_SLOT(slotCallBack(int, const TQCString&, const TQByteArray&)));
195 tqDebug("#1 countDown() call id = %d", result);
196 tqWarning("#2 Calling countDown");
197 result = client->callAsync(appId, "object1", "countDown()", data, &obj, TQ_SLOT(slotCallBack(int, const TQCString&, const TQByteArray&)));
198 tqDebug("#2 countDown() call id = %d", result);
199 app.exec();
200
201 return 0;
202 }
203
204// client->attach(); // attach to the server, now we can use DCOP service
205
206 client->registerAs( app.name(), false ); // register at the server, now others can call us.
207 tqDebug("I registered as '%s'", client->appId().data() );
208
209 if ( client->isApplicationRegistered( app.name() ) )
210 tqDebug("indeed, we are registered!");
211
212 TQDataStream dataStream( data, IO_WriteOnly );
213 dataStream << (int) 43;
214 client->emitDCOPSignal("alive(int,TQCString)", data);
215
216 MyDCOPObject *obj1 = new MyDCOPObject("object1");
217
218 bool connectResult = client->connectDCOPSignal("", "alive(int , TQCString)", "object1", "isAliveSlot(int)", false);
219 tqDebug("connectDCOPSignal returns %s", connectResult ? "true" : "false");
220
221 TQDataStream ds(data, IO_WriteOnly);
222 ds << TQString("fourty-two") << 42;
223 if (!client->call(app.name(), "object1", "aFunction(TQString,int)", data, replyType, reply)) {
224 tqDebug("I couldn't call myself");
225 assert( 0 );
226 }
227 else {
228 tqDebug("return type was '%s'", replyType.data() );
229 assert( replyType == "void" );
230 }
231
232 client->send(app.name(), "object1", "aFunction(TQString,int)", data );
233
234 int n = client->registeredApplications().count();
235 tqDebug("number of attached applications = %d", n );
236
237 TQObject::connect( client, TQ_SIGNAL( applicationRegistered( const TQCString&)),
238 obj1, TQ_SLOT( registered( const TQCString& )));
239
240 TQObject::connect( client, TQ_SIGNAL( applicationRemoved( const TQCString&)),
241 obj1, TQ_SLOT( unregistered( const TQCString& )));
242
243 // Enable the above signals
244 client->setNotifications( true );
245
246 TQCString foundApp;
247 TQCString foundObj;
248
249 // Find a object called "object1" in any application that
250 // meets the criteria "canLaunchRockets()"
251// bool boolResult = client->findObject( "", "object1", "canLaunchRockets()", data, foundApp, foundObj);
252// tqDebug("findObject: result = %s, %s, %s\n", boolResult ? "true" : "false",
253// foundApp.data(), foundObj.data());
254
255 // Find an application that matches with "konqueror*"
256 bool boolResult = client->findObject( "konqueror*", "", "", data, foundApp, foundObj);
257 tqDebug("findObject: result = %s, %s, %s\n", boolResult ? "true" : "false",
258 foundApp.data(), foundObj.data());
259
260 // Find an object called "object1" in any application.
261 boolResult = client->findObject( "", "tdesycoca", "", data, foundApp, foundObj);
262 tqDebug("findObject: result = %s, %s, %s\n", boolResult ? "true" : "false",
263 foundApp.data(), foundObj.data());
264
265 // Find ourselves in any application.
266 boolResult = client->findObject( "testdcop", "tdesycoca", "", data, foundApp, foundObj);
267 tqDebug("findObject: result = %s, %s, %s\n", boolResult ? "true" : "false",
268 foundApp.data(), foundObj.data());
269
270 DCOPClient *client2 = new DCOPClient();
271 client2->registerAs(app.name(), false);
272 tqDebug("I2 registered as '%s'", client2->appId().data() );
273
274tqDebug("Sending to object1");
275 client2->send(app.name(), "object1", "aFunction(TQString,int)", data );
276
277tqDebug("Calling object1");
278 if (!client2->call(app.name(), "object1", "aFunction(TQString,int)", data, replyType, reply))
279 tqDebug("I couldn't call myself");
280 else
281 tqDebug("return type was '%s'", replyType.data() );
282
283tqDebug("Calling countDown() in object1");
284 if (!client2->call(app.name(), "object1", "countDown()", data, replyType, reply))
285 tqDebug("I couldn't call myself");
286 else
287 tqDebug("return type was '%s'", replyType.data() );
288
289 // Find ourselves in any application.
290 boolResult = client2->findObject( "testdcop", "object1", "", data, foundApp, foundObj);
291 tqDebug("findObject: result = %s, %s, %s\n", boolResult ? "true" : "false",
292 foundApp.data(), foundObj.data());
293
294 client->detach();
295 return 0;
296}
297
298#include "testdcop.moc"
DCOPClient
Inter-process communication and remote procedure calls for KDE applications.
Definition: dcopclient.h:69
DCOPClient::endTransaction
void endTransaction(DCOPClientTransaction *t, TQCString &replyType, TQByteArray &replyData)
Sends the delayed reply of a function call.
Definition: dcopclient.cpp:2129
DCOPClient::appId
TQCString appId() const
Returns the current app id or a null string if the application hasn't yet been registered.
Definition: dcopclient.cpp:1036
DCOPClient::detach
bool detach()
Detaches from the DCOP server.
Definition: dcopclient.cpp:926
DCOPClient::beginTransaction
DCOPClientTransaction * beginTransaction()
Delays the reply of the current function call until endTransaction() is called.
Definition: dcopclient.cpp:2099
DCOPClient::isApplicationRegistered
bool isApplicationRegistered(const TQCString &remApp)
Checks whether remApp is registered with the DCOP server.
Definition: dcopclient.cpp:1262
DCOPClient::emitDCOPSignal
void emitDCOPSignal(const TQCString &object, const TQCString &signal, const TQByteArray &data)
Emits signal as DCOP signal from object object with data as arguments.
Definition: dcopclient.cpp:2179
DCOPClient::registeredApplications
QCStringList registeredApplications()
Retrieves the list of all currently registered applications from dcopserver.
Definition: dcopclient.cpp:1276
DCOPClient::disconnectDCOPSignal
bool disconnectDCOPSignal(const TQCString &sender, const TQCString &senderObj, const TQCString &signal, const TQCString &receiverObj, const TQCString &slot)
Disconnects a DCOP signal.
Definition: dcopclient.cpp:2227
DCOPClient::send
bool send(const TQCString &remApp, const TQCString &remObj, const TQCString &remFun, const TQByteArray &data)
Sends a data block to the server.
Definition: dcopclient.cpp:1089
DCOPClient::callAsync
int callAsync(const TQCString &remApp, const TQCString &remObj, const TQCString &remFun, const TQByteArray &data, TQObject *callBackObj, const char *callBackSlot)
Performs a asynchronous send with receive callback.
Definition: dcopclient.cpp:1830
DCOPClient::call
bool call(const TQCString &remApp, const TQCString &remObj, const TQCString &remFun, const TQByteArray &data, TQCString &replyType, TQByteArray &replyData, bool useEventLoop, int timeout, bool forceRemote)
Performs a synchronous send and receive.
Definition: dcopclient.cpp:1786
DCOPClient::setNotifications
void setNotifications(bool enabled)
Enables / disables the applicationRegistered() / applicationRemoved() signals.
Definition: dcopclient.cpp:1336
DCOPClient::findObject
bool findObject(const TQCString &remApp, const TQCString &remObj, const TQCString &remFun, const TQByteArray &data, TQCString &foundApp, TQCString &foundObj, bool useEventLoop, int timeout)
Searches for an object which matches a criteria.
Definition: dcopclient.cpp:1160
DCOPClient::connectDCOPSignal
bool connectDCOPSignal(const TQCString &sender, const TQCString &senderObj, const TQCString &signal, const TQCString &receiverObj, const TQCString &slot, bool Volatile)
Connects to a DCOP signal.
Definition: dcopclient.cpp:2192
DCOPClient::registerAs
TQCString registerAs(const TQCString &appId, bool addPID=true)
Registers at the DCOP server.
Definition: dcopclient.cpp:983
DCOPObject::functions
virtual QCStringList functions()
Returns the list of functions understood by the object.
Definition: dcopobject.cpp:205
DCOPObject::process
virtual bool process(const TQCString &fun, const TQByteArray &data, TQCString &replyType, TQByteArray &replyData)
Dispatches a message.
Definition: dcopobject.cpp:166
TestObject
$TQTDIR/bin/moc testdcop.cpp -o testdcop.moc g++ -o testdcop testdcop.cpp -I$TQTDIR/include -L$TQTDIR...
Definition: testdcop.h:45

dcop

Skip menu "dcop"
  • Main Page
  • Modules
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

dcop

Skip menu "dcop"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for dcop by doxygen 1.9.4
This website is maintained by Timothy Pearson.