#ifndef __DORIS_CLIENT_FETCH_IN_H__
#define __DORIS_CLIENT_FETCH_IN_H__

#include <openssl/md5.h>
#include <MESA/field_stat2.h>

#include "doris_client.h"
#include "doris_client_http.h"

struct easy_string
{
	char* buff;
	size_t len;
	size_t size;
};

enum FETCH_CFG_STATUS
{
	FETCH_STATUS_IDLE=0,
	FETCH_STATUS_META,
	FETCH_STATUS_FILE,
};

struct doris_parameter
{
	u_int32_t retry_interval;
	u_int32_t fetch_frag_size;
	u_int32_t fetch_max_tries;

	struct doris_http_parameter *param_master;
	struct doris_http_parameter *param_backup1;
	struct doris_http_parameter *param_backup2;
	struct event_base *manage_evbase;

	screen_stat_handle_t fsstat_handle;
	struct event fs_timer_output;
	char fsstat_dst_ip[64];
	char fsstat_appname[16];
	char fsstat_filepath[256];
	u_int32_t fsstat_period;
	int32_t fsstat_print_mode;
	int32_t fsstat_dst_port;
	int32_t fsstat_field[FSSTAT_DORIS_FILED_NUM];
	int32_t fsstat_status[FSSTAT_DORIS_STATUS_NUM];
};

struct fetch_file_meta
{
	const char *table_name;
	size_t size;
	size_t curoffset;
	u_int32_t cfg_num;
	u_int32_t validate_md5;
	char md5str[36];
	const char *user_region;
};

struct md5_long
{
	u_int64_t md5l;
	u_int64_t md5h;
};
union doris_md5
{
	char md5[16];
	struct md5_long md5long;
};

struct doris_confile_ctx
{
	struct doris_http_ctx *httpctx;
	char server[64];

	MD5_CTX md5ctx;
	long res_code;
	size_t contlength;
	size_t contl_start;
	size_t contl_end;
	size_t contl_total;
};

struct doris_instance
{
	struct doris_callbacks cbs;
	struct doris_arguments args;

	enum FETCH_CFG_STATUS status;
	u_int32_t retry_times;

	struct doris_http_instance *cur_httpins;
	int64_t cur_version; //元信息
	int64_t req_version; //文件
	int64_t new_version; //新的元信息
	struct easy_string estr;
	cJSON *meta, *array;
	u_int32_t array_size;
	u_int32_t array_index;
	struct fetch_file_meta curmeta;

	struct doris_confile_ctx ctx;

	struct doris_http_instance *httpins_master;
	struct doris_http_instance *httpins_backup1;
	struct doris_http_instance *httpins_backup2;

	struct event_base *worker_evbase;
	struct event timer_fetch;
	struct doris_parameter *param;
	struct event timer_statistic;
	struct doris_statistics statistic, statistic_last;
	void *runtime_log;
};

#endif

