// SvrParamsPage.cpp : implementation file
//

#include "stdafx.h"
#include "wmsoapsvccp.h"
#include "SvrParamsPage.h"
#include "..\soap_source\SOAPAutomationProxy.h"
#include "..\soap_source\bytearrayconv.h"
#include "..\soap_source\WMSOAPWSDLClient.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSvrParamsPage property page

IMPLEMENT_DYNCREATE(CSvrParamsPage, CPropertyPage)

CSvrParamsPage::CSvrParamsPage() : CPropertyPage(CSvrParamsPage::IDD)
{
	//{{AFX_DATA_INIT(CSvrParamsPage)
	m_uPort = 0;
	m_uClientTimeout = 0;
	m_uMaxClients = 0;
	m_uWatchdogTime = 0;
	m_uNumWorkThreads = 0;
	//}}AFX_DATA_INIT
}

CSvrParamsPage::~CSvrParamsPage()
{
}

void CSvrParamsPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSvrParamsPage)
	DDX_Text(pDX, IDC_SERVER_PORT, m_uPort);
	DDX_Text(pDX, IDC_SERVER_TIMEOUT, m_uClientTimeout);
	DDX_Text(pDX, IDC_SERVER_MAX_CONNECTS, m_uMaxClients);
	DDX_Text(pDX, IDC_SERVER_SANITY_TIME, m_uWatchdogTime);
	DDX_Text(pDX, IDC_SERVER_NUM_WORKTHREADS, m_uNumWorkThreads);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSvrParamsPage, CPropertyPage)
	//{{AFX_MSG_MAP(CSvrParamsPage)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSvrParamsPage message handlers

BOOL CSvrParamsPage::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	CWSSCPApp *pApp = (CWSSCPApp *)AfxGetApp();
	if (QueryServerSettings() == FALSE && pApp->m_bStaleNonce == true)
	{
		// server nonce stale, reauthenticate
		if (QueryServerSettings() == FALSE)
		{
			AfxMessageBox(_T("Failed to obtain settings from Server!"));
		}
	}
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

BOOL CSvrParamsPage::QueryServerSettings()
{
	CWaitCursor wait;
	CWSSCPApp *pApp = (CWSSCPApp *)AfxGetApp();
	// renew the message object
	if (pApp->m_SOAPClient.NewMessage() == false)
	{
		return FALSE;
	}
	// method
	wstring wstrMethodName = L"GetServerParams";
	// set up call
	_variant_t Args[5];
	wstring Names[5];
	_variant_t varReturn;

	Args[0].vt = VT_I4;
	Args[0].lVal = 0;
	Names[0] = L"ListenPort";

	Args[1].vt = VT_I4;
	Args[1].lVal = 0;
	Names[1] = L"NumWorkThreads";

	Args[2].vt = VT_I4;
	Args[2].lVal = 0;
	Names[2] = L"NumClientConn";

	Args[3].vt = VT_I4;
	Args[3].lVal = 0;
	Names[3] = L"ClientTimeout";

	Args[4].vt = VT_I4;
	Args[4].lVal = 0;
	Names[4] = L"WatchdogTimer";

	int nNumArgs = 5;

	// generate SOAP request message headers
	pApp->BuildRequestHeaders();

	// message buffer strings
	string strRequestXML;
	string strResponseXML;

	// attempt method invocation
	bool bFault = false;
	if (pApp->m_SOAPClient.InvokeSOAPMethod(wstrMethodName,
											Names,
											Args,
											nNumArgs,
											varReturn) == false)
	{
		bFault = true;
	}
	if (pApp->m_bViewBuffers == true)
	{
		MessageBox(pApp->m_SOAPClient.GetXMLOut(), "Request Message", MB_OK);
		MessageBox(pApp->m_SOAPClient.GetXMLIn(), "Response Message", MB_OK);
	}
	// load response message headers
	pApp->GetResponseHeaders();

	if (bFault == true ||
		varReturn.boolVal == VARIANT_FALSE ||
		pApp->m_SOAPClient.IsSOAPFault() == true ||
		pApp->AuthServer() == false)
	{
		return FALSE;
	}
	m_uPort = Args[0].lVal;
	m_uNumWorkThreads = Args[1].lVal;
	m_uMaxClients = Args[2].lVal;
	m_uClientTimeout = Args[3].lVal;
	m_uWatchdogTime = Args[4].lVal;
	UpdateData(FALSE);	
	return TRUE;
}
