/*************************************************
File name:sftpSend.c
Author: 	
Version:	9:00:00 
Date:  		2007-10-9
Description:定义sftp模块的定时器
			
			
			
												
History:               
No. 		
Author:		  
Date:		 
Version:	   
Description:
*************************************************/

#include "sftp.h"

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

/*************************************************
Function:        // main
Description:     // 
Calls:           // 
Called By:       // 
Table Accessed:  // 
Table Updated:   // 
Input:           // 
Output:          // 
Return:          // 
Others:          // 
*************************************************/
int main(int argc, char *argv[])
{
	pid_t pid;


	if (argc == 2 && strstr(argv[1], "-d")) {
		if ((pid = fork()) != 0)
			exit(0);
		setsid();
	}
	
	sftp_init();
	
	SetTimer();
	while (1) {
		usleep(50);
	}
	return 1;
}

/*************************************************
Function:        // On_Timer
Description:     // 定时器函数
Calls:           // sftpsend_timer
Called By:       // 
Table Accessed:  // 
Table Updated:   // 
Input:           // 
Output:          // 
Return:          // 
Others:          // 
*************************************************/
void On_Timer()
{
	//long l_time;
	//struct tm *t;
	
	//l_time = time(NULL);
	//t = localtime(&l_time);
	
	sftpsend_timer();
	/*****test************/
	
	//printf("timer-%02d\n",t->tm_sec);
}

/*************************************************
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");
		exitLog("sftpSend:SetTimer, sigaction function error");
		exit(1);
	}

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

	
	itimer.it_interval.tv_sec = 1;
	itimer.it_interval.tv_usec = 0;
	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("sftpSend:SetTimer, setitimer function error");
		exit(1);
	}
}
