#include 	<assert.h>
#include	<stdio.h>
#include	<stdlib.h>
#include	<errno.h>
#include	<string.h>
#include	<ctype.h>
#include	<math.h>
#include	<memory.h>
#include	<sys/types.h>
#include	<netinet/in.h>
#include	<sys/socket.h>
#include	<arpa/inet.h>
#include	<netdb.h>
#include	<sys/wait.h>
#include	<unistd.h>
#include	<signal.h>
#include	<sys/ipc.h>
#include	<sys/shm.h>
#include	<sys/sem.h>
#include	<sys/msg.h>
#include	<sys/time.h>
#include	<time.h>
#include  <net/if.h>
#include	<syslog.h>
#include	<termio.h>
#include  <fcntl.h>
#include  <stdarg.h>
#include  <syslog.h>
#include  <sys/io.h>
#include  <error.h>
#include	<sys/stat.h>
#include	<ftw.h>
#include	<stddef.h>

#include "../../../omcLib/c_program/omcLib/omcLib.h"
#include "../../../../plat/debug/src/include/debug.h"
#include "../../../../plat/snmp/src/include/snmp.h"
#include "../../../faultMgr/c_program/beatProc/beatProc.h"
#include "../../../faultMgr/c_program/beatSend/beatSend.h"
#include "mysql.h"

#define DUALSRV_DEBUG 1
#define DUALSRV_DEBUG_ID 16
#define DUALSRV_SOFTER_VERSION "EMS R9.1"
#define DUALSRV_MODULE_VERSION "R9.1"
#ifndef MAX_ASCOUT_BUF_LEN
#define MAX_ASCOUT_BUF_LEN 8192
#endif
#ifndef MAX_ASCIN_BUF_LEN
#define MAX_ASCIN_BUF_LEN 8192
#endif
#define ONE_SECOND_COUNT 100
#define TEN_SECOND_COUNT 10*ONE_SECOND_COUNT
#define ONE_MINUTE_COUNT 60*ONE_SECOND_COUNT
#define DEGRADE_HB_COUNT 2


#define OMCD_CONF_FILE "./conf/omcd.conf"
#define SERVER_CONF_FILE "./conf/server.conf"
#define SYNC_INFO_FILE "./conf/sync.conf"


#define RUN_MODE_SINGLE 0
#define RUN_MODE_DUAL 1
#define PLAT_0 0
#define PLAT_1 1


#define DUAL_STATE_SLAVE 3
#define DUAL_STATE_SYNC 4
#define DUAL_STATE_MASTER 5
#define DUAL_STATE_SINGLE 7


/* by simon at 23/9/26 */
// struct timeval starttime,endtime;
// int nouse;
#define START gettimeofday(&starttime,NULL)
#define END(label) nouse=(gettimeofday(&endtime,NULL),printf("[%s]consume time=%f\n",label,((endtime.tv_sec-starttime.tv_sec)*1000000+endtime.tv_usec-starttime.tv_usec)/1000000.0));

typedef struct _config_info
{
	int sys_no;
	int run_mode;	//0-single, 1-dual
	char host_name[32];
	unsigned long ip;
}_config_info;

typedef struct _sync_hb
{
	unsigned long tol_master_log_file; //get in the master
	unsigned long tol_master_log_pos; //get in the master
	unsigned long cur_master_log_file; //get in the slave
	unsigned long cur_master_log_pos; //get in the slave
	unsigned long tol_relay_log_file; //get in the slave
	unsigned long tol_relay_log_pos; //get in the slave
	unsigned long cur_relay_log_file; //get in the slave
	unsigned long cur_relay_log_pos; //get in the slave
}_sync_hb;

/******  beatSend.c  *************/
extern void chOmcStatus(BYTE status);
extern void fillSyncInfo(BYTE * info);
extern void fillProcStatus(BYTE procnum,BYTE *procstatus);

/******  dualsrv.c  **************/
void receive_trap_message(BYTE oidlen,DWORD* oid,BYTE* pdata,WORD len,snmp_addr * addr);

/******  dualsrv_func.c  *********/
void read_server_conf();
int read_conf_file();
void debug_log(const char *fmt,...);
int getent(FILE * fp, char *key, char *str, int maxlen);
unsigned long getipbyname(char *hostname);
unsigned long getlocalip();
int parse_confline(char *str, char (*row)[64], int maxrow);
void set_sync_hb();
void check_proc_status();
void report_status();

/******  dualsrv_debug.c  ********/
extern void debug_init_wxc2_ver(char *ver_info);
extern int debug_set_response(BYTE oidlen,DWORD *oid,BYTE *data_ptr,WORD data_len);
void send_ascout(char *sdBuf);
void send_error(char *sdBuf);
void send_vital_ascount(char *sdBuf);
int dualsrv_debug_init();
void dualsrv_debug_timer();
