📄 servermanager.cpp
字号:
/* Copyright (c) 2007, Nokia. All rights reserved */
//class CHTTPEngine;
#include <f32file.h>
#include <e32std.h>
#include <charconv.h>
#include <eikmsg.h>
#include <mtmuibas.h>//up the files
#include <aknnotewrappers.h>
#include <ASSISTANT.rsg>
#include "Common.h"
#include "BookEngine.h"
#include "ServerManager.h"
#include "ProgressNoteTASAO.h"
#include "timeouttimer.h"
#include "charconv.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;
}
void CServerManager::ConstructL()
{
m_pBookEngine = CBookEngine::NewL();
m_pHTTPEngine = CHTTPEngine::NewL(*this);
m_pProgressNote = CProgressNoteTASAO::NewL();
m_pTimeOut = CTimeOutTimer::NewL(0, *this);
m_pBookEngine->UpdatePhoneBook(0, m_BookInfoArr);
// GetContactInfo();
}
CServerManager::CServerManager()
{
m_pHttpBody = NULL;
m_pBookEngine = NULL;
m_pHTTPEngine = NULL;
m_pProgressNote = NULL;
m_pTimeOut = NULL;
}
CServerManager::~CServerManager()
{
m_BookInfoArr.ResetAndDestroy();
MEMORY_FREE(m_pBookEngine);
MEMORY_FREE(m_pHTTPEngine);
MEMORY_FREE(m_pProgressNote);
MEMORY_FREE(m_pHttpBody);
MEMORY_FREE(m_pTimeOut); //add by tommy
}
//make url and send to HTTPEngine
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/orcProject/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 HTTPEngine
// m_pProgressNote->SaveGameL();//it should be 正在注册请稍候
m_pHTTPEngine->GetRequestL(sUrl);
}
break;
case ELogin:
{
//http://www.ldci.mobi/orcProject/login?mobile=13800138001&pass=123456
if(m_pTimeOut->IsActive()) m_pTimeOut->Cancel();
m_pTimeOut->After(1*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 HTTPEngine
m_pHTTPEngine->GetRequestL(sUrl);
}
break;
case EUpdate:
{
if(m_pTimeOut->IsActive()) m_pTimeOut->Cancel();
m_pTimeOut->After(15*1000*1000);
//http://www.ldci.mobi/snsProject/update
sUrl.Copy(_L("http://www.ldci.mobi/snsProject/update")); //send to HTTPEngine
PackUpdateContent();
}
break;
case EDownload:
{
if(m_pTimeOut->IsActive()) m_pTimeOut->Cancel();
m_pTimeOut->After(15*1000*1000);
//http://www.ldci.mobi/snsProject/download?mobile=13800138001&pass=123456
sUrl.Copy(_L("http://www.ldci.mobi/snsProject/download?mobile="));
sUrl.Append(m_sTempPhone);
sUrl.Append(_L("&pass="));
sUrl.Append(m_sTempPassWord); //send to HTTPEngine
m_pHTTPEngine->GetRequestL(sUrl);
}
break;
default:
break;
}
}
void CServerManager::ParseRecponseContent(const TDesC& aResponse)
{
// CAknInformationNote* informationNote = new (ELeave) CAknInformationNote;
// informationNote->ExecuteLD(_L("here success!"));
// m_pServerToLogin->ServerReNotify(0);
// return;
HBufC16* m_result = HBufC16::NewL(aResponse.Length()+10);
HBufC16* m_temp = HBufC16::NewL(aResponse.Length()+10);
HBufC16* m_alldata = HBufC16::NewL(aResponse.Length()+10);
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("login"));
TBuf<6> m_bufupdate;
m_bufupdate.Copy(_L("update"));
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_alldata->Des().Copy(aResponse.Mid(aResponse.Find(m_buftype)+m_buftype.Length()));
m_result->Des().Copy(m_alldata->Mid(0,m_alldata->Find(KTYPEL)));
if (m_result->Compare(m_bufregi)==0) // Compare with 'regi'
{
m_temp->Des().Copy(m_alldata->Mid(m_alldata->Find(m_bufresult)+m_bufresult.Length()));
m_alldata->Des().Copy(m_temp->Mid(0,m_temp->Length()));
m_result->Des().Copy(m_alldata->Mid(0,m_alldata->Find(KRESULTL))); //0,1
//send result to login
TBuf<16> aresult;
aresult.Copy(m_result->Des());
TLex val(aresult);
TInt nVal;
val.Val(nVal);
m_pServerToLogin->ServerReNotify(nVal,ERegister); // 将状态值传给登录页面
}
if (m_result->Compare(m_buflogin)==0) // Compare with 'login'
{
m_temp->Des().Copy(m_alldata->Mid(m_alldata->Find(m_bufresult)+m_bufresult.Length()));
m_alldata->Des().Copy(m_temp->Mid(0,m_temp->Length()));
m_result->Des().Copy(m_alldata->Mid(0,m_alldata->Find(KRESULTL))); //0,1,2
//send result to login
TBuf<16> aresult;
aresult.Copy(m_result->Des());
TLex val(aresult);
TInt nVal;
val.Val(nVal);
m_pServerToLogin->ServerReNotify(nVal, ELogin);
}
if (m_result->Compare(m_bufupdate)==0) // Compare with 'update'
{
m_temp->Des().Copy(m_alldata->Mid(m_alldata->Find(m_bufresult)+m_bufresult.Length()));
m_alldata->Des().Copy(m_temp->Mid(0,m_temp->Length()));
m_result->Des().Copy(m_alldata->Mid(0,m_alldata->Find(KRESULTL))); //0,1,2
//send result to login
TBuf<16> aresult;
aresult.Copy(m_result->Des());
TLex val(aresult);
TInt nVal;
val.Val(nVal);
if (nVal == 0)
{
TBuf<32> sbuf;
CEikonEnv::Static()->ReadResource(sbuf, R_ASS_TAS_UPDATE_SUCCESS); //更新成功
CAknInformationNote* informationNote = new (ELeave) CAknInformationNote;
informationNote->ExecuteLD(sbuf);
}
else if (nVal == 1)
{
TBuf<32> sbuf;
CEikonEnv::Static()->ReadResource(sbuf, R_ASS_TAS_UPDATE_FAIL); //更新fail
CAknInformationNote* informationNote = new (ELeave) CAknInformationNote;
informationNote->ExecuteLD(sbuf);
}
// m_pServerToLogin->ServerReNotify(nVal,EUpdate);
}
if (m_result->Compare(m_bufdown)==0) // Compare with 'down'
{
return;
m_BookInfoArr.ResetAndDestroy(); //reset the array
CBookInfo* pBookInfo = NULL; //why not delete this point????
TInt buf;
buf = m_alldata->Find(m_buffname);
while (buf != KErrNotFound)//ishave <fname>
{
pBookInfo = CBookInfo::NewL();
pBookInfo->SetItemID(-1); //overload, -1 for adding new
TBuf<16> m_switch;
m_temp->Des().Copy(m_alldata->Mid(m_alldata->Find(m_buffname)+m_buffname.Length()));
m_alldata->Des().Copy(m_temp->Mid(0,m_temp->Length()));
m_result->Des().Copy(m_alldata->Mid(0,m_alldata->Find(KFNAMEL))); //fname
m_switch.Copy(m_result->Des()); //switch to the TBuf
pBookInfo->SetFirstName(m_switch); // add to pBookInfo
m_temp->Des().Copy(m_alldata->Mid(m_alldata->Find(m_bufhname)+m_bufhname.Length()));
m_alldata->Des().Copy(m_temp->Mid(0,m_temp->Length()));
m_result->Des().Copy(m_alldata->Mid(0,m_alldata->Find(KHNAMEL))); //hname
m_switch.Copy(m_result->Des()); //switch to the TBuf
pBookInfo->SetLastName(m_switch); // add to pBookInfo
m_temp->Des().Copy(m_alldata->Mid(m_alldata->Find(m_bufmob)+m_bufmob.Length()));
m_alldata->Des().Copy(m_temp->Mid(0,m_temp->Length()));
m_result->Des().Copy(m_alldata->Mid(0,m_alldata->Find(KMOBL))); //mob
m_switch.Copy(m_result->Des()); //switch to the TBuf
pBookInfo->SetTelephone(m_switch); // add to pBookInfo
m_BookInfoArr.Append(pBookInfo); //add to the RPointerArray<CBookInfo>
buf = m_alldata->Find(m_buffname);
}
/*将 m_BookInfoArr 的值添加至电话本中*/
m_pBookEngine->UpdatePhoneBook(1,m_BookInfoArr);
}
MEMORY_FREE(m_result);
MEMORY_FREE(m_temp);
MEMORY_FREE(m_alldata);
}
void CServerManager::AppendBodyL(const TDesC& aData)
{
if(!m_pHttpBody) //第一次分配的时候
{
m_pHttpBody = HBufC::NewL(aData.Length());
m_pHttpBody->Des().Copy(aData);
}
else
{
m_pHttpBody = m_pHttpBody->ReAlloc(m_pHttpBody->Length()+aData.Length());
m_pHttpBody->Des().Append(aData);
}
}
void CServerManager::PackUpdateContent()
{
MEMORY_FREE(m_pHttpBody);
AppendBodyL(KRSS);
AppendBodyL(KTYPE);
AppendBodyL(KUPDATE);
AppendBodyL(KTYPEL);
AppendBodyL(KMOBILE);
AppendBodyL(m_sTempPhone);
AppendBodyL(KMOBILEL);
// AppendBodyL(KRSSL);
// AppendBodyL(_L("\r\n--"));
// m_pHTTPEngine->PostRequestL(m_pHttpBody->Des());
// return;
for (int i=0;i<m_BookInfoArr.Count();i++)
{
CBookInfo* m_pBookInfo = static_cast<CBookInfo*>(m_BookInfoArr[i]);
if (m_BookInfoArr[i]->GetItemMarkState())
{
TBuf8<32> sTempBuf;
AppendBodyL(KRECORD);
AppendBodyL(KFNAME);
if (m_pBookInfo->GetFirstName())
{
CCnvCharacterSetConverter* converter1 = NULL;
converter1 = CCnvCharacterSetConverter::NewLC();
converter1->PrepareToConvertToOrFromL(KCharacterSetIdentifierUtf8, CEikonEnv::Static()->FsSession());
converter1->ConvertFromUnicode(sTempBuf, m_pBookInfo->GetFirstName()->Des());
CleanupStack::PopAndDestroy(converter1);
TBuf<32> sfname;
sfname.Copy(sTempBuf);
AppendBodyL(sfname); //fname
}
AppendBodyL(KFNAMEL);
AppendBodyL(KHNAME);
if (m_pBookInfo->GetLastName())
{
CCnvCharacterSetConverter* converter1 = NULL;
converter1 = CCnvCharacterSetConverter::NewLC();
converter1->PrepareToConvertToOrFromL(KCharacterSetIdentifierUtf8, CEikonEnv::Static()->FsSession());
converter1->ConvertFromUnicode(sTempBuf, m_pBookInfo->GetLastName()->Des());
CleanupStack::PopAndDestroy(converter1);
TBuf<32> shname;
shname.Copy(sTempBuf);
AppendBodyL(shname); //hname
}
AppendBodyL(KHNAMEL);
AppendBodyL(KMOB);
if (m_pBookInfo->GetHomeMobileNumber())
{
AppendBodyL(m_pBookInfo->GetHomeMobileNumber()->Des());//mob
}
AppendBodyL(KMOBL);
AppendBodyL(KRECORDL);
}
}
AppendBodyL(KRSSL);
AppendBodyL(_L("--\n"));
//进度框
m_pProgressNote->SaveGameL();
//send to the HttpEngine
m_pHTTPEngine->PostRequestL(m_pHttpBody->Des());
}
void CServerManager::LoginInfoNotify(const TDesC & aNumber, const TDesC & aPassword,TInt aModel)
{
m_sTempPhone.Copy(aNumber);
m_sTempPassWord.Copy(aPassword);
this->MakeRequest(aModel); //distinguish SendType'1,2'
}
void CServerManager::ResponseStatusL(TInt aStatusCode, const TDesC& aStatusText)
{
HBufC* buf = HBufC::NewLC(aStatusText.Length() + KStatusCodeLength);
buf->Des().Format(KStatusFormat, aStatusCode, &aStatusText);//
CAknInformationNote* note = new (ELeave) CAknInformationNote;
note->ExecuteLD(*buf);
CleanupStack::PopAndDestroy(buf);
}
void CServerManager::ResponseReceivedL(const TDesC& aResponseBuffer)
{
this->ParseRecponseContent(aResponseBuffer); //parse and send results to login
HBufC* buf = HBufC::NewLC(aResponseBuffer.Length() + KStatusCodeLength);
buf->Des().Format(KStatusFormat, 0, &aResponseBuffer);
CAknInformationNote* note = new (ELeave) CAknInformationNote;
note->ExecuteLD(*buf);
CleanupStack::PopAndDestroy(buf);
}
//设置服务器连接超时------------------cancer
void CServerManager::TimerExpired()
{
if(m_pTimeOut->IsActive()) m_pTimeOut->Cancel();
if(m_pProgressNote) m_pProgressNote->Cancel();
TBuf<16> sbuf;
CEikonEnv::Static()->ReadResource(sbuf, R_ASS_TAS_SERVER_OVERTIME); //服务器连接超时
CAknInformationNote* informationNote = new (ELeave) CAknInformationNote;
informationNote->ExecuteLD(sbuf);
}
/*
//通过电话本引擎获取名片夹中的信息
void CServerManager::GetContactInfo()
{
m_BookInfoArr.ResetAndDestroy();
m_pBookEngine->UpdatePhoneBook(0, m_BookInfoArr);
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -