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 <tqtimer.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 TQCString &fun, const TQByteArray &data,
00039 TQCString& replyType, TQByteArray &replyData)
00040 {
00041 tqDebug("in MyDCOPObject::process, fun = %s", fun.data());
00042
00043
00044 if (fun == "aFunction(TQString,int)") {
00045 TQDataStream args(data, IO_ReadOnly);
00046 TQString arg1;
00047 int arg2;
00048 args >> arg1 >> arg2;
00049 function(arg1, arg2);
00050 replyType = "void";
00051 return true;
00052 }
00053 if (fun == "canLaunchRockets(TQRect)") {
00054 TQDataStream args(data, IO_ReadOnly);
00055 TQRect 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 = TQRECT_OBJECT_NAME_STRING;
00061 TQDataStream reply( replyData, IO_WriteOnly );
00062 TQRect r(10,20,100,200);
00063 reply << r;
00064 return true;
00065 }
00066 if (fun == "isAliveSlot(int)") {
00067
00068 tqDebug("isAliveSlot(int)");
00069 bool connectResult = client->disconnectDCOPSignal("", objId(), "", objId(), "" );
00070 tqDebug("disconnectDCOPSignal returns %s", connectResult ? "true" : "false");
00071 return true;
00072 }
00073 if (fun == "countDown()") {
00074 tqDebug("countDown() countDownAction = %p", countDownAction);
00075 if (countDownAction2)
00076 {
00077 replyType = TQSTRING_OBJECT_NAME_STRING;
00078 TQDataStream reply( replyData, IO_WriteOnly );
00079 reply << TQString("Hey");
00080 return true;
00081 }
00082
00083 if (countDownAction == 0)
00084 {
00085 countDownCount = 10;
00086 countDownAction = client->beginTransaction();
00087 TQTimer::singleShot(1000, this, TQT_SLOT(slotTimeout()));
00088 }
00089 else
00090 {
00091 countDownCount2 = 10;
00092 countDownAction2 = client->beginTransaction();
00093 TQTimer::singleShot(1000, this, TQT_SLOT(slotTimeout2()));
00094 }
00095 return true;
00096 }
00097
00098 return DCOPObject::process(fun, data, replyType, replyData);
00099 }
00100
00101 void MyDCOPObject::slotTimeout()
00102 {
00103 tqDebug("MyDCOPObject::slotTimeout() %d", countDownCount);
00104 countDownCount--;
00105 if (countDownCount == 0)
00106 {
00107 TQCString replyType = TQSTRING_OBJECT_NAME_STRING;
00108 TQByteArray replyData;
00109 TQDataStream reply( replyData, IO_WriteOnly );
00110 reply << TQString("Hello World");
00111 client->endTransaction(countDownAction, replyType, replyData);
00112 countDownAction = 0;
00113 }
00114 else
00115 {
00116 TQTimer::singleShot(1000, this, TQT_SLOT(slotTimeout()));
00117 }
00118 }
00119
00120 void MyDCOPObject::slotTimeout2()
00121 {
00122 tqDebug("MyDCOPObject::slotTimeout2() %d", countDownCount2);
00123 countDownCount2--;
00124 if (countDownCount2 == 0)
00125 {
00126 TQCString replyType = TQSTRING_OBJECT_NAME_STRING;
00127 TQByteArray replyData;
00128 TQDataStream reply( replyData, IO_WriteOnly );
00129 reply << TQString("Hello World");
00130 client->endTransaction(countDownAction2, replyType, replyData);
00131 countDownAction2 = 0;
00132 }
00133 else
00134 {
00135 TQTimer::singleShot(1000, this, TQT_SLOT(slotTimeout2()));
00136 }
00137 }
00138
00139 QCStringList MyDCOPObject::functions()
00140 {
00141 QCStringList result = DCOPObject::functions();
00142 result << TQRECT_OBJECT_NAME_STRING " canLaunchRockets(" TQRECT_OBJECT_NAME_STRING ")";
00143 return result;
00144 }
00145
00146 TestObject::TestObject(const TQCString& app)
00147 : m_app(app)
00148 {
00149 TQTimer::singleShot(2500, this, TQT_SLOT(slotTimeout()));
00150 }
00151
00152 void TestObject::slotTimeout()
00153 {
00154 TQCString replyType;
00155 TQByteArray data, reply;
00156 tqWarning("#3 Calling countDown");
00157
00158 if (!client->call(m_app, "object1", "countDown()", data, replyType, reply))
00159 tqDebug("#3 I couldn't call countDown");
00160 else
00161 tqDebug("#3 countDown() return type was '%s'", replyType.data() );
00162
00163 }
00164
00165 void TestObject::slotCallBack(int callId, const TQCString &replyType, const TQByteArray &replyData)
00166 {
00167 tqWarning("Call Back! callId = %d", callId);
00168 tqWarning("Type = %s", replyType.data());
00169
00170 TQDataStream args(replyData, IO_ReadOnly);
00171 TQString arg1;
00172 args >> arg1;
00173
00174 tqWarning("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 TQApplication app(argc, argv, "testdcop");
00184
00185 TQCString replyType;
00186 TQByteArray data, reply;
00187 client = new DCOPClient();
00188
00189 if (argc == 2)
00190 {
00191 TQCString appId = argv[1];
00192 TestObject obj(appId);
00193 tqWarning("#1 Calling countDown");
00194 int result = client->callAsync(appId, "object1", "countDown()", data, &obj, TQT_SLOT(slotCallBack(int, const TQCString&, const TQByteArray&)));
00195 tqDebug("#1 countDown() call id = %d", result);
00196 tqWarning("#2 Calling countDown");
00197 result = client->callAsync(appId, "object1", "countDown()", data, &obj, TQT_SLOT(slotCallBack(int, const TQCString&, const TQByteArray&)));
00198 tqDebug("#2 countDown() call id = %d", result);
00199 app.exec();
00200
00201 return 0;
00202 }
00203
00204
00205
00206 client->registerAs( app.name(), false );
00207 tqDebug("I registered as '%s'", client->appId().data() );
00208
00209 if ( client->isApplicationRegistered( app.name() ) )
00210 tqDebug("indeed, we are registered!");
00211
00212 TQDataStream dataStream( data, IO_WriteOnly );
00213 dataStream << (int) 43;
00214 client->emitDCOPSignal("alive(int,TQCString)", data);
00215
00216 MyDCOPObject *obj1 = new MyDCOPObject("object1");
00217
00218 bool connectResult = client->connectDCOPSignal("", "alive(int , TQCString)", "object1", "isAliveSlot(int)", false);
00219 tqDebug("connectDCOPSignal returns %s", connectResult ? "true" : "false");
00220
00221 TQDataStream ds(data, IO_WriteOnly);
00222 ds << TQString("fourty-two") << 42;
00223 if (!client->call(app.name(), "object1", "aFunction(TQString,int)", data, replyType, reply)) {
00224 tqDebug("I couldn't call myself");
00225 assert( 0 );
00226 }
00227 else {
00228 tqDebug("return type was '%s'", replyType.data() );
00229 assert( replyType == "void" );
00230 }
00231
00232 client->send(app.name(), "object1", "aFunction(TQString,int)", data );
00233
00234 int n = client->registeredApplications().count();
00235 tqDebug("number of attached applications = %d", n );
00236
00237 TQObject::connect( client, TQT_SIGNAL( applicationRegistered( const TQCString&)),
00238 obj1, TQT_SLOT( registered( const TQCString& )));
00239
00240 TQObject::connect( client, TQT_SIGNAL( applicationRemoved( const TQCString&)),
00241 obj1, TQT_SLOT( unregistered( const TQCString& )));
00242
00243
00244 client->setNotifications( true );
00245
00246 TQCString foundApp;
00247 TQCString foundObj;
00248
00249
00250
00251
00252
00253
00254
00255
00256 bool boolResult = client->findObject( "konqueror*", "", "", data, foundApp, foundObj);
00257 tqDebug("findObject: result = %s, %s, %s\n", boolResult ? "true" : "false",
00258 foundApp.data(), foundObj.data());
00259
00260
00261 boolResult = client->findObject( "", "tdesycoca", "", data, foundApp, foundObj);
00262 tqDebug("findObject: result = %s, %s, %s\n", boolResult ? "true" : "false",
00263 foundApp.data(), foundObj.data());
00264
00265
00266 boolResult = client->findObject( "testdcop", "tdesycoca", "", data, foundApp, foundObj);
00267 tqDebug("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 tqDebug("I2 registered as '%s'", client2->appId().data() );
00273
00274 tqDebug("Sending to object1");
00275 client2->send(app.name(), "object1", "aFunction(TQString,int)", data );
00276
00277 tqDebug("Calling object1");
00278 if (!client2->call(app.name(), "object1", "aFunction(TQString,int)", data, replyType, reply))
00279 tqDebug("I couldn't call myself");
00280 else
00281 tqDebug("return type was '%s'", replyType.data() );
00282
00283 tqDebug("Calling countDown() in object1");
00284 if (!client2->call(app.name(), "object1", "countDown()", data, replyType, reply))
00285 tqDebug("I couldn't call myself");
00286 else
00287 tqDebug("return type was '%s'", replyType.data() );
00288
00289
00290 boolResult = client2->findObject( "testdcop", "object1", "", data, foundApp, foundObj);
00291 tqDebug("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"