// (c) 2003-2007 Electronic Arts Inc.

// $Id: hellobloodhound.csharp,v 1.0 2002/02/14 11:40:00 dada Exp $
// http://dada.perl.it/shootout/

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;

using EA;

namespace EA
{

	class Bloodhound
	{
		// ---------------------------------------------------------
		public Bloodhound()
		{
			mServerName      = "adze.eac.ea.com";
//			mServerName      = "amaretto.eac.ea.com";
			IPHostEntry host = System.Net.Dns.GetHostByName(mServerName);
			Setup(host,8888);
		}
		// ---------------------------------------------------------
		public Bloodhound(string servername,int port)
		{
			mServerName      = servername;
			IPHostEntry host = System.Net.Dns.Resolve(mServerName);
			Setup(host,port);
		}
		// ---------------------------------------------------------
		public Bloodhound(long ip,int port)
		{
			IPHostEntry host = System.Net.Dns.GetHostByAddress(new IPAddress(ip));			
			Setup(host,port);
		}
		protected void Setup(IPHostEntry host,int port)
		{
			if(host != null && host.AddressList.GetLength(0) > 0)
			{
				mServer     = host.AddressList[0].Address;
				mServerName = host.HostName;
				mPort       = port;
			}
			else
			{
				mServerName = null;
				mServer = 0;
				mPort   = 0;
			}
		}
		// ---------------------------------------------------------
		public bool LogApplication(string toolname,int toolversion,string user,string comment)
		{

			if(mServer != 0 && mServerName != null)
			{
				string message = toolname + toolversion.ToString() + "PC";

				if(comment != null && comment != "")
				{
					message += ":" + comment;
				}
				message += "\n" + user + "\0";


		Console.WriteLine("message: " + message);
				
				return SendUdp(message);
			}
			return false;
		}
		// ---------------------------------------------------------
		public int GetPort()
		{
			return mPort;
		}
		// ---------------------------------------------------------
		public long GetServer()
		{
			return mServer;
		}
		// ---------------------------------------------------------
		public string GetServerString()
		{
			IPAddress temp = new IPAddress(mServer);
			return temp.ToString();
		}
		// ---------------------------------------------------------
		public string GetServerName()
		{
			return mServerName;
		}
		// ---------------------------------------------------------
		public bool SendUdp(string message)
		{
			if(mServer != 0 && mServerName != null)
			{
					// create a udp client
					UdpClient client                 = new UdpClient(mPort);
					IPEndPoint receivePoint          = new IPEndPoint(new IPAddress(mServer),mPort);
					// convert the data to a byte array for transport.
					System.Text.ASCIIEncoding encode = new System.Text.ASCIIEncoding();
					byte[] sendData                  = encode.GetBytes(message);
					client.Send(sendData,sendData.Length,receivePoint);
					return true;
			}
			return false;
		}
		// A bloodhound object knows to which server it reports
		// store that information internally.
		protected string mServerName;
		protected long   mServer;
		protected int    mPort;
	}
}

class App {

	[STAThread]

	public static int Main(String[] args)
	{
		Bloodhound bh = new Bloodhound();
		bh.LogApplication("CsharpBloodhoundTest",100,"Frank Barchard",null);
		Console.WriteLine("Sending To: " + bh.GetServerName() + " at " + bh.GetServerString() + "(" + bh.GetPort().ToString() + ")");
		Console.WriteLine("IntIP: " + bh.GetServer().ToString());

		Console.WriteLine("hello world\n");
        return(123);
	}

}

