/*************************************************
File name:	cdr.c
Author: 	
Version:	9:00:00 
Date:  		2002-5-7
Description:cdrCollector模块的主文件，初始化snmp和iptrans模块，调用cdr.c里的函数
			定义定时器函数，设置定时
			
			调用外部模块的函数
			void snmp_init(WORD nport);
			void iptrans_init();
			
												
History:               
No. 		
Author:		  
Date:		 
Version:	   
Description:
*************************************************/

#include "cdr.h"

static struct itimerval itimer, old_itimer;
static void On_Timer();
static void SetTimer();

/*************************************************
Function:        // main
Description:     // 初始化snmp和iptrans模块,开始调用定时器函数
Calls:           // iptrans_init; snmp_init;
Called By:       // 
Table Accessed:  // 
Table Updated:   // 
Input:           // 
Output:          // 
Return:          // 
Others:          // 
*************************************************/
int main(int argc, char *argv[])
{
	
	pid_t pid;
	//debug_init();
	
	//fprintf(stderr, "\n Calling iptrans_init()");
	//iptrans_init();
	
	//fprintf(stderr, "\n Calling snmp_init()");
	//snmp_init(CDR_PORT);

	/*
	if (argc == 2 && strstr(argv[1], "-d")) {
		if ((pid = fork()) != 0)
			exit(0);
		setsid();
		//umask(0);
	}*/
	
	if (argc != 4) {
		exit(0);
	}
	
	strcpy(totalCdrFileName,argv[1]);
	strcpy(cdrFileName,argv[2]);
	cdrHour=atoi(argv[3]);
	
	
	cdr_init();
	
	//cdr_start();
	
	
	converseFile();
	
	/*
	SetTimer();
	while (1) {
		//cdr_loop();
		usleep(50);
	}
	return 1;
	*/
	
	
}

/*************************************************
Function:        // On_Timer
Description:     // 定时器函数
Calls:           // cdr_timer
Called By:       // 
Table Accessed:  // 
Table Updated:   // 
Input:           // 
Output:          // 
Return:          // 
Others:          // 
*************************************************/
void On_Timer()
{
	//debug_rt();
	//snmp_timer();
	cdr_timer();
}

/*************************************************
Function:        // SetTimer
Description:     // 设置定时器函数
Calls:           // On_Timer
Called By:       // 
Table Accessed:  // 
Table Updated:   // 
Input:           // 
Output:          // 
Return:          // 
Others:          // 
*************************************************/
void SetTimer()
{
	struct sigaction act;
	act.sa_handler = On_Timer;
	sigemptyset(&act.sa_mask);
	act.sa_flags = 0;
	if (sigaction(SIGALRM, &act, NULL) < 0) {
		perror("Produce Sigaction");
		exit(1);
	}

	itimer.it_interval.tv_sec = 0;
	itimer.it_interval.tv_usec = 20 * 1000;
	itimer.it_value.tv_sec = 0;
	itimer.it_value.tv_usec = 20 * 1000;

	if (setitimer(ITIMER_REAL, &itimer, &old_itimer) != 0) {
		printf("Setting Timer  error! \n");
		exit(1);
	}
}
