#include "dualsrv_pub.h"

extern _sync_hb sync_hb;
extern int ems_debug_flag;
extern char debug_message[MAX_ASCOUT_BUF_LEN];
extern MYSQL *pDualConn;
extern int dual_status;
_config_info config_info;
_config_info other_config_info;

void debug_log(const char *fmt,...)
{
#if DUALSRV_DEBUG == 1
	char logFile[64];
	FILE *fp;
	time_t l_time;
	va_list ap;
	char buf[8192];
	struct tm *t;
	char timestr[64];
	
	l_time=time(NULL);
	t=localtime(&l_time);
	sprintf(logFile,"../log/dualsrv_%04d%02d%02d.log",t->tm_year+1900,t->tm_mon+1,t->tm_mday);
	fp=fopen(logFile,"a");
	if(fp!=NULL)
	{
		va_start(ap,fmt);
		vsprintf(buf,fmt,ap);
		sprintf(timestr,"%02d:%02d:%02d: ",t->tm_hour,t->tm_min,t->tm_sec);
		fputs(timestr,fp);
		fputs(buf,fp);
		fputs("\n",fp);
		fflush(fp);
		va_end(ap);
		fclose(fp);
	}
#endif
}

unsigned long getipbyname(char *hostname)
{
	struct hostent *hptr;
	struct in_addr *inaddr;

	if (strlen(hostname) <= 0)
		return INADDR_NONE;
	if ((hptr = gethostbyname(hostname)) == NULL)
		return INADDR_NONE;

	inaddr = (struct in_addr *) hptr->h_addr_list[0];

	return inaddr->s_addr;
}

int getent(FILE * fp, char *key, char *str, int maxlen)
{
	char *pval, *pkey, *pchr;
	int i = 1, len;
	str[0] = '\0';
	fseek(fp, 0, SEEK_SET);
	pval = NULL;
	while (fgets(str, maxlen, fp) != NULL) {
		if ((pchr = strchr(str, '#')) != NULL)
			pchr[0] = '\0';
		if ((pkey = strstr(str, key)) != NULL) {
			if ((pval = strchr(str, '=')) == NULL || pkey >= pval)
				continue;
			else
				break;
		}
	}
	if (pval == NULL)
		return -1;
	pval++;
	len = strlen(pval);

	//delect last space
	while (isspace(pval[len - i]) && i <= len) {
		pval[len - i] = '\0';
		i++;
	}

	i = 0;
	while (isspace(pval[i++]) && i < len);
	sprintf(str, "%s", pval + i - 1);
	return 1;
}

int parse_confline(char *str, char (*row)[64], int maxrow)
{
	char *pstr = str;
	int len, index = 0, i;
	BOOL black = 0;
	len = strlen(str);
	for (i = 0; i < len && index < maxrow; i++) {
		if (isspace(str[i])) {
			if (black == 0)
				pstr = str + i + 1;
			else {
				str[i] = '\0';
				if (strlen(pstr) > 64)
					pstr[63] = 0;
				sprintf(row[index++], "%s", pstr);
				pstr = str + i + 1;
			}
			black = 0;
		} else
			black = 1;
	}
	if (black == 1) {
		if (strlen(pstr) > 64)
			pstr[63] = 0;
		sprintf(row[index++], "%s", pstr);
	}
	return index;
}

int read_conf_file()
{
	char fileName[512];
	unsigned char buf[4096];
	unsigned char param[32][512];
	char paramName[128];
	char paramValue[384];
	char* temp;
	FILE* fp;
	int fi;
	int i;
	int paramCount;
	int byteCount;
	
	paramCount=0;
	byteCount=0;
	memset(buf,0,4096);
	memset(param,0,32*512);
	sprintf(fileName,"%s",OMCD_CONF_FILE);
	fp=fopen(fileName,"r");
	if(fp)
	{
		debug_log("read the %s",OMCD_CONF_FILE);
		
		fi=fileno(fp);
		if(read(fi,buf,4096))
		{
			for(i=0;i<4096;i++)
			{
				if(buf[i] == 0x0A)
				{
					if(byteCount<512 && param[paramCount][0] != 0x00 && param[paramCount][0] != 0x23)
						paramCount++;
					else
						memset(param[paramCount],0,512);	
					byteCount=0;
				}	
				else
				{	
					param[paramCount][byteCount]=buf[i];
					byteCount++;
				}
			}
			
			for(i=0;i<paramCount;i++)
			{
				temp=strtok((char*)param[i],"=");
				sprintf(paramName,"%s",temp);
				//printf("Name=%s,",temp);
				temp=strtok(NULL,"=");
				sprintf(paramValue,"%s",temp);
				//printf("Value=%s\n",temp);
				
				//debug_log("%s=%s",paramName,paramValue);
							
				if(strcasecmp(paramName,"omcSysNo") == 0)
				{
					config_info.sys_no=atoi(paramValue);
				}
				else if(strcasecmp(paramName,"omcRunMode") == 0)
				{
					config_info.run_mode=atoi(paramValue);
				}
				
			}			
		}	
		close(fi);
		fclose(fp);
	}	
	else
	{
		debug_log("Fatal Error: The %s does not exist!",OMCD_CONF_FILE);
		printf("Fatal Error: The %s does not exist!",OMCD_CONF_FILE);
		exit(1);
	}
	
	return 1;
}


void read_server_conf()
{
	FILE *fp;
	char omc_master[32] = "\0", mysql_master[32] = "\0";
	char omc_slave[32] = "\0", mysql_slave[32] = "\0";
	char tmpstr[32];
	
	debug_log("read the %s",SERVER_CONF_FILE);

	fp=fopen(SERVER_CONF_FILE, "r");

	if(fp == NULL) 
	{
		debug_log("File not found:%s\n", SERVER_CONF_FILE);
		exitLog("dualsrv:load_srvconf, Can not find %s",SERVER_CONF_FILE);
		exit(1);
	}
	//.......
	getent(fp, "omc_master", omc_master, 32);
	getent(fp, "mysql_master", mysql_master, 32);
	getent(fp, "omc_slave", omc_slave, 32);
	getent(fp, "mysql_slave", mysql_slave, 32);

	sprintf(tmpstr,"-%d",config_info.sys_no);
	if(strstr(omc_master,tmpstr))
	{
		sprintf(config_info.host_name,"%s",omc_master);
		sprintf(other_config_info.host_name,"%s",omc_slave);
	}
	else
	{
		sprintf(config_info.host_name,"%s",omc_slave);
		sprintf(other_config_info.host_name,"%s",omc_master);
	}

	config_info.ip=getipbyname(config_info.host_name);
	other_config_info.ip=getipbyname(other_config_info.host_name);	
	
	fclose(fp);
	
	heartbeat_init(config_info.sys_no);
	printf("\nheartbeat_init(%d)...\n",config_info.sys_no);
		
	
}

void set_sync_hb()
{
	BYTE sync_hb_data[32];
	DWORD *p_dword;
	int index=0;
	
	chOmcStatus((unsigned char)dual_status);

	p_dword=(DWORD *)sync_hb_data;
	p_dword[index]=htonl(sync_hb.tol_master_log_file);
	index++;
	p_dword[index]=htonl(sync_hb.tol_master_log_pos);
	index++;
	p_dword[index]=htonl(sync_hb.cur_master_log_file);
	index++;
	p_dword[index]=htonl(sync_hb.cur_master_log_pos);
	index++;
	
	p_dword[index]=htonl(sync_hb.tol_relay_log_file);
	index++;
	p_dword[index]=htonl(sync_hb.tol_relay_log_pos);
	index++;
	p_dword[index]=htonl(sync_hb.cur_relay_log_file);
	index++;
	p_dword[index]=htonl(sync_hb.cur_relay_log_pos);
	index++;
	
	fillSyncInfo(sync_hb_data);
}

