state = CrawlEvent.STARTED;
sendCrawlEvent (state);
synchronized (crawlQueue) {
Timer timer = new CrawlTimer (this);
int timeout = dp.getCrawlTimeout();
if (timeout > 0)
timer.set (timeout*1000, false);
int nWorms = Math.max (dp.getMaxThreads (), 1);
worms = new Worm[nWorms];
for (int i=0; i<nWorms; ++i) {
worms[i] = new Worm (this, i);
worms[i].start ();
}
try {
while (state == CrawlEvent.STARTED) {
if (numPagesLeft == 0) {
// ran out of links to crawl
state = CrawlEvent.STOPPED;
sendCrawlEvent (state);
}
else if (synchronous) {
// Synchronous mode.
// Main thread calls process() on each link
// in crawlQueue, in priority order.
Link link = (Link)crawlQueue.getMin ();
if (link.getStatus () == LinkEvent.DOWNLOADED)
process (link);
else
crawlQueue.wait ();
}
else
// Asynchronous crawling.
// Main thread does nothing but wait, while
// background threads call process().
crawlQueue.wait ();
}
} catch (InterruptedException e) {}
timer.cancel ();
for (int i=0; i<worms.length; ++i)
worms[i].die ();
if (state == CrawlEvent.PAUSED) {
// put partly-processed links back in fetchQueue