examples/Networking/TcpIp/EchoClientUI/Eikecho.cpp

00001 /*
00002 Copyright (c) 2000-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:
00028 EIKON interface to ECHOENG
00029 Definitions of CConsoleControl, CEchoAppUi, 
00030 CEchoDocument, CEchoApplication  
00031 */
00032 
00033 
00034 #include "Eikecho.h"
00035 #include <eikstart.h>
00036 
00037 
00038 CConsoleControl* CConsoleControl::NewL(CEchoEngine* aEchoEngine)
00039         {
00040         CConsoleControl* self=new (ELeave) CConsoleControl;
00041         CleanupStack::PushL(self);
00042         self->ConstructL(aEchoEngine);
00043         CleanupStack::Pop();
00044         return self;
00045         }
00046 
00047 void CConsoleControl::ConstructL(CEchoEngine* aEchoEngine)
00048         {
00049         iEchoEngine=aEchoEngine;
00050     CreateWindowL();
00051     Window().SetShadowDisabled(ETrue);
00052     Window().SetBackgroundColor(KRgbGray);
00053     EnableDragEvents();
00054     SetExtentToWholeScreen();
00055         SetBlank();
00056     iConsole=new(ELeave) CEikConsoleScreen;
00057         _LIT(KEikEcho,"EikEcho");
00058         iConsole->ConstructL(KEikEcho,0);
00059         iConsole->SetHistorySizeL(10,10);
00060         }
00061 
00062 CConsoleControl::~CConsoleControl()
00063         {
00064         delete iConsole;
00065         }
00069 void CConsoleControl::ActivateL()
00070         {
00071         CCoeControl::ActivateL();
00072         iConsole->SetKeepCursorInSight(TRUE);
00073         iConsole->DrawCursor();
00074         iConsole->ConsoleControl()->SetFocus(ETrue, EDrawNow); 
00075         }
00076 
00077 TKeyResponse CConsoleControl::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
00078 // The key event handler
00079     {
00080         if (aType!=EEventKey)
00081                 return(EKeyWasConsumed);
00082     TInt aChar=aKeyEvent.iCode;
00083 
00084         if (aChar == '3')
00085                 // Connect using an IP address
00086                 {
00087                 _LIT(KConnecting1,"Attempting connection to KInetAddr\n");
00088                 iConsole->Printf(KConnecting1);
00089                 iEchoEngine->ConnectTo(KInetAddr);
00090                 }
00091         else if (aChar == '2')
00092                 {
00093                 // Connect using a hostname, calling DNS first to resolve this to an IP address
00094                 _LIT(KConnecting2,"Attempting connection to KInetHostName\n");
00095                 iConsole->Printf(KConnecting2);
00096                 iEchoEngine->ConnectL(KInetHostName);
00097                 }
00098         else if (aChar == '1')
00099                 {
00100                 // Connect using an IP address, obtaining the symbolic name for this IP address first.
00101                 _LIT(KConnecting3,"Testing GetHostByAddress and attempting connection\n");
00102                 iConsole->Printf(KConnecting3);
00103                 iEchoEngine->TestGetByAddrL(KInetAddr);
00104                 }
00105         else iEchoEngine->Write(aChar); // Send input character to echo server
00106         
00107     return(EKeyWasConsumed);
00108     }
00109 
00110 
00111 
00112 
00113 // The following functions are to implement MUINotify
00114 
00115 void CConsoleControl::PrintNotify(const TDesC& aDes) 
00116 // Print up calls: write information to screen
00117         {
00118         iConsole->Printf(aDes);
00119         };
00120 
00121 void CConsoleControl::PrintNotify(TInt aInt)
00122         {
00123         iConsole->Printf(KIntFormat,aInt);
00124         };
00125 
00126 void CConsoleControl::ErrorNotifyL(const TDesC& aErrMessage, TInt aErrCode)
00127 // Error up call: inform user and quit
00128         {
00129         _LIT(KErrorTitle,"Communications error ");
00130         TBuf<25> errorTitleCode(KErrorTitle);
00131         errorTitleCode.AppendNum(aErrCode);
00132         CEikonEnv::Static()->InfoWinL(errorTitleCode,aErrMessage);
00133         CBaActiveScheduler::Exit();
00134         }
00138 CEchoAppUi::CEchoAppUi(CEchoEngine* aEchoEngine)
00139                 : iEchoEngine(aEchoEngine)
00140         {
00141         }
00142 
00143 void CEchoAppUi::ConstructL()
00144 // Set up control and engine
00145     {
00146     BaseConstructL();
00147         CreateConsoleL();
00148         iEchoEngine->ConstructL(iConsoleControl);
00149         _LIT(KCommands,"\nList of Commands:\n\
00150 3 - Test connecting with IP\n\
00151 2 - Test DNS lookup\n\
00152 1 - Test Get hostname from IP address\n\n");
00153 
00154         iConsoleControl->PrintNotify(KCommands);
00155     }
00156 
00157 void CEchoAppUi::CreateConsoleL()
00158         {
00159         iConsoleControl=CConsoleControl::NewL(iEchoEngine);
00160         AddToStackL(iConsoleControl);
00161         iConsoleControl->ActivateL();
00162         }
00163 
00164 CEchoAppUi::~CEchoAppUi()
00165         {
00166         RemoveFromStack(iConsoleControl);
00167     delete iConsoleControl;
00168         }
00169 
00170 void CEchoAppUi::HandleCommandL(TInt aCommand)
00171 // Command handling
00172         {
00173     switch (aCommand)
00174         {
00175         // When Exit chosen, stop engine and quit
00176         case EEikCmdExit:
00177                 iEchoEngine->Stop();
00178                 Exit();
00179         default:;
00180                 }
00181         }
00182 
00183 /*
00184  Two-phase construction
00185  CEchoDocument: document class, which owns the engine
00186 */
00187 CEchoDocument::CEchoDocument(CEikApplication& aApp)
00188         : CEikDocument(aApp) { }
00189 
00190 CEchoDocument* CEchoDocument::NewL(CEikApplication& aApp)
00191         {
00192         CEchoDocument* self=new (ELeave) CEchoDocument(aApp);
00193         CleanupStack::PushL(self);
00194         self->ConstructL();
00195         CleanupStack::Pop();
00196         return self;
00197         }
00198 
00199 void CEchoDocument::ConstructL()
00200         {
00201         iEchoEngine = new (ELeave) CEchoEngine;
00202         }
00203 
00204 CEchoDocument::~CEchoDocument()
00205         {
00206         delete iEchoEngine;
00207         }
00208 
00209 CEikAppUi* CEchoDocument::CreateAppUiL()
00210         {
00211     return(new(ELeave) CEchoAppUi(iEchoEngine));
00212         }
00213 
00214 
00215 TUid CEchoApplication::AppDllUid() const
00216         {
00217         return(KUidEikEchoApp);
00218         }
00219 
00220 CApaDocument* CEchoApplication::CreateDocumentL()
00221         {
00222         return CEchoDocument::NewL(*this);
00223         }
00224 
00228 EXPORT_C CApaApplication* NewApplication()
00229         {
00230         return(new CEchoApplication);
00231         }
00232 
00233 GLDEF_C TInt E32Main()                                          
00234         {
00235         return EikStart::RunApplication(NewApplication);
00236         }
00237 

Generated by  doxygen 1.6.2