/*************************************************
File name:	subsCommMain.c
Author: 	He wan Dong
Version:	9:00:00 
Date:  		2003-2-14
Description:该文件调用了subsComm模块
			
			调用snmp模块的函数
			void snmp_init(WORD nport);
			int snmp_receive(snmp_pdu *msgbuf,snmp_addr *addr);
			int snmp_send(snmp_pdu *msgbuf,snmp_addr *addr);
		
			
												
History:               
No. 		
Author:		  
Date:		 
Version:	   
Description:
*************************************************/


#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>

#include "subsComm.h"

static struct itimerval itimer, old_itimer;

void onTimer();
void setTimer();
void setup_daemon(void);
int timerCounter = 0;

/*************************************************
Function:        // main
Description:     // 调用subsComm模块，是模块的主函数
Calls:           // snmp_init; iptrans_init; 
Called By:       // 
Table Accessed:  // 
Table Updated:   // 
Input:           // 
Output:          // 
Return:          // 1:successful; 0:failed;
Others:          // 
*************************************************/
int main(int argc, char *argv[])
{
	char runMode;
	debug_init();

	fprintf(stderr, "\n Calling iptrans_init()");
	iptrans_init();

	fprintf(stderr, "\n Calling snmp_init()");
	snmp_init(4959);
	subsCommInit();

	if (argc > 0) {
		runMode = getopt(argc, argv, "d");
		switch (runMode) {
		case 'd':
			setup_daemon();
			break;
		case '?':
			break;
		}
		//debug_init();//must do it at first
	}
	setTimer();
	while (1) {
		usleep(50);
	}

	return (1);
}

/*************************************************
Function:        // onTimer
Description:     // 定时器函数
Calls:           // subsCommTimer;
Called By:       // 
Table Accessed:  // 
Table Updated:   // 
Input:           // 
Output:          // 
Return:          // 
Others:          // 
*************************************************/
void onTimer()
{
	//debug_rt();
	//snmp_timer();
	if (timerCounter > 3) {
		timerCounter = 0;
		subsCommTimer();
	}
	timerCounter++;
}

/*************************************************
Function:        // setTimer
Description:     // 设置定时器函数
Calls:           // onTimer;
Called By:       // 
Table Accessed:  // 
Table Updated:   // 
Input:           // 
Output:          // 
Return:          // 
Others:          // 
*************************************************/
void setTimer()
{
	struct sigaction act;
	act.sa_handler = onTimer;
	sigemptyset(&act.sa_mask);
	act.sa_flags = 0;
	if (sigaction(SIGALRM, &act, NULL) < 0) {
		perror("Produce Sigaction");
		exitLog("subsComm:setTimer, sigaction function error");
		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");
		exitLog("subsComm:setTimer, setitimer function error");
		exit(1);
	}
}

void setup_daemon(void)
{
	/*
	int i;
	
	for (i = 0; i < 3; ++i) {
		close(i);
	}
	*/
	switch (fork()) {
	case -1:
		perror("setup_daemon(), 1st fork()");
		exit(2);
	default:
		exit(0);
	case 0:
		if (setsid() == -1) {
			perror("setup_daemon(), setsid()");
			exit(3);
		}
		switch (fork()) {
		case -1:
			perror("setup_daemon(), 2nd fork()");
			exit(3);
		default:
			exit(0);
		case 0:
			umask(0);
			/* and return with daemon set up */
			return;
		}
	}
}
