/************************************************************************
 * 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           *
 * HexiClient.java                Version 2.6                18.08.1999 *
 ***********************************************************************/

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

public class HexiClient extends Thread
{
	Hexi hexi;
	private boolean Mode;
	private String host;
	private int port = 8808;
	private Socket HexiSocket = null ;
	private DataOutputStream out = null;

	private double passcode = 70389367930;
	private String PlayerName;
	private long Score;

	HexiClient(Hexi parent)
	{
		hexi = parent;
		Mode = true;
	}

	HexiClient(Hexi parent, String name, long score)
	{
		hexi = parent;
		Mode = false;
		PlayerName = name;
		Score = score;
	}

	public void run()
	{
		System.out.println("Client is trying to connect to server !");
		host = hexi.getCodeBase().getHost();
		try
		{
			HexiSocket = new Socket(host, port);
			out = new DataOutputStream(HexiSocket.getOutputStream());
		}
		catch (UnknownHostException e)
		{
			System.err.println("Unknown host: "+host);
		}
		catch (IOException e)
		{
			System.err.println("Could not connect to: "+host);
		}
		if (HexiSocket != null && out != null)
		{
			try
			{
				out.writeDouble(passcode);
				out.writeBoolean(Mode);
				out.writeBoolean(hexi.Allow_More);
				if (Mode)
				{
					// Key Transfer
					for (int i=0;i<hexi.Key_To_Transfer.length;i++)
					{
						out.writeInt(hexi.Key_To_Transfer[i]);
						System.out.println ("Trans_Data_Thread: Write "+hexi.Key_To_Transfer[i]);
					}
				}
				else
				{
					// Hall of Fame
					PlayerName = PlayerName.concat("\n");
					out.writeBytes(PlayerName);
					out.writeLong(Score);
				}
				out.writeInt(0);
				System.out.println("Write 0 to end the transaction!");
				out.close();
				HexiSocket.close();
			}
			catch (IOException e)
			{
				System.err.println("Transfer to the connection");
			}
		} // If
	} // run

} // HexiClient