// SendReceUdpDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SendReceUdp.h"
#include "SendReceUdpDlg.h"

#include "..\..\include\AdamTcpCE.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

wchar_t	szwIPAddress[20];

//-------- default timeout ------
int     iSendTimeout=5000;
int     iReceiveTimeout=5000;

/////////////////////////////////////////////////////////////////////////////
// CSendReceUdpDlg dialog

CSendReceUdpDlg::CSendReceUdpDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSendReceUdpDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSendReceUdpDlg)
	m_strIPAddress = _T("");
	m_strSend = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSendReceUdpDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSendReceUdpDlg)
	DDX_Text(pDX, IDC_EDIT_IPADDRESS, m_strIPAddress);
	DDX_Text(pDX, IDC_EDIT_SEND, m_strSend);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSendReceUdpDlg, CDialog)
	//{{AFX_MSG_MAP(CSendReceUdpDlg)
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	ON_BN_CLICKED(IDC_SENDCOMMAND, OnSendcommand)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSendReceUdpDlg message handlers

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

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here
	m_strIPAddress="172.18.1.41";
	SetDlgItemText(IDC_EDIT_IPADDRESS,m_strIPAddress);
	m_strSend="$01F";
	SetDlgItemText(IDC_EDIT_SEND,m_strSend);

	// get the DLL version 
	char     szCh[20];
	//wchar_t  szwCh[20];
    int    iVersion;    
 
	iVersion=ADAMTCP_GetDLLVersion();
	_ltoa( iVersion, szCh, 16 );

	size_t len = strlen (szCh);
	wchar_t  wcConverter[20];

	memset (wcConverter, 0, sizeof(wcConverter));

	mbstowcs(wcConverter, szCh, len);
	//wsprintf(szwCh,"%s",szCh);
	SetDlgItemText(IDC_EDIT_DLLVER, wcConverter);

    //--- To initialize DLL to work ---
    int   iRetVal;

    iRetVal=ADAMTCP_Open();
    if( iRetVal!=0 )
    {
        MessageBox(_T("ADAMTCP_Open() Failure !!!"));
    }	
	
	//--- reset the content in CListBox ---
	((CListBox*)GetDlgItem(IDC_LIST1))->ResetContent();
	return TRUE;  // return TRUE  unless you set the focus to a control

}



void CSendReceUdpDlg::OnExit() 
{
	// TODO: Add your control notification handler code here
    ADAMTCP_Close();	
	
	// TODO: Add your control notification handler code here
	CDialog::OnOK();	
}

void CSendReceUdpDlg::OnSendcommand() 
{
	// TODO: Add your control notification handler code here
	int       iRetVal;
	wchar_t   szwSend[80],szwReceive[80];
    CString   strText; 

	
	//--- Secondly, open a UDP socket and setting the timeout interval -------
    iRetVal=ADAMTCP_UDPOpen(iSendTimeout,iReceiveTimeout);
	if( iRetVal!=0 )
	{
        MessageBox(_T("ADAMTCP_UDPOpen() Failure !!!"));
	}

    // get the 5000/TCP's IP Address
	GetDlgItemText(IDC_EDIT_IPADDRESS,szwIPAddress,20);

    // get the command to send
	GetDlgItemText(IDC_EDIT_SEND,szwSend,80);

	// sending command to 5000/TCP and receiving response from 5000/TCP
    iRetVal=ADAMTCP_SendReceiveUDPCmd(szwIPAddress,szwSend,szwReceive);
    if( iRetVal<0 )
    {
		//sprintf(szDump,"ADAMTCP_SendReceiveUDPCmd() Failure !!!  Error Code:%d",iRetVal);
        MessageBox( _T("SendReceiveUDPCmd() Error !") );
		return;
    }
	//strText.Format( "-> Send:%s", szwSend);
	((CListBox*)GetDlgItem(IDC_LIST1))->InsertString( -1, szwSend );
	//strText.Format( "   Receive:%s", szwReceive);
	((CListBox*)GetDlgItem(IDC_LIST1))->InsertString( -1, szwReceive );

	//--- releasing UDP resource ---
    ADAMTCP_UDPClose();	
}
