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

📄 voip.c

📁 telcobridges voip develop
💻 C
📖 第 1 页 / 共 3 页
字号:
/*--------------------------------------------------------------------------------------------------------------------------------
 |
 |	Project:    	VOIP sample
 |
 |	Filename:   	voip.c
 |
 |	Copyright:  	TelcoBridges 2002-2004, All Rights Reserved
 |
 |	Description:	This file is the main entry point of the VOIP
 |
 |	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
 *------------------------------------------------------------------------------------------------------------------------------*/

/* Variables related to quitting the application */
TBX_BOOL		g_fContinue				= TBX_TRUE;
TBX_BOOL		g_fRunStateMachine		= TBX_TRUE;
time_t			g_QuitLastPrintTime		= 0;
TBX_RESULT		g_AppResult				= TBX_RESULT_FAIL;

/* Context of this application */
PVOIP_CONTEXT	g_pContext				= NULL;


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


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

static TBX_VOID VoipTerm( TBX_VOID );

static TBX_HASH_KEY VoipAdapterHashGetKey
(
	IN		PTBX_VOID				in_pUserContext,
	IN		PTBX_VOID				in_pAdapterContext
);

static TBX_VOID VoipTbxLibErrorHandler
(
  IN		PTBX_VOID				in_pContext,
  IN		TBX_LIB_HANDLE			in_hTbxLib,
  IN		TBX_ADAPTER_HANDLE		in_hAdapter,
  IN		PTBX_LIB_ERROR_INFO		in_pErrorInfo,
  IN		PTBX_LIB_ERROR_ACTION	out_pActionToTake
);

static void VoipTbxLibErrorPrint
(
	IN		PTBX_VOID								in_pContext,
	IN		TBX_LIB_HANDLE							in_hLib,
	IN		PTBX_CHAR								in_pszError
);

#ifdef WIN32
	static BOOL WINAPI ControlC_Handler(DWORD CtrlType);
#else
	void ControlC_Handler( int in_iSignal );
#endif


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

/*-------------------------------------------------------------------------------------------------------------------------------
 |
 |  main            :	This function is the main entry point of the application
 |
 |  in_argc         :	Number of arguments passed to this application (including the application's name)
 |  in_argv			:	Array of strings containing every arguments
 |
 |  Note			:	~
 |
 |  Return          :	0 if everything was successful
 |						!= 0, error code that occurred
 |
 *------------------------------------------------------------------------------------------------------------------------------*/
int
main (
  IN	int			in_argc,
  IN	char *		in_argv [])
{
	TBX_RESULT			Result;
	PVOIP_CLI_CONTEXT	pCliContext;

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Code section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	CODE
	{
		/* Initialize local variables */
		Result		= TBX_RESULT_OK;
		pCliContext	= NULL;

		/* Attach function called at application termination */
		atexit( VoipTerm );


		#ifdef WIN32
		{
			/* Install control-C handler function */
			SetConsoleCtrlHandler( ControlC_Handler, TBX_TRUE );
		}
		#else
		{
			TBX_UINT32	l_un32SigCount;
			int			l_pSignalsToTrap[] 	= { SIGINT, SIGHUP, SIGALRM, SIGQUIT, SIGTERM, SIGUSR1 };
			/* Trap normal exit signals */
			for( l_un32SigCount = 0; l_un32SigCount < sizeof( l_pSignalsToTrap ) / sizeof( int ); l_un32SigCount++ )
			{
				signal( l_pSignalsToTrap[ l_un32SigCount ], ControlC_Handler );
			}
			/* Ignore sig pipe (broken pipe that occurs when a socket closes) */
			signal( SIGPIPE, SIG_IGN );
		}
		#endif

		/* Allocate application context */
		g_pContext = (PVOIP_CONTEXT)calloc( sizeof(*g_pContext), 1 );
		if( !g_pContext )
		{
			TBX_EXIT_ERROR (TBX_RESULT_OUT_OF_MEMORY, 0, "Failed to allocate application context..");
		}

		/* Initialize the application context */
		g_pContext->un32TimerCurrentTime					= TBX_GET_TICK();
		g_pContext->un32AppStartTimestamp					= g_pContext->un32TimerCurrentTime;
		g_pContext->CurrentConfig.TraceLevelStdout			= TRACE_LEVEL_0;
		g_pContext->CurrentConfig.TraceLevelLogFile			= TRACE_LEVEL_0;
		g_pContext->CurrentConfig.un32NetworkDownDelayMs	= VOIP_DEFAULT_NETWORK_DOWN_DELAY_MS;
		g_pContext->CurrentConfig.un32AdapterDownDelayMs	= VOIP_DEFAULT_ADAPTER_DOWN_DELAY_MS;
		g_pContext->CurrentConfig.un32PollDelayMs			= VOIP_DEFAULT_POLL_DELAY_MS;

		/* Allocate the pool of adapter contexts */
		g_pContext->hPoolOfAdapters = TBXPoolOfBuffersCreate
		(
			VOIP_MAX_ADAPTERS,
			sizeof(VOIP_ADAPTER_CONTEXT_NODE),
			VoipAdapterNameGetKey,
			NULL,
			TBX_TRUE
		);
		if( g_pContext->hPoolOfAdapters == (TBX_POOL_OF_BUFFERS_HANDLE)TBX_HANDLE_INVALID )
		{
			TBX_EXIT_ERROR (TBX_RESULT_OUT_OF_MEMORY, 0, "Failed to allocate pool of adapter contexts.");
		}
		TBXPoolOfBuffersSetCreateFileLine( g_pContext->hPoolOfAdapters, __FILE__, __LINE__ );

		/* Allocate the hash table to retrieve adapter contexts from adapter handles */
		g_pContext->hAdapterHandleHash = TBXHashCreate
		(
			VOIP_MAX_ADAPTERS,
			VoipAdapterHashGetKey,
			NULL
		);
		if( g_pContext->hAdapterHandleHash == (TBX_HASH_HANDLE)TBX_HANDLE_INVALID )
		{
			TBX_EXIT_ERROR (TBX_RESULT_OUT_OF_MEMORY, 0, "Failed to allocate hash of adapter contexts by handle.");
		}
		TBXHashSetCreateFileLine( g_pContext->hAdapterHandleHash, __FILE__, __LINE__ );

		/* Allocate the timer for adapter context's state machines */
		g_pContext->hAdapterTimers = TBXTimerCreate
		(
			VOIP_MAX_ADAPTERS,
			VOIP_TIMER_GRANULARITY_MS / TBX_MSEC_PER_TICKS,
			VOIP_TIMER_MAX_TIME_MS / TBX_MSEC_PER_TICKS,
			TBX_GET_TICK()
		);
		if( g_pContext->hAdapterTimers == (TBX_TIMER_HANDLE)TBX_HANDLE_INVALID )
		{
			TBX_EXIT_ERROR (TBX_RESULT_OUT_OF_MEMORY, 0, "Failed to allocate timer of adapter contexts.");
		}

		/* Create the thread mutex */
		TBX_SEM_CREATE (&g_pContext->hThreadSem, 1, 1);

		/* Create the semaphore for hostlib log callback */
		TBX_SEM_CREATE (&g_pContext->TbxCliLogSem, 1, 1);

		/* Initialize CLI context */
		Result = VoipCliInit();
		if( TBX_RESULT_FAILURE( Result ) )
		{
			TBX_EXIT_ERROR (Result, 0, "Failed to initialize Cli context.");
		}
		pCliContext = &g_pContext->CliContext;

		/* Load the initial configuration for this voip */
		if( in_argc >= TB_TESTLIB_SCRIPT_PARAMETER_MIN_NUMBER )
		{
			//fprintf( stdout, "in_argv[1]: %s\n", in_argv[1]);

			/* check if the application is started in test mode */
			if( strcmp( in_argv[1], TB_TESTLIB_SCRIPT_PARAMETER_TEST_APP_MODE ) == 0 )
			{
				if( strcmp( in_argv[2], TB_TESTLIB_SCRIPT_PARAMETER_TEST_ID ) == 0 )
				{
					TBX_UINT32		un32AppId;
					/* application started in test mode */
					g_pContext->TestModeContext.fTestMode = TBX_TRUE;

					/* Application id must be kept in context */
					un32AppId = atoi( in_argv[3] );
					if( un32AppId == 0 )
					{
						//TBX_EXIT_ERROR (TBX_RESULT_INVALID_PARAM, 0, "invalid id.");
						fprintf( stdout, "invalid id\n");
					}		
					//fprintf( stdout, "main: application started in test mode\n" );

					g_pContext->TestModeContext.un32TestId = un32AppId;

					if( in_argc >= TB_TESTLIB_SCRIPT_PARAMETER_MIN_NUMBER + 1 )
					{
						//fprintf( stdout, "copy file name: %s configuration file\n", in_argv[4] );
						Strncpy( g_pContext->szConfigFileName, in_argv[4], sizeof(g_pContext->szConfigFileName) );

#if 0
						if( in_argc >= TB_TESTLIB_SCRIPT_PARAMETER_MIN_NUMBER + 2 )
						{
							/* get the application name */
							Strncpy( g_pContext->szApplicationName, in_argv[5], sizeof(g_pContext->szApplicationName) );
							g_pContext->fApplicationNameSpecifiedFromCommandline = TBX_TRUE;
						}
#endif
					}
				}
				else
				{
					//TBX_EXIT_ERROR (TBX_RESULT_INVALID_PARAM, 0, "-id parameter is not present.");
					fprintf( stdout, "-id parameter is not present\n");
				}

			}
			else
			{
				//TBX_EXIT_ERROR (TBX_RESULT_INVALID_PARAM, 0, "-test parameter is not present.");
				fprintf( stdout, "-test parameter is not present\n");
			}
		}
		else
		{
			/* Load the initial configuration */
			if( in_argc > 1 )
			{
				strncpy( g_pContext->szConfigFileName, in_argv[1], sizeof(g_pContext->szConfigFileName) );
				g_pContext->szConfigFileName[sizeof(g_pContext->szConfigFileName) - 1] = '\0';
			}
			else
			{
				strncpy( g_pContext->szConfigFileName, VOIP_DEFAULT_CONFIG_FILE_NAME, sizeof(g_pContext->szConfigFileName) );
			}
		}

		/* Initialize the TBX Cli Tools library */
		{
			TBX_CLI_TOOLS_INIT_PARAMS	CliToolsParams;

			/* Override some parameters */
			memset( &CliToolsParams, 0, sizeof(CliToolsParams) );
			CliToolsParams.un32MaxScreenWidth		= VOIP_CLI_SCREEN_WIDTH_MAX;
			CliToolsParams.un32MaxScreenHeight		= VOIP_CLI_SCREEN_HEIGHT_MAX;
			CliToolsParams.un32MinScreenWidth		= VOIP_CLI_SCREEN_WIDTH_MIN;
			CliToolsParams.un32MinScreenHeight		= VOIP_CLI_SCREEN_HEIGHT_MIN;
			CliToolsParams.un32DefaultScreenWidth	= VOIP_CLI_SCREEN_WIDTH_MIN;
			CliToolsParams.un32DefaultScreenHeight	= VOIP_CLI_SCREEN_HEIGHT_MIN;
			CliToolsParams.un32MaxPromptLines		= VOIP_CLI_PROMPT_NB_LINES;
			CliToolsParams.fDisplayLog				= TBX_TRUE;
			CliToolsParams.un32MinRefreshDelay		= VOIP_CLI_MIN_DELAY_BETWEEN_REFRESH_MS;
			CliToolsParams.un32MaxRefreshDelay		= VOIP_CLI_MAX_DELAY_BETWEEN_REFRESH_MS;
			CliToolsParams.un32MinClsDelay			= 0;
			CliToolsParams.fLowPrioryThread			= TBX_TRUE;
			CliToolsParams.fDisableTerminalInput	= TBX_FALSE;
			CliToolsParams.fDisableTerminalOutput	= TBX_FALSE;
			CliToolsParams.TraceLevelStdout			= TRACE_LEVEL_0;
			CliToolsParams.TraceLevelLogFile		= TRACE_LEVEL_0;
			strcpy( CliToolsParams.szLogFileName, VOIP_APPLICATION_NAME );
			CliToolsParams.un32LogBufferMaxLines	= VOIP_CLI_NB_LOG_LINES_SCROLLBACK;
			CliToolsParams.un32MaxLogFileSize		= VOIP_CLI_MAX_LOG_FILE_SIZE;
			#ifdef TBX_DEBUG
				CliToolsParams.fFlushLogOnlyOnError	= TBX_FALSE;
			#else
				CliToolsParams.fFlushLogOnlyOnError	= TBX_TRUE;	/* With release build, flush only on error to increase logs performance */
			#endif

			CliToolsParams.pFctDisplayMenu			= VoipCliDisplay;

⌨️ 快捷键说明

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