examples/Networking/TcpIp/EchoClientUI/Eikecho.cpp

00001 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // This component and the accompanying materials are made available
00004 // under the terms of "Eclipse Public License v1.0"
00005 // which accompanies this distribution, and is available
00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 // EIKON interface to ECHOENG
00015 // Definitions of CConsoleControl, CEchoAppUi, 
00016 // CEchoDocument, CEchoApplication
00017 //
00018 
00019 #include "Eikecho.h"
00020 #include <eikstart.h>
00021 
00022 
00023 CConsoleControl* CConsoleControl::NewL(CEchoEngine* aEchoEngine)
00024         {
00025         CConsoleControl* self=new (ELeave) CConsoleControl;
00026         CleanupStack::PushL(self);
00027         self->ConstructL(aEchoEngine);
00028         CleanupStack::Pop();
00029         return self;
00030         }
00031 
00032 void CConsoleControl::ConstructL(CEchoEngine* aEchoEngine)
00033         {
00034         iEchoEngine=aEchoEngine;
00035     CreateWindowL();
00036     Window().SetShadowDisabled(ETrue);
00037     Window().SetBackgroundColor(KRgbGray);
00038     EnableDragEvents();
00039     SetExtentToWholeScreen();
00040         SetBlank();
00041     iConsole=new(ELeave) CEikConsoleScreen;
00042         _LIT(KEikEcho,"EikEcho");
00043         iConsole->ConstructL(KEikEcho,0);
00044         iConsole->SetHistorySizeL(10,10);
00045         }
00046 
00047 CConsoleControl::~CConsoleControl()
00048         {
00049         delete iConsole;
00050         }
00054 void CConsoleControl::ActivateL()
00055         {
00056         CCoeControl::ActivateL();
00057         iConsole->SetKeepCursorInSight(TRUE);
00058         iConsole->DrawCursor();
00059         iConsole->ConsoleControl()->SetFocus(ETrue, EDrawNow); 
00060         }
00061 
00062 TKeyResponse CConsoleControl::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
00063 // The key event handler
00064     {
00065         if (aType!=EEventKey)
00066                 return(EKeyWasConsumed);
00067     TInt aChar=aKeyEvent.iCode;
00068 
00069         if (aChar == '3')
00070                 // Connect using an IP address
00071                 {
00072                 _LIT(KConnecting1,"Attempting connection to KInetAddr\n");
00073                 iConsole->Printf(KConnecting1);
00074                 iEchoEngine->ConnectTo(KInetAddr);
00075                 }
00076         else if (aChar == '2')
00077                 {
00078                 // Connect using a hostname, calling DNS first to resolve this to an IP address
00079                 _LIT(KConnecting2,"Attempting connection to KInetHostName\n");
00080                 iConsole->Printf(KConnecting2);
00081                 iEchoEngine->ConnectL(KInetHostName);
00082                 }
00083         else if (aChar == '1')
00084                 {
00085                 // Connect using an IP address, obtaining the symbolic name for this IP address first.
00086                 _LIT(KConnecting3,"Testing GetHostByAddress and attempting connection\n");
00087                 iConsole->Printf(KConnecting3);
00088                 iEchoEngine->TestGetByAddrL(KInetAddr);
00089                 }
00090         else iEchoEngine->Write(aChar); // Send input character to echo server
00091         
00092     return(EKeyWasConsumed);
00093     }
00094 
00095 
00096 
00097 
00098 // The following functions are to implement MUINotify
00099 
00100 void CConsoleControl::PrintNotify(const TDesC& aDes) 
00101 // Print up calls: write information to screen
00102         {
00103         iConsole->Printf(aDes);
00104         };
00105 
00106 void CConsoleControl::PrintNotify(TInt aInt)
00107         {
00108         iConsole->Printf(KIntFormat,aInt);
00109         };
00110 
00111 void CConsoleControl::ErrorNotifyL(const TDesC& aErrMessage, TInt aErrCode)
00112 // Error up call: inform user and quit
00113         {
00114         _LIT(KErrorTitle,"Communications error ");
00115         TBuf<25> errorTitleCode(KErrorTitle);
00116         errorTitleCode.AppendNum(aErrCode);
00117         CEikonEnv::Static()->InfoWinL(errorTitleCode,aErrMessage);
00118         CBaActiveScheduler::Exit();
00119         }
00123 CEchoAppUi::CEchoAppUi(CEchoEngine* aEchoEngine)
00124                 : iEchoEngine(aEchoEngine)
00125         {
00126         }
00127 
00128 void CEchoAppUi::ConstructL()
00129 // Set up control and engine
00130     {
00131     BaseConstructL();
00132         CreateConsoleL();
00133         iEchoEngine->ConstructL(iConsoleControl);
00134         _LIT(KCommands,"\nList of Commands:\n\
00135 3 - Test connecting with IP\n\
00136 2 - Test DNS lookup\n\
00137 1 - Test Get hostname from IP address\n\n");
00138 
00139         iConsoleControl->PrintNotify(KCommands);
00140     }
00141 
00142 void CEchoAppUi::CreateConsoleL()
00143         {
00144         iConsoleControl=CConsoleControl::NewL(iEchoEngine);
00145         AddToStackL(iConsoleControl);
00146         iConsoleControl->ActivateL();
00147         }
00148 
00149 CEchoAppUi::~CEchoAppUi()
00150         {
00151         RemoveFromStack(iConsoleControl);
00152     delete iConsoleControl;
00153         }
00154 
00155 void CEchoAppUi::HandleCommandL(TInt aCommand)
00156 // Command handling
00157         {
00158     switch (aCommand)
00159         {
00160         // When Exit chosen, stop engine and quit
00161         case EEikCmdExit:
00162                 iEchoEngine->Stop();
00163                 Exit();
00164         default:;
00165                 }
00166         }
00167 
00168 /*
00169  Two-phase construction
00170  CEchoDocument: document class, which owns the engine
00171 */
00172 CEchoDocument::CEchoDocument(CEikApplication& aApp)
00173         : CEikDocument(aApp) { }
00174 
00175 CEchoDocument* CEchoDocument::NewL(CEikApplication& aApp)
00176         {
00177         CEchoDocument* self=new (ELeave) CEchoDocument(aApp);
00178         CleanupStack::PushL(self);
00179         self->ConstructL();
00180         CleanupStack::Pop();
00181         return self;
00182         }
00183 
00184 void CEchoDocument::ConstructL()
00185         {
00186         iEchoEngine = new (ELeave) CEchoEngine;
00187         }
00188 
00189 CEchoDocument::~CEchoDocument()
00190         {
00191         delete iEchoEngine;
00192         }
00193 
00194 CEikAppUi* CEchoDocument::CreateAppUiL()
00195         {
00196     return(new(ELeave) CEchoAppUi(iEchoEngine));
00197         }
00198 
00199 
00200 TUid CEchoApplication::AppDllUid() const
00201         {
00202         return(KUidEikEchoApp);
00203         }
00204 
00205 CApaDocument* CEchoApplication::CreateDocumentL()
00206         {
00207         return CEchoDocument::NewL(*this);
00208         }
00209 
00213 EXPORT_C CApaApplication* NewApplication()
00214         {
00215         return(new CEchoApplication);
00216         }
00217 
00218 GLDEF_C TInt E32Main()                                          
00219         {
00220         return EikStart::RunApplication(NewApplication);
00221         }
00222 

Generated by  doxygen 1.6.2