// DistInfoDlg.cpp : 구현 파일입니다.
//

#include "stdafx.h"
#include "ConsoleApp.h"
#include "DistInfoDlg.h"
#include ".\distinfodlg.h"
#include "../ConsoleTransfer(DN)/NoticeStruct.h"
#include "../CommLib/CommLib.h"

// CDistInfoDlg 대화 상자입니다.

IMPLEMENT_DYNAMIC(CDistInfoDlg, CDialog)
CDistInfoDlg::CDistInfoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDistInfoDlg::IDD, pParent)
	, m_dist_path(_T(""))
	, m_file_cnt(_T(""))
	, m_dist_complete(_T(""))
	, m_dist_now(_T(""))
	, m_dist_fail(_T(""))
{
}

CDistInfoDlg::~CDistInfoDlg()
{
}

void CDistInfoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_DIST_PATH, m_dist_path);
	DDX_Text(pDX, IDC_FILE_CNT, m_file_cnt);
	DDX_Text(pDX, IDC_DIST_COMPLETE, m_dist_complete);
	DDX_Text(pDX, IDC_DIST_NOW, m_dist_now);
	DDX_Text(pDX, IDC_DIST_FAIL, m_dist_fail);
}


BEGIN_MESSAGE_MAP(CDistInfoDlg, CDialog)
END_MESSAGE_MAP()


// CDistInfoDlg 메시지 처리기입니다.

BOOL CDistInfoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// TODO:  여기에 추가 초기화 작업을 추가합니다.
	
	GetDistInfo();
	UpdateData(FALSE);	

	return TRUE;  // return TRUE unless you set the focus to a control
	// 예외: OCX 속성 페이지는 FALSE를 반환해야 합니다.
}


void CDistInfoDlg::GetDistInfo(void)
{	

	tsvc_req_header_t tsvc_req_header;
	tsvc_auth_info_t auth_req_body;
	tsvc_ddi_req_body_t ddi_req_body;
	tsvc_rsp_header_t *tsvc_rsp_header_ptr;
	tsvc_ddi_rsp_body_t *ddi_rsp_body_ptr;
	char recv_buffer[8196];
	int result_code;
	CString ip;

	// get auth string /////////////////////////
	CString auth_str;
	auth_str = this->m_cp + "@" + this->m_user;
	///////////////////////////////////////////

	SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (sock == INVALID_SOCKET) {
		goto EXIT_FUNCTION;
	}

	bool bRet = false;
	DWORD dwWritten;
	char s[1024];

	sockaddr_in sa; 
	sa.sin_family      = AF_INET;

	ip = m_sutil.get_first_rcts(this->m_RCTSAddress);
	if (ip.GetLength()>0)
		sa.sin_addr.s_addr = m_sutil.conv_addr(ip.GetString());
	else
		goto EXIT_FUNCTION;

	sa.sin_port        = htons(RCTS_NOTICE_PORT);

	connect(sock, (SOCKADDR*)&sa, sizeof(sa));


	tsvc_req_header.op_num_w = ::htons(OP_NUM_AUTH);
	tsvc_req_header.op_seq_w = ::htons(OP_SEQ_REQ);

	::sprintf(auth_req_body.ua_str, auth_str);
	::sprintf(auth_req_body.up_str, this->m_passwd);

	m_sutil._sh_send(sock, (char*)&tsvc_req_header, sizeof(tsvc_req_header));
	m_sutil._sh_send(sock, (char*)&auth_req_body, sizeof(auth_req_body));

	memset(recv_buffer , 0x00 , sizeof(recv_buffer));
	if (m_sutil._rtxTimer(5 , sock) < 0)
		goto EXIT_FUNCTION;
	m_sutil._sh_recv(sock, (char*)&recv_buffer, sizeof(recv_buffer));

	tsvc_rsp_header_ptr = reinterpret_cast<tsvc_rsp_header_t *>(&recv_buffer);
	result_code = ntohs(tsvc_rsp_header_ptr->op_result_code_w);

	// 인증
	if (RSP_CODE_AUTH_SUCCESS==result_code)
	{
//		SetNoticeStep(200);
	}
	else
	{
//		SetNoticeStep(201);
		goto EXIT_FUNCTION;
	}

	tsvc_req_header.op_num_w = ::htons(OP_NUM_DDI);
	tsvc_req_header.op_seq_w = ::htons(OP_SEQ_REQ);

	::sprintf(ddi_req_body.uri_str, "%s", this->m_dist_path);

	m_sutil._sh_send(sock,(char *)&tsvc_req_header, sizeof(tsvc_req_header));
	m_sutil._sh_send(sock,(char *)&ddi_req_body, sizeof(ddi_req_body));


	memset(recv_buffer , 0x00 , sizeof(recv_buffer));
	if (m_sutil._rtxTimer(5 , sock) < 0)
		goto EXIT_FUNCTION;

	m_sutil._sh_recv(sock, (char*)&recv_buffer, sizeof(tsvc_rsp_header_t));
	result_code = ntohs(tsvc_rsp_header_ptr->op_result_code_w);

	// 배포정보
	if (RSP_CODE_DIST_SUCCESS==result_code)
	{
		//		SetNoticeStep(200);
	}
	else
	{
		//		SetNoticeStep(201);
		goto EXIT_FUNCTION;
	}

	memset(&recv_buffer , 0x00 , sizeof(recv_buffer));
	if (m_sutil._rtxTimer(5 , sock) < 0)
		goto EXIT_FUNCTION;

	m_sutil._sh_recv(sock, (char*)&recv_buffer, sizeof(tsvc_ddi_rsp_body_t));
	ddi_rsp_body_ptr = reinterpret_cast<tsvc_ddi_rsp_body_t *>(&recv_buffer);

	char t[128];
	sprintf(t , "%d" , ntohs(ddi_rsp_body_ptr->file_cnt));
	m_file_cnt = t;

	sprintf(t , "%d" , ntohs(ddi_rsp_body_ptr->dist_fail));
	m_dist_fail = t;

	sprintf(t , "%d" , ntohs(ddi_rsp_body_ptr->dist_now));
	m_dist_now = t;

	sprintf(t , "%d" , ntohs(ddi_rsp_body_ptr->dist_complete));
	m_dist_complete = t;

	//배포
	if (RSP_CODE_DIST_SUCCESS==result_code)
	{
//		SetNoticeStep(400);
	}
	else
	{

//		SetNoticeStep(401);
		goto EXIT_FUNCTION;
	}

EXIT_FUNCTION:	

	if (sock != INVALID_SOCKET)
		closesocket(sock);

//	PostMessage(WM_CLOSE);
}

