trafficSDKHelper.h 2.54 KB
/*
*对TrafficSDK.h的再封装,实现简单的单例模式
*/
#ifndef _TRAFFIC_SDK_HELPER_H
#define _TRAFFIC_SDK_HELPER_H

#include "include/include/TrafficSDK.h"

#include "include/app/trafficQueue.h"

static HTrafficSDK hTrafficSDK = NULL;
static HSCConfig hConfig = NULL;
static CirQueue trafficQueue;
static int callbackCount = 0;        //用于判断是否需要注册回调

static pthread_mutex_t trafficMut = PTHREAD_MUTEX_INITIALIZER;

//简单的单例模式(加锁没有考虑)
HTrafficSDK getTrafficSDK()
{
	pthread_mutex_lock(&trafficMut);
    if(hTrafficSDK == NULL)
    {
		hTrafficSDK = TrafficSDK_Init("127.0.0.1", 41065, "IVEClientLog.txt", 7, 1024);
		if(SC_ErrSuccess != TrafficSDK_Start(hTrafficSDK))
		{
			TrafficSDK_UnInit(hTrafficSDK);
			hTrafficSDK = NULL;
			pthread_mutex_unlock(&trafficMut);
			return NULL;
		}
	}
    pthread_mutex_unlock(&trafficMut);
    return hTrafficSDK;
}


HSCConfig getHSCConfig()
{
	if(hConfig)
	{
		TrafficSDK_ReleaseHSCConfig(hTrafficSDK, hConfig);
		hConfig = NULL;
	}
	if(hConfig == NULL)
	{
		if(getTrafficSDK() == NULL)
		{
			return NULL;
		}

		void *pParamXml = NULL;
		int nParamXmlLen = 0;
		ScErr err = TrafficSDK_GetHSCConfig(hTrafficSDK, &hConfig, pParamXml, nParamXmlLen);
		if(err != SC_ErrSuccess)
		{
//			printf("----------FAILED: TrafficSDK_GetHSCConfig--------\n");
			return NULL;
		}
	}
	return hConfig;
}

int getCallbackCount()
{
	return callbackCount;
}

void addCallbackCount()
{
	++callbackCount;
}

void initCallbackCount()
{
	callbackCount = 0;
}

//封装后的注册回调函数
void tcRegisterCallback()
{
	printf("---------------------registerCallback-----------------------\n");
	HTrafficSDK pTrafficSDK = getTrafficSDK();
	if(pTrafficSDK == NULL)
	{
		printf("---------------------getTrafficSDK error-----------------------\n");
		return;
	}
		//initialTrafficQueue();
		//TrafficSDK_SetResultCallBack(pTrafficSDK, tcTrafficResult, 0);
}


ScErr releaseTrafficResources()
{
	ScErr err = SC_ErrSuccess;
	/*
	pthread_mutex_lock(&trafficMut);
	if(hTrafficSDK != NULL)
	{
		if(hConfig != NULL)
		{
			err = TrafficSDK_ReleaseHSCConfig(hTrafficSDK, hConfig);
			if(err == SC_ErrSuccess)
			{
				hConfig = NULL;
			}
			else
			{
				pthread_mutex_unlock(&trafficMut);
				return err;
			}
		}

		err = TrafficSDK_UnInit(hTrafficSDK);
		if(err == SC_ErrSuccess)
		{
			hTrafficSDK = NULL;
		}
		else
		{
			pthread_mutex_unlock(&trafficMut);
			return err;
		}
		initCallbackCount();
	}
	pthread_mutex_unlock(&trafficMut);
	*/
	return err;

}

void initialTrafficQueue()
{
	Initial(&trafficQueue);
}

#endif