You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
165 lines
5.4 KiB
165 lines
5.4 KiB
13 years ago
|
// loaderThreads.java
|
||
20 years ago
|
// ---------------------------
|
||
17 years ago
|
// (C) by Michael Peter Christen; mc@yacy.net
|
||
20 years ago
|
// first published on http://www.anomic.de
|
||
|
// Frankfurt, Germany, 2004
|
||
14 years ago
|
//
|
||
|
// $LastChangedDate$
|
||
|
// $LastChangedRevision$
|
||
|
// $LastChangedBy$
|
||
20 years ago
|
//
|
||
|
// This program is free software; you can redistribute it and/or modify
|
||
|
// it under the terms of the GNU General Public License as published by
|
||
|
// the Free Software Foundation; either version 2 of the License, or
|
||
|
// (at your option) any later version.
|
||
|
//
|
||
|
// This program 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 General Public License for more details.
|
||
|
//
|
||
|
// You should have received a copy of the GNU General Public License
|
||
|
// along with this program; if not, write to the Free Software
|
||
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||
|
|
||
12 years ago
|
package net.yacy.utils;
|
||
20 years ago
|
|
||
13 years ago
|
import java.util.HashMap;
|
||
|
import java.util.Map;
|
||
20 years ago
|
|
||
14 years ago
|
import net.yacy.cora.protocol.ClientIdentification;
|
||
14 years ago
|
import net.yacy.cora.protocol.http.ProxySettings;
|
||
15 years ago
|
import net.yacy.kelondro.data.meta.DigestURI;
|
||
|
|
||
20 years ago
|
public class loaderThreads {
|
||
13 years ago
|
|
||
20 years ago
|
// global values for loader threads
|
||
19 years ago
|
protected int timeout;
|
||
|
protected String user;
|
||
|
protected String password;
|
||
15 years ago
|
protected ProxySettings remoteProxyConfig;
|
||
20 years ago
|
|
||
|
// management objects for collection of threads
|
||
13 years ago
|
private final Map<String, Thread> threads;
|
||
13 years ago
|
private int completed, failed;
|
||
13 years ago
|
|
||
20 years ago
|
public loaderThreads() {
|
||
17 years ago
|
this(10000, null, null);
|
||
20 years ago
|
}
|
||
13 years ago
|
|
||
19 years ago
|
public loaderThreads(
|
||
13 years ago
|
final int timeout,
|
||
|
final String user,
|
||
17 years ago
|
final String password
|
||
19 years ago
|
) {
|
||
20 years ago
|
this.timeout = timeout;
|
||
|
this.user = user;
|
||
|
this.password = password;
|
||
13 years ago
|
this.threads = new HashMap<String, Thread>();
|
||
20 years ago
|
this.completed = 0;
|
||
|
this.failed = 0;
|
||
|
}
|
||
13 years ago
|
|
||
15 years ago
|
public void newThread(final String name, final DigestURI url, final loaderProcess process) {
|
||
17 years ago
|
final Thread t = new loaderThread(url, process);
|
||
13 years ago
|
this.threads.put(name, t);
|
||
20 years ago
|
t.start();
|
||
|
}
|
||
13 years ago
|
|
||
17 years ago
|
public void terminateThread(final String name) {
|
||
13 years ago
|
final loaderThread t = (loaderThread) this.threads.get(name);
|
||
20 years ago
|
if (t == null) throw new RuntimeException("no such thread: " + name);
|
||
17 years ago
|
t.terminate();
|
||
20 years ago
|
}
|
||
13 years ago
|
|
||
17 years ago
|
public int threadCompleted(final String name) {
|
||
13 years ago
|
final loaderThread t = (loaderThread) this.threads.get(name);
|
||
20 years ago
|
if (t == null) throw new RuntimeException("no such thread: " + name);
|
||
17 years ago
|
return t.completed();
|
||
20 years ago
|
}
|
||
13 years ago
|
|
||
17 years ago
|
public int threadStatus(final String name) {
|
||
13 years ago
|
final loaderThread t = (loaderThread) this.threads.get(name);
|
||
20 years ago
|
if (t == null) throw new RuntimeException("no such thread: " + name);
|
||
17 years ago
|
return t.status();
|
||
20 years ago
|
}
|
||
13 years ago
|
|
||
20 years ago
|
public int completed() {
|
||
13 years ago
|
return this.completed;
|
||
20 years ago
|
}
|
||
13 years ago
|
|
||
20 years ago
|
public int failed() {
|
||
13 years ago
|
return this.failed;
|
||
20 years ago
|
}
|
||
13 years ago
|
|
||
20 years ago
|
public int count() {
|
||
13 years ago
|
return this.threads.size();
|
||
20 years ago
|
}
|
||
13 years ago
|
|
||
17 years ago
|
public Exception threadError(final String name) {
|
||
13 years ago
|
final loaderThread t = (loaderThread) this.threads.get(name);
|
||
20 years ago
|
if (t == null) throw new RuntimeException("no such thread: " + name);
|
||
17 years ago
|
return t.error();
|
||
20 years ago
|
}
|
||
|
|
||
|
protected class loaderThread extends Thread {
|
||
15 years ago
|
private final DigestURI url;
|
||
20 years ago
|
private Exception error;
|
||
17 years ago
|
private final loaderProcess process;
|
||
18 years ago
|
private byte[] page;
|
||
20 years ago
|
private boolean loaded;
|
||
13 years ago
|
|
||
15 years ago
|
public loaderThread(final DigestURI url, final loaderProcess process) {
|
||
20 years ago
|
this.url = url;
|
||
|
this.process = process;
|
||
|
this.error = null;
|
||
|
this.page = null;
|
||
|
this.loaded = false;
|
||
|
}
|
||
|
|
||
13 years ago
|
@Override
|
||
20 years ago
|
public void run() {
|
||
|
try {
|
||
13 years ago
|
this.page = this.url.get(ClientIdentification.getUserAgent(), loaderThreads.this.timeout);
|
||
|
this.loaded = true;
|
||
|
this.process.feed(this.page);
|
||
|
if (this.process.status() == loaderCore.STATUS_FAILED) {
|
||
|
this.error = this.process.error();
|
||
20 years ago
|
}
|
||
13 years ago
|
if ((this.process.status() == loaderCore.STATUS_COMPLETED) ||
|
||
|
(this.process.status() == loaderCore.STATUS_FINALIZED)) loaderThreads.this.completed++;
|
||
|
if ((this.process.status() == loaderCore.STATUS_ABORTED) ||
|
||
|
(this.process.status() == loaderCore.STATUS_FAILED)) loaderThreads.this.failed++;
|
||
17 years ago
|
} catch (final Exception e) {
|
||
13 years ago
|
this.error = e;
|
||
|
loaderThreads.this.failed++;
|
||
20 years ago
|
}
|
||
|
}
|
||
13 years ago
|
|
||
20 years ago
|
public void terminate() {
|
||
13 years ago
|
this.process.terminate();
|
||
20 years ago
|
}
|
||
13 years ago
|
|
||
20 years ago
|
public boolean loaded() {
|
||
13 years ago
|
return this.loaded;
|
||
20 years ago
|
}
|
||
13 years ago
|
|
||
20 years ago
|
public int completed() {
|
||
13 years ago
|
if (this.process.status() == loaderCore.STATUS_READY) return 1;
|
||
|
if (this.process.status() == loaderCore.STATUS_RUNNING) return 9 + ((this.process.completed() * 9) / 10);
|
||
|
if (this.process.status() == loaderCore.STATUS_COMPLETED) return 100;
|
||
20 years ago
|
return 0;
|
||
|
}
|
||
|
|
||
|
public int status() {
|
||
13 years ago
|
return this.process.status(); // see constants in loaderCore
|
||
20 years ago
|
}
|
||
13 years ago
|
|
||
20 years ago
|
public Exception error() {
|
||
13 years ago
|
return this.error;
|
||
20 years ago
|
}
|
||
13 years ago
|
|
||
20 years ago
|
}
|
||
15 years ago
|
|
||
20 years ago
|
}
|