/************************************************************************
 * This library is free software; you can redistribute it and/or        *
 * modify it under the terms of the GNU Library General Public          *
 * License as published by the Free Software Foundation; either         *
 * version 2 of the License, or (at your option) any later version.     *
 *                                                                      *
 * 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; if not, write to the                *
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,         *
 * Boston, MA  02111-1307, USA.                                         *
 *                                                                      *
 * The GNU Library General Public License file is included in           *
 * Online-Documentation                                                 *
 *                                                                      *
 * GNU's Homepage - http://www.gnu.org/                                 *
 *                                                                      *
 * Questions & comments referring to Hexi: please contact us:           *
 *                                                                      *
 * eMail:                                                               *
 *      ramsey@dbai.tuwien.ac.at                                        *
 * WWW:                                                                 *
 *      http://www.dbai.tuwien.ac.at/proj/ramsey/                       *
 *                                                                      *
 * (c) Wolfgang Slany 1987, Bidan Zhu 1996, Andreas Beer 1999           *
 * LoadDataThread.java              Version 2.2              19.07.1999 *
 ***********************************************************************/

import java.net.*;
import java.io.*;

public class LoadDataThread extends Thread
{
	private static int	portnumber = 8808;
	private URL Winner_FileURL1, Key_FileURL1;
	private URL Winner_FileURL2, Key_FileURL2;
	private DataInputStream Winner_InputStream1,Key_InputStream1;
	private DataInputStream Winner_InputStream2,Key_InputStream2;
	Hexi hexi;
	int key[];

    	public LoadDataThread (Hexi parent)
	{
		hexi = parent;
	}

	public void run()
	{
		boolean Data1Error = false, Data2Error = false;
		System.out.println ("Load database from server begins ");
		try
		{
			Winner_FileURL2 = new URL(hexi.getCodeBase(),"hexi.mem2");
			Key_FileURL2    = new URL(hexi.getCodeBase(),"hexi.key2");
//			Key_InputStream2	= new DataInputStream(new BufferedInputStream(Key_FileURL2.openStream()));
//		 	Winner_InputStream2 = new DataInputStream(new BufferedInputStream(Winner_FileURL2.openStream()));
			Key_InputStream2	= new DataInputStream(Key_FileURL2.openStream());
		 	Winner_InputStream2 = new DataInputStream(Winner_FileURL2.openStream());
			Read_To_Array(Winner_InputStream2,hexi.winner[0]);
			Read_To_Array(Key_InputStream2,hexi.hashkey1[0]);
			Read_To_Array(Key_InputStream2,hexi.hashkey1[1]);
			Read_To_Array(Key_InputStream2,hexi.hashkey1[2]);
		}
		catch (MalformedURLException e)
		{
			System.out.println ("Failed getting data2 input from server");
			Data2Error = true;
		}
		catch (IOException e)
		{
			System.err.println("Couldn't get I/O for the data2 loading."+ e.getMessage());
			Data2Error = true;
		}
		finally
		{
			try
			{
				if (Key_InputStream2 != null)	Key_InputStream2.close();
				if (Winner_InputStream2 != null) Winner_InputStream2.close();
			}
			catch (IOException e) {};
		}
		System.out.println("Read database for HEXI");
		try
		{
			Winner_FileURL1 = new URL(hexi.getCodeBase(),"hexi.mem1");
			Key_FileURL1    = new URL(hexi.getCodeBase(),"hexi.key1");
//			Key_InputStream1	= new DataInputStream(new BufferedInputStream(Key_FileURL1.openStream()));
//		 	Winner_InputStream1 = new DataInputStream(new BufferedInputStream(Winner_FileURL1.openStream()));
			Key_InputStream1	= new DataInputStream(Key_FileURL1.openStream());
		 	Winner_InputStream1 = new DataInputStream(Winner_FileURL1.openStream());
			Read_To_Array(Winner_InputStream1,hexi.winner[1]);
			Read_To_Array(Winner_InputStream1,hexi.winner[2]);
			Read_To_Array(Key_InputStream1,hexi.hashkey2[0]);
			Read_To_Array(Key_InputStream1,hexi.hashkey2[1]);
			Read_To_Array(Key_InputStream1,hexi.hashkey2[2]);
		}
		catch (MalformedURLException e)
		{
			System.out.println ("Failed building URL for getting data1 input from server");
			Data1Error = true;
		}
		catch (IOException e)
		{
			System.err.println("Couldn't get I/O for the data1 loading."+ e.getMessage());
			Data1Error = true;
		}
		finally
		{
			try
			{
				if (Key_InputStream1 != null)	Key_InputStream1.close();
				if (Winner_InputStream1 != null) Winner_InputStream1.close();
			}
			catch (IOException e) {};
		}
		System.out.println("Read database for Ramsey-6");
		if (Data2Error)
		{
			System.out.println ("Database for HEXI must be regenerated!");
			hexi.initWinner(2);
			System.out.println ("Database for HEXI regenerated!");
		}
		hexi.ReadTreeReady2 = true;
		if (Data1Error)
		{
			System.out.println ("Database for Ramsey-6 must be regenerated!");
			hexi.initWinner(1);
			System.out.println ("Database for Ramsey-6 regenerated!");
		}
		hexi.ReadTreeReady1 = true;
		System.out.println ("Reading database from server is over!");
	} // run

	void Read_To_Array (DataInputStream in, byte w[]) throws IOException
	{
		int Read_One_Time, Read_Tillnow = 0, offset;
		int size = w.length;
		byte buffer[] = new byte[size];

		while ((Read_One_Time=in.read(buffer)) != -1)
		{
			System.out.println ("Data read: "+Read_One_Time+"  Till now: "+Read_Tillnow);
			offset = Read_Tillnow + Read_One_Time - size;
			if (offset > 0) Read_One_Time -= offset;
			for (int i=0;i<Read_One_Time;i++) w[i+Read_Tillnow] = buffer[i];
			if (offset >= 0) return;
			Read_Tillnow += Read_One_Time;
		}
	} // Read_To_Array

} // LoadDataThread