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

/*------------------------------------------------------------------*/
/*                                                                  */
/*                       Bloodhound API v1.00                       */
/*                                                                  */
/*                     by FrANK G. Barchard, EAC                    */
/*                                                                  */
/*                        Code - Oct 02, 2002                       */
/*                                                                  */
/*------------------------------------------------------------------*/
/*                                                                  */
/* Version Date   SE History                                        */
/* ------- ------ -- -------                                        */
/*    1.00 021002 FB from gx                                        */
/*                                                                  */
/*------------------------------------------------------------------*/

/****************************************************************/
/*  Includes                                                    */
/****************************************************************/

/*!
 * \file bloodhound.cpp
 * \brief support functions for logging user activity to EAC servers
 */

#include "spit.h"

#if defined(__PPU__)
extern "C" void BLOODHOUND_Message(const char *ToolName,int ToolVersion, const char *Comment)
{
	return;
}
#else

#if defined(_MSC_VER)
#pragma warning(push,1)
#include "process.h"
#include "windows.h"
#include "winbase.h"
#pragma warning(pop)
#endif

#include "bloodhound.h"
#include "gsocket.h"

// For UDP socket communications
// Temporary point to \\2Step
#define UDP_LOG
#define SOCKET_SERVER "10.10.12.138" // adze
//#define SOCKET_SERVER "adze.eac.ea.com"
#define SOCKET_PORT   8888
static  char    LOGAPPuser[SPITMAXLINE] = { "UNKNOWN" };
static  GSOCKET *logsocket = NULL;

//------------------------------------------------------------------------
// report what version of windows the user is running, otherwise return
// non-windows
#ifdef _MSC_VER
static void getusername(char *user)
{
    char    *registry;
    char    *os;
    char    *Windows    = "Software\\Microsoft\\Windows\\CurrentVersion";
    char    *WindowsNT  = "Software\\Microsoft\\Windows NT\\CurrentVersion";
    char    *User       = "RegisteredOwner";
//  char    *Company    = "RegisteredOrganization";
    int     regerror;
    HKEY    phKeyResult;
    char    data[1024];
    DWORD   datalen;

    os = getenv("OS");
    if (os != NULL)
    {
        if (strcmp(os, "Windows_NT") == 0)
            registry = WindowsNT;
        else
            registry = Windows;
    } else
        registry = Windows;

    regerror = RegOpenKeyEx(HKEY_LOCAL_MACHINE,registry,0,KEY_ALL_ACCESS, &phKeyResult);
    if (!regerror)
    {
        datalen = sizeof(data);
        regerror = RegQueryValueEx(phKeyResult,User,NULL,NULL,(LPBYTE)data,&datalen);
        if (regerror == 0)
            strcpy(user, data);

        RegCloseKey(phKeyResult);
    }

}
#else
static void getusername(char *user)
{
    strcpy(user, "non-windows");
}
#endif


#ifdef WIN0
    _beginthread(LogThread,NULL,8192,msg);
    atexit(LogExit);
#endif

//------------------------------------------------------------------------
// what is user doing? Report to Tools and Libs how Gx is being used.
static int LOGAPP_sendmessage(const char *ToolName,int ToolVersion, const char *User, const char *Comment)
{
    int ok=1;
    char msgbuffer[SPITMAXLINE];

    if (logsocket)
    {
        if (Comment)
            sprintf(msgbuffer, "%s%d%s:%s\n%s",ToolName,ToolVersion,SPIT_MACHINE,Comment,User);
        else
            sprintf(msgbuffer, "%s%d%s\n%s",ToolName,ToolVersion,SPIT_MACHINE,User);

        ok = GSOCKET_write(logsocket,msgbuffer,strlen(msgbuffer)+1);
    }
    else
    {
        if (OPTverbose>=4)
            printf( "%s - ERROR GSOCKET_wopen failed\n",ToolName);
    }

    return(ok);
}

//------------------------------------------------------------------------
// open socket and begin spying on user activity.
static int LOGAPP_init(void)
{
    if (logsocket == NULL)
        logsocket = GSOCKET_wopen(SOCKET_SERVER,SOCKET_PORT);

    return(!(logsocket == NULL));
}

//------------------------------------------------------------------------
// close socket and quit spying on user activity.
static void LOGAPP_restore(void)
{
    if (logsocket)
    {
        GSOCKET_wclose(logsocket);
        logsocket = NULL;
    }
    return;
}



/*!
************************************************************
 \brief send logging message to the EAC server.
        Lets us watch out users and find out how they use our tools
   \param ToolName the name of the application that is running
   \param ToolVersion the version of the application
   \param Comment a comment you wished passed to bloodhound, causes a seperate entry for different comment values
************************************************************/
extern "C" void BLOODHOUND_Message(const char *ToolName,int ToolVersion, const char *Comment)
{
    static bool loggedonce = false;                                              // keep track if I have been here already.

    if (OPTlog && !loggedonce)                                                   // note only log user activity once and if requested.
    {
        int i             = 0;
        int ok            = 0;
        double logtime    = SPIT_gettime();

        ok = LOGAPP_init();                                                      // prepare to send to the server.
        if (ok)
        {
            getusername(LOGAPPuser);                                             // send who is doing the using.

            LOGAPP_sendmessage(ToolName, ToolVersion, LOGAPPuser, Comment);
            LOGAPP_restore();
        }
        else                                                                     // if we have a problem report it to the user.
        {
            if (OPTverbose>=4)
                printf("%s - ERROR OPENING LOG SOCKET\n",ToolName);
        }
        loggedonce = true;                                                       // set that we have done this once and don't want
                                                                                 // to do again.
        if (OPTtime && OPTverbose>=4)
        {
            logtime = SPIT_gettime()-logtime;                                    // if the user needs to know let them know how much
            if (logtime>=10000.0)                                                // time I wasted logging their activity.
                printf("TIME %s logtime %.2f ms (%.2f seconds)\n",ToolName,logtime,logtime/1000.0);
            else
                printf("TIME %s logtime %.2f ms\n",ToolName,logtime);
        }
    }
}

#endif
