00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include "weatherinfo.h"
00042
00043 CWeatherInfo* CWeatherInfo::NewL(MWeatherObserver& aObserver)
00044 {
00045 CWeatherInfo* self = new(ELeave) CWeatherInfo(aObserver);
00046 CActiveScheduler::Add(self);
00047 return self;
00048 }
00049
00050
00051
00052 CWeatherInfo::CWeatherInfo(MWeatherObserver& aObserver)
00053 : CActive(CActive::EPriorityStandard), iObserver(aObserver)
00054 {}
00055
00056
00057 void CWeatherInfo::GetTemperature(const TDesC& aCity)
00058 {
00059
00060
00061 if (IsActive())
00062 return;
00063
00064 iCommState=EInitializing;
00065
00066 TInt res =iSocketSrv.Connect();
00067 if (res != KErrNone)
00068 {
00069 Cleanup(res);
00070 return;
00071 }
00072
00073 iCityCode.Copy(aCity);
00074
00075
00076
00077 res = iConnection.Open(iSocketSrv);
00078 if (res != KErrNone)
00079 {
00080 Cleanup(res);
00081 return;
00082 }
00083 res = iConnection.Start();
00084 if (res != KErrNone)
00085 {
00086 Cleanup(res);
00087 return;
00088 }
00089
00090
00091
00092 iSocket.Open(iSocketSrv,KAfInet,KSockStream,KProtocolInetTcp,iConnection);
00093 if (res != KErrNone)
00094 {
00095 Cleanup(res);
00096 return;
00097 }
00098
00099
00100
00101 res = iResolver.Open(iSocketSrv, KAfInet, KProtocolInetTcp,iConnection);
00102 if (res != KErrNone)
00103 {
00104 Cleanup(res);
00105 return;
00106 }
00107 iCommState=EResolvingName;
00108
00109 _LIT(KWeatherServerName,"rainmaker.wunderground.com");
00110 iResolver.GetByName(KWeatherServerName, iNameEntry, iStatus);
00111 SetActive();
00112 }
00113
00114 CWeatherInfo::~CWeatherInfo()
00115 {
00116
00117 Cancel();
00118 }
00119
00120 void CWeatherInfo::RunL()
00121 {
00122 if (iStatus != KErrNone)
00123 {
00124 Cleanup(iStatus.Int());
00125 }
00126 else
00127 {
00128 switch(iCommState)
00129 {
00130 case EResolvingName:
00131 {
00132 TInetAddr destAddr;
00133 destAddr=iNameEntry().iAddr;
00134 destAddr.SetPort(3000);
00135
00136
00137 iCommState=EConnecting;
00138 iSocket.Connect(destAddr,iStatus);
00139 SetActive();
00140 break;
00141 }
00142 case EConnecting:
00143 {
00144 _LIT(KCRLF,"\xD\xA");
00145
00146 TBuf8<300> getBuff;
00147 getBuff.Copy(KCRLF);
00148 getBuff.Append(iCityCode);
00149 getBuff.Append(KCRLF);
00150
00151 iCommState=ESending;
00152 iSocket.Send(getBuff,0,iStatus);
00153 SetActive();
00154 break;
00155 }
00156 case ESending:
00157 {
00158
00159
00160 iCommState=EReceiving;
00161
00162 iSocket.RecvOneOrMore(iNetBuff,0,iStatus,iLen);
00163 SetActive();
00164 break;
00165 }
00166 case EReceiving:
00167 {
00168
00169
00170
00171
00172 _LIT8(KFileTok,"=\xA");
00173 TInt pos = iNetBuff.FindF(KFileTok);
00174 TBuf<100> temp;
00175 if (pos != KErrNotFound)
00176 {
00177 temp.Copy(iNetBuff.Mid(pos+2,10));
00178 temp.Trim();
00179 iObserver.TemperatureReport(iCityCode,temp);
00180 Cleanup(KErrNone);
00181 }
00182 else
00183 {
00184 iSocket.RecvOneOrMore(iNetBuff,0,iStatus,iLen);
00185 SetActive();
00186 }
00187 break;
00188 }
00189 default:
00190 ASSERT(EFalse);
00191 }
00192 }
00193 }
00194
00195 void CWeatherInfo::DoCancel()
00196 {
00197 iSocket.CancelAll();
00198 Cleanup(KErrCancel);
00199 }
00200
00201 void CWeatherInfo::Cleanup(TInt aError)
00202 {
00203 iSocket.Close();
00204 iResolver.Close();
00205 iConnection.Close();
00206 iSocketSrv.Close();
00207
00208 TBuf<50> errStr;
00209 if (aError!=KErrNone)
00210 {
00211 switch (iCommState)
00212 {
00213 case EInitializing:
00214 {
00215 _LIT(KErrStr,"Error initializing communications");
00216 errStr.Copy(KErrStr);
00217 break;
00218 }
00219 case EResolvingName:
00220 {
00221 _LIT(KErrStr,"Error resolving name");
00222 errStr.Copy(KErrStr);
00223 break;
00224 }
00225 case EConnecting:
00226 {
00227 _LIT(KErrStr,"Error connecting to server");
00228 errStr.Copy(KErrStr);
00229 break;
00230 }
00231 case ESending:
00232 {
00233 _LIT(KErrStr,"Error sending request");
00234 errStr.Copy(KErrStr);
00235 break;
00236 }
00237 case EReceiving:
00238 {
00239 _LIT(KErrStr,"Error receiving data");
00240 errStr.Copy(KErrStr);
00241 break;
00242 }
00243 default:
00244 {
00245 _LIT(KErrStr,"Unknown error");
00246 errStr.Copy(KErrStr);
00247 break;
00248 }
00249 }
00250 iObserver.TemperatureError(errStr,aError);
00251 }
00252 }
00253