/**************************************/
/*Title:	alarmagent.c										*/
/*Descr:	Alarm Agent										*/
/*          --MIB(WXC2-ALARM)								*/
/*Author:	Yi zheng                                       	*/
/*Create:	2006-6-1                                         	*/
/**************************************/
#include "alarmGen.h"
static struct itimerval itimer, old_itimer;
static void On_Timer();
static void SetTimer();

extern int alarm_GenInit(void);
extern int alarm_db_init(void);
extern void alarm_Gen_timer(void);

int main(int argc, char *argv[])
{
	pid_t pid;
	
	if (argc == 2 && strstr(argv[1], "-d"))
	{
		if ((pid = fork()) != 0)
			exit(0);
		setsid();
	}
#ifndef __RUN_SELF
	iptrans_init();
	debug_init(1);
#else	
	debug_init(1);
	iptrMainInit();	
//	iptrans_init();
#endif	
	snmp_init(GENPORT);
	
	alarm_GenInit();
	SetTimer();
	while (1)			
		usleep(50);	
	return 1;	
}


void On_Timer() 
{
	static BYTE flag=0;
#ifdef __RUN_SELF
	iptrans_timer();
	debug_rt();
#endif
	if(flag==0)
	{
		flag = 1;	
		snmp_timer();
		alarm_Gen_timer();
		flag = 0;
	}
}


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);		
	}	
}


