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

📄 rvmegacotermmgrutil.c

📁 h.248协议源码
💻 C
字号:
/******************************************************************************
Filename   : rvmegacotermmgrutil.c
Description: Utililiary functions that can be used in more than one file
******************************************************************************
                Copyright (c) 1999 RADVision Inc.
************************************************************************
NOTICE:
This document contains information that is proprietary to RADVision LTD.
No part of this publication may be reproduced in any form whatsoever 
without written prior approval by RADVision LTD..

RADVision LTD. reserves the right to revise this publication and make 
changes without obligation to notify any person of such revisions or 
changes.
******************************************************************************
$Revision:$
$Date:11.15.00$
$Author: D.Elbert$
******************************************************************************/

#include <string.h>
#include "rvmegacotermmgrutil.h"
#include "rvmdm.h"
#include "rvmutex.h"

void rvMegacoTermMgrUtilOverwriteError(RvMdmError * error,unsigned int code_,const char * msg_) {
	const char * msg = rvMdmErrorGetMsg_(error);
	unsigned int code = rvMdmErrorGetCode_(error);

	/* Overwrite the values if not set */
	if(!strcmp(msg,"")) 	
		msg = msg_;
	if(!code)
		code = code_;

	rvMdmErrorSet(error,code,msg);
}

void rvMegacoTermMgrUtilAddError(RvMegacoCommandReply * reply,unsigned int code,const char * msg,RvAlloc * alloc) {
	RvMegacoErrorDescriptor errorDescr;

	rvMegacoErrorDescriptorConstructA(&errorDescr,code,msg,alloc);
	rvMegacoCommandReplySetError(reply,&errorDescr);	
	rvMegacoErrorDescriptorDestruct(&errorDescr);
}

RvBool rvMegacoTermMgrUtilAddError2(RvMegacoCommandReply * reply,RvMdmError * error,unsigned int code_,const char * msg_,RvAlloc * alloc) {
	const char * msg = rvMdmErrorGetMsg_(error);
	unsigned int code = rvMdmErrorGetCode_(error);

	/* Overwrite the values if not set */
	/*rvMegacoTermMgrUtilOverwriteError(error,code_,msg_);*/
	if(!strcmp(msg,"")) 	
		msg = msg_;
	if(!code)
		code = code_;

	rvMegacoTermMgrUtilAddError(reply,code,msg,alloc);
	return rvFalse;
}


/*************************************************************************************
   Utililiary functions to compare two list of parameters and extract a list of  
   differences.
*************************************************************************************/

typedef struct {
	RvBool state;
	const RvMegacoParameterList * list;
	RvMegacoParameterList * newList;
} RvMegacoTermParameterValueInfo;
static void constructParamInfo(RvMegacoTermParameterValueInfo * info,const RvMegacoParameterList * l1,RvMegacoParameterList * l2) {
	info->list = l1;
	info->newList = l2;
	info->state = rvFalse;
}

static RvBool isParamNotEqual(const RvMegacoParameterValue *v1,const RvMegacoParameterValue *v2) {
	RvRelation relation;

	relation = rvMegacoParameterValueGetType(v1);
	if(relation!=rvMegacoParameterValueGetType(v2)) 
		return rvTrue;

	else if(relation==RV_RELATION_EQUAL || relation==RV_RELATION_LESS || 
		  relation==RV_RELATION_GREATER || relation==RV_RELATION_INEQUAL) 
			if(strcmp(rvMegacoParameterValueGetValue(v1),rvMegacoParameterValueGetValue(v2))!=0)
				return rvTrue;

	return rvFalse;
}

static void addParam(const RvMegacoPackageItem *name, const RvMegacoParameterValue *value, void *data) {
	RvMegacoTermParameterValueInfo * info = (RvMegacoTermParameterValueInfo*)data;
	const RvMegacoParameterValue *v2 = rvMegacoParameterListGet(info->list,name);
	if( v2==NULL || isParamNotEqual(value,v2) ) 
		rvMegacoParameterListSet(info->newList,name,value);
}

static void compareParam(const RvMegacoPackageItem *name, const RvMegacoParameterValue *value, void *data) {
	RvMegacoTermParameterValueInfo * info = (RvMegacoTermParameterValueInfo*)data;
	if(info->state==rvFalse) {
		info->state = !isParamNotEqual(value,rvMegacoParameterListGet(info->list,name));
	}
}

static void paramListExtractDif( RvMegacoParameterList * l1,const 
								RvMegacoParameterList * l2,RvMegacoParameterList * l3) {
	RvMegacoTermParameterValueInfo info;
	constructParamInfo(&info,l2,l3);
	rvMegacoParameterListForEach(l1,addParam,&info);		
	return;
}

RvBool paramListChanged(const RvMegacoParameterList * l1,RvMegacoParameterList * l2) {
	RvMegacoTermParameterValueInfo info;

	if(rvMegacoParameterListGetSize(l1) != rvMegacoParameterListGetSize(l2)) 
		return rvTrue;

	constructParamInfo(&info,l2,0);
	rvMegacoParameterListForEach(l1,compareParam,&info);		
	return info.state;
}

void rvMegacoTermMgrUtilParamListFillDif(const RvMegacoParameterList * inList,RvMegacoParameterList * outList,RvMegacoParameterList * difList) {
	if(paramListChanged(inList,outList)) 
		/* Extract all the parameters which are different in the 
		  out list than in the in list (including the new ones) */
		paramListExtractDif(outList,inList,difList);
}

/*************************************************************************************/
/* Return rvTrue if error not set */
RvBool	rvMegacoTermMgrUtilIsCmdRspValid(const RvMegacoCommandReply *x) {
	const RvMegacoErrorDescriptor * error;
	if(x==NULL) 
		return rvTrue;
	error = rvMegacoCommandReplyGetError(x);
	return !rvMegacoErrorDescriptorIsSet(error);
}

/*************************************************************************************/

/*--------------------------------------------------------------------------------------*/
/*   Timers                                                                             */
/*--------------------------------------------------------------------------------------*/
/* TODO: Allocate them from their own pool so they don't depende on term memory
   Needs a special allocator in the termination or the term mgr
*/

#define RV_MEGACOTERMTIMER_MAXTIME 30000

/* Call only from inside the timeout callback!*/
static void timerRelease(RvMegacoTermTimer* t) {
	if(t!=NULL) {
		rvTimerDestruct(&t->timer);
		rvAllocDeallocate(t->alloc,sizeof(RvMegacoTermTimer),t);
	}
}

/* TODO: Make the function return some value to reset the timer
   (for example return 500 to reset to 500 msecs .
   The term state mutex can be used as the timer mutex
   Then the function will check the state.
   If is processing, will just reset the timer.
   If not, will lock the term mutex and do the work.
*/
static void processTimer(RvTimer* timer, void* data) {
	RvMegacoTermTimer* t = (RvMegacoTermTimer*)data;

	/* Check valid twice: one for the case that the timer was cancelled
	   before expiration and the mutex data may be false */
	if(t->valid!=rvTrue) {
		timerRelease(t);	
		return;
	}

	rvMutexLock(t->mutex);
	/* This is for the case that the timer was just cancelled concurrently
	   with expiration, the mutex will insure seriality, i.e., the full 
	   cancel must be done before the process or the other way around */
	if(t->valid==rvTrue) {
		if(t->loopCnt) {
			t->loopCnt--;	
			rvTimerReset(&t->timer,RV_MEGACOTERMTIMER_MAXTIME);
			rvMutexUnlock(t->mutex);
			return;
		}
		/* Call callback function */
		t->func(timer,data);
	}
	rvMutexUnlock(t->mutex);
	timerRelease(t);	
}

RvMegacoTermTimer* rvMegacoTermTimerCreate(RvAlloc* alloc,RvMilliseconds ms, 
										  rvMegacoTermTimerCB func, void* data,
										  RvMutex* mutex) {
	RvMegacoTermTimer* t = rvAllocAllocate(alloc,sizeof(RvMegacoTermTimer));
	t->valid = rvTrue;
	t->data = data;
	t->alloc = alloc;
	t->func = func;
	t->mutex = mutex;
	t->loopCnt = 0;
	/* Total sleep time will be split, so the timer never sleeps more
	   than max. This is allow the timer to release itself quickly if
	   is canceled and avoid timers hanging around forever. */
	if(ms > RV_MEGACOTERMTIMER_MAXTIME) {
		t->loopCnt = ms/RV_MEGACOTERMTIMER_MAXTIME;
		ms = ms - t->loopCnt*RV_MEGACOTERMTIMER_MAXTIME;	
	}
	rvTimerConstruct(&t->timer,ms,processTimer,t);
	rvTimerStart(&t->timer);
	return t;
}	
void rvMegacoTermTimerCancel(RvMegacoTermTimer* t) {
	if(t!=NULL) {
        rvMutexLock(t->mutex);
        t->valid = rvFalse;
	    rvMutexUnlock(t->mutex);
    }
}

⌨️ 快捷键说明

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