// GPRSDemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "GPRSDemo.h"
#include "GPRSDemoDlg.h"
#include <afxsock.h>
#include<connmgr.h>

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

/////////////////////////////////////////////////////////////////////////////
// CGPRSDemoDlg dialog

CGPRSDemoDlg::CGPRSDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGPRSDemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGPRSDemoDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGPRSDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGPRSDemoDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGPRSDemoDlg, CDialog)
	//{{AFX_MSG_MAP(CGPRSDemoDlg)
	ON_BN_CLICKED(IDC_BUTTON_GetConnMgrAvailable, OnBUTTONGetConnMgrAvailable)
	ON_BN_CLICKED(IDC_BUTTON_MapURLAndGUID, OnBUTTONMapURLAndGUID)
	ON_BN_CLICKED(IDC_BUTTON_EnumNetIdentifier, OnBUTTONEnumNetIdentifier)
	ON_BN_CLICKED(IDC_BUTTON_ConnNet, OnBUTTONConnNet)
	ON_BN_CLICKED(IDC_BUTTON_GetConnectionStatus, OnBUTTONGetConnectionStatus)
	ON_BN_CLICKED(IDC_BUTTON_ConnectToInternet, OnBUTTONConnectToInternet)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGPRSDemoDlg message handlers

BOOL CGPRSDemoDlg::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
	WSADATA wsaData;
	// 初始化socket
	if (WSAStartup(MAKEWORD(1, 1), &wsaData))
	{
		AfxMessageBox(_T("Initialize Winsock Failed!\nexit software"));
		PostQuitMessage(0);
	}
	
	AfxSocketInit ();

	return TRUE;  // return TRUE  unless you set the focus to a control
}



void CGPRSDemoDlg::OnBUTTONGetConnMgrAvailable() 
{
	if ( m_ConnectManager.GetConnMgrAvailable() )
	{
		AfxMessageBox ( _T("Connection manager is available"), MB_ICONINFORMATION );
	}
	else
	{
		AfxMessageBox ( _T("Connection manager is available") );
	}
}

void CGPRSDemoDlg::OnBUTTONMapURLAndGUID() 
{
	CWaitCursor* pwait = new CWaitCursor();//启动等待光标 
	CString csDesc;
	GUID guidNetworkObject = {0};
	int nIndex = m_ConnectManager.MapURLAndGUID(_T("http://www.baidu.com/"), guidNetworkObject, &csDesc );
	if ( nIndex >= 0 )
	{
		pwait->Restore();
		CString csMsg;
		csMsg.Format ( _T("Map URL successfully. Net description is : %s"), csDesc );
		AfxMessageBox ( csMsg, MB_ICONINFORMATION );
		CListBox *pListBox = (CListBox*)GetDlgItem(IDC_LIST_NetIdentifier);
		ASSERT ( pListBox );
		
		if ( nIndex < pListBox->GetCount() )
		{
			pListBox->SetCurSel ( nIndex );
			OnBUTTONConnNet ();
		}
	}
	pwait->Restore();
	delete pwait;
	pwait=NULL;
	
}

void CGPRSDemoDlg::OnBUTTONEnumNetIdentifier() 
{
	ResetListBoxContent ();
	CListBox *pListBox = (CListBox*)GetDlgItem(IDC_LIST_NetIdentifier);
	ASSERT ( pListBox );
	
	CStringArray StrAry;
	m_ConnectManager.EnumNetIdentifier ( StrAry );
	for ( int i=0; i<StrAry.GetSize(); i++ )
	{
		pListBox->AddString ( StrAry.GetAt(i) );
	}
	
}

void CGPRSDemoDlg::ResetListBoxContent ()
{
	CListBox *pListBox = (CListBox*)GetDlgItem(IDC_LIST_NetIdentifier);
	ASSERT ( pListBox );
	pListBox->ResetContent ();
}


void CGPRSDemoDlg::OnBUTTONConnNet() 
{
	CWaitCursor waitCursor;
	CListBox *pListBox = (CListBox*)GetDlgItem(IDC_LIST_NetIdentifier);
	ASSERT ( pListBox );
	DWORD dwIndex = pListBox->GetCurSel();
	if ( dwIndex >= 0 && dwIndex < (DWORD)pListBox->GetCount() )
	{
		if ( m_ConnectManager.EstablishConnection ( dwIndex ) != NULL )
		{
			waitCursor.Restore();
			AfxMessageBox ( _T("Connection net successfully"), MB_ICONINFORMATION );
		}
		else
		{
			waitCursor.Restore();
			DWORD dwLastError = GetLastError ();
			AfxMessageBox ( _T("Connection net failed") );
		}
	}
	waitCursor.Restore();
}

void CGPRSDemoDlg::OnBUTTONGetConnectionStatus() 
{
	CWaitCursor waitCursor;
	DWORD dwStatus = 0;
	BOOL bRet = m_ConnectManager.WaitForConnected ( 10, &dwStatus );
	waitCursor.Restore();
	if ( bRet )
	{
		AfxMessageBox ( _T("Connection is OK"), MB_ICONINFORMATION );
	}
	else
	{
		CString csMsg;
		csMsg.Format ( _T("Connection failed. Status is : 0x%.2x"), dwStatus );
		AfxMessageBox ( csMsg );
	}
}

void CGPRSDemoDlg::OnBUTTONConnectToInternet() 
{
	
// 	CString csDesc;
// 	GUID guidNetworkObject = {0};
// 	//int nIndex = m_ConnectManager.MapURLAndGUID(_T("http://www.baidu.com/"), guidNetworkObject, &csDesc );
// 	//AfxMessageBox(csDesc);
// 
// 	memset ( &guidNetworkObject, 0, sizeof(GUID) );
// 	int nIndex = 0;
// 	HRESULT hResult = ConnMgrMapURL ( _T("http://www.baidu.com/"), &guidNetworkObject, (DWORD*)&nIndex );
// 
// 	if ( FAILED(hResult) )
// 	{
// 		nIndex = -1;
// 		DWORD dwLastError = GetLastError ();
// 		AfxMessageBox ( _T("Could not map a request to a network identifier") );
// 	}
// 	else
// 	{
// 		CONNMGR_DESTINATION_INFO DestInfo = {0};
// 		if ( SUCCEEDED(ConnMgrEnumDestinations(nIndex, &DestInfo)) )
// 		{
// 			csDesc = DestInfo.szDescription;			
// 		}
// 	}
// 	AfxMessageBox(csDesc);

	CWaitCursor waitCursor;
	GUID guidNetworkObject = {0};
	memset ( &guidNetworkObject, 0, sizeof(GUID) );
	int nIndex = 0;
	HRESULT hResult = ConnMgrMapURL ( _T("http://www.leadmap.net/"), &guidNetworkObject, (DWORD*)&nIndex );
	if ( FAILED(hResult) )
	{
		nIndex = -1;
		DWORD dwLastError = GetLastError ();
		AfxMessageBox ( _T("Could not map a request to a network identifier") );
	}
	else
	{
		AfxMessageBox(_T("http is conn"));
	}
	//GUID guidNet = {0x436ef144, 0xb4fb, 0x4863, 0xa0, 0x41, 0x8f, 0x90, 0x5a, 0x62, 0xc5, 0x72}; 
	BOOL bConn=m_ConnectManager.EstablishConnection(guidNetworkObject);
	waitCursor.Restore();
	if (bConn)
	{
		AfxMessageBox ( _T("GPRS Connection is OK"), MB_ICONINFORMATION );
	}
	else
	{
		AfxMessageBox ( _T("GPRS Connection net failed") );
	}

	CSocket sock;
	sock.Create ();
	if ( sock.Connect ( _T("http://www.leadmap.net/"), 80 ) )
	{
		waitCursor.Restore();
		AfxMessageBox ( _T("Connect to www.leadmap.net successfully"), MB_ICONINFORMATION );
	}
	else
	{
		waitCursor.Restore();
		AfxMessageBox ( _T("Connect to www.leadmap.net failed") );
	}
}
