⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 voip_config.c

📁 telcobridges voip develop
💻 C
📖 第 1 页 / 共 3 页
字号:
/*--------------------------------------------------------------------------------------------------------------------------------
 |
 |	Project:    	VOIP sample
 |
 |	Filename:   	voip_config.c
 |
 |	Copyright:  	TelcoBridges 2002-2004, All Rights Reserved
 |
 |	Description:	This file contains the code that loads and saves configuration of this application
 |
 |	Notes:      	Tabs = 4
 |
 *-------------------------------------------------------------------------------------------------------------------------------
 |
 |	Revision:   	$Revision: 1.27 $
 |
 *------------------------------------------------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------------------------------------------------
 |  Includes
 *------------------------------------------------------------------------------------------------------------------------------*/

#include "voip_includes.h"


/*--------------------------------------------------------------------------------------------------------------------------------
 |  Forward declarations
 *------------------------------------------------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------------------------------------------------
 |  Defines
 *------------------------------------------------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------------------------------------------------
 |  Types
 *------------------------------------------------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------------------------------------------------
 |  Versioning
 *------------------------------------------------------------------------------------------------------------------------------*/

#ifdef WIN32
/*@unused@*/ static char g_szFileVersion [] = "$Revision: 1.27 $";
#endif


/*--------------------------------------------------------------------------------------------------------------------------------
 |  Global variables
 *------------------------------------------------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------------------------------------------------
 |  Macros
 *------------------------------------------------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------------------------------------------------
 |  Function Prototypes
 *------------------------------------------------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------------------------------------------------
 |  Implementation
 *------------------------------------------------------------------------------------------------------------------------------*/

/*-------------------------------------------------------------------------------------------------------------------------------
 |
 |  VoipLoadInitialConfig		:	Load the initial configuration of the VoIP application
 |
 |  Note						:	~
 |
 |  Return						:	TBX_RESULT_OK if the configuration has been properly loaded
 |									Other error code if the configuration could not be loaded.
 |
 *------------------------------------------------------------------------------------------------------------------------------*/
TBX_RESULT	VoipLoadInitialConfig()
{
	TBX_RESULT					Result;
	PVOIP_CLI_CONTEXT			pCliContext;
	PVOIP_CONFIG				pTempConfig;
	PVOIP_CONFIG				pCurrentConfig;
	PVOIP_ADAPTER_CONTEXT_NODE	pAdapterContextNode;

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Code section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	CODE
	{
		/* Initialize local variables */
		Result				= TBX_RESULT_OK;
		pCliContext			= &g_pContext->CliContext;
		pTempConfig			= &g_pContext->TempConfig;
		pCurrentConfig		= &g_pContext->CurrentConfig;

		/* Allocate a temporary pool of adapters */
		g_pContext->hTempPoolOfAdapters = TBXPoolOfBuffersCreate
		(
			VOIP_MAX_ADAPTERS,
			sizeof(VOIP_ADAPTER_CONTEXT_NODE),
			VoipAdapterNameGetKey,
			NULL,
			TBX_TRUE
		);
		if( g_pContext->hTempPoolOfAdapters == (TBX_POOL_OF_BUFFERS_HANDLE)TBX_HANDLE_INVALID )
		{
			TBX_EXIT_ERROR (TBX_RESULT_OUT_OF_MEMORY, 0, "Failed to allocate pool of adapter contexts.");
		}
#ifdef SMARTALLOC	/* Patch for a C0001: Internal compilation error (release compilation) */
		/* I really don't know why... if this function is called when compiling in release the compiler is reporting this: */
		/* Error executing cl.exe. voip.exe - 1 error(s), 204 warning(s) */
		TBXPoolOfBuffersSetCreateFileLine( g_pContext->hTempPoolOfAdapters, __FILE__, __LINE__ );
#endif
		TbxCliToolsLogPrint( pCliContext->hCliTools, TRACE_LEVEL_ALWAYS, NULL, "Parsing and loading configuration file...\n");

		/* Load configuration */
		Result = VoipParseAndLoadConfiguration();
		if( TBX_RESULT_FAILURE( Result ) )
		{
			TBX_EXIT_ERROR (Result, 0, "Failed to load configuration.");
		}

		TbxCliToolsLogPrint( pCliContext->hCliTools, TRACE_LEVEL_ALWAYS, NULL, "Validating configuration file...\n");

		/* Validate trace level of log file */
		if( pTempConfig->TraceLevelLogFile > TRACE_LEVEL_MAX )
		{
			TBX_CHAR	szErr[64];
			sprintf( szErr, "Invalid trace level. Supporting 0-%d.", TRACE_LEVEL_MAX );
			TBX_EXIT_ERROR (TBX_RESULT_INVALID_PARAM, 0,szErr );
		}

		/* Validate trace level of display output */
		if( pTempConfig->TraceLevelStdout > TRACE_LEVEL_MAX )
		{
			TBX_CHAR	szErr[64];
			sprintf( szErr, "Invalid trace level. Supporting 0-%d.", TRACE_LEVEL_MAX );
			TBX_EXIT_ERROR (TBX_RESULT_INVALID_PARAM, 0,szErr );
		}

		/* Validate network redundancy settings */
		if( pTempConfig->un32NetworkDownDelayMs < TBX_NETWORK_REDUNDANCY_MIN_NETWORK_DOWN_DELAY_MS )
		{
			TBX_EXIT_ERROR (TBX_RESULT_NOT_FOUND, 0, "network_down_delay_ms is too small!");
		}
		if( pTempConfig->un32AdapterDownDelayMs <= (pTempConfig->un32NetworkDownDelayMs * 2) )
		{
			TBX_EXIT_ERROR (TBX_RESULT_NOT_FOUND, 0, "adapter_down_delay_ms must be greater than 2 x network_down_delay_ms!");
		}
		if( pTempConfig->un32PollDelayMs && (pTempConfig->un32PollDelayMs < TBX_NETWORK_REDUNDANCY_MIN_POLL_DELAY_MS) )
		{
			TBX_EXIT_ERROR (TBX_RESULT_NOT_FOUND, 0, "poll_delay_ms is too small!");
		}
		if( pTempConfig->un32PollDelayMs >= (pTempConfig->un32NetworkDownDelayMs / 2) )
		{
			TBX_EXIT_ERROR (TBX_RESULT_NOT_FOUND, 0, "poll_delay_ms must be smaller than (network_down_delay_ms / 2)!");
		}

		if( (g_pContext->StreamServerContext.fEnable) &&
			(strcmp( pTempConfig->szStreamServerIp0, pCurrentConfig->szStreamServerIp0 ) != 0) ||
			(strcmp( pTempConfig->szStreamServerIp1, pCurrentConfig->szStreamServerIp1 ) != 0) )
		{
			TbxCliToolsLogPrint( pCliContext->hCliTools, TRACE_LEVEL_ALWAYS, NULL, "Disconnecting from stream server...\n");

			/* Close stream server library */
			Result = VoipStreamServerClose();
			if( TBX_RESULT_FAILURE( Result ) )
			{
				TBX_EXIT_ERROR (Result, 0, "Failed to close stream server library.");
			}
		}

		if(	!g_pContext->StreamServerContext.fEnable &&
			pTempConfig->szStreamServerIp0[0] != '\0' )
		{
			/* We need to open stream server */
			g_pContext->fOpenStreamServer = TBX_TRUE;
		}
		else
		{
			g_pContext->fOpenStreamServer = TBX_FALSE;
		}


		/* Update current config */
		*pCurrentConfig = *pTempConfig;

		/* End of the code (skip to cleanup) */
		TBX_EXIT_SUCCESS (TBX_RESULT_OK);
	}

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Error handling section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	ERROR_HANDLING
	{
		/* Print the error to the user */
		TbxCliToolsLogPrint(
			pCliContext->hCliTools,
			TRACE_LEVEL_ERROR, NULL,
			"VoipLoadInitialConfig: %s (Result 0x%08X, %s, line %d)\n",
			TBX_ERROR_DESCRIPTION,
			(int)TBX_ERROR_RESULT,
			__FILE__,
			TBX_ERROR_LINE);

		/* Clear the "target configuration" context for all adapters */
		pAdapterContextNode = TBXPoolOfBuffersFirst( g_pContext->hTempPoolOfAdapters );
		while( pAdapterContextNode )
		{
			/* Clear the "target configuration" context of this adapter */
			VoipAdapterClearConfig( pAdapterContextNode->pAdapterContext->pTargetConfig );
			free( pAdapterContextNode->pAdapterContext );
			pAdapterContextNode->pAdapterContext = NULL;

			TBXPoolOfBuffersFree( g_pContext->hTempPoolOfAdapters, pAdapterContextNode );

			pAdapterContextNode = TBXPoolOfBuffersFirst( g_pContext->hTempPoolOfAdapters );
		}

		/* Free the temporary pool of adapters */
		if( g_pContext->hTempPoolOfAdapters != (TBX_POOL_OF_BUFFERS_HANDLE)TBX_HANDLE_INVALID )
		{
			/* Free the pool of adapter contexts */
			TBXPoolOfBuffersDestroy( g_pContext->hTempPoolOfAdapters );
			g_pContext->hTempPoolOfAdapters = (TBX_POOL_OF_BUFFERS_HANDLE)TBX_HANDLE_INVALID;
		}
	}

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Cleanup section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	CLEANUP
	{
	}

	RETURN;
}

/*-------------------------------------------------------------------------------------------------------------------------------
 |
 |  VoipStartInitialConfig		:	Start the initial configuration of the VoIP application
 |
 |  Note						:	~
 |
 |  Return						:	TBX_RESULT_OK if the configuration has been properly loaded
 |									Other error code if the configuration could not be loaded.
 |
 *------------------------------------------------------------------------------------------------------------------------------*/
TBX_RESULT	VoipStartInitialConfig()
{
	TBX_RESULT					Result;
	PVOIP_CLI_CONTEXT			pCliContext;
	PVOIP_CONFIG				pTempConfig;
	PVOIP_CONFIG				pCurrentConfig;
	PVOIP_ADAPTER_CONTEXT		pAdapterContext;
	PVOIP_ADAPTER_CONTEXT_NODE	pAdapterContextNode;
	PVOIP_ADAPTER_CONTEXT		pNewAdapterContext;
	PVOIP_ADAPTER_CONTEXT_NODE	pNewAdapterContextNode;
	PVOIP_ADAPTER_CONFIG		pNextTargetConfig;

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Code section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	CODE
	{
		/* Initialize local variables */
		Result				= TBX_RESULT_OK;
		pCliContext			= &g_pContext->CliContext;
		pTempConfig			= &g_pContext->TempConfig;
		pCurrentConfig		= &g_pContext->CurrentConfig;
		pAdapterContext		= NULL;
		pAdapterContextNode	= NULL;

		TbxCliToolsLogPrint( pCliContext->hCliTools, TRACE_LEVEL_ALWAYS, NULL, "Removing adapters from system\n");

		/*
		 * Stop state machine for all adapters that don't exist in the new config
		 */
		pAdapterContextNode = TBXPoolOfBuffersFirst( g_pContext->hPoolOfAdapters );
		while( pAdapterContextNode )
		{
			pAdapterContext = pAdapterContextNode->pAdapterContext;
			
			/* Find the corresponding adapter context */
			pNewAdapterContextNode = TBXPoolOfBuffersFind
			(
				g_pContext->hTempPoolOfAdapters, 
				(TBX_HASH_KEY)pAdapterContext->szAdapterName
			);
			if( pNewAdapterContextNode != NULL && pNewAdapterContextNode->pAdapterContext == NULL )
			{
				TbxCliToolsLogPrint( pCliContext->hCliTools, TRACE_LEVEL_ALWAYS, NULL, "Removing %s from system\n", pAdapterContext->szAdapterName);

				/* Stop using this adapter */
				VoipAdapterStopUsing( pAdapterContext );
			}

			pAdapterContextNode = TBXPoolOfBuffersNext( g_pContext->hPoolOfAdapters, pAdapterContextNode );
		}

		TbxCliToolsLogPrint( pCliContext->hCliTools, TRACE_LEVEL_ALWAYS, NULL, "Adding adapters to system\n");

		/*
		 * Update the actual pool of adapters according to this new list of adapters
		 */
		pNewAdapterContextNode = TBXPoolOfBuffersFirst( g_pContext->hTempPoolOfAdapters );
		while( pNewAdapterContextNode )
		{
			if( pNewAdapterContextNode->pAdapterContext )
			{
				pNewAdapterContext = pNewAdapterContextNode->pAdapterContext;

				/* Find the corresponding adapter context */
				pAdapterContextNode = TBXPoolOfBuffersFind
				(
					g_pContext->hPoolOfAdapters,
					(TBX_HASH_KEY)pNewAdapterContext->szAdapterName
				);
				if( pAdapterContextNode && pAdapterContextNode->pAdapterContext->State != VOIP_ADAPTER_STATE_NOT_USED )
				{
					pAdapterContext = pAdapterContextNode->pAdapterContext;

					/* Adapter remains valid. New config pending. */
					pAdapterContext->fNewConfigPending = TBX_TRUE;

					/* Point on the next target config structure */
					if( pAdapterContext->pTargetConfig == &pAdapterContext->TargetConfig1 )
					{
						pNextTargetConfig = &pAdapterContext->TargetConfig2;
					}
					else
					{
						pNextTargetConfig = &pAdapterContext->TargetConfig1;
					}

					/* Clear the "target configuration" context */
					VoipAdapterClearConfig( pNextTargetConfig );
				}
				else
				{
					/* We need to add a new adapter to current pool of adapters */
					Result = VoipAdapterAdd(
						g_pContext->hPoolOfAdapters,
						pNewAdapterContext->szAdapterName,
						&pAdapterContext);
					if( TBX_RESULT_FAILURE( Result ) )
					{
						TBX_EXIT_ERROR (Result, 0, "Failed to create new adapter context.");
					}

					/* Point on the next target config structure */
					pNextTargetConfig = pAdapterContext->pTargetConfig;
				}

				TbxCliToolsLogPrint( pCliContext->hCliTools, TRACE_LEVEL_ALWAYS, NULL, "Adding %s to system\n", pAdapterContext->szAdapterName);

				/* Save stress testing configuration parameters */
				pAdapterContext->StressTest = pNewAdapterContext->StressTest;

				/* Save demo parameters */
				pAdapterContext->Demo = pNewAdapterContext->Demo;

				/* Load adapter configuration */
				*pNextTargetConfig = *pNewAdapterContext->pTargetConfig;

				/* Now that we copied the config to pNextTargetConfig, we must NULL our pointers
				   which we are no more owner */
				memset( pNewAdapterContext->pTargetConfig, 0, sizeof(*pNewAdapterContext->pTargetConfig) );

				/* Set configuration as loaded */
				pNextTargetConfig->fLoaded = TBX_TRUE;

				/* Kick the state machine for this adapter */
				if( pAdapterContext->State == VOIP_ADAPTER_STATE_NOT_READY )
				{
					VoipAdapterChangeState( pAdapterContext, pAdapterContext->State );
				}
			}

			pNewAdapterContextNode = TBXPoolOfBuffersNext( g_pContext->hTempPoolOfAdapters, pNewAdapterContextNode );
		}

		if( g_pContext->fOpenStreamServer == TBX_TRUE )
		{
			TbxCliToolsLogPrint( pCliContext->hCliTools, TRACE_LEVEL_ALWAYS, NULL, "Connecting to stream server...\n");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -