• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

dcop

testdcop.cpp

Go to the documentation of this file.
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 #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   // note "fun" is normlized here (i.e. whitespace clean)
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 //  client->attach(); // attach to the server, now we can use DCOP service
00205 
00206   client->registerAs( app.name(), false ); // register at the server, now others can call us.
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   // Enable the above signals
00244   client->setNotifications( true );
00245 
00246   QCString foundApp;
00247   QCString foundObj;
00248 
00249   // Find a object called "object1" in any application that
00250   // meets the criteria "canLaunchRockets()"
00251 //  bool boolResult = client->findObject( "", "object1", "canLaunchRockets()", data, foundApp, foundObj);
00252 //  qDebug("findObject: result = %s, %s, %s\n", boolResult ? "true" : "false",
00253 //  foundApp.data(), foundObj.data());
00254 
00255   // Find an application that matches with "konqueror*"
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   // Find an object called "object1" in any application.
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   // Find ourselves in any application.
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   // Find ourselves in any application.
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"

dcop

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

API Reference

Skip menu "API Reference"
  • dcop
  • DNSSD
  • interfaces
  • Kate
  • kconf_update
  • KDECore
  • KDED
  • kdefx
  • KDEsu
  • kdeui
  • KDocTools
  • KHTML
  • KImgIO
  • KInit
  • kio
  • kioslave
  • KJS
  • KNewStuff
  • KParts
  • KUtils
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal