tcpudp.cpp
上传用户:sempras
上传日期:2007-03-04
资源大小:821k
文件大小:11k
源码类别:

Symbian

开发平台:

C/C++

  1. //
  2. // Module: tcpudp.cpp
  3. //
  4. // Copyright (C) 2001, Forum Nokia - The Developer Community
  5. //
  6. //
  7. ////////////////////////////////////////////////////////////////
  8. #include "tcpudp.h"
  9. //
  10. // EXPORTed functions
  11. //
  12. EXPORT_C CApaApplication* NewApplication()
  13. {
  14. return new CTcpUdpApplication;
  15. }
  16. GLDEF_C TInt E32Dll(TDllReason)
  17. {
  18. return KErrNone;
  19. }
  20. ////////////////////////////////////////////////////////////////
  21. //
  22. // Application class, CTcpUdpApplication
  23. //
  24. ////////////////////////////////////////////////////////////////
  25. TUid CTcpUdpApplication::AppDllUid() const
  26. {
  27. return KUidTcpUdpApp;
  28. }
  29. CApaDocument* CTcpUdpApplication::CreateDocumentL()
  30. {
  31. return new (ELeave) CTcpUdpDocument(*this);
  32. }
  33. ////////////////////////////////////////////////////////////////
  34. //
  35. // Document class, CTcpUdpDocument
  36. //
  37. ////////////////////////////////////////////////////////////////
  38. CTcpUdpDocument::CTcpUdpDocument(CEikApplication& aApp)
  39. : CEikDocument(aApp)
  40. {
  41. }
  42. CEikAppUi* CTcpUdpDocument::CreateAppUiL()
  43. {
  44.     return new(ELeave) CTcpUdpAppUi;
  45. }
  46. ////////////////////////////////////////////////////////////////
  47. //
  48. // App UI class, CTcpUdpAppUi
  49. //
  50. ////////////////////////////////////////////////////////////////
  51. void CTcpUdpAppUi::ConstructL()
  52.     {
  53.     BaseConstructL();
  54.     iAppView=new(ELeave) CTcpUdpView;
  55. CleanupStack::PushL(iAppView);
  56.     iAppView->ConstructL(ClientRect());
  57. CleanupStack::Pop();
  58. // Read the ini-file
  59. TPreferences param;
  60. RestorePreferencesL(param); 
  61. iAppView->Engine()->SetPreferences(param);
  62.     // add app view to stack; enables key event handling.
  63.     AddToStackL(iAppView);
  64. }
  65. CTcpUdpAppUi::~CTcpUdpAppUi()
  66. {
  67.     delete iAppView;
  68. }
  69. void CTcpUdpAppUi::DynInitMenuPaneL(TInt aMenuId,CEikMenuPane* aMenuPane)
  70. {
  71. iAppView->DynInitMenuPaneL(aMenuId, aMenuPane);
  72. }
  73. void CTcpUdpAppUi::HandleCommandL(TInt aCommand)
  74. {
  75. switch (aCommand)
  76. {
  77. case ESocketStart:
  78. iAppView->Start();
  79. break;
  80. case ESocketStop:
  81. iAppView->Stop();
  82. break;
  83. case ESocketOptions:
  84.     if (LaunchOptionsDialogL(iAppView->Engine()))
  85. {
  86. TPreferences param;
  87. iAppView->Engine()->GetPreferences(param);
  88. StorePreferencesL(param);
  89. }
  90. break;
  91. case ESocketClearScreen:
  92. iAppView->ClearScreen();
  93. break;
  94. case ESocketAbout:
  95. LaunchAboutDialogL();
  96. break;
  97. case EEikCmdExit:
  98. iAppView->Stop();
  99. Exit();
  100. break;
  101. default: //Console Specific Commands
  102. break;
  103. }
  104. }
  105. void CTcpUdpAppUi::RestorePreferencesL(TPreferences &aPreferences)
  106. {
  107. CDictionaryStore *iniFile = Application()->OpenIniFileLC(iCoeEnv->FsSession());
  108. TBool found = iniFile->IsPresentL(KUidTcpUdpApp);
  109. TInt error = KErrNone;
  110. if (found) 
  111. {
  112. RDictionaryReadStream readStream;
  113. readStream.OpenLC (*iniFile, KUidTcpUdpApp);
  114. // ignore any reads off the end of the file etc - clear later on
  115. TRAP(error, aPreferences.iProtocol      = readStream.ReadUint16L());
  116. TRAP(error, aPreferences.iRemotePort    = readStream.ReadUint16L());
  117. TRAP(error, aPreferences.iLocalPort     = readStream.ReadUint16L());
  118. TRAP(error, aPreferences.iCount         = readStream.ReadUint16L());
  119. TRAP(error, aPreferences.iTrace         = readStream.ReadUint32L());
  120. TRAP(error, aPreferences.iPacketSize    = readStream.ReadUint16L());
  121. // aPreferences.iRemoteAddr
  122. TInt length=0;
  123. TRAP(error, length = readStream.ReadInt16L());
  124. TRAP(error, readStream.ReadL(aPreferences.iRemoteAddr,length));
  125. // aPreferences.iLocalAddr
  126. length=0;
  127. TRAP(error, length = readStream.ReadInt16L());
  128. TRAP(error, readStream.ReadL(aPreferences.iLocalAddr,length));
  129. CleanupStack::PopAndDestroy(); // readStream
  130. }
  131. CleanupStack::PopAndDestroy(); // iniFile
  132. if (error!=KErrNone || !found) 
  133. {
  134. // missing stream initialise
  135. CRunEngine::DefaultPreferences(aPreferences);
  136. //.... // and whatever is appropriate for all the other fields
  137. StorePreferencesL(aPreferences); // store the default ones - update inifile
  138. }
  139. }
  140. void CTcpUdpAppUi::StorePreferencesL(const TPreferences &aPreferences)
  141. {
  142. CDictionaryStore *iniFile = Application()->OpenIniFileLC(iCoeEnv->FsSession());
  143. RDictionaryWriteStream writeStream;
  144. writeStream.AssignLC(*iniFile, KUidTcpUdpApp);
  145. writeStream.WriteUint16L (aPreferences.iProtocol);
  146. writeStream.WriteUint16L (aPreferences.iRemotePort);
  147. writeStream.WriteUint16L (aPreferences.iLocalPort);
  148. writeStream.WriteUint16L (aPreferences.iCount);
  149. writeStream.WriteUint32L (aPreferences.iTrace);
  150. writeStream.WriteUint16L (aPreferences.iPacketSize);
  151. // iRemoteAddr
  152. writeStream.WriteInt16L (aPreferences.iRemoteAddr.Length());
  153. writeStream.WriteL (aPreferences.iRemoteAddr);
  154. // iLocalAddr
  155. writeStream.WriteInt16L (aPreferences.iLocalAddr.Length());
  156. writeStream.WriteL (aPreferences.iLocalAddr);
  157. writeStream.CommitL();
  158. CleanupStack::PopAndDestroy(); // write stream
  159. // in this replace XXVersionUid with another unique UID - usually the next one up from XXUid
  160. writeStream.AssignLC(*iniFile, KUidTcpUdpVersionUid); // write version 1.0 (major.minor)
  161. writeStream.WriteInt8L(1); // major
  162. writeStream.WriteInt8L(0); // minor
  163. writeStream.CommitL(); // flush
  164. CleanupStack::PopAndDestroy(); // writeStream;
  165. // commit changes to the store
  166. if (iniFile->Commit()!=KErrNone)
  167. iniFile->RevertL();
  168. CleanupStack::PopAndDestroy();
  169. }
  170.    
  171. // Launches a dialog to ask for new options
  172. TBool CTcpUdpAppUi::LaunchOptionsDialogL(CRunEngine* aEngine)
  173. {
  174. COptionsDialog *dialog = new (ELeave) COptionsDialog(aEngine);
  175. CleanupStack::PushL(dialog);
  176. TInt button=dialog->ExecuteLD(R_TCPUDP_OPTIONS_DIALOG); //Final D means the dialog is destructed by itself
  177. CleanupStack::Pop();
  178. return (button==EEikBidOk);
  179. // If button is CANCEL then no peration is made
  180. }
  181. // Launches a dialog to show an about box
  182. void CTcpUdpAppUi::LaunchAboutDialogL()
  183. {
  184. CEikDialog* dialog = new (ELeave) CEikDialog();
  185. CleanupStack::PushL(dialog);
  186. dialog->ExecuteLD(R_TCPUDP_ABOUT); //Final D means the dialog is destructed by itself
  187. CleanupStack::Pop();
  188. }
  189. ////////////////////////////////////////////////////////////////
  190. //
  191. // Application view class, CTcpUdpView
  192. //
  193. ////////////////////////////////////////////////////////////////
  194. void CTcpUdpView::ConstructL(const TRect& aRect)
  195. {
  196.     CreateWindowL();
  197.     SetRect(aRect);
  198.     iContext=this;
  199. iBrushStyle=CGraphicsContext::ESolidBrush;
  200.     iBrushColor=KRgbWhite;
  201. // create the engine
  202. iEngine= new (ELeave) CRunEngine;
  203. CleanupStack::PushL(iEngine);
  204. iEngine->ConstructL(this);
  205. CleanupStack::Pop();
  206. CreateBigConsoleL(CEikConsoleScreen::ENoInitialCursor);
  207.     ActivateL();
  208. }
  209. CTcpUdpView::~CTcpUdpView()
  210. {
  211. delete iConsole;
  212. delete iEngine;
  213. }
  214. void CTcpUdpView::DynInitMenuPaneL(TInt aMenuId,CEikMenuPane* aMenuPane)
  215. {
  216. if (aMenuId == R_TCPUDP_TOOLS_MENU)
  217. {
  218. if (iEngine->Running())
  219. aMenuPane->SetItemDimmed(ESocketOptions, ETrue);
  220. else
  221. aMenuPane->SetItemDimmed(ESocketOptions, EFalse);
  222. }
  223. }
  224. void CTcpUdpView::CreateBigConsoleL(TInt aFlags)
  225. {
  226. iConsole=new(ELeave) CConsoleControl;
  227. TRect rect=Rect();
  228. TSize size;
  229. size.iHeight = rect.iBr.iY - rect.iTl.iY;     // 
  230. size.iWidth = rect.iBr.iX - rect.iTl.iX - 90; // 
  231.   
  232. TPoint position;
  233. position.iY = rect.iTl.iY;   // 
  234. position.iX = 90;            // 
  235. CleanupStack::PushL(iConsole);
  236. iConsole->ConstructL(position, size, aFlags);
  237. CleanupStack::Pop();
  238. }
  239. void CTcpUdpView::Draw(const TRect& ) const
  240. {
  241. CWindowGc& gc = SystemGc();
  242. gc.SetPenStyle(CGraphicsContext::ESolidPen);
  243. TSize penSizeBold(3,3);
  244. gc.SetPenSize(penSizeBold);
  245. gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
  246. TRect rect=iConsole->Rect(); //Lines around the Console
  247. rect.Grow(3,3);
  248. gc.DrawRect(rect);
  249. }
  250. void CTcpUdpView::Write(const TDesC &aMsg)
  251. {
  252. iConsole->Write(aMsg);
  253. }
  254. void CTcpUdpView::Start()
  255. {
  256. TInt err;
  257.     if (iEngine->Running())
  258. {
  259. CEikonEnv::Static()->InfoMsg(_L("Already Running!"));
  260. return;
  261. }
  262. err = iEngine->Start();
  263. if (err != KErrNone)
  264. {
  265. Write(_L("Error initializing TcpUdpn"));
  266. }
  267.     UpdateBusyMsg();
  268. }
  269. void CTcpUdpView::Stop()
  270. {
  271. if (iEngine->Running())
  272. iEngine->Stop();
  273.     else
  274. CEikonEnv::Static()->InfoMsg(_L("Already Stopped!"));
  275.     UpdateBusyMsg();
  276. }
  277. void CTcpUdpView::ClearScreen()
  278. {
  279. delete iConsole;
  280. iConsole=NULL;
  281. CreateBigConsoleL(CEikConsoleScreen::ENoInitialCursor);
  282. }
  283. void CTcpUdpView::UpdateBusyMsg()
  284. {
  285. CEikButtonGroupContainer *cba = iEikonEnv->AppUiFactory()->ToolBar();
  286. if (iEngine->Running())
  287. {
  288. // Running
  289. CEikonEnv::Static()->BusyMsgL(R_BUSY);
  290. cba->DimCommand(ESocketOptions, ETrue) ;
  291. }
  292. else
  293. {
  294. // NOT running
  295. CEikonEnv::Static()->BusyMsgCancel();
  296. cba->DimCommand(ESocketOptions, EFalse) ;
  297. }
  298. cba->DrawNow() ;
  299. }
  300. //// DIALOG CLASSES ////
  301. COptionsDialog::COptionsDialog(CRunEngine* aEngine)
  302. {
  303. iEngine=aEngine;
  304. }
  305. TBool COptionsDialog::OkToExitL(TInt)
  306. {
  307. iEngine->iProtocol    = ChoiceListCurrentItem(EProtocol);
  308.     iEngine->iLocalPort   = NumberEditorValue(ELocalPort);
  309. iEngine->iRemotePort  = NumberEditorValue(ERemotePort);
  310. STATIC_CAST( CEikEdwin*, Control( ERemoteAddr ) )->GetText( iEngine->iRemoteAddr );
  311. iEngine->iCount       = NumberEditorValue(ECount);
  312. iEngine->iPacketSize  = NumberEditorValue(EPacketSize);
  313. iEngine->iTrace       = ChoiceListCurrentItem(ETrace);
  314. return ETrue;
  315. }
  316. void COptionsDialog::PreLayoutDynInitL()
  317. {
  318. SetChoiceListCurrentItem(EProtocol,iEngine->iProtocol);
  319. SetNumberEditorValue(ELocalPort, iEngine->iLocalPort);
  320. SetNumberEditorValue(ERemotePort, iEngine->iRemotePort);
  321. CEikEdwin* edwin=(CEikEdwin*)Control(ERemoteAddr);
  322. edwin->SetTextL(&iEngine->iRemoteAddr);
  323. SetNumberEditorValue(ECount, iEngine->iCount);
  324. SetNumberEditorValue(EPacketSize, iEngine->iPacketSize);
  325. SetChoiceListCurrentItem(ETrace,iEngine->iTrace);
  326. }
  327. //
  328. // CConsoleControl
  329. //
  330. void CConsoleControl::ConstructL(TPoint aTopLeft,const TSize& aSize,TInt aFlags)
  331. {
  332. TRect rect(aTopLeft,aTopLeft + aSize.AsPoint());
  333.     SetRect(rect);
  334.     iConsole=new(ELeave) CEikConsoleScreen;
  335. CleanupStack::PushL(iConsole);
  336. iConsole->ConstructL(_L("TEST"),aTopLeft,aSize,aFlags,EEikConsWinInPixels);
  337. CleanupStack::Pop();
  338. iConsole->SetHistorySizeL(200,0);
  339. iHistory=200;
  340. }
  341. CConsoleControl::~CConsoleControl()
  342. {
  343. delete iConsole;
  344. }
  345. void CConsoleControl::ActivateL()
  346. {
  347. CCoeControl::ActivateL();
  348. iConsole->SetKeepCursorInSight(TRUE);
  349. iConsole->DrawCursor();
  350. iConsole->SetAtt(ATT_NORMAL);
  351. }
  352. void CConsoleControl::FocusChanged(TDrawNow aDrawNow)
  353. {
  354. iConsole->ConsoleControl()->SetFocus(IsFocused(), aDrawNow);
  355. }
  356. void CConsoleControl::DrawCursor()
  357. {
  358. iConsole->DrawCursor();
  359. }
  360. void CConsoleControl::Write(const TDesC &aMsg)
  361. {
  362. iConsole->Write(aMsg);
  363. iConsole->FlushChars();
  364. }
  365. CEikConsoleControl *CConsoleControl::ConsoleControl()
  366. {
  367. return iConsole->ConsoleControl();
  368. }
  369. TBool CConsoleControl::UpdateScrollBars()
  370. {
  371. return iConsole->UpdateScrollBars();
  372. }
  373. void CConsoleControl::ClearScreen()
  374. {
  375. iConsole->ClearScreen();
  376. }
  377. void CConsoleControl::Redraw(const TRect &aRect)
  378. {
  379. iConsole->Redraw(aRect);
  380. }
  381. void CConsoleControl::Lf()
  382. {
  383. iConsole->Lf();
  384. }