import java.util.*;
import java.util.concurrent.*;
public class spider {
public spider(){
}
public static void main (String[] argv) {
int urllimit = 0, threadPoolSize = -1;
String starturl = "", word;
spider myspider = new spider();
Scanner keyboard = new Scanner(System.in);
while (urllimit < 1) {
System.out.print("Enter the maximum URLs to download: ");
word = keyboard.nextLine();
urllimit = Integer.parseInt(word);
if (word.length() == 0)
break;
}
while (threadPoolSize < 0) {
System.out.print("Enter the number of threads to use (0 for no threads): ");
word = keyboard.nextLine();
threadPoolSize = Integer.parseInt(word);
if (word.length() == 0)
break;
}
urlmonitor umon = new urlmonitor(1);
monitor mon = new monitor(urllimit);
data_gatherer wdata = new data_gatherer();
wdata.userInput();
starturl = wdata.getSeed();
umon.addurl(starturl,0);
parser myparser = new parser(mon,umon, wdata, "P1");
if(threadPoolSize == 0){
retriever myretriever = new retriever(mon,umon,"R1");
mon.stop();
while(true){
myretriever.run();
myparser.run();
if(wdata.geturls() >= urllimit){
break;
}
}
System.out.println(wdata);
}else {
ExecutorService tpes = Executors.newFixedThreadPool(threadPoolSize);
retriever[] workers = new retriever[threadPoolSize];
for (int i = 0; i < threadPoolSize; i++) {
workers[i] = new retriever(mon,umon, "R" + i);
tpes.execute(workers[i]);
}
myparser.start();
while (true) {
System.out.print("Enter a command(type ? for help): ");
word = keyboard.nextLine().toLowerCase();
if (word.length() == 0)
break;
if(word.equals("?")){
printhelp();
}
else if(word.equals("stop")){
mon.stop();
}
else if(word.equals("stats")){
System.out.print(wdata);
}
else if(word.equals("add")){
System.out.print("Enter a url: ");
starturl = keyboard.nextLine();
umon.addurl(starturl,0);
}
else if(word.equals("links")){
System.out.println("Links Found****************");
umon.printurls();
}
else if(word.equals("size")){
System.out.println("Size of webpage buffer: " + mon.webSize());
}
else if(word.equals("debug")){
umon.toggledbug();
System.out.println("Debug is " + umon.dbug());
}
else if(word.equals("proc")){
System.out.println("Number of pages processed: " + mon.processed());
}
}
System.out.println(wdata);
tpes.shutdownNow();
}
}
private static void printhelp(){
System.out.print("Command\t- description \nstats \t- display current information\n");
System.out.print("stop \t- halts processing \nadd \t- add a url\n");
System.out.print("links \t- print urls in queue \nsize \t- print the size of the page buffer\n");
System.out.print("debug \t- toggle debug mode \nproc \tprint the number of pages processed\n");
}
}