#ifndef __DORIS_CLIENT_HTTP_IN_H__
#define __DORIS_CLIENT_HTTP_IN_H__

#include <openssl/ssl.h>
#include <openssl/crypto.h>
#include <openssl/x509.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <openssl/rsa.h>
#include <event.h>

#include <MESA/MESA_handle_logger.h>

#include <map>
#include <string>

#include "doris_client_transfer.h"
#include "nirvana_conhash.h"

using namespace std;

#ifndef __FILENAME__
#define __FILENAME__ __FILE__
#endif
#define MESA_HANDLE_RUNTIME_LOGV2(handle, lv, fmt, args...)   \
    MESA_handle_runtime_log((handle), (lv), "DorisClient", "%s:%d, " fmt, __FILENAME__, __LINE__, ##args)

#define DEFAULT_HOST_CAPACITY  4
#define LOAD_BALANC_VIRT_TIMES 16

enum TCP_CONNECTION_STATUS
{
	TCP_STATUS_IDLE=0,
	TCP_STATUS_CONNECTING,    //只有正常发起链接才会缓存数据
	TCP_STATUS_CONNECTED,
	TCP_STATUS_DISCONNECT,
};


struct dst_ipaddr_group
{
	u_int32_t *dstaddrs;
	u_int32_t dstaddr_num;
};

struct doris_http_parameter;
struct dst_host_cnn_balance
{
	struct sockaddr_in addr;
	char srvaddr[64];
	
	struct event timer_detect;
	struct bufferevent *bev;

	u_int32_t dstip;
	enum TCP_CONNECTION_STATUS connection_status;
	bool connect_failed;

	struct doris_http_parameter *param;
};

struct doris_http_parameter
{
	u_int32_t server_port;
	u_int32_t manage_port;
	int32_t timer_period;
	u_int32_t max_http_sessions;
	int32_t connected_hosts;
	int32_t failed_hosts;
	int32_t ssl_connection;

	struct dst_ipaddr_group ipgroup;
	struct consistent_hash *conhash;
	struct dst_host_cnn_balance *balance;

	long maximum_host_cnns;
	long transfer_timeout;//传输总时间限制
	long maximum_pipelines;
	void *runtime_log;
	struct event_base* evbase;
};

struct doris_http_instance
{
	struct event_base* evbase;
	SSL_CTX *ssl_instance;
	void *privdata;

	map<u_int32_t, doris_curl_multihd*> *server_hosts;

	struct doris_http_parameter *param;
	void *runtime_log;
};

struct time_event
{
	struct event timer_event;
	void *data;
};

int32_t param_get_connected_hosts(struct doris_http_parameter *param);
int32_t param_get_failed_hosts(struct doris_http_parameter *param);

struct doris_http_parameter *doris_http_parameter_new(const char* profile_path, const char* section, struct event_base* evbase, void *runtime_log);
struct doris_http_instance *doris_http_instance_new(struct doris_http_parameter *param, struct event_base* evbase, void *runtimelog);

#endif

