tcpudp.cpp
上传用户:sempras
上传日期:2007-03-04
资源大小:821k
文件大小:11k
- //
- // Module: tcpudp.cpp
- //
- // Copyright (C) 2001, Forum Nokia - The Developer Community
- //
- //
- ////////////////////////////////////////////////////////////////
- #include "tcpudp.h"
- //
- // EXPORTed functions
- //
- EXPORT_C CApaApplication* NewApplication()
- {
- return new CTcpUdpApplication;
- }
- GLDEF_C TInt E32Dll(TDllReason)
- {
- return KErrNone;
- }
- ////////////////////////////////////////////////////////////////
- //
- // Application class, CTcpUdpApplication
- //
- ////////////////////////////////////////////////////////////////
- TUid CTcpUdpApplication::AppDllUid() const
- {
- return KUidTcpUdpApp;
- }
- CApaDocument* CTcpUdpApplication::CreateDocumentL()
- {
- return new (ELeave) CTcpUdpDocument(*this);
- }
- ////////////////////////////////////////////////////////////////
- //
- // Document class, CTcpUdpDocument
- //
- ////////////////////////////////////////////////////////////////
- CTcpUdpDocument::CTcpUdpDocument(CEikApplication& aApp)
- : CEikDocument(aApp)
- {
- }
- CEikAppUi* CTcpUdpDocument::CreateAppUiL()
- {
- return new(ELeave) CTcpUdpAppUi;
- }
- ////////////////////////////////////////////////////////////////
- //
- // App UI class, CTcpUdpAppUi
- //
- ////////////////////////////////////////////////////////////////
- void CTcpUdpAppUi::ConstructL()
- {
- BaseConstructL();
- iAppView=new(ELeave) CTcpUdpView;
- CleanupStack::PushL(iAppView);
- iAppView->ConstructL(ClientRect());
- CleanupStack::Pop();
- // Read the ini-file
- TPreferences param;
- RestorePreferencesL(param);
- iAppView->Engine()->SetPreferences(param);
- // add app view to stack; enables key event handling.
- AddToStackL(iAppView);
- }
- CTcpUdpAppUi::~CTcpUdpAppUi()
- {
- delete iAppView;
- }
- void CTcpUdpAppUi::DynInitMenuPaneL(TInt aMenuId,CEikMenuPane* aMenuPane)
- {
- iAppView->DynInitMenuPaneL(aMenuId, aMenuPane);
- }
- void CTcpUdpAppUi::HandleCommandL(TInt aCommand)
- {
- switch (aCommand)
- {
- case ESocketStart:
- iAppView->Start();
- break;
- case ESocketStop:
- iAppView->Stop();
- break;
- case ESocketOptions:
- if (LaunchOptionsDialogL(iAppView->Engine()))
- {
- TPreferences param;
- iAppView->Engine()->GetPreferences(param);
- StorePreferencesL(param);
- }
- break;
- case ESocketClearScreen:
- iAppView->ClearScreen();
- break;
- case ESocketAbout:
- LaunchAboutDialogL();
- break;
- case EEikCmdExit:
- iAppView->Stop();
- Exit();
- break;
- default: //Console Specific Commands
- break;
- }
- }
-
- void CTcpUdpAppUi::RestorePreferencesL(TPreferences &aPreferences)
- {
- CDictionaryStore *iniFile = Application()->OpenIniFileLC(iCoeEnv->FsSession());
- TBool found = iniFile->IsPresentL(KUidTcpUdpApp);
- TInt error = KErrNone;
- if (found)
- {
- RDictionaryReadStream readStream;
- readStream.OpenLC (*iniFile, KUidTcpUdpApp);
- // ignore any reads off the end of the file etc - clear later on
- TRAP(error, aPreferences.iProtocol = readStream.ReadUint16L());
- TRAP(error, aPreferences.iRemotePort = readStream.ReadUint16L());
- TRAP(error, aPreferences.iLocalPort = readStream.ReadUint16L());
- TRAP(error, aPreferences.iCount = readStream.ReadUint16L());
- TRAP(error, aPreferences.iTrace = readStream.ReadUint32L());
- TRAP(error, aPreferences.iPacketSize = readStream.ReadUint16L());
- // aPreferences.iRemoteAddr
- TInt length=0;
- TRAP(error, length = readStream.ReadInt16L());
- TRAP(error, readStream.ReadL(aPreferences.iRemoteAddr,length));
- // aPreferences.iLocalAddr
- length=0;
- TRAP(error, length = readStream.ReadInt16L());
- TRAP(error, readStream.ReadL(aPreferences.iLocalAddr,length));
- CleanupStack::PopAndDestroy(); // readStream
- }
- CleanupStack::PopAndDestroy(); // iniFile
- if (error!=KErrNone || !found)
- {
- // missing stream initialise
- CRunEngine::DefaultPreferences(aPreferences);
-
- //.... // and whatever is appropriate for all the other fields
- StorePreferencesL(aPreferences); // store the default ones - update inifile
- }
- }
- void CTcpUdpAppUi::StorePreferencesL(const TPreferences &aPreferences)
- {
- CDictionaryStore *iniFile = Application()->OpenIniFileLC(iCoeEnv->FsSession());
- RDictionaryWriteStream writeStream;
- writeStream.AssignLC(*iniFile, KUidTcpUdpApp);
-
- writeStream.WriteUint16L (aPreferences.iProtocol);
- writeStream.WriteUint16L (aPreferences.iRemotePort);
- writeStream.WriteUint16L (aPreferences.iLocalPort);
- writeStream.WriteUint16L (aPreferences.iCount);
- writeStream.WriteUint32L (aPreferences.iTrace);
- writeStream.WriteUint16L (aPreferences.iPacketSize);
- // iRemoteAddr
- writeStream.WriteInt16L (aPreferences.iRemoteAddr.Length());
- writeStream.WriteL (aPreferences.iRemoteAddr);
- // iLocalAddr
- writeStream.WriteInt16L (aPreferences.iLocalAddr.Length());
- writeStream.WriteL (aPreferences.iLocalAddr);
- writeStream.CommitL();
- CleanupStack::PopAndDestroy(); // write stream
- // in this replace XXVersionUid with another unique UID - usually the next one up from XXUid
- writeStream.AssignLC(*iniFile, KUidTcpUdpVersionUid); // write version 1.0 (major.minor)
- writeStream.WriteInt8L(1); // major
- writeStream.WriteInt8L(0); // minor
- writeStream.CommitL(); // flush
- CleanupStack::PopAndDestroy(); // writeStream;
- // commit changes to the store
- if (iniFile->Commit()!=KErrNone)
- iniFile->RevertL();
- CleanupStack::PopAndDestroy();
- }
-
-
- // Launches a dialog to ask for new options
- TBool CTcpUdpAppUi::LaunchOptionsDialogL(CRunEngine* aEngine)
- {
- COptionsDialog *dialog = new (ELeave) COptionsDialog(aEngine);
- CleanupStack::PushL(dialog);
- TInt button=dialog->ExecuteLD(R_TCPUDP_OPTIONS_DIALOG); //Final D means the dialog is destructed by itself
- CleanupStack::Pop();
- return (button==EEikBidOk);
- // If button is CANCEL then no peration is made
- }
- // Launches a dialog to show an about box
- void CTcpUdpAppUi::LaunchAboutDialogL()
- {
- CEikDialog* dialog = new (ELeave) CEikDialog();
- CleanupStack::PushL(dialog);
- dialog->ExecuteLD(R_TCPUDP_ABOUT); //Final D means the dialog is destructed by itself
- CleanupStack::Pop();
- }
- ////////////////////////////////////////////////////////////////
- //
- // Application view class, CTcpUdpView
- //
- ////////////////////////////////////////////////////////////////
- void CTcpUdpView::ConstructL(const TRect& aRect)
- {
- CreateWindowL();
- SetRect(aRect);
- iContext=this;
- iBrushStyle=CGraphicsContext::ESolidBrush;
- iBrushColor=KRgbWhite;
- // create the engine
- iEngine= new (ELeave) CRunEngine;
- CleanupStack::PushL(iEngine);
- iEngine->ConstructL(this);
- CleanupStack::Pop();
- CreateBigConsoleL(CEikConsoleScreen::ENoInitialCursor);
-
- ActivateL();
- }
- CTcpUdpView::~CTcpUdpView()
- {
- delete iConsole;
- delete iEngine;
- }
- void CTcpUdpView::DynInitMenuPaneL(TInt aMenuId,CEikMenuPane* aMenuPane)
- {
- if (aMenuId == R_TCPUDP_TOOLS_MENU)
- {
- if (iEngine->Running())
- aMenuPane->SetItemDimmed(ESocketOptions, ETrue);
- else
- aMenuPane->SetItemDimmed(ESocketOptions, EFalse);
- }
- }
- void CTcpUdpView::CreateBigConsoleL(TInt aFlags)
- {
- iConsole=new(ELeave) CConsoleControl;
- TRect rect=Rect();
- TSize size;
- size.iHeight = rect.iBr.iY - rect.iTl.iY; //
- size.iWidth = rect.iBr.iX - rect.iTl.iX - 90; //
-
- TPoint position;
- position.iY = rect.iTl.iY; //
- position.iX = 90; //
-
- CleanupStack::PushL(iConsole);
- iConsole->ConstructL(position, size, aFlags);
- CleanupStack::Pop();
- }
- void CTcpUdpView::Draw(const TRect& ) const
- {
- CWindowGc& gc = SystemGc();
- gc.SetPenStyle(CGraphicsContext::ESolidPen);
- TSize penSizeBold(3,3);
- gc.SetPenSize(penSizeBold);
- gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
-
- TRect rect=iConsole->Rect(); //Lines around the Console
- rect.Grow(3,3);
- gc.DrawRect(rect);
- }
- void CTcpUdpView::Write(const TDesC &aMsg)
- {
- iConsole->Write(aMsg);
- }
- void CTcpUdpView::Start()
- {
- TInt err;
- if (iEngine->Running())
- {
- CEikonEnv::Static()->InfoMsg(_L("Already Running!"));
- return;
- }
-
- err = iEngine->Start();
- if (err != KErrNone)
- {
- Write(_L("Error initializing TcpUdpn"));
- }
- UpdateBusyMsg();
- }
- void CTcpUdpView::Stop()
- {
- if (iEngine->Running())
- iEngine->Stop();
- else
- CEikonEnv::Static()->InfoMsg(_L("Already Stopped!"));
- UpdateBusyMsg();
- }
- void CTcpUdpView::ClearScreen()
- {
- delete iConsole;
- iConsole=NULL;
- CreateBigConsoleL(CEikConsoleScreen::ENoInitialCursor);
- }
- void CTcpUdpView::UpdateBusyMsg()
- {
- CEikButtonGroupContainer *cba = iEikonEnv->AppUiFactory()->ToolBar();
- if (iEngine->Running())
- {
- // Running
- CEikonEnv::Static()->BusyMsgL(R_BUSY);
- cba->DimCommand(ESocketOptions, ETrue) ;
- }
- else
- {
- // NOT running
- CEikonEnv::Static()->BusyMsgCancel();
- cba->DimCommand(ESocketOptions, EFalse) ;
- }
- cba->DrawNow() ;
- }
- //// DIALOG CLASSES ////
- COptionsDialog::COptionsDialog(CRunEngine* aEngine)
- {
- iEngine=aEngine;
- }
- TBool COptionsDialog::OkToExitL(TInt)
- {
- iEngine->iProtocol = ChoiceListCurrentItem(EProtocol);
- iEngine->iLocalPort = NumberEditorValue(ELocalPort);
- iEngine->iRemotePort = NumberEditorValue(ERemotePort);
- STATIC_CAST( CEikEdwin*, Control( ERemoteAddr ) )->GetText( iEngine->iRemoteAddr );
- iEngine->iCount = NumberEditorValue(ECount);
- iEngine->iPacketSize = NumberEditorValue(EPacketSize);
- iEngine->iTrace = ChoiceListCurrentItem(ETrace);
-
- return ETrue;
- }
- void COptionsDialog::PreLayoutDynInitL()
- {
- SetChoiceListCurrentItem(EProtocol,iEngine->iProtocol);
- SetNumberEditorValue(ELocalPort, iEngine->iLocalPort);
- SetNumberEditorValue(ERemotePort, iEngine->iRemotePort);
- CEikEdwin* edwin=(CEikEdwin*)Control(ERemoteAddr);
- edwin->SetTextL(&iEngine->iRemoteAddr);
- SetNumberEditorValue(ECount, iEngine->iCount);
- SetNumberEditorValue(EPacketSize, iEngine->iPacketSize);
- SetChoiceListCurrentItem(ETrace,iEngine->iTrace);
- }
- //
- // CConsoleControl
- //
- void CConsoleControl::ConstructL(TPoint aTopLeft,const TSize& aSize,TInt aFlags)
- {
- TRect rect(aTopLeft,aTopLeft + aSize.AsPoint());
- SetRect(rect);
-
- iConsole=new(ELeave) CEikConsoleScreen;
- CleanupStack::PushL(iConsole);
- iConsole->ConstructL(_L("TEST"),aTopLeft,aSize,aFlags,EEikConsWinInPixels);
- CleanupStack::Pop();
- iConsole->SetHistorySizeL(200,0);
- iHistory=200;
- }
- CConsoleControl::~CConsoleControl()
- {
- delete iConsole;
- }
- void CConsoleControl::ActivateL()
- {
- CCoeControl::ActivateL();
- iConsole->SetKeepCursorInSight(TRUE);
- iConsole->DrawCursor();
- iConsole->SetAtt(ATT_NORMAL);
- }
- void CConsoleControl::FocusChanged(TDrawNow aDrawNow)
- {
- iConsole->ConsoleControl()->SetFocus(IsFocused(), aDrawNow);
- }
- void CConsoleControl::DrawCursor()
- {
- iConsole->DrawCursor();
- }
- void CConsoleControl::Write(const TDesC &aMsg)
- {
- iConsole->Write(aMsg);
- iConsole->FlushChars();
- }
- CEikConsoleControl *CConsoleControl::ConsoleControl()
- {
- return iConsole->ConsoleControl();
- }
- TBool CConsoleControl::UpdateScrollBars()
- {
- return iConsole->UpdateScrollBars();
- }
- void CConsoleControl::ClearScreen()
- {
- iConsole->ClearScreen();
- }
- void CConsoleControl::Redraw(const TRect &aRect)
- {
- iConsole->Redraw(aRect);
- }
- void CConsoleControl::Lf()
- {
- iConsole->Lf();
- }