📄 servermanager.cpp
字号:
#include <aknviewappui.h>
#include <aknnotewrappers.h>
#include <avkon.hrh>
#include "Frmes.rsg"
#include "ServerManager.h"
//#include "OperatorFile.h"
#include "FrMesAppUi.h"
#include "RegisterView.h"
#include "LogonView.h"
_LIT(KStatusFormat, "%d: %S"); // Format string for status returned from web server, e.g. "200: OK" or "404: Not Found"
const TInt KStatusCodeLength = 100; // Enough room in a descriptor for code + ": "
//Xml Data
_LIT(KRSS,"<RSS>");
_LIT(KRSSL,"</RSS>");
_LIT(KTYPE,"<type>");
_LIT(KTYPEL,"</type>");
_LIT(KMOBILE,"<mobile>");
_LIT(KMOBILEL,"</mobile>");
_LIT(KRECORD,"<record>");
_LIT(KRECORDL,"</record>");
_LIT(KFNAME,"<fname>");
_LIT(KFNAMEL,"</fname>");
_LIT(KHNAME,"<hname>");
_LIT(KHNAMEL,"</hname>");
_LIT(KMOB,"<mob>");
_LIT(KMOBL,"</mob>");
_LIT(KUPDATE,"update");
_LIT(KRESULT,"<result>");
_LIT(KRESULTL,"</result>");
CServerManager* CServerManager::NewL()
{
CServerManager* self = NewLC();
CleanupStack::Pop(self);
return self;
}
CServerManager* CServerManager::NewLC()
{
CServerManager* self = new(ELeave) CServerManager;
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
CServerManager::CServerManager()
{
m_psHttpBody = NULL;
m_pHTTPEngine = NULL;
// m_pBookEngine = NULL;
// m_pTimeOut = NULL;
}
void CServerManager::ConstructL()
{
// m_pBookEngine = CBookEngine::NewL();
m_pHTTPEngine = CHTTPEngine::NewL(*this);
// m_pTimeOut = CTimeOutTimer::NewL(0, *this);
}
CServerManager::~CServerManager()
{
if(m_psHttpBody)
{
delete m_psHttpBody;
m_psHttpBody = NULL;
}
if(m_pHTTPEngine)
{
delete m_pHTTPEngine;
m_pHTTPEngine = NULL;
}
// MEMORY_FREE(m_psHttpBody);
// MEMORY_FREE(m_pHTTPEngine);
// MEMORY_FREE(m_pBookEngine);
// MEMORY_FREE(m_pTimeOut);
// m_pBookInfoArr.ResetAndDestroy();
}
void CServerManager::MakeRequest(TInt aSendType)
{
TBuf<128> sUrl;
switch(aSendType)
{
case ERegister:
{
// if (m_pTimeOut->IsActive()) m_pTimeOut->Cancel();
// m_pTimeOut->After(15*1000*1000);
/*http://www.ldci.mobi/snsProject/register?mobile=13800138001&pass=123456*/
sUrl.Copy(_L("http://www.ldci.mobi/snsProject/register?mobile="));
sUrl.Append(m_sTempPhone);
sUrl.Append(_L("&pass="));
sUrl.Append(m_sTempPassWord);
//send to http
m_pHTTPEngine->GetRequestL(sUrl);
}
break;
case ELogin:
{
//http://www.ldci.mobi/snsProject/login?mobile=13800138001&pass=123456
// if (m_pTimeOut->IsActive()) m_pTimeOut->Cancel();
// m_pTimeOut->After(15*1000*1000);
sUrl.Copy(_L("http://www.ldci.mobi/snsProject/login?mobile="));
sUrl.Append(m_sTempPhone);
sUrl.Append(_L("&pass="));
sUrl.Append(m_sTempPassWord);
//send to http
m_pHTTPEngine->GetRequestL(sUrl);
}
break;
case EUpdate:
{
//http://www.ldci.mobi/snsProject/update
// if (m_pTimeOut->IsActive()) m_pTimeOut->Cancel();
// m_pTimeOut->After(15*1000*1000);
//send to http
sUrl.Copy(_L("http://www.ldci.mobi/snsProject/update"));
this->PackUpdateContent();
}
break;
case EDownload:
{
//http://www.ldci.mobi/snsProject/download?mobile=13800138001&pass=123456
// if (m_pTimeOut->IsActive()) m_pTimeOut->Cancel();
// m_pTimeOut->After(15*1000*1000);
sUrl.Copy(_L("http://www.ldci.mobi/snsProject/download?mobile="));
sUrl.Append(m_sTempPhone);
sUrl.Append(_L("&pass="));
sUrl.Append(m_sTempPassWord);
//send to http
m_pHTTPEngine->GetRequestL(sUrl);
}
break;
default:
break;
}
}
void CServerManager::AppendBodyL(const TDesC& aData)
{
if (!m_psHttpBody) //第一次添加时
{
m_psHttpBody = HBufC::NewL(aData.Length());
m_psHttpBody->Des().Copy(aData);
}
else
{
m_psHttpBody = m_psHttpBody->ReAlloc(m_psHttpBody->Length()+aData.Length());
m_psHttpBody->Des().Append(aData);
}
}
void CServerManager::PackUpdateContent()
{/*
MEMORY_FREE(m_psHttpBody);
AppendBodyL(KRSS);
AppendBodyL(KTYPE);
AppendBodyL(KUPDATE);
AppendBodyL(KTYPEL);
AppendBodyL(KMOBILE);
AppendBodyL(m_sTempPhone);
AppendBodyL(KMOBILEL);
for (int i=0;i<m_sIndexArray.Count(); i++)
{
TInt nIndex = m_sIndexArray[i];
TBuf8<32> sTempBuf;
AppendBodyL(KRECORD);
AppendBodyL(KFNAME);
if (m_pFile->m_MessageArray[nIndex]->GetFName())
{
CCnvCharacterSetConverter* converter1 = NULL;
converter1 = CCnvCharacterSetConverter::NewLC();
converter1->PrepareToConvertToOrFromL(KCharacterSetIdentifierUtf8, CEikonEnv::Static()->FsSession());
converter1->ConvertFromUnicode(sTempBuf, m_pFile->m_MessageArray[nIndex]->GetFName()->Des());
CleanupStack::PopAndDestroy(converter1);
TBuf<32> sfname;
sfname.Copy(sTempBuf);
AppendBodyL(sfname);
}
AppendBodyL(KFNAMEL);
AppendBodyL(KHNAME);
if (m_pFile->m_MessageArray[nIndex]->GetHName())
{
CCnvCharacterSetConverter* converter1 = NULL;
converter1 = CCnvCharacterSetConverter::NewLC();
converter1->PrepareToConvertToOrFromL(KCharacterSetIdentifierUtf8, CEikonEnv::Static()->FsSession());
converter1->ConvertFromUnicode(sTempBuf, m_pFile->m_MessageArray[nIndex]->GetHName()->Des());
CleanupStack::PopAndDestroy(converter1);
TBuf<32> shname;
shname.Copy(sTempBuf);
AppendBodyL(shname); //hname
}
AppendBodyL(KHNAMEL);
AppendBodyL(KMOB);
if (m_pFile->m_MessageArray[nIndex]->GetTel())
{
AppendBodyL(m_pFile->m_MessageArray[nIndex]->GetTel()->Des());//mob
}
AppendBodyL(KMOBL);
AppendBodyL(KRECORDL);
}
AppendBodyL(KRSSL);
AppendBodyL(_L("--\n"));
m_pHTTPEngine->PostRequestL(m_psHttpBody->Des());
m_sIndexArray.Reset();
*/
}
void CServerManager::ParseRecponseContent(const TDesC& aResponse)
{
TBuf<8> m_result;
TBuf<8> m_pStatus;
TBuf<6> m_buftype;
m_buftype.Copy(KTYPE);
TBuf<5> m_bufregi;
m_bufregi.Copy(_L("regi"));
TBuf<5> m_buflogin;
m_buflogin.Copy(_L("logi"));
// TBuf<6> m_bufupdate;
// m_bufupdate.Copy(_L("upda"));
// TBuf<4> m_bufdown;
// m_bufdown.Copy(_L("down"));
TBuf<8> m_bufresult;
m_bufresult.Copy(KRESULT);
// TBuf<7> m_buffname;
// m_buffname.Copy(KFNAME);
// TBuf<7> m_bufhname;
// m_bufhname.Copy(KHNAME);
// TBuf<5> m_bufmob;
// m_bufmob.Copy(KMOB);
m_result.Copy(aResponse.Mid(13,4));
CFrMesAppUi* pApp = (CFrMesAppUi*)CEikonEnv::Static()->AppUi();
if (m_result.Compare(m_bufregi)==0) // Compare with 'regi'
{
m_pStatus.Copy(aResponse.Mid((aResponse.Find(m_bufresult)+m_bufresult.Length()),1)); //0,1
pApp->iRegisterView->SetRes(m_nStatusCode);
pApp->iRegisterView->SetContent(m_result,m_pStatus);
}
if (m_result.Compare(m_buflogin)==0) // Compare with 'login'
{
m_pStatus.Copy(aResponse.Mid((aResponse.Find(m_bufresult)+m_bufresult.Length()),1)); //0,1,2
pApp->iLogonView->SetRes(m_nStatusCode);
pApp->iLogonView->SetContent(m_result,m_pStatus);
//send result to login
// TBuf<16> aresult;
// aresult.Copy(m_pStatus->Des());
// TLex val(aresult);
// TInt nVal;
// val.Val(nVal);
// m_pServerToLogin->ServerReNotify(/*nVal*/m_pStatus, ELogin);
}
/*
if (m_result->Compare(m_bufdown)==0)
{
m_pBookInfoArr.ResetAndDestroy();
CBookInfo* pBookInfo = NULL;
TInt bufIndex = aResponse.Find(m_bufhname);
m_result->Des().Copy(aResponse.Mid(bufIndex+m_bufhname.Length()));
while (bufIndex!=KErrNotFound) //have fname
{
pBookInfo = CBookInfo::NewL();
pBookInfo->SetItemID(-1); //overload, -1 for adding new
TBuf<16> stemp;
TInt nfnamel = m_result->Des().Find(KHNAMEL);
stemp.Copy(m_result->Des().Mid(0,nfnamel));
pBookInfo->SetLastName(stemp);
nfnamel = m_result->Des().Find(m_buffname)+m_buffname.Length();
stemp.Copy(m_result->Des().Mid(nfnamel,(m_result->Des().Find(KFNAMEL)-nfnamel)));
pBookInfo->SetFirstName(stemp);
nfnamel = m_result->Des().Find(m_bufmob)+m_bufmob.Length();
stemp.Copy(m_result->Des().Mid(nfnamel,(m_result->Des().Find(KMOBL)-nfnamel)));
pBookInfo->SetTelephone(stemp);
m_pBookInfoArr.Append(pBookInfo);
bufIndex = m_result->Find(m_bufhname);
m_result->Des().Copy(m_result->Mid(bufIndex+m_bufhname.Length()));
}
TInt len = m_pBookInfoArr.Count();
m_pBookEngine->UpdatePhoneBook(1,m_pBookInfoArr);
}
*/
// delete m_result;
// delete m_pStatusl;
// MEMORY_FREE(m_result);
// MEMORY_FREE(m_pStatus);
}
//继承自M类的虚方法
void CServerManager::ResponseStatusL(TInt aStatusCode, const TDesC& aStatusText)
{
m_nStatusCode = aStatusCode;
HBufC* buf = HBufC::NewLC(aStatusText.Length());
buf->Des().Copy(aStatusText);
CAknInformationNote* note = new (ELeave) CAknInformationNote;
note->ExecuteLD(buf->Des());
CleanupStack::PopAndDestroy(buf);
}
void CServerManager::ResponseReceivedL(const TDesC& aResponseBuffer)
{
this->ParseRecponseContent(aResponseBuffer); //parse and send results to login
}
/*
void CServerManager::TimerExpired()
{
// if (m_pTimeOut->IsActive()) m_pTimeOut->Cancel();
//
// TBuf<16> sbuf;
// CEikonEnv::Static()->ReadResource(sbuf, R_WYZL_SM_SERVER_OVERTIME); //服务器连接超时
// CAknInformationNote* informationNote = new (ELeave) CAknInformationNote;
// informationNote->ExecuteLD(sbuf);
}
*/
void CServerManager::LoginInfoNotify(const TDesC & aNumber, const TDesC & aPassword,TInt aModel)
{
m_sTempPhone.Copy(aNumber);
m_sTempPassWord.Copy(aPassword);
this->MakeRequest(aModel); //1&2
}
/*
void CServerManager::SetFilePointer(COperatorFile* aPointer)
{
m_pFile = aPointer;
}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -