SVERROR RegisterFX()

in host/common/hostagenthelpers.cpp [557:773]


SVERROR RegisterFX( const char *pszRegKey,
                     const char *pszSVServerName,
                     SV_INT HttpPort,
                     const char *pszURL,
                     const char *installPath,
                     char *pszHostID,
                     SV_ULONG *pdwHostIDSize,bool https)     // BUGBUG: no reason for this to be out parameter
{
    DebugPrintf(SV_LOG_DEBUG, "Entering %s\n", __FUNCTION__);
    const std::string strInstallPath = std::string(installPath) + "\\"; //Windows Specific code: FX-Windows-4.1
    const std::string patchfile = strInstallPath + "patch.log";
    SVERROR svError = S_OK;
    CRegKey cregkey, cregkeyCacheDir, cregkeyRoot;
    char szFileSystemType[MAX_DRIVES] [ BUFSIZ ] = {0};
    char szIpAddress[ 4*4 ];
    char insVolCapacityBuf[20], insVolFreeSpaceBuf[20], compatibilityNoBuf[20];
    SV_ULONGLONG insVolCapacity = 0, insVolFreeSpace = 0;
    //
    // Gather hostname, IP address, unique id, mounted volume list and send it to SV box
    //
    do
    {
        DebugPrintf( "@ LINE %d in FILE %s \n", __LINE__, __FILE__ );
        DebugPrintf( "ENTERED RegisterHost()...\n" );

        if( ( NULL == pszRegKey ) ||
            ( NULL == pszSVServerName )||
            ( NULL == pszURL ) )
        {
            svError = E_INVALIDARG;
            DebugPrintf( "@ LINE %d in FILE %s \n", __LINE__, __FILE__ );
            DebugPrintf( SV_LOG_ERROR, "FAILED RegisterHost(), err=%s\n", svError.toString() );
            break;
        }

        std::string strHostName;
        if (0 != s_szConfiguredHostname[0])
        {
            strHostName = std::string(s_szConfiguredHostname);
        }
        else
        {
            strHostName = Host::GetInstance().GetHostName();
        }

        // ip address
        std::string szIpAddress = Host::GetInstance().GetIPAddress();

        //
        // Determine what our host guid id is
        //
        DWORD dwResult = ERROR_SUCCESS;
        dwResult = cregkey.Create( HKEY_LOCAL_MACHINE, pszRegKey );
        if( ERROR_SUCCESS != dwResult )
        {
            svError = dwResult;
            DebugPrintf( "@ LINE %d in FILE %s \n", __LINE__, __FILE__ );
            DebugPrintf( SV_LOG_ERROR, "FAILED cregkey.Create()... RegKey: %s, err=%s\n", pszRegKey, svError.toString() );
            break;
        }

        HRESULT hr2 = GetHostId( pszHostID, *pdwHostIDSize );
        if( FAILED( hr2 ) )
        {
            svError = hr2;
            DebugPrintf( SV_LOG_ERROR, "FAILED Couldn't get host id, err=%s\n", svError.toString());
            break;
        }


        if( ERROR_SUCCESS != dwResult )
        {
            svError = dwResult;
            DebugPrintf( "@ LINE %d in FILE %s \n", __LINE__, __FILE__ );
            DebugPrintf( SV_LOG_ERROR, "FAILED cregkey.SetValue(), err=%s\n", svError.toString() );
            break;
        }
        std::string strOSInfo;    
        GetOperatingSystemInfo(strOSInfo);
        DebugPrintf( SV_LOG_DEBUG,"[HostAgentHelpers::RegisterHost()] OS Info = %s\n",
                                                            strOSInfo.c_str());

// osVal is added to identify osType 1 for windows and  2 for UNIX
char osVal[2];
#ifdef SV_WINDOWS
inm_strcpy_s(osVal, ARRAYSIZE(osVal), "1");
#else
inm_strcpy_s(osVal, ARRAYSIZE(osVal), "2");
#endif          
        // Patch History
        // Read patch.log which is present in installation directory 
        // Prepare patch_history with fields seperated by ',' & records seperated by '|'    

        int patchsize = 0;
        char *patch_history;

        long begin,end;
        ifstream myfile (patchfile.c_str());
        if(!myfile.fail())
        {
            begin = myfile.tellg();
            myfile.seekg (0, ios::end);
            end = myfile.tellg();
            myfile.close();
        }
        else
        {
            DebugPrintf(SV_LOG_WARNING, "Could not open patch file: %s", patchfile.c_str());
            patch_history = new char[1];
            patch_history[0] = '\0';
            patchsize = 0;
        }

        fstream filestrm;
        filestrm.open (patchfile.c_str(), fstream::in); // | fstream::out | fstream::app);
        if(!filestrm.fail())
        {
            int k=0;
            int deletedflag = 0;
        
            char chr = ' ';
            INM_SAFE_ARITHMETIC(patchsize = InmSafeInt<long>::Type(end) - begin, INMAGE_EX(end)(begin))

            if(patchsize)
            {
                patch_history = new char[patchsize];
                memset(patch_history, 0,patchsize);
                DebugPrintf(SV_LOG_DEBUG," Opened patch file of length %ld characters\n", patchsize);

                while((chr=filestrm.get())!= EOF)
                {
                    if( chr == '#')
                    {
                        deletedflag = 1;
                        continue;
                    }

                    if(deletedflag && chr == '\n')
                    {
                        deletedflag = 0;
                        continue;
                    }
                    
                    if(deletedflag)
                        continue;

                if (chr == '\n')
                        patch_history[k++] = '|';
                    else
                        patch_history[k++] = chr;                
                }
                patch_history[k] = '\0';
                patchsize = k;
                filestrm.close();
                DebugPrintf(SV_LOG_DEBUG," patch_history = %s\n", patch_history );
            }
            else
            {
                patch_history = new char[1];
                patch_history[0] = '\0';
                patchsize = 0;
            }
        }

        string installDir, zone, postBuffer;
        zone = getTimeZone();
        getInstallVolumeParameters(strInstallPath, &insVolCapacity, &insVolFreeSpace);
        inm_sprintf_s(insVolCapacityBuf, ARRAYSIZE(insVolCapacityBuf), "%llu", insVolCapacity);
        inm_sprintf_s(insVolFreeSpaceBuf, ARRAYSIZE(insVolFreeSpaceBuf), "%llu", insVolFreeSpace);
        inm_sprintf_s(compatibilityNoBuf, ARRAYSIZE(compatibilityNoBuf), "%lu", CurrentCompatibilityNum());

        postBuffer += "hostname=" + strHostName
            + "&ip=" + szIpAddress
            + "&id=" + string(pszHostID)
            + "&os=" + strOSInfo
            + "&av=" + string(GetProductVersion())
            + "&installpath=" + strInstallPath
            + "&compatibilityNo=" + string(compatibilityNoBuf)
            + "&osVal=" + string(osVal)
            + "&invc=" + string(insVolCapacityBuf)
            + "&infs=" + string(insVolFreeSpaceBuf)
            + "&dttm=" + getLocalTime()
            + "&patch_history=" + string(patch_history)
            + ((zone[0] == '+')? "&tmzn=%2B" : "&tmzn=-" ) ;// Following [HTTP:PostMethod] standard. 
                                                            // Replace '+' with '%2B' escape sequence 
        postBuffer += zone.substr(1);                  
        std::string prod_version = PROD_VERSION;
        postBuffer = postBuffer + "&prodVerson=" + prod_version;
        DebugPrintf(SV_LOG_DEBUG,"[HostAgentHelpers::RegisterHost()] postBuffer = %s\n",postBuffer.c_str());
        DebugPrintf( "@ LINE %d in FILE %s \n", __LINE__, __FILE__ );
        
        svError = postToCx( pszSVServerName,
                        HttpPort,
                        pszURL,
                        (char*)postBuffer.c_str(),
                        NULL,https);

    

        if( svError.failed() )
        {
            DebugPrintf( "@ LINE %d in FILE %s \n", __LINE__, __FILE__ );
            DebugPrintf( SV_LOG_ERROR, "FAILED PostToSVServer()... SVServerName: %s; URL: %s, err=%s\n",
                pszSVServerName,
                pszURL,
                svError.toString() );
            break;
        }
    }
    while( FALSE );

    cregkey.Close();
    cregkeyRoot.Close();
    cregkeyCacheDir.Close();
    DebugPrintf(SV_LOG_DEBUG, "Exiting %s\n", __FUNCTION__);
    return( svError );
}