/*H***************************************************************************
**
**  Copyright  (c)  2002,  Video Surveillance, Inc.
**		  All Rights Reserved.
**
**  Subsystem:	
**  File:	 	vms_api.h
**  Created By:   	cham.li
**  Created On:	 07/09/2007
**  
**  Purpose: 
**   vms client api header file
**  
**  History:
**  Programmer	  Date	  Rev Number Description
** 
**
***************************************************************************H*/
#ifndef _VMS_CLIENT_INTERFACE_H_
#define _VMS_CLIENT_INTERFACE_H_
#include "auth_msg.h"

#ifdef WIN32
#pragma warning(disable : 4786)
#define VMS_TRACE printf
#define VMS_ERROR printf
#define VMS_FATAL printf
#define VMS_INFO printf
#define VMS_WARN printf
#endif
#define AUTH_TCP_REC_BUF_LEN 1024*20
#define VMSC_MAX_SND_BUF_LEN 32*1024
#define VMSC_TRANS_ENCODE_BUFLEN (VMSC_MAX_SND_BUF_LEN - sizeof(MSMsgHead_t))
#define VMSC_TRANS_DECODE_BUFLEN	VMSC_MAX_SND_BUF_LEN




class VmsClientIf
{

public:
#ifdef WIN32
	static DWORD WINAPI heartBeatTask( LPVOID lpParam );
#else
	static WS_STATUS heartBeatTask( void* lpParam ) ;
#endif

	U32 StartHBThread();
	void StopHeartBeat();

	void HeartBeatProc();
	VmsClientIf(VMS_CLIENT_TYPE type);
	virtual ~VmsClientIf();	

	U32 OpenConnection(const U32 nAaaIPAddr,const U16 nAaaLoginPort);
	U32 OpenConnection(string strWWWName, U32 nSvrPort);
	U32 CloseConnection();
	
	S32 getVmsSock() { return m_vmsSock;}
	
	U32 sendReqMsg(S8 *pMsg, U32 nLength);//zlib压缩后发送
	U32 recvVmsMsg(S8** data, U32* dataLen);	//接收一个VMS消息包，zlib解压.data指针需要调用后由调用者释放。

	U32 logIn(const string& strUsrName, const string& strPsWd, 
						   S8** outData, U32* dataLen);

	//login成功后客户端才能开启接收VMS TCP消息线程，否则login中接收消息会与客户端接收线程冲突
	U32 logIn(const string& strUsrName, const string& strPsWd, 
			   S8** outData, U32* dataLen, DbsConfigFtpInfo &configFtpInfo);
	
	//logout调用前客户端需要停止接收VMS TCP消息线程，否则logout中接收消息会与客户端接收线程冲突
	U32 logOut();
	

	U32 sendChPWMsg(const string& strOperatorId, 
					 const string& strUsrId, 
					 const string& strOpPW, 
					 const string& strNewPW,
					 auth_req* req);


//以下2个API，在客户端开启接收VMS TCP消息县城后不能调用，否则API接收消息会与客户端接收线程冲突
	U32 authenId(const string& strPsWd);
	U32 changePsWd(const string& strOwnPW, const string& strUsrId, const string& strUsrNewPW);

private:

	U32 sendLogInMsg(auth_req* req, const string& strUsrName, const string& strPsWd, U16 nClientType, U16 nSubType);
	U32 recvLogInMsg(const string& strPsWd, const auth_req* req, S8** data, U32* dataLen, DbsConfigFtpInfo &configFtpInfo);
	S32 sendMsg(S8* data, U32 dataLen);	//封装底层send
	U32 recvMsg(S8** data, U32* dataLen);	//接收一个VMS消息包
	
	U32 getResult(const auth_req* req);
	U32 rspMsgCheck(auth_rsp* rsp, const auth_req* req);

public:

	U16 m_clientType;
	U32 m_aaaIPAddr;
	U16 m_aaaLoginPort;
	U16 m_aaaHbPort;
	string m_strUserId;
	string m_strUsrName;

private:
	S8 m_psWd[AUTH_HASH_PSWD_LEN];
	S32 m_vmsSock;
	U8  m_Logined;
#ifdef WIN32
	HANDLE m_hHbClientThreadID;
#else
	WS_TASK_ID m_hHbClientThreadID;
#endif

};

#endif

