Actions

CTBCAFServiceCmMgmtClient

Revision as of 09:54, 8 September 2022 by Allyntree (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

The class CTBCAFServiceCmMgmtClient is used by applications that want to be informed of the current active configuration, and active database connection string.

An application that initializes this class has to provide a callback to interface ITBCAFServiceCmMgmtClient. This interface provides 3 functions that the application must implement:

  • OnValidateConfig: Tells the application to validate the given configuration. This is already handled by standard, Toolpack applications so it's not required to do anything here.
  • OnReloadConfig: Tells the application that the configuration has been modified (or active configuration changed), and the application must reload any information from the database that it previously loaded and kept locally
  • OnDbMaintenance: This function is called to indicate to the application that the database is in maintenance mode (or unavailable) and should not be accessed. A call to OnReloadConfig will indicate when the database is ready again.


Example usage

class MyApplication : public ITBCAFServiceCmMgmtClient
{
	// your class definition

	// Functions inherited from ITBCAFServiceCmMgmtClient
 	virtual TBX_VOID OnValidateConfig( TBX_UINT32 in_un32ConfigurationId, TBX_UINT32 in_un32ApplicationId, CTBCAFString in_strName );
 	virtual TBX_VOID OnReloadConfig( TBX_UINT32 in_un32ConfigurationId, CTBCAFString in_strName, CTBCAFString in_strDbConnString );
	virtual TBX_VOID OnDbMaintenance();

	PITBCAFDb						mpConfigDb;
	TBX_UINT32						mun32ConfigurationId;
	TBX_BOOL						mfDbMaintenance;
};

TBX_RESULT MyApplication::Init()
{
	TBX_RESULT			Result = TBX_RESULT_OK;

	// Database access object will be initialized later (once we know the connection string)
	mpConfigDb		= NULL;
	mfDbMaintenance	= TBX_TRUE;

	// Initialize the CM client
	Result = CTBCAFServiceCmMgmtClient::Init(this);

	return Result;
}

TBX_RESULT MyApplication::UnInit()
{
	TBX_RESULT			Result = TBX_RESULT_OK;

	// Terminate CM client
	Result = CTBCAFServiceCmMgmtClient::Uninit();

	// Destroy the database access object
	delete mpConfigDb;
	mpConfigDb = NULL;

	return Result;
}

TBX_VOID MyApplication::OnValidateConfig
(
  IN		TBX_UINT32	 					in_un32ConfigurationId,
  IN		TBX_UINT32	 					in_un32ApplicationId,
  IN		CTBCAFString 					in_strName
)
{
	// This application has nothing to validate in the database configuration
	TBX_RESULT			ValidateResult	= TBX_RESULT_OK;
	CTBCAFString		strValidateResult( "MyApplication: Ok (nothing to validate)" );
	CTBCAFServiceCmMgmtClient::SendReloadDone( ValidateResult, strValidateResult.c_str() );
}

TBX_VOID MyApplication::OnReloadConfig
(
  IN		TBX_UINT32	 					in_un32ConfigurationId,
  IN		CTBCAFString 					in_strName,
  IN		CTBCAFString 					in_strDbConnString
)
{
	TBX_RESULT			Result	= TBX_RESULT_OK;
	CTBCAFString		strReloadResult( "MyApplication: configuration reloaded successfully" );

	CAFCODE( MyApplication::OnReloadConfig )
	{
		// Remember the current active configuration id
		mun32ConfigurationId = in_un32ConfigurationId;

		// Update the database connection string
		if( mpConfigDb )
		{
			// Close current database connection.
			mpConfigDb->Close();
			mpConfigDb->SetConnectString(in_strDbConnString);
		}
		else
		{
			mpConfigDb = tbnew CTBCAFDb( in_strDbConnString.c_str() );
			if( !mpConfigDb )
			{
				strReloadResult.Format( "Failed to allocate mpConfigDb" );
				TBX_EXIT_ERROR( TBX_RESULT_OUT_OF_MEMORY, 0, strReloadResult.c_str() );
			}

			Result = mpConfigDb->SetOptions(ITBCAFDb::DB_OPTION_AUTO_RECONNECT);
			if( TBX_RESULT_FAILURE( Result ) )
			{
				strReloadResult.Format( "Failed SetOptions (Result 0x%08X)", Result );
				TBX_EXIT_ERROR( Result, 0, strReloadResult.c_str() );
			}
		}

		// Re-open using the new connection string
		if( mpConfigDb )
		{
			Result = mpConfigDb->Open();
			if( TBX_RESULT_FAILURE( Result ) )
			{
				strReloadResult.Format( "Unable to open database (Result 0x%08X): %s", Result, mpConfigDb->GetLastError().c_str() );
			    TBX_EXIT_ERROR( Result, 0, strReloadResult.c_str() );
			}
		}

		// We are no more in maintenance
		mfDbMaintenance = TBX_FALSE;

		// Reload whatever information required by this application from mpConfigDb

		TBX_EXIT_SUCCESS( TBX_RESULT_OK );
	}

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Exception handling section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	CAF_EXCEPTION_HANDLING

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Error handling section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	CAF_ERROR_HANDLING( CAF_VERBOSE )
	{
		if( mpConfigDb && mpConfigDb->IsOpened() )
		{
			mpConfigDb->Close();
		}
	}

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Cleanup section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	CAF_CLEANUP
	{
		// Reload from the database any information required by this application
		CTBCAFServiceCmMgmtClient::SendReloadDone( Result, strReloadResult.c_str() );
	}

	RETURN_VOID;
}

TBX_VOID MyApplication::OnDbMaintenance()
{
	// Remember that we are in DB maintenance
	mfDbMaintenance = TBX_TRUE;
}