dcop Library API Documentation

testdcop.cpp

00001 /*****************************************************************
00002 
00003 Copyright (c) 1999 Preston Brown <pbrown@kde.org>
00004 Copyright (c) 1999 Matthias Ettrich <ettrich@kde.org>
00005  
00006 Permission is hereby granted, free of charge, to any person obtaining a copy
00007 of this software and associated documentation files (the "Software"), to deal
00008 in the Software without restriction, including without limitation the rights
00009 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00010 copies of the Software, and to permit persons to whom the Software is
00011 furnished to do so, subject to the following conditions:
00012  
00013 The above copyright notice and this permission notice shall be included in
00014 all copies or substantial portions of the Software.
00015  
00016 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
00019 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00020 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00021 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00022  
00023 ******************************************************************
00024 */
00025 
00026 #include <testdcop.h>
00027 #include <qtimer.h>
00028 
00029 DCOPClientTransaction *countDownAction = 0;
00030 int countDownCount = 0;
00031 
00032 DCOPClientTransaction *countDownAction2 = 0;
00033 int countDownCount2 = 0;
00034 
00035 bool MyDCOPObject::process(const QCString &fun, const QByteArray &data,
00036                QCString& replyType, QByteArray &replyData)
00037 {
00038   qDebug("in MyDCOPObject::process, fun = %s", fun.data());
00039   
00040   // note "fun" is normlized here (i.e. whitespace clean)
00041   if (fun == "aFunction(QString,int)") {
00042     QDataStream args(data, IO_ReadOnly);
00043     QString arg1;
00044     int arg2;
00045     args >> arg1 >> arg2;
00046     function(arg1, arg2);
00047     replyType = "void";
00048     return true;
00049   }
00050   if (fun == "canLaunchRockets(QRect)") {
00051     QDataStream args(data, IO_ReadOnly);
00052     QRect arg1;
00053     args >> arg1;
00054 
00055     printf("Rect x = %d, y = %d, w = %d, h = %d\n", arg1.x(), arg1.y(), arg1.width(), arg1.height());
00056 
00057     replyType = "QRect";
00058     QDataStream reply( replyData, IO_WriteOnly );
00059     QRect r(10,20,100,200);
00060     reply << r;
00061     return true;
00062   }
00063   if (fun == "isAliveSlot(int)") {
00064     
00065     qDebug("isAliveSlot(int)");
00066     bool connectResult = kapp->dcopClient()->disconnectDCOPSignal("", objId(), "", objId(), "" );
00067     qDebug("disconnectDCOPSignal returns %s", connectResult ? "true" : "false");
00068     return true;
00069   }
00070   if (fun == "countDown()") {
00071 qDebug("countDown() countDownAction = %p", countDownAction);
00072     if (countDownAction2)
00073     {
00074        replyType = "QString";
00075        QDataStream reply( replyData, IO_WriteOnly );
00076        reply << QString("Hey");
00077        return true;
00078     }
00079 
00080     if (countDownAction == 0)
00081     {
00082        countDownCount = 10;       
00083        countDownAction = kapp->dcopClient()->beginTransaction();
00084        QTimer::singleShot(1000, this, SLOT(slotTimeout()));
00085     }
00086     else
00087     {
00088        countDownCount2 = 10;       
00089        countDownAction2 = kapp->dcopClient()->beginTransaction();
00090        QTimer::singleShot(1000, this, SLOT(slotTimeout2()));
00091     }
00092     return true;
00093   }
00094 
00095   return DCOPObject::process(fun, data, replyType, replyData);
00096 }
00097 
00098 void MyDCOPObject::slotTimeout()
00099 {
00100   qDebug("MyDCOPObject::slotTimeout() %d", countDownCount);
00101   countDownCount--;
00102   if (countDownCount == 0)
00103   {
00104      QCString replyType = "QString";
00105      QByteArray replyData;
00106      QDataStream reply( replyData, IO_WriteOnly );
00107      reply << QString("Hello World");
00108      kapp->dcopClient()->endTransaction(countDownAction, replyType, replyData);
00109      countDownAction = 0;
00110   }
00111   else
00112   {
00113      QTimer::singleShot(1000, this, SLOT(slotTimeout()));
00114   }
00115 }
00116 
00117 void MyDCOPObject::slotTimeout2()
00118 {
00119   qDebug("MyDCOPObject::slotTimeout2() %d", countDownCount2);
00120   countDownCount2--;
00121   if (countDownCount2 == 0)
00122   {
00123      QCString replyType = "QString";
00124      QByteArray replyData;
00125      QDataStream reply( replyData, IO_WriteOnly );
00126      reply << QString("Hello World");
00127      kapp->dcopClient()->endTransaction(countDownAction2, replyType, replyData);
00128      countDownAction2 = 0;
00129   }
00130   else
00131   {
00132      QTimer::singleShot(1000, this, SLOT(slotTimeout2()));
00133   }
00134 }
00135 
00136 QCStringList MyDCOPObject::functions()
00137 {
00138    QCStringList result = DCOPObject::functions();
00139    result << "QRect canLaunchRockets(QRect)";
00140    return result;
00141 }
00142 
00143 TestObject::TestObject(const QCString& app)
00144  :  m_app(app)
00145 {
00146    QTimer::singleShot(2500, this, SLOT(slotTimeout()));
00147 }
00148 
00149 void TestObject::slotTimeout()
00150 {
00151    QCString replyType;
00152    QByteArray data, reply;
00153    qWarning("#3 Calling countDown");
00154 
00155    if (!kapp->dcopClient()->call(m_app, "object1", "countDown()", data, replyType, reply))
00156       qDebug("#3 I couldn't call countDown");
00157    else
00158       qDebug("#3 countDown() return type was '%s'", replyType.data() ); 
00159    
00160 }
00161 
00162 void TestObject::slotCallBack(int callId, const QCString &replyType, const QByteArray &replyData)
00163 {
00164    qWarning("Call Back! callId = %d", callId);
00165    qWarning("Type = %s", replyType.data());
00166    
00167    QDataStream args(replyData, IO_ReadOnly);
00168    QString arg1;
00169    args >> arg1;
00170    
00171    qWarning("Value = %s", arg1.latin1());
00172 }
00173 
00174 int main(int argc, char **argv)
00175 {
00176   KApplication app(argc, argv, "testdcop");
00177 
00178   QCString replyType;
00179   QByteArray data, reply;
00180   DCOPClient *client; client = app.dcopClient();
00181 
00182   if (argc == 2)
00183   {
00184       QCString app = argv[1];
00185       TestObject obj(app);
00186       qWarning("#1 Calling countDown");
00187       int result = kapp->dcopClient()->callAsync(app, "object1", "countDown()", data, &obj, SLOT(slotCallBack(int, const QCString&, const QByteArray&)));
00188       qDebug("#1 countDown() call id = %d", result);
00189       qWarning("#2 Calling countDown");
00190       result = kapp->dcopClient()->callAsync(app, "object1", "countDown()", data, &obj, SLOT(slotCallBack(int, const QCString&, const QByteArray&)));
00191       qDebug("#2 countDown() call id = %d", result);
00192       kapp->exec();
00193     
00194       return 0;
00195   }
00196 
00197 //  client->attach(); // attach to the server, now we can use DCOP service
00198 
00199   client->registerAs( app.name(), false ); // register at the server, now others can call us.
00200   qDebug("I registered as '%s'", client->appId().data() );
00201 
00202   if ( client->isApplicationRegistered( app.name() ) )
00203       qDebug("indeed, we are registered!");
00204 
00205   QDataStream dataStream( data, IO_WriteOnly );
00206   dataStream << (int) 43;
00207   client->emitDCOPSignal("alive(int,QCString)", data);
00208 
00209   MyDCOPObject *obj1 = new MyDCOPObject("object1");
00210 
00211   bool connectResult = client->connectDCOPSignal("", "alive(int , QCString)", "object1", "isAliveSlot(int)", false);
00212   qDebug("connectDCOPSignal returns %s", connectResult ? "true" : "false");
00213 
00214   QDataStream ds(data, IO_WriteOnly);
00215   ds << QString("fourty-two") << 42;
00216   if (!client->call(app.name(), "object1", "aFunction(QString,int)", data, replyType, reply))
00217     qDebug("I couldn't call myself");
00218   else
00219       qDebug("return type was '%s'", replyType.data() ); 
00220 
00221   client->send(app.name(), "object1", "aFunction(QString,int)", data );
00222 
00223   int n = client->registeredApplications().count();
00224   qDebug("number of attached applications = %d", n );
00225 
00226   QObject::connect( client, SIGNAL( applicationRegistered( const QCString&)),
00227                     obj1, SLOT( registered( const QCString& )));
00228 
00229   QObject::connect( client, SIGNAL( applicationRemoved( const QCString&)),
00230                     obj1, SLOT( unregistered( const QCString& )));
00231 
00232   // Enable the above signals
00233   client->setNotifications( true );
00234 
00235   QCString foundApp;
00236   QCString foundObj;
00237 
00238   // Find a object called "object1" in any application that
00239   // meets the criteria "canLaunchRockets()"
00240 //  bool boolResult = client->findObject( "", "object1", "canLaunchRockets()", data, foundApp, foundObj);
00241 //  qDebug("findObject: result = %s, %s, %s\n", boolResult ? "true" : "false",
00242 //  foundApp.data(), foundObj.data());
00243 
00244   // Find an application that matches with "konqueror*"
00245   bool boolResult = client->findObject( "konqueror*", "", "", data, foundApp, foundObj);
00246   qDebug("findObject: result = %s, %s, %s\n", boolResult ? "true" : "false",
00247     foundApp.data(), foundObj.data());
00248 
00249   // Find an object called "object1" in any application.
00250   boolResult = client->findObject( "", "ksycoca", "", data, foundApp, foundObj);
00251   qDebug("findObject: result = %s, %s, %s\n", boolResult ? "true" : "false",
00252     foundApp.data(), foundObj.data());
00253 
00254   // Find ourselves in any application.
00255   boolResult = client->findObject( "testdcop", "ksycoca", "", data, foundApp, foundObj);
00256   qDebug("findObject: result = %s, %s, %s\n", boolResult ? "true" : "false",
00257     foundApp.data(), foundObj.data());
00258 
00259   DCOPClient *client2 = new DCOPClient();
00260   client2->registerAs(app.name(), false);
00261   qDebug("I2 registered as '%s'", client2->appId().data() );
00262 
00263 qDebug("Sending to object1");
00264   client2->send(app.name(), "object1", "aFunction(QString,int)", data );
00265 
00266 qDebug("Calling object1");
00267   if (!client2->call(app.name(), "object1", "aFunction(QString,int)", data, replyType, reply))
00268     qDebug("I couldn't call myself");
00269   else
00270       qDebug("return type was '%s'", replyType.data() ); 
00271 
00272 qDebug("Calling countDown() in object1");
00273   if (!client2->call(app.name(), "object1", "countDown()", data, replyType, reply))
00274     qDebug("I couldn't call myself");
00275   else
00276       qDebug("return type was '%s'", replyType.data() ); 
00277 
00278   // Find ourselves in any application.
00279   boolResult = client2->findObject( "testdcop", "object1", "", data, foundApp, foundObj);
00280   qDebug("findObject: result = %s, %s, %s\n", boolResult ? "true" : "false",
00281     foundApp.data(), foundObj.data());
00282 
00283 
00284   return app.exec();
00285 
00286   client->detach();
00287 }
00288 
00289 #include "testdcop.moc"
KDE Logo
This file is part of the documentation for dcop Library Version 3.3.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jan 22 16:42:39 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003