examples/ForumNokia/HTTPClientExample/inc/ClientEngine.h

00001 /*
00002  * Copyright (c) 2009 Nokia Corporation.
00003  */
00004 
00005 #ifndef __CLIENTENGINE_H__
00006 #define __CLIENTENGINE_H__
00007 
00008 #include <coecntrl.h>
00009 #include <http\mhttpdatasupplier.h>
00010 #include <http\mhttptransactioncallback.h>
00011 #include <http\mhttpauthenticationcallback.h>
00012 #include <es_sock.h>
00013 #include <comms-infras/cs_mobility_apiext.h>
00014 #include <cmapplicationsettingsui.h>
00015 
00016 class RHTTPSession;
00017 class RHTTPTransaction;
00018 
00019 const TInt KDefaultBufferSize = 256;
00020 
00021 /*
00022 * MClientObserver
00023 * CClientEngine passes events and responses body data with this interface.
00024 * An instance of this class must be provided for construction of CClientEngine.
00025 */
00026 class MClientObserver
00027   {
00028   public:
00029     /*
00030     * ClientEvent()
00031     *
00032     * Called when event occurs in CClientEngine.
00033     *
00034     * Params:
00035     *   aEventDescription:  A event in textual format, e.g.
00036     *             "Transaction Successful"
00037     *
00038     * Returns:
00039     *     -
00040     *
00041     */
00042     virtual void ClientEvent(const TDesC& aEventDescription) = 0;
00043 
00044     /*
00045     * ClientBodyReceived()
00046     *
00047     * Called when a part of the HTTP body is received.
00048     *
00049     * Params:
00050     *   aBodyData:  Part of the body data received. (e.g. part of
00051     *         the received HTML page)
00052     *
00053     * Returns:
00054     *     -
00055     *
00056     */
00057     virtual void ClientBodyReceived(const TDesC8& aBodyData) = 0;
00058   };
00059 
00060 /*
00061 * Provides simple interface to HTTP Client API.
00062 */
00063 class CClientEngine : public CActive,
00064             public MHTTPTransactionCallback,
00065             public MHTTPDataSupplier,
00066             public MHTTPAuthenticationCallback,
00067             public MMobilityProtocolResp
00068   {
00069 public:
00070 
00071     enum EngineState
00072         {
00073         EIdle = 0,
00074         EGet,
00075         EPost
00076         };
00077 
00078   /*
00079   * NewL()
00080   *
00081   * Create a CClientEngine object.
00082   *
00083   * Params:
00084   *   iObserver:
00085   *
00086   * Returns:
00087   *     A pointer to the created instance of CClientEngine
00088   *
00089   */
00090   static CClientEngine* NewL(MClientObserver& iObserver);
00091 
00092   /*
00093   * NewLC()
00094   *
00095   * Create a CClientEngine object.
00096   *
00097   * Params:
00098   *   iObserver:
00099   *
00100   * Returns:
00101   *     A pointer to the created instance of CClientEngine
00102   *
00103   */
00104   static CClientEngine* NewLC(MClientObserver& iObserver);
00105 
00106   /*
00107   * ~CClientEngine()
00108   *
00109   * Destroy the object
00110   *
00111   * Params:
00112   *   -
00113   *
00114   * Returns:
00115   *     -
00116   *
00117   */
00118   ~CClientEngine();
00119 
00120   /*
00121   * IssueHTTPGetL()
00122   *
00123   * Starts a new HTTP GET transaction.
00124   *
00125   * Params:
00126   *   aUri: URI to get. (e.g. http://host.org")
00127   *
00128   * Returns:
00129   *     -
00130   *
00131   */
00132   void IssueHTTPGetL(const TDesC8& aUri);
00133 
00134   /*
00135   * IssueHTTPPostL()
00136   *
00137   * Starts a new HTTP POST transaction.
00138   *
00139   * Params:
00140   *   aUri: URI where to post the data (e.g. http://host.org")
00141   *   aContentType: Content type of the body, e.g. "text/plain"
00142   *   aBody: Body data for the transaction.
00143   *
00144   * Returns:
00145   *     -
00146   *
00147   */
00148   void IssueHTTPPostL(const TDesC8& aUri,
00149             const TDesC8& aContentType,
00150             const TDesC8& aBody);
00151 
00152   /*
00153   * CancelTransaction()
00154   *
00155   * Closes currently running transaction and frees resources related to it.
00156   *
00157   * Params:
00158   *   -
00159   *
00160   * Returns:
00161   *     -
00162   *
00163   */
00164   void CancelTransaction();
00165 
00166   /*
00167   * IsRunning()
00168   *
00169   * Checks if the transaction is running.
00170   *
00171   * Params:
00172   *   -
00173   *
00174   * Returns:
00175   *     ETrue, if transaction is currently running.
00176   *
00177   */
00178   TBool IsRunning();
00179 
00180 private:
00181   /*
00182   * ConstructL()
00183   *
00184   * Perform the second phase construction of a CClientEngine object.
00185   *
00186   * Params:
00187   *   -
00188   *
00189   * Returns:
00190   *     -
00191   *
00192   */
00193   void ConstructL();
00194 
00195   /*
00196   * CClientEngine()
00197   *
00198   * Perform the first phase of two phase construction.
00199   *
00200   * Params:
00201   *   iObserver:
00202   *
00203   * Returns:
00204   *     -
00205   *
00206   */
00207   CClientEngine(MClientObserver& iObserver);
00208 
00209   /*
00210   * SetHeaderL()
00211   *
00212   * Sets header value of an HTTP request.
00213   *
00214   * Params:
00215   *   aHeaders: Headers of the HTTP request
00216   *   aHdrField:  Enumerated HTTP header field, e.g. HTTP::EUserAgent
00217   *   aHdrValue:  New value for header field
00218   *
00219   * Returns:
00220   *     -
00221   *
00222   */
00223   void SetHeaderL(RHTTPHeaders aHeaders, TInt aHdrField,
00224           const TDesC8& aHdrValue);
00225 
00226   void SetupConnectionL();
00227 
00231   TBool CClientEngine::FindExistingConnection();
00232   
00233 /*
00234 * From MHTTPSessionEventCallback
00235 */
00236 private:
00237   /*
00238   * MHFRunL()
00239   *
00240   * Called by framework to notify about transaction events.
00241   *
00242   * Params:
00243   *   aTransaction: Transaction, where the event occured.
00244   *   aEvent: Occured event.
00245   *
00246   * Returns:
00247   *     -
00248   *
00249   */
00250   void MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent);
00251 
00252   /*
00253   * MHFRunError()
00254   *
00255   * Called by framework when *leave* occurs in handling of transaction event.
00256   *
00257   * Params:
00258   *   aError:     The leave code that occured.
00259   *   aTransaction: The transaction that was being processed when leave
00260   *           occured.
00261   *   aEvent:     The event that was being processed when leave
00262   *           occured.
00263   *
00264   * Returns:
00265   *     KErrNone, if the error was handled. Otherwise the value of aError, or
00266   *   some other error value. Returning error value causes causes
00267   *   HTTP-CORE 6 panic.
00268   *
00269   */
00270   TInt MHFRunError( TInt aError,
00271             RHTTPTransaction aTransaction,
00272             const THTTPEvent& aEvent);
00273 
00274 /*
00275 * From MHTTPDataSupplier (needed for HTTP POST)
00276 */
00277 private:
00278   /*
00279   * ReleaseData()
00280   *
00281   * Called by framework to allow data supplier to release resources
00282   * related to previous data part.
00283   *
00284   * Params:
00285   *   -
00286   *
00287   * Returns:
00288   *     -
00289   *
00290   */
00291   void ReleaseData();
00292 
00293   /*
00294   * GetNextDataPart()
00295   *
00296   * Called when next data part is needed.
00297   *
00298   * Params:
00299   *   aDataPart: Must be set to point to the next data part.
00300   *
00301   * Returns:
00302   *     ETrue if the provided data part is the last one. If more data parts
00303   *   are needed after the provided one, return EFalse.
00304   *
00305   */
00306   TBool GetNextDataPart(TPtrC8& aDataPart);
00307 
00308   /*
00309   * Reset()
00310   *
00311   * Called by framework to reset the data supplier to its initial state.
00312   *
00313   * Params:
00314   *   -
00315   *
00316   * Returns:
00317   *   KErrNone if successfull.
00318   *
00319   */
00320   TInt Reset();
00321 
00322   /*
00323   * OverallDataSize()
00324   *
00325   * Called by framework when it needs to know the size of the
00326   * body data.
00327   *
00328   * Params:
00329   *   -
00330   *
00331   * Returns:
00332   *     Size of the data, or KErrNotFound (or KErrNotSupported)
00333   *   if the size of the data is not known.
00334   *
00335   */
00336   TInt OverallDataSize();
00337 
00338 /*
00339 * From MHTTPAuthenticationCallback (needed for HTTP authentication)
00340 */
00341 private:
00342   /*
00343   * GetCredentialsL()
00344   *
00345   * Called by framework when username and password for requested URI is
00346   * needed.
00347   *
00348   * Params:
00349   *   aURI: The URI being requested (e.g. "http://host.org")
00350   *   aRealm: The realm being requested (e.g. "[email protected]")
00351   *   aAuthenticationType: Authentication type. (e.g. "Basic")
00352   *   aUsername: Given user name.
00353   *   aPassword: Given password.
00354   *
00355   * Returns:
00356   *     A pointer to the created document
00357   *
00358   */
00359   TBool GetCredentialsL(  const TUriC8& aURI,
00360               RString aRealm,
00361               RStringF aAuthenticationType,
00362               RString& aUsername,
00363               RString& aPassword);
00364 
00365   
00366 private:  // from MMobilityProtocolResp
00367     void PreferredCarrierAvailable(TAccessPointInfo aOldAPInfo,
00368                                    TAccessPointInfo aNewAPInfo,
00369                                    TBool aIsUpgrade,
00370                                    TBool aIsSeamless);
00371     void NewCarrierActive(TAccessPointInfo aNewAPInfo, TBool aIsSeamless);
00372     void Error(TInt aError);
00373   
00374 private: // from CActive
00375     void RunL();
00376     void DoCancel();
00377 
00378 private:
00379     void DoHTTPGetL();
00380     void DoHTTPPostL();    
00381     
00382 private:
00383   // declare members
00384     RSocketServ                 iSocketServ;
00385     RConnection                 iConnection;
00386     TUint32                     iSelectedIap;
00387 
00388     RHTTPSession                iSession;
00389     RHTTPTransaction            iTransaction;
00390 
00391     MClientObserver&            iObserver;  // Used for passing body data and events to UI
00392     HBufC8*                     iPostData;  // Data for HTTP POST
00393     TBool                       iRunning;   // ETrue, if transaction running
00394     TBool                       iConnectionSetupDone;
00395     
00396     TInt                        iPrevProfileId;
00397     
00398     CActiveCommsMobilityApiExt* iMobility;
00399     TBool                       iTransactionOpen;
00400     EngineState                 iEngineState;
00401     HBufC8*                     iUri;
00402     HBufC8*                     iContentType;
00403     HBufC8*                     iBody;
00404     
00405     
00406   };
00407 
00408 #endif // __CLIENTENGINE_H__

Generated by  doxygen 1.6.2