#ifndef __REST_PROXY_H__
#define __REST_PROXY_H__

#ifdef __cplusplus
extern "C" {
#endif


#include "restapi.h"
#include "curl_adaptor.h"
#include "rest_proxy_conf.h"


#define HTTP_OK						200


#define SERVER_PORT					4949

#define WAIT_TIMER					500 //5seconds

// #define RESTPROXY_MISISDN_LEN		24


#define	MAX_THREADS				1024//256

#define THREAD_INVALID_INDEX		0xFFFF

typedef enum _STATE
{
	ST_SEND_QUERY_SUBSID = 1,
	ST_SEND_QUERY,
	ST_SEND_TOPUP,
	ST_SEND_TOPUP_SUBSCRIBID,

	ST_SEND_TRANS_SUBSID_SENDER,
	ST_SEND_TRANS_SUBSID_RECEIVER,
	ST_SEND_TRANS,
	ST_WAIT_RESTAPI_TRANS_SUBSID_SENDER,
	ST_WAIT_RESTAPI_TRANS_SUBID_RECEIVER,
	ST_WAIT_RESTAPI_TRANS,
	
	ST_WAIT_RESTAPI,
	ST_WAIT_RESTAPI_QUERY_SUBSID,
	ST_WAIT_RESTAPI_TOPUP_SUBID,
	
	
}STATE;

typedef enum _STATISTICS
{
	STAT_QUERY_REQ,
	STAT_QUERY_RES,
	STAT_QUERY_REST_SUBSID_REQ,	/* 增加subscriber id请求统计 */
	STAT_QUERY_REST_SUBSID_RES, /* 增加subscriber id回复统计 */
	STAT_QUERY_REST_REQ,
	STAT_QUERY_REST_RES,

	STAT_TRANSFER_REQ,
	STAT_TRANSFER_RES,
	STAT_TRANSFER_REST_SUBSID_REQ_SENDER,
	STAT_TRANSFER_REST_SUBSID_RES_SENDER,
	STAT_TRANSFER_REST_SUBSID_REQ_RECEIVER,
	STAT_TRANSFER_REST_SUBSID_RES_RECEIVER,
	STAT_TRANSFER_REST_REQ,
	STAT_TRANSFER_REST_RES,

	STAT_TOPUP_REQ,
	STAT_TOPUP_RES,
	STAT_TOPUP_REST_SUBSID_REQ,	/* 增加subscriber id请求统计 */
	STAT_TOPUP_REST_SUBSID_RES,	/* 增加subscriber id回复统计 */
	STAT_TOPUP_REST_REQ,
	STAT_TOPUP_REST_RES,

	STAT_MAX,
	
}STATISTICS;

typedef struct rest_thread
{
	char			used;
	char			state;
	int				timer;
	short			ppsAppID;
	short			restproxyAppID;
	char			msisdn[RESTFUL_MSISDN_LEN];
    char			pin[RESTFUL_MSISDN_LEN];

	char			msisdn_out[RESTFUL_MSISDN_LEN];
	char			msisdn_in[RESTFUL_MSISDN_LEN];
	u32			 money;
	
	char			username[RESTFUL_USERNAME];
	char			password[RESTFUL_PASSWOR];
	int			pps_ip;
	short		pps_port;

	SCurlAdaptor	curAdaptor;
}_rest_thread;

typedef struct _s_rest_proxy_env
{
	s_restproxy_conf	cnf;					/* rest proxy configure infor */

	_rest_thread		threads[MAX_THREADS];	/* handle messge from/to pps&rest */

	int					work_as_proxy_emulator;

	int					query_req_num;			/* counts pps<->restproxy<->restful */
	int					query_res_num;
	int					query_rest_subscrib_req_num;
	int					query_rest_subscrib_res_num;
	int					query_rest_req_num;
	int					query_rest_res_num;

	int					trans_req_num;			/* counts pps<->restproxy<->restful */
	int					trans_res_num;
	int					trans_rest_subscrib_req_num_out;
	int					trans_rest_subscrib_res_num_out;
	int					trans_rest_subscrib_req_num_in;
	int					trans_rest_subscrib_res_num_in;
	int					trans_rest_req_num;
	int					trans_rest_res_num;
	
	int					topup_req_num;
	int					topup_res_num;
	int					topup_rest_subscrib_req_num;
	int					topup_rest_subscrib_res_num;
	int					topup_rest_req_num;
	int					topup_rest_res_num;
}s_rest_proxy_env;

#define REST_PROXY_SUBSCRIBID_FLAG (g_rest_proxy_env.cnf.b_fetch_subscribid)

extern s_rest_proxy_env g_rest_proxy_env;


int rest_conf_set_emulator_flag(int flag);


int rest_proxy_init();

void rest_proxy_uninit();

int rest_proxy_fsm_main();

#ifdef __cplusplus
}
#endif

#endif /* __REST_PROXY_H__ */
