1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
|
/* This file is part of the KDE libraries
Copyright (C) 2000 Stephan Kulow <coolo@kde.org>
Waldo Bastian <bastian@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "kio/sessiondata.h"
#include "kio/slaveconfig.h"
#include "kio/scheduler.h"
#include "kio/authinfo.h"
#include "kio/slave.h"
#include <tqptrlist.h>
#include <tqdict.h>
#include <dcopclient.h>
#include <kdebug.h>
#include <kglobal.h>
#include <kprotocolmanager.h>
#include <kprotocolinfo.h>
#include <assert.h>
#include <kstaticdeleter.h>
#include <kdesu/client.h>
// Slaves may be idle for MAX_SLAVE_IDLE time before they are being returned
// to the system wide slave pool. (3 minutes)
#define MAX_SLAVE_IDLE (3*60)
using namespace KIO;
template class TQDict<KIO::Scheduler::ProtocolInfo>;
Scheduler *Scheduler::instance = 0;
class KIO::SlaveList: public TQPtrList<Slave>
{
public:
SlaveList() { }
};
//
// There are two kinds of protocol:
// (1) The protocol of the url
// (2) The actual protocol that the io-slave uses.
//
// These two often match, but not necasserily. Most notably, they don't
// match when doing ftp via a proxy.
// In that case (1) is ftp, but (2) is http.
//
// JobData::protocol stores (2) while Job::url().protocol() returns (1).
// The ProtocolInfoDict is indexed with (2).
//
// We schedule slaves based on (2) but tell the slave about (1) via
// Slave::setProtocol().
class KIO::Scheduler::JobData
{
public:
JobData() : checkOnHold(false) { }
public:
TQString protocol;
TQString proxy;
bool checkOnHold;
};
class KIO::Scheduler::ExtraJobData: public TQPtrDict<KIO::Scheduler::JobData>
{
public:
ExtraJobData() { setAutoDelete(true); }
};
class KIO::Scheduler::ProtocolInfo
{
public:
ProtocolInfo() : maxSlaves(1), skipCount(0)
{
joblist.setAutoDelete(false);
}
TQPtrList<SimpleJob> joblist;
SlaveList activeSlaves;
int maxSlaves;
int skipCount;
TQString protocol;
};
class KIO::Scheduler::ProtocolInfoDict : public TQDict<KIO::Scheduler::ProtocolInfo>
{
public:
ProtocolInfoDict() { }
KIO::Scheduler::ProtocolInfo *get( const TQString &protocol);
};
KIO::Scheduler::ProtocolInfo *
KIO::Scheduler::ProtocolInfoDict::get(const TQString &protocol)
{
ProtocolInfo *info = tqfind(protocol);
if (!info)
{
info = new ProtocolInfo;
info->protocol = protocol;
info->maxSlaves = KProtocolInfo::maxSlaves( protocol );
insert(protocol, info);
}
return info;
}
Scheduler::Scheduler()
: DCOPObject( "KIO::Scheduler" ),
TQObject(kapp, "scheduler"),
slaveTimer(0, "Scheduler::slaveTimer"),
coSlaveTimer(0, "Scheduler::coSlaveTimer"),
cleanupTimer(0, "Scheduler::cleanupTimer")
{
checkOnHold = true; // !! Always check with KLauncher for the first request.
slaveOnHold = 0;
protInfoDict = new ProtocolInfoDict;
slaveList = new SlaveList;
idleSlaves = new SlaveList;
coIdleSlaves = new SlaveList;
extraJobData = new ExtraJobData;
sessionData = new SessionData;
slaveConfig = SlaveConfig::self();
connect(&slaveTimer, TQT_SIGNAL(timeout()), TQT_SLOT(startStep()));
connect(&coSlaveTimer, TQT_SIGNAL(timeout()), TQT_SLOT(slotScheduleCoSlave()));
connect(&cleanupTimer, TQT_SIGNAL(timeout()), TQT_SLOT(slotCleanIdleSlaves()));
busy = false;
}
Scheduler::~Scheduler()
{
protInfoDict->setAutoDelete(true);
delete protInfoDict; protInfoDict = 0;
delete idleSlaves; idleSlaves = 0;
delete coIdleSlaves; coIdleSlaves = 0;
slaveList->setAutoDelete(true);
delete slaveList; slaveList = 0;
delete extraJobData; extraJobData = 0;
delete sessionData; sessionData = 0;
instance = 0;
}
void
Scheduler::debug_info()
{
}
bool Scheduler::process(const TQCString &fun, const TQByteArray &data, TQCString &replyType, TQByteArray &replyData )
{
if ( fun != "reparseSlaveConfiguration(TQString)" )
return DCOPObject::process( fun, data, replyType, replyData );
slaveConfig = SlaveConfig::self();
replyType = "void";
TQDataStream stream( data, IO_ReadOnly );
TQString proto;
stream >> proto;
kdDebug( 7006 ) << "reparseConfiguration( " << proto << " )" << endl;
KProtocolManager::reparseConfiguration();
slaveConfig->reset();
sessionData->reset();
NetRC::self()->reload();
Slave *slave = slaveList->first();
for (; slave; slave = slaveList->next() )
if ( slave->slaveProtocol() == proto || proto.isEmpty() )
{
slave->send( CMD_REPARSECONFIGURATION );
slave->resetHost();
}
return true;
}
QCStringList Scheduler::functions()
{
QCStringList funcs = DCOPObject::functions();
funcs << "void reparseSlaveConfiguration(TQString)";
return funcs;
}
void Scheduler::_doJob(SimpleJob *job) {
JobData *jobData = new JobData;
jobData->protocol = KProtocolManager::slaveProtocol(job->url(), jobData->proxy);
// kdDebug(7006) << "Scheduler::_doJob protocol=" << jobData->protocol << endl;
if (job->command() == CMD_GET)
{
jobData->checkOnHold = checkOnHold;
checkOnHold = false;
}
extraJobData->tqreplace(job, jobData);
newJobs.append(job);
slaveTimer.start(0, true);
#ifndef NDEBUG
if (newJobs.count() > 150)
kdDebug() << "WARNING - KIO::Scheduler got more than 150 jobs! This shows a misuse in your app (yes, a job is a TQObject)." << endl;
#endif
}
void Scheduler::_scheduleJob(SimpleJob *job) {
newJobs.removeRef(job);
JobData *jobData = extraJobData->tqfind(job);
if (!jobData)
{
kdFatal(7006) << "BUG! _ScheduleJob(): No extraJobData for job!" << endl;
return;
}
TQString protocol = jobData->protocol;
// kdDebug(7006) << "Scheduler::_scheduleJob protocol=" << protocol << endl;
ProtocolInfo *protInfo = protInfoDict->get(protocol);
protInfo->joblist.append(job);
slaveTimer.start(0, true);
}
void Scheduler::_cancelJob(SimpleJob *job) {
// kdDebug(7006) << "Scheduler: canceling job " << job << endl;
Slave *slave = job->slave();
if ( !slave )
{
// was not yet running (don't call this on a finished job!)
JobData *jobData = extraJobData->tqfind(job);
if (!jobData)
return; // I said: "Don't call this on a finished job!"
newJobs.removeRef(job);
ProtocolInfo *protInfo = protInfoDict->get(jobData->protocol);
protInfo->joblist.removeRef(job);
// Search all slaves to see if job is in the queue of a coSlave
slave = slaveList->first();
for(; slave; slave = slaveList->next())
{
JobList *list = coSlaves.tqfind(slave);
if (list && list->removeRef(job))
break; // Job was found and removed.
// Fall through to kill the slave as well!
}
if (!slave)
{
extraJobData->remove(job);
return; // Job was not yet running and not in a coSlave queue.
}
}
kdDebug(7006) << "Scheduler: killing slave " << slave->slave_pid() << endl;
slave->kill();
_jobFinished( job, slave );
slotSlaveDied( slave);
}
void Scheduler::startStep()
{
while(newJobs.count())
{
(void) startJobDirect();
}
TQDictIterator<KIO::Scheduler::ProtocolInfo> it(*protInfoDict);
while(it.current())
{
if (startJobScheduled(it.current())) return;
++it;
}
}
void Scheduler::setupSlave(KIO::Slave *slave, const KURL &url, const TQString &protocol, const TQString &proxy , bool newSlave, const KIO::MetaData *config)
{
TQString host = url.host();
int port = url.port();
TQString user = url.user();
TQString passwd = url.pass();
if ((newSlave) ||
(slave->host() != host) ||
(slave->port() != port) ||
(slave->user() != user) ||
(slave->passwd() != passwd))
{
slaveConfig = SlaveConfig::self();
MetaData configData = slaveConfig->configData(protocol, host);
sessionData->configDataFor( configData, protocol, host );
configData["UseProxy"] = proxy;
TQString autoLogin = configData["EnableAutoLogin"].lower();
if ( autoLogin == "true" )
{
NetRC::AutoLogin l;
l.login = user;
bool usern = (protocol == "ftp");
if ( NetRC::self()->lookup( url, l, usern) )
{
configData["autoLoginUser"] = l.login;
configData["autoLoginPass"] = l.password;
if ( usern )
{
TQString macdef;
TQMap<TQString, TQStringList>::ConstIterator it = l.macdef.begin();
for ( ; it != l.macdef.end(); ++it )
macdef += it.key() + '\\' + it.data().join( "\\" ) + '\n';
configData["autoLoginMacro"] = macdef;
}
}
}
if (config)
configData += *config;
slave->setConfig(configData);
slave->setProtocol(url.protocol());
slave->setHost(host, port, user, passwd);
}
}
bool Scheduler::startJobScheduled(ProtocolInfo *protInfo)
{
if (protInfo->joblist.isEmpty())
return false;
// kdDebug(7006) << "Scheduling job" << endl;
debug_info();
bool newSlave = false;
SimpleJob *job = 0;
Slave *slave = 0;
if (protInfo->skipCount > 2)
{
bool dummy;
// Prevent starvation. We skip the first entry in the queue at most
// 2 times in a row. The
protInfo->skipCount = 0;
job = protInfo->joblist.tqat(0);
slave = findIdleSlave(protInfo, job, dummy );
}
else
{
bool exact=false;
SimpleJob *firstJob = 0;
Slave *firstSlave = 0;
for(uint i = 0; (i < protInfo->joblist.count()) && (i < 10); i++)
{
job = protInfo->joblist.tqat(i);
slave = findIdleSlave(protInfo, job, exact);
if (!firstSlave)
{
firstJob = job;
firstSlave = slave;
}
if (!slave) break;
if (exact) break;
}
if (!exact)
{
slave = firstSlave;
job = firstJob;
}
if (job == firstJob)
protInfo->skipCount = 0;
else
protInfo->skipCount++;
}
if (!slave)
{
if ( protInfo->maxSlaves > static_cast<int>(protInfo->activeSlaves.count()) )
{
newSlave = true;
slave = createSlave(protInfo, job, job->url());
if (!slave)
slaveTimer.start(0, true);
}
}
if (!slave)
{
// kdDebug(7006) << "No slaves available" << endl;
// kdDebug(7006) << " -- active: " << protInfo->activeSlaves.count() << endl;
return false;
}
protInfo->activeSlaves.append(slave);
idleSlaves->removeRef(slave);
protInfo->joblist.removeRef(job);
// kdDebug(7006) << "scheduler: job started " << job << endl;
JobData *jobData = extraJobData->tqfind(job);
setupSlave(slave, job->url(), jobData->protocol, jobData->proxy, newSlave);
job->start(slave);
slaveTimer.start(0, true);
return true;
}
bool Scheduler::startJobDirect()
{
debug_info();
SimpleJob *job = newJobs.take(0);
JobData *jobData = extraJobData->tqfind(job);
if (!jobData)
{
kdFatal(7006) << "BUG! startjobDirect(): No extraJobData for job!"
<< endl;
return false;
}
TQString protocol = jobData->protocol;
ProtocolInfo *protInfo = protInfoDict->get(protocol);
bool newSlave = false;
bool dummy;
// Look for matching slave
Slave *slave = findIdleSlave(protInfo, job, dummy);
if (!slave)
{
newSlave = true;
slave = createSlave(protInfo, job, job->url());
}
if (!slave)
return false;
idleSlaves->removeRef(slave);
// kdDebug(7006) << "scheduler: job started " << job << endl;
setupSlave(slave, job->url(), protocol, jobData->proxy, newSlave);
job->start(slave);
return true;
}
static Slave *searchIdleList(SlaveList *idleSlaves, const KURL &url, const TQString &protocol, bool &exact)
{
TQString host = url.host();
int port = url.port();
TQString user = url.user();
exact = true;
for( Slave *slave = idleSlaves->first();
slave;
slave = idleSlaves->next())
{
if ((protocol == slave->slaveProtocol()) &&
(host == slave->host()) &&
(port == slave->port()) &&
(user == slave->user()))
return slave;
}
exact = false;
// Look for slightly matching slave
for( Slave *slave = idleSlaves->first();
slave;
slave = idleSlaves->next())
{
if (protocol == slave->slaveProtocol())
return slave;
}
return 0;
}
Slave *Scheduler::findIdleSlave(ProtocolInfo *, SimpleJob *job, bool &exact)
{
Slave *slave = 0;
JobData *jobData = extraJobData->tqfind(job);
if (!jobData)
{
kdFatal(7006) << "BUG! findIdleSlave(): No extraJobData for job!" << endl;
return 0;
}
if (jobData->checkOnHold)
{
slave = Slave::holdSlave(jobData->protocol, job->url());
if (slave)
return slave;
}
if (slaveOnHold)
{
// Make sure that the job wants to do a GET or a POST, and with no offset
bool bCanReuse = (job->command() == CMD_GET);
KIO::TransferJob * tJob = dynamic_cast<KIO::TransferJob *>(job);
if ( tJob )
{
bCanReuse = (job->command() == CMD_GET || job->command() == CMD_SPECIAL);
if ( bCanReuse )
{
KIO::MetaData outgoing = tJob->outgoingMetaData();
TQString resume = (!outgoing.tqcontains("resume")) ? TQString() : outgoing["resume"];
kdDebug(7006) << "Resume metadata is '" << resume << "'" << endl;
bCanReuse = (resume.isEmpty() || resume == "0");
}
}
// kdDebug(7006) << "bCanReuse = " << bCanReuse << endl;
if (bCanReuse)
{
if (job->url() == urlOnHold)
{
kdDebug(7006) << "HOLD: Reusing held slave for " << urlOnHold.prettyURL() << endl;
slave = slaveOnHold;
}
else
{
kdDebug(7006) << "HOLD: Discarding held slave (" << urlOnHold.prettyURL() << ")" << endl;
slaveOnHold->kill();
}
slaveOnHold = 0;
urlOnHold = KURL();
}
if (slave)
return slave;
}
return searchIdleList(idleSlaves, job->url(), jobData->protocol, exact);
}
Slave *Scheduler::createSlave(ProtocolInfo *protInfo, SimpleJob *job, const KURL &url)
{
int error;
TQString errortext;
Slave *slave = Slave::createSlave(protInfo->protocol, url, error, errortext);
if (slave)
{
slaveList->append(slave);
idleSlaves->append(slave);
connect(slave, TQT_SIGNAL(slaveDied(KIO::Slave *)),
TQT_SLOT(slotSlaveDied(KIO::Slave *)));
connect(slave, TQT_SIGNAL(slaveStatus(pid_t,const TQCString &,const TQString &, bool)),
TQT_SLOT(slotSlaveStatus(pid_t,const TQCString &, const TQString &, bool)));
connect(slave,TQT_SIGNAL(authorizationKey(const TQCString&, const TQCString&, bool)),
sessionData,TQT_SLOT(slotAuthData(const TQCString&, const TQCString&, bool)));
connect(slave,TQT_SIGNAL(delAuthorization(const TQCString&)), sessionData,
TQT_SLOT(slotDelAuthData(const TQCString&)));
}
else
{
kdError() <<": couldn't create slave : " << errortext << endl;
if (job)
{
protInfo->joblist.removeRef(job);
extraJobData->remove(job);
job->slotError( error, errortext );
}
}
return slave;
}
void Scheduler::slotSlaveStatus(pid_t, const TQCString &, const TQString &, bool)
{
}
void Scheduler::_jobFinished(SimpleJob *job, Slave *slave)
{
JobData *jobData = extraJobData->take(job);
if (!jobData)
{
kdFatal(7006) << "BUG! _jobFinished(): No extraJobData for job!" << endl;
return;
}
ProtocolInfo *protInfo = protInfoDict->get(jobData->protocol);
delete jobData;
slave->disconnect(job);
protInfo->activeSlaves.removeRef(slave);
if (slave->isAlive())
{
JobList *list = coSlaves.tqfind(slave);
if (list)
{
assert(slave->isConnected());
assert(!coIdleSlaves->contains(slave));
coIdleSlaves->append(slave);
if (!list->isEmpty())
coSlaveTimer.start(0, true);
return;
}
else
{
assert(!slave->isConnected());
idleSlaves->append(slave);
slave->setIdle();
_scheduleCleanup();
// slave->send( CMD_SLAVE_STATUS );
}
}
if (protInfo->joblist.count())
{
slaveTimer.start(0, true);
}
}
void Scheduler::slotSlaveDied(KIO::Slave *slave)
{
assert(!slave->isAlive());
ProtocolInfo *protInfo = protInfoDict->get(slave->slaveProtocol());
protInfo->activeSlaves.removeRef(slave);
if (slave == slaveOnHold)
{
slaveOnHold = 0;
urlOnHold = KURL();
}
idleSlaves->removeRef(slave);
JobList *list = coSlaves.tqfind(slave);
if (list)
{
// coSlave dies, kill jobs waiting in queue
disconnectSlave(slave);
}
if (!slaveList->removeRef(slave))
kdDebug(7006) << "Scheduler: BUG!! Slave " << slave << "/" << slave->slave_pid() << " died, but is NOT in slaveList!!!\n" << endl;
else
slave->deref(); // Delete slave
}
void Scheduler::slotCleanIdleSlaves()
{
for(Slave *slave = idleSlaves->first();slave;)
{
if (slave->idleTime() >= MAX_SLAVE_IDLE)
{
// kdDebug(7006) << "Removing idle slave: " << slave->slaveProtocol() << " " << slave->host() << endl;
Slave *removeSlave = slave;
slave = idleSlaves->next();
idleSlaves->removeRef(removeSlave);
slaveList->removeRef(removeSlave);
removeSlave->connection()->close();
removeSlave->deref();
}
else
{
slave = idleSlaves->next();
}
}
_scheduleCleanup();
}
void Scheduler::_scheduleCleanup()
{
if (idleSlaves->count())
{
if (!cleanupTimer.isActive())
cleanupTimer.start( MAX_SLAVE_IDLE*1000, true );
}
}
void Scheduler::_putSlaveOnHold(KIO::SimpleJob *job, const KURL &url)
{
Slave *slave = job->slave();
slave->disconnect(job);
if (slaveOnHold)
{
slaveOnHold->kill();
}
slaveOnHold = slave;
urlOnHold = url;
slaveOnHold->suspend();
}
void Scheduler::_publishSlaveOnHold()
{
if (!slaveOnHold)
return;
slaveOnHold->hold(urlOnHold);
}
void Scheduler::_removeSlaveOnHold()
{
if (slaveOnHold)
{
slaveOnHold->kill();
}
slaveOnHold = 0;
urlOnHold = KURL();
}
Slave *
Scheduler::_getConnectedSlave(const KURL &url, const KIO::MetaData &config )
{
TQString proxy;
TQString protocol = KProtocolManager::slaveProtocol(url, proxy);
bool dummy;
Slave *slave = searchIdleList(idleSlaves, url, protocol, dummy);
if (!slave)
{
ProtocolInfo *protInfo = protInfoDict->get(protocol);
slave = createSlave(protInfo, 0, url);
}
if (!slave)
return 0; // Error
idleSlaves->removeRef(slave);
setupSlave(slave, url, protocol, proxy, true, &config);
slave->send( CMD_CONNECT );
connect(slave, TQT_SIGNAL(connected()),
TQT_SLOT(slotSlaveConnected()));
connect(slave, TQT_SIGNAL(error(int, const TQString &)),
TQT_SLOT(slotSlaveError(int, const TQString &)));
coSlaves.insert(slave, new TQPtrList<SimpleJob>());
// kdDebug(7006) << "_getConnectedSlave( " << slave << ")" << endl;
return slave;
}
void
Scheduler::slotScheduleCoSlave()
{
Slave *nextSlave;
slaveConfig = SlaveConfig::self();
for(Slave *slave = coIdleSlaves->first();
slave;
slave = nextSlave)
{
nextSlave = coIdleSlaves->next();
JobList *list = coSlaves.tqfind(slave);
assert(list);
if (list && !list->isEmpty())
{
SimpleJob *job = list->take(0);
coIdleSlaves->removeRef(slave);
// kdDebug(7006) << "scheduler: job started " << job << endl;
assert(!coIdleSlaves->contains(slave));
KURL url =job->url();
TQString host = url.host();
int port = url.port();
if (slave->host() == "<reset>")
{
TQString user = url.user();
TQString passwd = url.pass();
MetaData configData = slaveConfig->configData(url.protocol(), url.host());
slave->setConfig(configData);
slave->setProtocol(url.protocol());
slave->setHost(host, port, user, passwd);
}
assert(slave->protocol() == url.protocol());
assert(slave->host() == host);
assert(slave->port() == port);
job->start(slave);
}
}
}
void
Scheduler::slotSlaveConnected()
{
Slave *slave = (Slave *)sender();
// kdDebug(7006) << "slotSlaveConnected( " << slave << ")" << endl;
slave->setConnected(true);
disconnect(slave, TQT_SIGNAL(connected()),
this, TQT_SLOT(slotSlaveConnected()));
emit slaveConnected(slave);
assert(!coIdleSlaves->contains(slave));
coIdleSlaves->append(slave);
coSlaveTimer.start(0, true);
}
void
Scheduler::slotSlaveError(int errorNr, const TQString &errorMsg)
{
Slave *slave = (Slave *)sender();
if (!slave->isConnected() || (coIdleSlaves->tqfind(slave) != -1))
{
// Only forward to application if slave is idle or still connecting.
emit slaveError(slave, errorNr, errorMsg);
}
}
bool
Scheduler::_assignJobToSlave(KIO::Slave *slave, SimpleJob *job)
{
// kdDebug(7006) << "_assignJobToSlave( " << job << ", " << slave << ")" << endl;
TQString dummy;
if ((slave->slaveProtocol() != KProtocolManager::slaveProtocol( job->url(), dummy ))
||
(!newJobs.removeRef(job)))
{
kdDebug(7006) << "_assignJobToSlave(): ERROR, nonmatching or unknown job." << endl;
job->kill();
return false;
}
JobList *list = coSlaves.tqfind(slave);
assert(list);
if (!list)
{
kdDebug(7006) << "_assignJobToSlave(): ERROR, unknown slave." << endl;
job->kill();
return false;
}
assert(list->contains(job) == 0);
list->append(job);
coSlaveTimer.start(0, true); // Start job on timer event
return true;
}
bool
Scheduler::_disconnectSlave(KIO::Slave *slave)
{
// kdDebug(7006) << "_disconnectSlave( " << slave << ")" << endl;
JobList *list = coSlaves.take(slave);
assert(list);
if (!list)
return false;
// Kill jobs still in queue.
while(!list->isEmpty())
{
Job *job = list->take(0);
job->kill();
}
delete list;
coIdleSlaves->removeRef(slave);
assert(!coIdleSlaves->contains(slave));
disconnect(slave, TQT_SIGNAL(connected()),
this, TQT_SLOT(slotSlaveConnected()));
disconnect(slave, TQT_SIGNAL(error(int, const TQString &)),
this, TQT_SLOT(slotSlaveError(int, const TQString &)));
if (slave->isAlive())
{
idleSlaves->append(slave);
slave->send( CMD_DISCONNECT );
slave->setIdle();
slave->setConnected(false);
_scheduleCleanup();
}
return true;
}
void
Scheduler::_checkSlaveOnHold(bool b)
{
checkOnHold = b;
}
void
Scheduler::_registerWindow(TQWidget *wid)
{
if (!wid)
return;
TQObject *obj = TQT_TQOBJECT(wid);
if (!m_windowList.tqcontains(obj))
{
// We must store the window Id because by the time
// the destroyed signal is emitted we can no longer
// access TQWidget::winId() (already destructed)
WId windowId = wid->winId();
m_windowList.insert(obj, windowId);
connect(TQT_TQOBJECT(wid), TQT_SIGNAL(destroyed(TQObject *)),
this, TQT_SLOT(slotUnregisterWindow(TQObject*)));
TQByteArray params;
TQDataStream stream(params, IO_WriteOnly);
stream << windowId;
if( !kapp->dcopClient()->send( "kded", "kded",
"registerWindowId(long int)", params ) )
kdDebug(7006) << "Could not register window with kded!" << endl;
}
}
void
Scheduler::slotUnregisterWindow(TQObject *obj)
{
if (!obj)
return;
TQMap<TQObject *, WId>::Iterator it = m_windowList.tqfind(obj);
if (it == m_windowList.end())
return;
WId windowId = it.data();
disconnect( it.key(), TQT_SIGNAL(destroyed(TQObject *)),
this, TQT_SLOT(slotUnregisterWindow(TQObject*)));
m_windowList.remove( it );
if (kapp)
{
TQByteArray params;
TQDataStream stream(params, IO_WriteOnly);
stream << windowId;
kapp->dcopClient()->send( "kded", "kded",
"unregisterWindowId(long int)", params );
}
}
Scheduler* Scheduler::self() {
if ( !instance ) {
instance = new Scheduler;
}
return instance;
}
void Scheduler::virtual_hook( int id, void* data )
{ DCOPObject::virtual_hook( id, data ); }
#include "scheduler.moc"
|