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

KIO

scheduler.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2000 Stephan Kulow <coolo@kde.org>
00003                       Waldo Bastian <bastian@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License version 2 as published by the Free Software Foundation.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017    Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "scheduler.h"
00021 
00022 #include "sessiondata.h"
00023 #include "slaveconfig.h"
00024 #include "authinfo.h"
00025 #include "slave.h"
00026 #include "connection.h"
00027 #include "job_p.h"
00028 
00029 #include <kdebug.h>
00030 #include <kglobal.h>
00031 #include <kprotocolmanager.h>
00032 #include <kprotocolinfo.h>
00033 #include <assert.h>
00034 #include <kdesu/client.h>
00035 
00036 #include <QtCore/QHash>
00037 #include <QtGui/QWidget>
00038 #include <QtDBus/QtDBus>
00039 
00040 // Slaves may be idle for MAX_SLAVE_IDLE time before they are being returned
00041 // to the system wide slave pool. (3 minutes)
00042 #define MAX_SLAVE_IDLE (3*60)
00043 
00044 using namespace KIO;
00045 
00046 #ifndef KDE_USE_FINAL // already defined in job.cpp
00047 static inline Slave *jobSlave(SimpleJob *job)
00048 {
00049     return SimpleJobPrivate::get(job)->m_slave;
00050 }
00051 #endif
00052 
00053 static inline int jobCommand(SimpleJob *job)
00054 {
00055     return SimpleJobPrivate::get(job)->m_command;
00056 }
00057 
00058 static inline void startJob(SimpleJob *job, Slave *slave)
00059 {
00060     SimpleJobPrivate::get(job)->start(slave);
00061 }
00062 
00063 typedef QList<Slave *> SlaveList;
00064 
00065 class KIO::SchedulerPrivate
00066 {
00067 public:
00068     class JobData;
00069     class ProtocolInfo;
00070     class ProtocolInfoDict : public QHash<QString, ProtocolInfo*>
00071     {
00072     public:
00073         ProtocolInfoDict() { }
00074 
00075         ProtocolInfo *get(const QString &protocol);
00076     };
00077 
00078     typedef QHash<KIO::SimpleJob*, JobData> ExtraJobData;
00079     typedef QList<SimpleJob *> JobList;
00080     typedef QMap<Slave*, JobList *> CoSlaveMap;
00081 
00082     SchedulerPrivate() :
00083         q(new Scheduler),
00084         busy( false ),
00085         slaveOnHold( 0 ),
00086         slaveConfig( SlaveConfig::self() ),
00087         sessionData( new SessionData ),
00088         checkOnHold( true ) // !! Always check with KLauncher for the first request
00089     {
00090         slaveTimer.setObjectName( "Scheduler::slaveTimer" );
00091         slaveTimer.setSingleShot( true );
00092         q->connect(&slaveTimer, SIGNAL(timeout()), SLOT(startStep()));
00093         coSlaveTimer.setObjectName( "Scheduler::coSlaveTimer" );
00094         coSlaveTimer.setSingleShot( true );
00095         q->connect(&coSlaveTimer, SIGNAL(timeout()), SLOT(slotScheduleCoSlave()));
00096         cleanupTimer.setObjectName( "Scheduler::cleanupTimer" );
00097         cleanupTimer.setSingleShot( true );
00098         q->connect(&cleanupTimer, SIGNAL(timeout()), SLOT(slotCleanIdleSlaves()));
00099     }
00100     ~SchedulerPrivate()
00101     {
00102         delete q; q = 0;
00103         qDeleteAll( protInfoDict );
00104         qDeleteAll( slaveList );
00105         delete sessionData;
00106     }
00107     Scheduler *q;
00108 
00109     QTimer slaveTimer;
00110     QTimer coSlaveTimer;
00111     QTimer cleanupTimer;
00112     bool busy;
00113 
00114     SlaveList slaveList;
00115     SlaveList idleSlaves;
00116     SlaveList coIdleSlaves;
00117 
00118     ProtocolInfoDict protInfoDict;
00119     Slave *slaveOnHold;
00120     KUrl urlOnHold;
00121     JobList newJobs;
00122 
00123     CoSlaveMap coSlaves;
00124     ExtraJobData extraJobData;
00125     SlaveConfig *slaveConfig;
00126     SessionData *sessionData;
00127     bool checkOnHold;
00128     QMap<QObject *,WId> m_windowList;
00129 
00130     void doJob(SimpleJob *job);
00131     void scheduleJob(SimpleJob *job);
00132     void cancelJob(SimpleJob *job);
00133     void jobFinished(KIO::SimpleJob *job, KIO::Slave *slave);
00134     void scheduleCleanup();
00135     void putSlaveOnHold(KIO::SimpleJob *job, const KUrl &url);
00136     void removeSlaveOnHold();
00137     Slave *getConnectedSlave(const KUrl &url, const KIO::MetaData &metaData );
00138     bool assignJobToSlave(KIO::Slave *slave, KIO::SimpleJob *job);
00139     bool disconnectSlave(KIO::Slave *slave);
00140     void checkSlaveOnHold(bool b);
00141     void publishSlaveOnHold();
00142     void registerWindow(QWidget *wid);
00143 
00144     Slave *findIdleSlave(ProtocolInfo *protInfo, SimpleJob *job, bool &exact);
00145     Slave *createSlave(ProtocolInfo *protInfo, SimpleJob *job, const KUrl &url);
00146 
00147     void debug_info();
00148 
00149     void setupSlave(KIO::Slave *slave, const KUrl &url, const QString &protocol, const QString &proxy , bool newSlave, const KIO::MetaData *config=0);
00150     bool startJobScheduled(ProtocolInfo *protInfo);
00151     bool startJobDirect();
00152 
00153     void slotSlaveDied(KIO::Slave *slave);
00154     void slotSlaveStatus(pid_t pid, const QByteArray &protocol,
00155                          const QString &host, bool connected);
00156 
00157     void slotReparseSlaveConfiguration(const QString &);
00158 
00159     void startStep();
00160     void slotCleanIdleSlaves();
00161     void slotSlaveConnected();
00162     void slotSlaveError(int error, const QString &errorMsg);
00163     void slotScheduleCoSlave();
00164     void slotUnregisterWindow(QObject *);
00165 };
00166 
00167 class KIO::SchedulerPrivate::ProtocolInfo
00168 {
00169 public:
00170     ProtocolInfo() : maxSlaves(1), skipCount(0)
00171     {
00172     }
00173 
00174     QList<SimpleJob *> joblist;
00175     SlaveList activeSlaves;
00176     int maxSlaves;
00177     int skipCount;
00178     QString protocol;
00179 };
00180 
00181 KIO::SchedulerPrivate::ProtocolInfo *
00182 KIO::SchedulerPrivate::ProtocolInfoDict::get(const QString &protocol)
00183 {
00184     ProtocolInfo *info = value(protocol, 0);
00185     if (!info)
00186     {
00187         info = new ProtocolInfo;
00188         info->protocol = protocol;
00189         info->maxSlaves = KProtocolInfo::maxSlaves( protocol );
00190 
00191         insert(protocol, info);
00192     }
00193     return info;
00194 }
00195 
00196 K_GLOBAL_STATIC(SchedulerPrivate, schedulerPrivate)
00197 Scheduler* Scheduler::self()
00198 {
00199     return schedulerPrivate->q;
00200 }
00201 
00202 
00203 //
00204 // There are two kinds of protocol:
00205 // (1) The protocol of the url
00206 // (2) The actual protocol that the io-slave uses.
00207 //
00208 // These two often match, but not necessarily. Most notably, they don't
00209 // match when doing ftp via a proxy.
00210 // In that case (1) is ftp, but (2) is http.
00211 //
00212 // JobData::protocol stores (2) while Job::url().protocol() returns (1).
00213 // The ProtocolInfoDict is indexed with (2).
00214 //
00215 // We schedule slaves based on (2) but tell the slave about (1) via
00216 // Slave::setProtocol().
00217 
00218 class KIO::SchedulerPrivate::JobData
00219 {
00220 public:
00221     JobData() : checkOnHold(false) { }
00222 
00223 public:
00224     QString protocol;
00225     QString proxy;
00226     bool checkOnHold;
00227 };
00228 
00229 
00230 Scheduler::Scheduler()
00231     : QObject(), d(0)
00232 {
00233     setObjectName( "scheduler" );
00234 
00235     const QString dbusPath = "/KIO/Scheduler";
00236     const QString dbusInterface = "org.kde.KIO.Scheduler";
00237     QDBusConnection dbus = QDBusConnection::sessionBus();
00238     dbus.registerObject( "/KIO/Scheduler", this, QDBusConnection::ExportScriptableSlots |
00239                                                  QDBusConnection::ExportScriptableSignals );
00240     dbus.connect(QString(), dbusPath, dbusInterface, "reparseSlaveConfiguration",
00241                  this, SLOT(slotReparseSlaveConfiguration(QString)));
00242 }
00243 
00244 Scheduler::~Scheduler()
00245 {
00246 }
00247 
00248 void Scheduler::doJob(SimpleJob *job)
00249 {
00250     schedulerPrivate->doJob(job);
00251 }
00252 
00253 void Scheduler::scheduleJob(SimpleJob *job)
00254 {
00255     schedulerPrivate->scheduleJob(job);
00256 }
00257 
00258 void Scheduler::cancelJob(SimpleJob *job)
00259 {
00260     schedulerPrivate->cancelJob(job);
00261 }
00262 
00263 void Scheduler::jobFinished(KIO::SimpleJob *job, KIO::Slave *slave)
00264 {
00265     schedulerPrivate->jobFinished(job, slave);
00266 }
00267 
00268 void Scheduler::putSlaveOnHold(KIO::SimpleJob *job, const KUrl &url)
00269 {
00270     schedulerPrivate->putSlaveOnHold(job, url);
00271 }
00272 
00273 void Scheduler::removeSlaveOnHold()
00274 {
00275     schedulerPrivate->removeSlaveOnHold();
00276 }
00277 
00278 void Scheduler::publishSlaveOnHold()
00279 {
00280     schedulerPrivate->publishSlaveOnHold();
00281 }
00282 
00283 KIO::Slave *Scheduler::getConnectedSlave(const KUrl &url,
00284         const KIO::MetaData &config )
00285 {
00286     return schedulerPrivate->getConnectedSlave(url, config);
00287 }
00288 
00289 bool Scheduler::assignJobToSlave(KIO::Slave *slave, KIO::SimpleJob *job)
00290 {
00291     return schedulerPrivate->assignJobToSlave(slave, job);
00292 }
00293 
00294 bool Scheduler::disconnectSlave(KIO::Slave *slave)
00295 {
00296     return schedulerPrivate->disconnectSlave(slave);
00297 }
00298 
00299 void Scheduler::registerWindow(QWidget *wid)
00300 {
00301     schedulerPrivate->registerWindow(wid);
00302 }
00303 
00304 void Scheduler::unregisterWindow(QObject *wid)
00305 {
00306     schedulerPrivate->slotUnregisterWindow(wid);
00307 }
00308 
00309 bool Scheduler::connect( const char *signal, const QObject *receiver,
00310                          const char *member)
00311 {
00312     return QObject::connect(self(), signal, receiver, member);
00313 }
00314 
00315 bool Scheduler::connect( const QObject* sender, const char* signal,
00316                          const QObject* receiver, const char* member )
00317 {
00318     return QObject::connect(sender, signal, receiver, member);
00319 }
00320 
00321 bool Scheduler::disconnect( const QObject* sender, const char* signal,
00322                             const QObject* receiver, const char* member )
00323 {
00324     return QObject::disconnect(sender, signal, receiver, member);
00325 }
00326 
00327 bool Scheduler::connect( const QObject *sender, const char *signal,
00328                          const char *member )
00329 {
00330     return QObject::connect(sender, signal, member);
00331 }
00332 
00333 void Scheduler::checkSlaveOnHold(bool b)
00334 {
00335     schedulerPrivate->checkSlaveOnHold(b);
00336 }
00337 
00338 void Scheduler::emitReparseSlaveConfiguration()
00339 {
00340     self()->reparseSlaveConfiguration( QString() );
00341 }
00342 
00343 void
00344 SchedulerPrivate::debug_info()
00345 {
00346 }
00347 
00348 void SchedulerPrivate::slotReparseSlaveConfiguration(const QString &proto)
00349 {
00350     kDebug( 7006 ) << "reparseSlaveConfiguration( " << proto << " )";
00351     KProtocolManager::reparseConfiguration();
00352     slaveConfig->reset();
00353     sessionData->reset();
00354     NetRC::self()->reload();
00355 
00356     foreach( Slave *slave, slaveList )
00357     {
00358         if ( slave->slaveProtocol() == proto || proto.isEmpty() )
00359         {
00360             slave->send( CMD_REPARSECONFIGURATION );
00361             slave->resetHost();
00362         }
00363     }
00364 }
00365 
00366 void SchedulerPrivate::doJob(SimpleJob *job) {
00367     JobData jobData;
00368     jobData.protocol = KProtocolManager::slaveProtocol(job->url(), jobData.proxy);
00369 //    kDebug(7006) << "protocol=" << jobData->protocol;
00370     if (jobCommand(job) == CMD_GET)
00371     {
00372        jobData.checkOnHold = checkOnHold;
00373        checkOnHold = false;
00374     }
00375     extraJobData.insert(job, jobData);
00376     newJobs.append(job);
00377     slaveTimer.start(0);
00378 #ifndef NDEBUG
00379     if (newJobs.count() > 150)
00380     kDebug() << "WARNING - KIO::Scheduler got more than 150 jobs! This shows a misuse in your app (yes, a job is a QObject).";
00381 #endif
00382 }
00383 
00384 void SchedulerPrivate::scheduleJob(SimpleJob *job) {
00385     newJobs.removeAll(job);
00386     const JobData& jobData = extraJobData.value(job);
00387 
00388     QString protocol = jobData.protocol;
00389 //    kDebug(7006) << "protocol=" << protocol;
00390     ProtocolInfo *protInfo = protInfoDict.get(protocol);
00391     protInfo->joblist.append(job);
00392 
00393     slaveTimer.start(0);
00394 }
00395 
00396 void SchedulerPrivate::cancelJob(SimpleJob *job) {
00397 //    kDebug(7006) << "Scheduler: canceling job " << job;
00398     Slave *slave = jobSlave(job);
00399     if ( !slave  )
00400     {
00401         // was not yet running (don't call this on a finished job!)
00402         JobData jobData = extraJobData.value(job);
00403         newJobs.removeAll(job);
00404         ProtocolInfo *protInfo = protInfoDict.get(jobData.protocol);
00405         protInfo->joblist.removeAll(job);
00406 
00407         // Search all slaves to see if job is in the queue of a coSlave
00408         foreach( Slave* coSlave, slaveList )
00409         {
00410            JobList *list = coSlaves.value(slave);
00411            if (list && list->removeAll(job)) {
00412                // Job was found and removed.
00413                // Fall through to kill the slave as well!
00414                slave = coSlave;
00415            }
00416         }
00417         if (!slave)
00418         {
00419            extraJobData.remove(job);
00420            return; // Job was not yet running and not in a coSlave queue.
00421         }
00422     }
00423     kDebug(7006) << "Scheduler: killing slave " << slave->slave_pid();
00424     slave->kill();
00425     jobFinished( job, slave );
00426     slotSlaveDied(slave);
00427 }
00428 
00429 void SchedulerPrivate::startStep()
00430 {
00431     while (newJobs.count()) {
00432        (void) startJobDirect();
00433     }
00434 
00435     QHashIterator<QString, ProtocolInfo*> it(protInfoDict);
00436     while(it.hasNext()) {
00437        it.next();
00438        if (startJobScheduled(it.value())) return;
00439     }
00440 }
00441 
00442 void SchedulerPrivate::setupSlave(KIO::Slave *slave, const KUrl &url, const QString &protocol, const QString &proxy , bool newSlave, const KIO::MetaData *config)
00443 {
00444     QString host = url.host();
00445     int port = url.port();
00446     if ( port == -1 ) // no port is -1 in QUrl, but in kde3 we used 0 and the kioslaves assume that.
00447         port = 0;
00448     QString user = url.user();
00449     QString passwd = url.pass();
00450 
00451     if ((newSlave) ||
00452         (slave->host() != host) ||
00453         (slave->port() != port) ||
00454         (slave->user() != user) ||
00455         (slave->passwd() != passwd))
00456     {
00457         slaveConfig = SlaveConfig::self();
00458 
00459         MetaData configData = slaveConfig->configData(protocol, host);
00460         sessionData->configDataFor( configData, protocol, host );
00461 
00462         configData["UseProxy"] = proxy;
00463 
00464         QString autoLogin = configData["EnableAutoLogin"].toLower();
00465         if ( autoLogin == "true" )
00466         {
00467             NetRC::AutoLogin l;
00468             l.login = user;
00469             bool usern = (protocol == "ftp");
00470             if ( NetRC::self()->lookup( url, l, usern) )
00471             {
00472                 configData["autoLoginUser"] = l.login;
00473                 configData["autoLoginPass"] = l.password;
00474                 if ( usern )
00475                 {
00476                     QString macdef;
00477                     QMap<QString, QStringList>::ConstIterator it = l.macdef.begin();
00478                     for ( ; it != l.macdef.end(); ++it )
00479                         macdef += it.key() + '\\' + it.value().join( "\\" ) + '\n';
00480                     configData["autoLoginMacro"] = macdef;
00481                 }
00482             }
00483         }
00484         if (config)
00485            configData += *config;
00486         slave->setConfig(configData);
00487         slave->setProtocol(url.protocol());
00488         slave->setHost(host, port, user, passwd);
00489     }
00490 }
00491 
00492 bool SchedulerPrivate::startJobScheduled(ProtocolInfo *protInfo)
00493 {
00494     if (protInfo->joblist.isEmpty())
00495        return false;
00496 
00497 //       kDebug(7006) << "Scheduling job";
00498     debug_info();
00499     bool newSlave = false;
00500 
00501     SimpleJob *job = 0;
00502     Slave *slave = 0;
00503 
00504     if (protInfo->skipCount > 2)
00505     {
00506        bool dummy;
00507        // Prevent starvation. We skip the first entry in the queue at most
00508        // 2 times in a row. The
00509        protInfo->skipCount = 0;
00510        job = protInfo->joblist.at(0);
00511        slave = findIdleSlave(protInfo, job, dummy );
00512     }
00513     else
00514     {
00515        bool exact=false;
00516        SimpleJob *firstJob = 0;
00517        Slave *firstSlave = 0;
00518        for(int i = 0; (i < protInfo->joblist.count()) && (i < 10); i++)
00519        {
00520           job = protInfo->joblist.at(i);
00521           slave = findIdleSlave(protInfo, job, exact);
00522           if (!firstSlave)
00523           {
00524              firstJob = job;
00525              firstSlave = slave;
00526           }
00527           if (!slave) break;
00528           if (exact) break;
00529        }
00530 
00531        if (!exact)
00532        {
00533          slave = firstSlave;
00534          job = firstJob;
00535        }
00536        if (job == firstJob)
00537          protInfo->skipCount = 0;
00538        else
00539          protInfo->skipCount++;
00540     }
00541 
00542     if (!slave)
00543     {
00544        if ( protInfo->maxSlaves > static_cast<int>(protInfo->activeSlaves.count()) )
00545        {
00546           newSlave = true;
00547           slave = createSlave(protInfo, job, job->url());
00548           if (!slave)
00549              slaveTimer.start(0);
00550        }
00551     }
00552 
00553     if (!slave)
00554     {
00555 //          kDebug(7006) << "No slaves available";
00556 //          kDebug(7006) << " -- active: " << protInfo->activeSlaves.count();
00557        return false;
00558     }
00559 
00560     protInfo->activeSlaves.append(slave);
00561     idleSlaves.removeAll(slave);
00562     protInfo->joblist.removeAll(job);
00563 //       kDebug(7006) << "scheduler: job started " << job;
00564 
00565 
00566     SchedulerPrivate::JobData jobData = extraJobData.value(job);
00567     setupSlave(slave, job->url(), jobData.protocol, jobData.proxy, newSlave);
00568     startJob(job, slave);
00569 
00570     slaveTimer.start(0);
00571     return true;
00572 }
00573 
00574 bool SchedulerPrivate::startJobDirect()
00575 {
00576     debug_info();
00577     SimpleJob *job = newJobs.takeFirst();
00578     SchedulerPrivate::JobData jobData = extraJobData.value(job);
00579 
00580     QString protocol = jobData.protocol;
00581     ProtocolInfo *protInfo = protInfoDict.get(protocol);
00582 
00583     bool newSlave = false;
00584     bool dummy;
00585 
00586     // Look for matching slave
00587     Slave *slave = findIdleSlave(protInfo, job, dummy);
00588 
00589     if (!slave)
00590     {
00591        newSlave = true;
00592        slave = createSlave(protInfo, job, job->url());
00593     }
00594 
00595     if (!slave)
00596        return false;
00597 
00598     idleSlaves.removeAll(slave);
00599 //       kDebug(7006) << "scheduler: job started " << job;
00600 
00601     setupSlave(slave, job->url(), protocol, jobData.proxy, newSlave);
00602     startJob(job, slave);
00603     return true;
00604 }
00605 
00606 static Slave *searchIdleList(SlaveList &idleSlaves, const KUrl &url, const QString &protocol, bool &exact)
00607 {
00608     QString host = url.host();
00609     int port = url.port();
00610     if ( port == -1 ) // no port is -1 in QUrl, but in kde3 we used 0 and the kioslaves assume that.
00611         port = 0;
00612     QString user = url.user();
00613     exact = true;
00614 
00615     foreach( Slave *slave, idleSlaves )
00616     {
00617        if ((protocol == slave->slaveProtocol()) &&
00618            (host == slave->host()) &&
00619            (port == slave->port()) &&
00620            (user == slave->user()))
00621            return slave;
00622     }
00623 
00624     exact = false;
00625 
00626     // Look for slightly matching slave
00627     foreach( Slave *slave, idleSlaves )
00628     {
00629        if (protocol == slave->slaveProtocol())
00630           return slave;
00631     }
00632     return 0;
00633 }
00634 
00635 Slave *SchedulerPrivate::findIdleSlave(ProtocolInfo *, SimpleJob *job, bool &exact)
00636 {
00637     Slave *slave = 0;
00638     JobData jobData = extraJobData.value(job);
00639 
00640     if (jobData.checkOnHold)
00641     {
00642        slave = Slave::holdSlave(jobData.protocol, job->url());
00643        if (slave)
00644           return slave;
00645     }
00646     if (slaveOnHold)
00647     {
00648        // Make sure that the job wants to do a GET or a POST, and with no offset
00649        bool bCanReuse = (jobCommand(job) == CMD_GET);
00650        KIO::TransferJob * tJob = qobject_cast<KIO::TransferJob *>(job);
00651        if ( tJob )
00652        {
00653           bCanReuse = (jobCommand(job) == CMD_GET || jobCommand(job) == CMD_SPECIAL);
00654           if ( bCanReuse )
00655           {
00656             KIO::MetaData outgoing = tJob->outgoingMetaData();
00657             QString resume = (!outgoing.contains("resume")) ? QString() : outgoing["resume"];
00658             kDebug(7006) << "Resume metadata is" << resume;
00659             bCanReuse = (resume.isEmpty() || resume == "0");
00660           }
00661        }
00662 //       kDebug(7006) << "bCanReuse = " << bCanReuse;
00663        if (bCanReuse)
00664        {
00665           if (job->url() == urlOnHold)
00666           {
00667              kDebug(7006) << "HOLD: Reusing held slave for" << urlOnHold;
00668              slave = slaveOnHold;
00669           }
00670           else
00671           {
00672              kDebug(7006) << "HOLD: Discarding held slave (" << urlOnHold << ")";
00673              slaveOnHold->kill();
00674           }
00675           slaveOnHold = 0;
00676           urlOnHold = KUrl();
00677        }
00678        if (slave)
00679           return slave;
00680     }
00681 
00682     return searchIdleList(idleSlaves, job->url(), jobData.protocol, exact);
00683 }
00684 
00685 Slave *SchedulerPrivate::createSlave(ProtocolInfo *protInfo, SimpleJob *job, const KUrl &url)
00686 {
00687    int error;
00688    QString errortext;
00689    Slave *slave = Slave::createSlave(protInfo->protocol, url, error, errortext);
00690    if (slave)
00691    {
00692       slaveList.append(slave);
00693       idleSlaves.append(slave);
00694       q->connect(slave, SIGNAL(slaveDied(KIO::Slave *)),
00695                  SLOT(slotSlaveDied(KIO::Slave *)));
00696       q->connect(slave, SIGNAL(slaveStatus(pid_t,const QByteArray&,const QString &, bool)),
00697                  SLOT(slotSlaveStatus(pid_t,const QByteArray&, const QString &, bool)));
00698    }
00699    else
00700    {
00701       kError() << "couldn't create slave:" << errortext;
00702       if (job)
00703       {
00704          protInfo->joblist.removeAll(job);
00705          extraJobData.remove(job);
00706          job->slotError( error, errortext );
00707       }
00708    }
00709    return slave;
00710 }
00711 
00712 void SchedulerPrivate::slotSlaveStatus(pid_t, const QByteArray&, const QString &, bool)
00713 {
00714 }
00715 
00716 void SchedulerPrivate::jobFinished(SimpleJob *job, Slave *slave)
00717 {
00718     JobData jobData = extraJobData.take(job);
00719 
00720     ProtocolInfo *protInfo = protInfoDict.get(jobData.protocol);
00721     slave->disconnect(job);
00722     protInfo->activeSlaves.removeAll(slave);
00723     if (slave->isAlive())
00724     {
00725        JobList *list = coSlaves.value(slave);
00726        if (list)
00727        {
00728           assert(slave->isConnected());
00729           assert(!coIdleSlaves.contains(slave));
00730           coIdleSlaves.append(slave);
00731           if (!list->isEmpty())
00732              coSlaveTimer.start(0);
00733           return;
00734        }
00735        else
00736        {
00737           assert(!slave->isConnected());
00738           idleSlaves.append(slave);
00739           slave->setIdle();
00740           scheduleCleanup();
00741 //          slave->send( CMD_SLAVE_STATUS );
00742        }
00743     }
00744     if (protInfo->joblist.count())
00745     {
00746        slaveTimer.start(0);
00747     }
00748 }
00749 
00750 void SchedulerPrivate::slotSlaveDied(KIO::Slave *slave)
00751 {
00752     assert(!slave->isAlive());
00753     ProtocolInfo *protInfo = protInfoDict.get(slave->slaveProtocol());
00754     protInfo->activeSlaves.removeAll(slave);
00755     if (slave == slaveOnHold)
00756     {
00757        slaveOnHold = 0;
00758        urlOnHold = KUrl();
00759     }
00760     idleSlaves.removeAll(slave);
00761     JobList *list = coSlaves.value(slave);
00762     if (list)
00763     {
00764        // coSlave dies, kill jobs waiting in queue
00765        disconnectSlave(slave);
00766     }
00767 
00768     if (!slaveList.removeAll(slave))
00769         kDebug(7006) << "Scheduler: BUG!! Slave " << slave << "/" << slave->slave_pid() << " died, but is NOT in slaveList!!!\n";
00770     else
00771         slave->deref(); // Delete slave
00772 }
00773 
00774 void SchedulerPrivate::slotCleanIdleSlaves()
00775 {
00776     SlaveList::iterator it = idleSlaves.begin();
00777     for( ; it != idleSlaves.end(); )
00778     {
00779         Slave *slave = *it;
00780         if (slave->idleTime() >= MAX_SLAVE_IDLE)
00781         {
00782            // kDebug(7006) << "Removing idle slave: " << slave->slaveProtocol() << " " << slave->host();
00783            Slave *removeSlave = slave;
00784            it = idleSlaves.erase( it );
00785            slaveList.removeAll( removeSlave );
00786            removeSlave->connection()->close();
00787            removeSlave->deref();
00788         }
00789         else
00790         {
00791             ++it;
00792         }
00793     }
00794     scheduleCleanup();
00795 }
00796 
00797 void SchedulerPrivate::scheduleCleanup()
00798 {
00799     if (idleSlaves.count())
00800     {
00801         if (!cleanupTimer.isActive())
00802             cleanupTimer.start( MAX_SLAVE_IDLE*1000 );
00803     }
00804 }
00805 
00806 void SchedulerPrivate::putSlaveOnHold(KIO::SimpleJob *job, const KUrl &url)
00807 {
00808     Slave *slave = jobSlave(job);
00809     slave->disconnect(job);
00810 
00811     if (slaveOnHold)
00812     {
00813         slaveOnHold->kill();
00814     }
00815     slaveOnHold = slave;
00816     urlOnHold = url;
00817     slaveOnHold->suspend();
00818 }
00819 
00820 void SchedulerPrivate::publishSlaveOnHold()
00821 {
00822     if (!slaveOnHold)
00823        return;
00824 
00825     slaveOnHold->hold(urlOnHold);
00826 }
00827 
00828 void SchedulerPrivate::removeSlaveOnHold()
00829 {
00830     if (slaveOnHold)
00831     {
00832         slaveOnHold->kill();
00833     }
00834     slaveOnHold = 0;
00835     urlOnHold = KUrl();
00836 }
00837 
00838 Slave *
00839 SchedulerPrivate::getConnectedSlave(const KUrl &url, const KIO::MetaData &config )
00840 {
00841     QString proxy;
00842     QString protocol = KProtocolManager::slaveProtocol(url, proxy);
00843     bool dummy;
00844     Slave *slave = searchIdleList(idleSlaves, url, protocol, dummy);
00845     if (!slave)
00846     {
00847        ProtocolInfo *protInfo = protInfoDict.get(protocol);
00848        slave = createSlave(protInfo, 0, url);
00849     }
00850     if (!slave)
00851        return 0; // Error
00852     idleSlaves.removeAll(slave);
00853 
00854     setupSlave(slave, url, protocol, proxy, true, &config);
00855 
00856     slave->send( CMD_CONNECT );
00857     q->connect(slave, SIGNAL(connected()),
00858                SLOT(slotSlaveConnected()));
00859     q->connect(slave, SIGNAL(error(int, const QString &)),
00860                SLOT(slotSlaveError(int, const QString &)));
00861 
00862     coSlaves.insert(slave, new JobList);
00863 //    kDebug(7006) << "_getConnectedSlave( " << slave << ")";
00864     return slave;
00865 }
00866 
00867 void
00868 SchedulerPrivate::slotScheduleCoSlave()
00869 {
00870     slaveConfig = SlaveConfig::self();
00871     SlaveList::iterator it = coIdleSlaves.begin();
00872     for( ; it != coIdleSlaves.end(); )
00873     {
00874         Slave* slave = *it;
00875         JobList *list = coSlaves.value(slave);
00876         assert(list);
00877         if (list && !list->isEmpty())
00878         {
00879            SimpleJob *job = list->takeFirst();
00880            it = coIdleSlaves.erase( it );
00881 //           kDebug(7006) << "scheduler: job started " << job;
00882 
00883            KUrl url =job->url();
00884            QString host = url.host();
00885            int port = url.port();
00886            if ( port == -1 ) // no port is -1 in QUrl, but in kde3 we used 0 and the kioslaves assume that.
00887                port = 0;
00888 
00889            if (slave->host() == "<reset>")
00890            {
00891               QString user = url.user();
00892               QString passwd = url.pass();
00893 
00894               MetaData configData = slaveConfig->configData(url.protocol(), url.host());
00895               slave->setConfig(configData);
00896               slave->setProtocol(url.protocol());
00897               slave->setHost(host, port, user, passwd);
00898            }
00899 
00900            assert(slave->protocol() == url.protocol());
00901            assert(slave->host() == host);
00902            assert(slave->port() == port);
00903            startJob(job, slave);
00904         } else {
00905             ++it;
00906         }
00907     }
00908 }
00909 
00910 void
00911 SchedulerPrivate::slotSlaveConnected()
00912 {
00913     Slave *slave = static_cast<Slave *>(q->sender());
00914 //    kDebug(7006) << "slotSlaveConnected( " << slave << ")";
00915     slave->setConnected(true);
00916     q->disconnect(slave, SIGNAL(connected()),
00917                   q, SLOT(slotSlaveConnected()));
00918     emit q->slaveConnected(slave);
00919     assert(!coIdleSlaves.contains(slave));
00920     coIdleSlaves.append(slave);
00921     coSlaveTimer.start(0);
00922 }
00923 
00924 void
00925 SchedulerPrivate::slotSlaveError(int errorNr, const QString &errorMsg)
00926 {
00927     Slave *slave = static_cast<Slave *>(q->sender());
00928     if (!slave->isConnected() || coIdleSlaves.contains(slave))
00929     {
00930        // Only forward to application if slave is idle or still connecting.
00931        emit q->slaveError(slave, errorNr, errorMsg);
00932     }
00933 }
00934 
00935 bool
00936 SchedulerPrivate::assignJobToSlave(KIO::Slave *slave, SimpleJob *job)
00937 {
00938 //    kDebug(7006) << "_assignJobToSlave( " << job << ", " << slave << ")";
00939     QString dummy;
00940     if ((slave->slaveProtocol() != KProtocolManager::slaveProtocol( job->url(), dummy ))
00941         ||
00942         (!newJobs.removeAll(job)))
00943     {
00944         kDebug(7006) << "_assignJobToSlave(): ERROR, nonmatching or unknown job.";
00945         job->kill();
00946         return false;
00947     }
00948 
00949     JobList *list = coSlaves.value(slave);
00950     assert(list);
00951     if (!list)
00952     {
00953         kDebug(7006) << "_assignJobToSlave(): ERROR, unknown slave.";
00954         job->kill();
00955         return false;
00956     }
00957 
00958     assert(!list->contains(job));
00959     list->append(job);
00960     coSlaveTimer.start(0); // Start job on timer event
00961 
00962     return true;
00963 }
00964 
00965 bool
00966 SchedulerPrivate::disconnectSlave(KIO::Slave *slave)
00967 {
00968 //    kDebug(7006) << "_disconnectSlave( " << slave << ")";
00969     CoSlaveMap::iterator coSlaveIt = coSlaves.find( slave );
00970     assert( coSlaveIt != coSlaves.end() );
00971     JobList *list = *coSlaveIt;
00972     coSlaves.erase( coSlaveIt );
00973     assert(list);
00974     if (!list)
00975        return false;
00976     // Kill jobs still in queue.
00977     while(!list->isEmpty())
00978     {
00979        Job *job = list->takeFirst();
00980        job->kill();
00981     }
00982     delete list;
00983     coIdleSlaves.removeAll(slave);
00984     assert(!coIdleSlaves.contains(slave));
00985     QObject::disconnect(slave, SIGNAL(connected()),
00986                         q, SLOT(slotSlaveConnected()));
00987     QObject::disconnect(slave, SIGNAL(error(int, const QString &)),
00988                         q, SLOT(slotSlaveError(int, const QString &)));
00989     if (slave->isAlive())
00990     {
00991        idleSlaves.append(slave);
00992        slave->send( CMD_DISCONNECT );
00993        slave->setIdle();
00994        slave->setConnected(false);
00995        scheduleCleanup();
00996     }
00997     return true;
00998 }
00999 
01000 void
01001 SchedulerPrivate::checkSlaveOnHold(bool b)
01002 {
01003     checkOnHold = b;
01004 }
01005 
01006 void
01007 SchedulerPrivate::registerWindow(QWidget *wid)
01008 {
01009    if (!wid)
01010       return;
01011 
01012    QObject *obj = static_cast<QObject *>(wid);
01013    if (!m_windowList.contains(obj))
01014    {
01015       // We must store the window Id because by the time
01016       // the destroyed signal is emitted we can no longer
01017       // access QWidget::winId() (already destructed)
01018       WId windowId = wid->winId();
01019       m_windowList.insert(obj, windowId);
01020       q->connect(wid, SIGNAL(destroyed(QObject *)),
01021                  SLOT(slotUnregisterWindow(QObject*)));
01022       QDBusInterface("org.kde.kded", "/kded", "org.kde.kded").
01023           call(QDBus::NoBlock, "registerWindowId", qlonglong(windowId));
01024    }
01025 }
01026 
01027 void
01028 SchedulerPrivate::slotUnregisterWindow(QObject *obj)
01029 {
01030    if (!obj)
01031       return;
01032 
01033    QMap<QObject *, WId>::Iterator it = m_windowList.find(obj);
01034    if (it == m_windowList.end())
01035       return;
01036    WId windowId = it.value();
01037    q->disconnect(it.key(), SIGNAL(destroyed(QObject *)),
01038                  q, SLOT(slotUnregisterWindow(QObject*)));
01039    m_windowList.erase( it );
01040    QDBusInterface("org.kde.kded", "/kded", "org.kde.kded").
01041        call(QDBus::NoBlock, "unregisterWindowId", qlonglong(windowId));
01042 }
01043 
01044 #include "scheduler.moc"

KIO

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
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