⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 weatherinfo.cpp

📁 Developing.Software.for.Symbian.OS 书籍配套源码
💻 CPP
字号:


/*===========================================================================

     File:weatherinfo.cpp

     This file contains the active object for collecting the weather info
     from the network.

     The sequence of events to collect the temparature is started by the
     GetTemperature() method and issueing the host resolve.  When that is complete
     RunL() is invoked and the next network call in the sequence and is called and so on
     until temperature is retrieved. 
============================================================================*/

#include "weatherinfo.h"

CWeatherInfo* CWeatherInfo::NewL()
{
    
    CWeatherInfo* self = new(ELeave) CWeatherInfo;
    CActiveScheduler::Add(self);
    return self;
}

CWeatherInfo::CWeatherInfo() 
	: CActive(CActive::EPriorityUserInput)
{
   

}


void CWeatherInfo::GetTemperatureL(const TDesC& aCity)

{
// if we are already in the middle of getting the temperture, then
// return.
       if (IsActive())
           return;

 
       iSocketSrv.Connect();
       iCityCode.Copy(aCity);

       TInt res = iSocket.Open(iSocketSrv,KAfInet,KSockStream,KProtocolInetTcp);  
       User::LeaveIfError(res);

/* Resolve name, rest handled by RunL() */
 
       iCommState=EResolvingName;
       res = iResolver.Open(iSocketSrv, KAfInet, KProtocolInetTcp);
       User::LeaveIfError(res);

       _LIT(KWeatherServerName,"rainmaker.wunderground.com");
       iResolver.GetByName(KWeatherServerName, iNameEntry, iStatus);
       SetActive();       

}

CWeatherInfo::~CWeatherInfo()
{
	// Make sure we're cancelled
	Cancel();
}

void CWeatherInfo::RunL()
{
    if (iStatus != KErrNone)
    {
         iSocket.Close();
         iSocketSrv.Close();
         _LIT(KErrorMsg,"Error getting temperature");
         User::InfoPrint(KErrorMsg);
    }
    else
    {
       switch(iCommState)
       {

          case  EResolvingName:
            {
                 TInetAddr destAddr;
                 destAddr=iNameEntry().iAddr;
                 destAddr.SetPort(3000);

 // Connect to the remote host
                 iCommState=EConnecting;

                 iSocket.Connect(destAddr,iStatus);
                 SetActive();
                 break;
            }
          case EConnecting:
            {


                TBuf8<300> getBuff;
                getBuff.Copy(_L("\xD\xA"));
                getBuff.Append(iCityCode);
                getBuff.Append(_L("\xD\xA"));
                
                iCommState=ESending;
                iSocket.Send(getBuff,0,iStatus);
                SetActive();
                break;

            }
          case ESending:
            {
// Start receiving
              
              iCommState=EReceiving;
             
              iSocket.RecvOneOrMore(iNetBuff,0,iStatus,iLen);
              SetActive();
                break;            
                
            }
           case EReceiving:
            {

/*------------------------------------------------------------------- 
   The rainmaker.wunderground.com line with the temperature starts 
   after a line filled with '='s. 
-------------------------------------------------------------------*/

               TInt pos = iNetBuff.FindF(_L8("=\xA"));
               TBuf<100> temp;
               if (pos != KErrNotFound)
                {
                     temp.Copy(iNetBuff.Mid(pos+2,10));
                     temp.Trim();
                     temp.Insert(0,_L("Temperature = "));
                     User::InfoPrint(temp);
                     iSocket.Close();
                     iSocketSrv.Close();
                } else
                {                 
                      iSocket.RecvOneOrMore(iNetBuff,0,iStatus,iLen);
                      SetActive();
                }
              break;
             }
                    
            }
          
            
    }
}
void CWeatherInfo::DoCancel()

{
      iSocket.CancelAll();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -