00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <testdcop.h>
00027 #include <qtimer.h>
00028
00029 #include <assert.h>
00030
00031 DCOPClientTransaction *countDownAction = 0;
00032 int countDownCount = 0;
00033
00034 DCOPClientTransaction *countDownAction2 = 0;
00035 int countDownCount2 = 0;
00036 DCOPClient *client = 0;
00037
00038 bool MyDCOPObject::process(const QCString &fun, const QByteArray &data,
00039 QCString& replyType, QByteArray &replyData)
00040 {
00041 qDebug("in MyDCOPObject::process, fun = %s", fun.data());
00042
00043
00044 if (fun == "aFunction(QString,int)") {
00045 QDataStream args(data, IO_ReadOnly);
00046 QString arg1;
00047 int arg2;
00048 args >> arg1 >> arg2;
00049 function(arg1, arg2);
00050 replyType = "void";
00051 return true;
00052 }
00053 if (fun == "canLaunchRockets(QRect)") {
00054 QDataStream args(data, IO_ReadOnly);
00055 QRect arg1;
00056 args >> arg1;
00057
00058 printf("Rect x = %d, y = %d, w = %d, h = %d\n", arg1.x(), arg1.y(), arg1.width(), arg1.height());
00059
00060 replyType = "QRect";
00061 QDataStream reply( replyData, IO_WriteOnly );
00062 QRect r(10,20,100,200);
00063 reply << r;
00064 return true;
00065 }
00066 if (fun == "isAliveSlot(int)") {
00067
00068 qDebug("isAliveSlot(int)");
00069 bool connectResult = client->disconnectDCOPSignal("", objId(), "", objId(), "" );
00070 qDebug("disconnectDCOPSignal returns %s", connectResult ? "true" : "false");
00071 return true;
00072 }
00073 if (fun == "countDown()") {
00074 qDebug("countDown() countDownAction = %p", countDownAction);
00075 if (countDownAction2)
00076 {
00077 replyType = "QString";
00078 QDataStream reply( replyData, IO_WriteOnly );
00079 reply << QString("Hey");
00080 return true;
00081 }
00082
00083 if (countDownAction == 0)
00084 {
00085 countDownCount = 10;
00086 countDownAction = client->beginTransaction();
00087 QTimer::singleShot(1000, this, SLOT(slotTimeout()));
00088 }
00089 else
00090 {
00091 countDownCount2 = 10;
00092 countDownAction2 = client->beginTransaction();
00093 QTimer::singleShot(1000, this, SLOT(slotTimeout2()));
00094 }
00095 return true;
00096 }
00097
00098 return DCOPObject::process(fun, data, replyType, replyData);
00099 }
00100
00101 void MyDCOPObject::slotTimeout()
00102 {
00103 qDebug("MyDCOPObject::slotTimeout() %d", countDownCount);
00104 countDownCount--;
00105 if (countDownCount == 0)
00106 {
00107 QCString replyType = "QString";
00108 QByteArray replyData;
00109 QDataStream reply( replyData, IO_WriteOnly );
00110 reply << QString("Hello World");
00111 client->endTransaction(countDownAction, replyType, replyData);
00112 countDownAction = 0;
00113 }
00114 else
00115 {
00116 QTimer::singleShot(1000, this, SLOT(slotTimeout()));
00117 }
00118 }
00119
00120 void MyDCOPObject::slotTimeout2()
00121 {
00122 qDebug("MyDCOPObject::slotTimeout2() %d", countDownCount2);
00123 countDownCount2--;
00124 if (countDownCount2 == 0)
00125 {
00126 QCString replyType = "QString";
00127 QByteArray replyData;
00128 QDataStream reply( replyData, IO_WriteOnly );
00129 reply << QString("Hello World");
00130 client->endTransaction(countDownAction2, replyType, replyData);
00131 countDownAction2 = 0;
00132 }
00133 else
00134 {
00135 QTimer::singleShot(1000, this, SLOT(slotTimeout2()));
00136 }
00137 }
00138
00139 QCStringList MyDCOPObject::functions()
00140 {
00141 QCStringList result = DCOPObject::functions();
00142 result << "QRect canLaunchRockets(QRect)";
00143 return result;
00144 }
00145
00146 TestObject::TestObject(const QCString& app)
00147 : m_app(app)
00148 {
00149 QTimer::singleShot(2500, this, SLOT(slotTimeout()));
00150 }
00151
00152 void TestObject::slotTimeout()
00153 {
00154 QCString replyType;
00155 QByteArray data, reply;
00156 qWarning("#3 Calling countDown");
00157
00158 if (!client->call(m_app, "object1", "countDown()", data, replyType, reply))
00159 qDebug("#3 I couldn't call countDown");
00160 else
00161 qDebug("#3 countDown() return type was '%s'", replyType.data() );
00162
00163 }
00164
00165 void TestObject::slotCallBack(int callId, const QCString &replyType, const QByteArray &replyData)
00166 {
00167 qWarning("Call Back! callId = %d", callId);
00168 qWarning("Type = %s", replyType.data());
00169
00170 QDataStream args(replyData, IO_ReadOnly);
00171 QString arg1;
00172 args >> arg1;
00173
00174 qWarning("Value = %s", arg1.latin1());
00175 }
00176
00177 #ifdef Q_OS_WIN
00178 # define main kdemain
00179 #endif
00180
00181 int main(int argc, char **argv)
00182 {
00183 QApplication app(argc, argv, "testdcop");
00184
00185 QCString replyType;
00186 QByteArray data, reply;
00187 client = new DCOPClient();
00188
00189 if (argc == 2)
00190 {
00191 QCString appId = argv[1];
00192 TestObject obj(appId);
00193 qWarning("#1 Calling countDown");
00194 int result = client->callAsync(appId, "object1", "countDown()", data, &obj, SLOT(slotCallBack(int, const QCString&, const QByteArray&)));
00195 qDebug("#1 countDown() call id = %d", result);
00196 qWarning("#2 Calling countDown");
00197 result = client->callAsync(appId, "object1", "countDown()", data, &obj, SLOT(slotCallBack(int, const QCString&, const QByteArray&)));
00198 qDebug("#2 countDown() call id = %d", result);
00199 app.exec();
00200
00201 return 0;
00202 }
00203
00204
00205
00206 client->registerAs( app.name(), false );
00207 qDebug("I registered as '%s'", client->appId().data() );
00208
00209 if ( client->isApplicationRegistered( app.name() ) )
00210 qDebug("indeed, we are registered!");
00211
00212 QDataStream dataStream( data, IO_WriteOnly );
00213 dataStream << (int) 43;
00214 client->emitDCOPSignal("alive(int,QCString)", data);
00215
00216 MyDCOPObject *obj1 = new MyDCOPObject("object1");
00217
00218 bool connectResult = client->connectDCOPSignal("", "alive(int , QCString)", "object1", "isAliveSlot(int)", false);
00219 qDebug("connectDCOPSignal returns %s", connectResult ? "true" : "false");
00220
00221 QDataStream ds(data, IO_WriteOnly);
00222 ds << QString("fourty-two") << 42;
00223 if (!client->call(app.name(), "object1", "aFunction(QString,int)", data, replyType, reply)) {
00224 qDebug("I couldn't call myself");
00225 assert( 0 );
00226 }
00227 else {
00228 qDebug("return type was '%s'", replyType.data() );
00229 assert( replyType == "void" );
00230 }
00231
00232 client->send(app.name(), "object1", "aFunction(QString,int)", data );
00233
00234 int n = client->registeredApplications().count();
00235 qDebug("number of attached applications = %d", n );
00236
00237 QObject::connect( client, SIGNAL( applicationRegistered( const QCString&)),
00238 obj1, SLOT( registered( const QCString& )));
00239
00240 QObject::connect( client, SIGNAL( applicationRemoved( const QCString&)),
00241 obj1, SLOT( unregistered( const QCString& )));
00242
00243
00244 client->setNotifications( true );
00245
00246 QCString foundApp;
00247 QCString foundObj;
00248
00249
00250
00251
00252
00253
00254
00255
00256 bool boolResult = client->findObject( "konqueror*", "", "", data, foundApp, foundObj);
00257 qDebug("findObject: result = %s, %s, %s\n", boolResult ? "true" : "false",
00258 foundApp.data(), foundObj.data());
00259
00260
00261 boolResult = client->findObject( "", "ksycoca", "", data, foundApp, foundObj);
00262 qDebug("findObject: result = %s, %s, %s\n", boolResult ? "true" : "false",
00263 foundApp.data(), foundObj.data());
00264
00265
00266 boolResult = client->findObject( "testdcop", "ksycoca", "", data, foundApp, foundObj);
00267 qDebug("findObject: result = %s, %s, %s\n", boolResult ? "true" : "false",
00268 foundApp.data(), foundObj.data());
00269
00270 DCOPClient *client2 = new DCOPClient();
00271 client2->registerAs(app.name(), false);
00272 qDebug("I2 registered as '%s'", client2->appId().data() );
00273
00274 qDebug("Sending to object1");
00275 client2->send(app.name(), "object1", "aFunction(QString,int)", data );
00276
00277 qDebug("Calling object1");
00278 if (!client2->call(app.name(), "object1", "aFunction(QString,int)", data, replyType, reply))
00279 qDebug("I couldn't call myself");
00280 else
00281 qDebug("return type was '%s'", replyType.data() );
00282
00283 qDebug("Calling countDown() in object1");
00284 if (!client2->call(app.name(), "object1", "countDown()", data, replyType, reply))
00285 qDebug("I couldn't call myself");
00286 else
00287 qDebug("return type was '%s'", replyType.data() );
00288
00289
00290 boolResult = client2->findObject( "testdcop", "object1", "", data, foundApp, foundObj);
00291 qDebug("findObject: result = %s, %s, %s\n", boolResult ? "true" : "false",
00292 foundApp.data(), foundObj.data());
00293
00294 client->detach();
00295 return 0;
00296 }
00297
00298 #include "testdcop.moc"