📄 protocalapi.c
字号:
/****************************************************************************** Copyright(C) 2005,2006 Frank ZHANG All Rights Reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. ****************************************************************************** If you want to distribute this software with modifications under any terms other than the GPL or distribute the software linked with proprietary applications that are not distributed in full compliance with the GNU Public License, a Commercial License is needed. Commercial licensing and support of this software are available at a fee. For more information, See http://www.openmgcp.org ******************************************************************************//******************************************************************************* Authors : Frank ZHANG* Description : *** Date of creation : 08/03/2005*** History :* 2005/08/03 Frank ZHANG : - Creation******************************************************************************/#include <stdlib.h>#include <string.h>#include "typedef.h"#include "debg.h"#include "misc.h"#include "list.h"#include "posix.h"#include "mgcpdef.h"#include "stackcb.h"#include "endpointctrl.h"#include "transacmng.h"#include "protocalapi.h"/* MGCP version name */#define MGCP_VERSION "1.0"/* Task numbers of the stack */#define TOTAL_TASK_NUM 3/****************************************************************************** * Function : MgcpStackCreate * * Description : Create the control block of the protocal stack and * initialize it. * * Input parameters : pCbkNotfyApp - Callback function of the stack * pCbkGetConnParams - Callback function of get the * connection parameters. * * Output parameters : ppStack - Handle of the stack * * Return value : Return OK if the stack is created successfully, otherwise * return FAIL. * * Comments : * * History : * 2005/12/15 : Creation * * Date : Dec 15 2005, Frank ZHANG ******************************************************************************/LONG MgcpStackCreate(H_STACK *ppStack, CBK_MGCP_STACK pCbkNotfyApp, CBK_GET_CONNEC_PARAMS pCbkGetConnParams){ MGCP_STACK *pStack = (MGCP_STACK*)calloc(1, sizeof(MGCP_STACK)); Assert(ppStack); Assert(pCbkNotfyApp); Assert(pStack); /* Firstly chekc Call back function */ if (!pCbkNotfyApp || !pCbkGetConnParams) return FAIL; pStack->pCbkNotfyApp = pCbkNotfyApp; pStack->pCbkGetConnParams = pCbkGetConnParams; /* MGCP version */ StrClone(&pStack->pcMgcpVersion, MGCP_VERSION); /* Init thread barrier */ if (pthread_barrier_init(&pStack->pThreadBarrier, NULL, TOTAL_TASK_NUM) != 0) { MgcpStackDestroy((H_STACK)pStack); return FAIL; } /* Create EndpointCtrl CB */ if (EndpointControlCreate((H_MGCP_ENDPOINT_CONTROL*)(&pStack->pEndpointCtl), pStack) != OK) { MgcpStackDestroy((H_STACK)pStack); return FAIL; } /* Create TransactionMng CB */ if (TransationManagerCreate((H_MGCP_TRANSAC_MANAGER*)(&pStack->pTransacMng), pStack) != OK) { MgcpStackDestroy((H_STACK)pStack); return FAIL; } *ppStack = (H_STACK)pStack; /* Stack state */ pStack->eStackState = STACK_STATE_CREATED; return OK;}/****************************************************************************** * Function : MgcpStackStart * * Description : Start the protocal stack. * * Input parameters : pStack - Handle of the stack * * Output parameters : * * Return value : Return OK if the stack is started successfully, otherwise * return FAIL. * * Comments : * * History : * 2005/12/15 : Creation * * Date : Dec 15 2005, Frank ZHANG ******************************************************************************/LONG MgcpStackStart(H_STACK pStack){ MGCP_STACK *pTmpStack = (MGCP_STACK*)pStack; if (pTmpStack && pTmpStack->eStackState == STACK_STATE_CREATED) { pTmpStack->eStackState = STACK_STATE_RUNNING; if (EndpointControlStart((H_MGCP_ENDPOINT_CONTROL) (pTmpStack->pEndpointCtl)) != OK) return FAIL; if (TransationManagerStart((H_MGCP_TRANSAC_MANAGER) (pTmpStack->pTransacMng)) != OK) return FAIL; } else return FAIL; return OK;}/****************************************************************************** * Function : MgcpStackDestroy * * Description : Destroy the protocal stack. * * Input parameters : pStack - Handle of the stack * * Output parameters : * * Return value : NONE * * Comments : NOTE: The order to termiante the threads and when to * reclaim the resource must be cauciously arranged! * * History : * 2005/12/15 : Creation * * Date : Dec 15 2005, Frank ZHANG ******************************************************************************/void MgcpStackDestroy(H_STACK pStack){ MGCP_STACK *pTmpStack = (MGCP_STACK*)pStack; MGCP_ENDPOINT_CONTROL *pEndpointCtrl = (MGCP_ENDPOINT_CONTROL*)pTmpStack->pEndpointCtl; MGCP_TRANSACTION_MANAGER *pTranMng = (MGCP_TRANSACTION_MANAGER*)pTmpStack->pTransacMng; H_MGCP_ENDPOINT pEndpoint; Assert(pStack); Assert(pEndpointCtrl); Assert(pTranMng); /************************************************************** Firstly muse terminate the tasks according to below order ! **************************************************************/ /* Shut the socket to be sending only */ shutdown(pTranMng->iSocket_fd, SHUT_RD); /* Set the stack state to beTerminating */ pTmpStack->eStackState = STACK_STATE_TERMINATING; /* Sleep 1 seconde for all the tasks to have time to free the messages in their message queue to avoid memory leak */ sleep(1); /* Set the stack state to be DESTROY for the tasks to exit their loop */ pTmpStack->eStackState = STACK_STATE_DESTROY; /* Detach the TransacMng TX thread */ pthread_join(pTranMng->MsgTxThread, NULL); /* Delete tick timer of TransacMng */ timer_delete(pTranMng->TickTimer.TimerID); /* Detach the EndpointCtrl thread */ pthread_join(pEndpointCtrl->EndpointCtrlThread, NULL); /* Delete tick timer of EndpntCtrl */ timer_delete(pEndpointCtrl->TickTimer.TimerID); /* Close the socket */ close(pTranMng->iSocket_fd); /* Detach the TransacMng RX thread, before this join, the socket must closed, because this thread is maybe blocked at receive operation of socket, the close action will canceal the blocking */ pthread_join(pTranMng->MsgRxThread, NULL); /************************************************************** Free the system resources **************************************************************/ /* Close message queue */ mq_close(pEndpointCtrl->MsgQueue); mq_close(pTranMng->MsgQueue); /* Destroy mutex */ pthread_mutex_destroy(&pEndpointCtrl->pEndpoinCtrltMutex); pthread_mutex_destroy(&pTranMng->pTranMngMutex); /* Destroy barrier */ pthread_barrier_destroy(&pTmpStack->pThreadBarrier); /* Free the memories of TransacMng */ /* Free the domain name */ free(pTranMng->pcDomainName); /* Free the mgcp message list */ ClearMgcpMessageList(&pTranMng->MsgList); /* Free the control block */ free(pTranMng); /* Free the memories of EndpntCrl */ /* Clear the endpoint list */ SListReset(&pEndpointCtrl->EndpointList); while((pEndpoint = SListGetCurData(&pEndpointCtrl->EndpointList)) != NULL) { ClearMgcpEndpoint(pEndpoint); free(pEndpoint); SListDelCurNode(&pEndpointCtrl->EndpointList); } /* Clear endpoint tree */ ClearEndpointTree(&pEndpointCtrl->EndpointTree); /* Free the CB */ free(pEndpointCtrl); /* Free the memories of Stack */ free(pTmpStack->pcMgcpVersion); free(pTmpStack->pcProfileName); free(pTmpStack);}/****************************************************************************** * Function : MgcpStackSetNetwork * * Description : Set the network of the stack * * Input parameters : pStack - Handle of the stack * dwLocalIPAddr - IP address * wPort - MGCP port * * Output parameters : * * Return value : Return OK if the stack is configured successfully, * otherwise return FAIL. * * Comments : * * History : * 2005/12/16 : Creation * * Date : Dec 16 2005, Frank ZHANG ******************************************************************************/LONG MgcpStackSetNetwork(H_STACK pStack, DWORD dwLocalIPAddr, WORD wPort){ H_MGCP_TRANSAC_MANAGER pTransacMng; Assert(pStack); pTransacMng = (H_MGCP_TRANSAC_MANAGER)(((MGCP_STACK*)pStack)->pTransacMng); Assert(pTransacMng); if (pTransacMng) { pthread_mutex_lock(&pTransacMng->pTranMngMutex); pTransacMng->dwLocalIpAddr = dwLocalIPAddr; pTransacMng->wLocalPort = wPort; pthread_mutex_unlock(&pTransacMng->pTranMngMutex); return OK; } return FAIL;}/****************************************************************************** * Function : MgcpStackSetDomainName * * Description : Set the domain name of the stack. * * Input parameters : pStack - Handle of the stack * pcDomainName - Domain name * * Output parameters : * * Return value : Return OK if the stack is configured successfully, * otherwise return FAIL. * * Comments : * * History : * 2005/12/16 : Creation * * Date : Dec 16 2005, Frank ZHANG ******************************************************************************/LONG MgcpStackSetDomainName(H_STACK pStack, char *pcDomainName){ H_MGCP_TRANSAC_MANAGER pTransacMng; Assert(pStack); pTransacMng = (H_MGCP_TRANSAC_MANAGER)(((MGCP_STACK*)pStack)->pTransacMng); Assert(pTransacMng); if (pTransacMng) { pthread_mutex_lock(&pTransacMng->pTranMngMutex); StrClone(&pTransacMng->pcDomainName, pcDomainName); pthread_mutex_unlock(&pTransacMng->pTranMngMutex); return OK; } return FAIL;}/****************************************************************************** * Function : MgcpStackRegisterEndpoint * * Description : Creat a endpoint, initialize it and add it into the stack * * Input parameters : pStack - Handle of the stack * pcEndpointName - Endpoint name * pCbkSigCheck - Signal chekc callback function of the * endpoint * * Output parameters : ppEndpoint - Handle of the endpoint * * Return value : Return OK if the endpoint is registered successfully, * otherwise return FAIL. * * Comments : * * History : * 2005/12/16 : Creation * * Date : Dec 16 2005, Frank ZHANG ******************************************************************************/#define DEF_AVER_ACK_DELAY 200 /* 200ms */#define DEF_AVER_DEVIATION 50 /* 50ms */LONG MgcpStackRegisterEndpoint(H_STACK pStack, H_ENDPOINT *ppEndpoint, char *pcEndpointName, CBK_SIG_CHECK pCbkSigCheck){ MGCP_ENDPOINT *pEndpnt; MGCP_STACK *pTmpStack = (MGCP_STACK*)pStack; Assert(pStack); Assert(ppEndpoint); Assert(pcEndpointName); Assert(pCbkSigCheck); /* The endpoint can not be registered when stack is running */ if (pTmpStack->eStackState != STACK_STATE_CREATED) return FAIL; pEndpnt = (MGCP_ENDPOINT*)calloc(1, sizeof(MGCP_ENDPOINT)); Assert(pEndpnt); /* Signal check CBK */ pEndpnt->pCbkSigCheck = pCbkSigCheck; /* Endpoint name */ StrClone(&pEndpnt->pcEndpointName, pcEndpointName); /* Service state */ pEndpnt->bInService = TRUE; /* RTT to the CA */ pEndpnt->lAverAckDelay = DEF_AVER_ACK_DELAY; pEndpnt->lAverDeviation = DEF_AVER_DEVIATION; pEndpnt->bNotifiedEntityProvided = FALSE; pEndpnt->bOnHookState = TRUE; /* Default is process and step */ pEndpnt->bQuarantineProcess = TRUE; pEndpnt->bQuarantineLoop = FALSE; StrClone(&pEndpnt->pcRequestedID, "0"); /* Initial connection ID (1-100) */ pEndpnt->dwCurConnecID = ((DWORD)rand() % 100) + 1; /* Register the new endpoint */ Assert(pTmpStack->pEndpointCtl); EndpointControlRegisterEndpoint((H_MGCP_ENDPOINT_CONTROL) (pTmpStack->pEndpointCtl), pEndpnt); /* Initial restart wait delay */ pEndpnt->dwRestartWaitDelay = InitRestartWaitDelay(pEndpnt); /* Initial restart method */ pEndpnt->RestartMethod.eType = RESTART_METHOD_RESTART; /* Updata SM */ pEndpnt->eState = SM_INIT; *ppEndpoint = (H_ENDPOINT)pEndpnt; return OK;}/****************************************************************************** * Function : MgcpStackSetDoaminNameValidatione * * Description : Set the flag of whether need check the incoming mgcp * commands's domain name. * * Input parameters : pStack - Handle of the stack * bOnOff - Whether use validition check * * Output parameters : * * Return value : Return OK if the stack is configured successfully,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -