

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.URL;
import java.net.URLConnection;

//
/**
 * Spider: Main class for the web spider 'bot.
 *
 * 
 */
public class Spider {

	public static void main(String[] args) {
		Spider s = new Spider();
		s.loadModel();
		System.out.println("Crawl begun");
		s.crawlWEB();
		System.out.println("Crawl ended");
		s.saveModel();
	}

	/* *********** End of public interface ********** */
	/** Spider constructor */ 
	private Spider() {
		loadFile = "defaultCrawlFile.moog";
		saveFile = loadFile;
		model = new SpiderModel();
		currentPage = null;
		currentURL = null;
		pageDelay = 3;

	}

	/**
	 * Load the model from the file specified in {@link #_loadFile}.  If no
	 * such file exists, exits silently without changing the current model.
	 */
	private void loadModel() throws SpiderWarningException {
		System.out.println("Opening file " + loadFile);
		if (loadFile != null) {
			ObjectInputStream inS = null;
			File fp = new File(loadFile);
			inS = new ObjectInputStream(new BufferedInputStream(new FileInputStream(loadFile)));
			Object o = inS.readObject();
			model = (SpiderModel) o;
			inS.close();
		}
	}

	/**
	 * Save the model to the file specified in {@link #_saveFile}
	 */
	private void saveModel() throws SpiderWarningException {
		System.out.println("Saving file " + loadFile);
		if (saveFile != null) {
			ObjectOutputStream outS = null;
			outS =	new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(saveFile)));
			outS.writeObject(model);
			outS.flush();
			outS.close();
			
		}
	}

	/**
	 * Actually execute the crawl.  This is the function that does the bulk of
	 * the work of this model.
	 */
	private void crawlWEB() throws SpiderFatalException {
		for (int ii = 0; ii < model.getSize(); ii++) {
			currentPage = model.getURL(ii);
			System.out.println("Connecting to: " + currentPage);
			currentURL = new URL(currentPage);
			URLConnection c = currentURL.openConnection();
			c.connect();
			Thread.sleep(pageDelay * 1000);
		}
	}
	/** name of the file to be loaded */
	private String loadFile;
	/** name of the file to be saved */
	private String saveFile;
	/** model Object: here you can find URLs */
	private SpiderModel model;
	/** String with URL to be checked */
	private String currentPage;
	/** URL object used for connection */
	private URL currentURL;
	/** time delay between page loads, in seconds */
	private int pageDelay;
}
