commands/ping/ping.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // ping.cpp
       
     2 // 
       
     3 // Copyright (c) 2009 - 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "Eclipse Public License v1.0"
       
     6 // which accompanies this distribution, and is available
       
     7 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 // 
       
     9 // Initial Contributors:
       
    10 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 #include <fshell/ioutils.h>
       
    14 using namespace IoUtils;
       
    15 
       
    16 #include "ping_misc.h"
       
    17 #include "pingmodel.h"
       
    18 
       
    19 
       
    20 
       
    21 class CCmdPing : public CCommandBase, public CPingContainer
       
    22 	{
       
    23 public:
       
    24 	static CCommandBase* NewLC();
       
    25 	~CCmdPing();
       
    26 private:
       
    27 	CCmdPing();
       
    28 	void InitModelL();
       
    29 
       
    30 private: // From CCommandBase.
       
    31 	virtual const TDesC& Name() const;
       
    32 	virtual void DoRunL();
       
    33 	virtual void ArgumentsL(RCommandArgumentList& aArguments);
       
    34 	virtual void OptionsL(RCommandOptionList& aOptions);
       
    35 
       
    36 public:
       
    37 	virtual void WriteHostL(TDes& aHostname);
       
    38 	virtual void UpdateStatisticsL();
       
    39 	virtual void WriteLine(const TDesC& abuf);
       
    40 	virtual void OnEnd();
       
    41 	
       
    42 private:
       
    43 	CActiveSchedulerWait* iActiveWait;
       
    44 	CPing *iPingModel;	//contains all Ping related Data
       
    45 	TInt iOptTotalPackets;		//total packets
       
    46 	TInt iOptSecInterval;		//interval between packets
       
    47 	TInt iIap;
       
    48 	HBufC* iAddress;
       
    49 	};
       
    50 
       
    51 CCommandBase* CCmdPing::NewLC()
       
    52 	{
       
    53 	CCmdPing* self = new(ELeave) CCmdPing();
       
    54 	CleanupStack::PushL(self);
       
    55 	self->BaseConstructL();
       
    56 	return self;
       
    57 	}
       
    58 
       
    59 CCmdPing::CCmdPing()
       
    60 	{
       
    61 	iActiveWait = new (ELeave) CActiveSchedulerWait;
       
    62 	}
       
    63 
       
    64 
       
    65 CCmdPing::~CCmdPing()
       
    66 	{
       
    67 	delete iAddress;
       
    68 	delete iPingModel;
       
    69 	delete iActiveWait;
       
    70 	}
       
    71 
       
    72 void CCmdPing::WriteHostL(TDes& aHostname)
       
    73 	{
       
    74 	Printf(_L("Host: %S \r\n"), &(aHostname) );
       
    75 	}
       
    76 
       
    77 void CCmdPing::WriteLine(const TDesC& abuf)
       
    78 	{
       
    79 	Printf(_L("%S\r\n"), &(abuf) );
       
    80 	}
       
    81 
       
    82 void CCmdPing::UpdateStatisticsL()
       
    83 	{
       
    84 	}
       
    85 
       
    86 void CCmdPing::OnEnd()
       
    87 	{
       
    88 	TBool bWait = iActiveWait->IsStarted(); 
       
    89 	if (bWait)
       
    90 		iActiveWait->AsyncStop();
       
    91 	}
       
    92 
       
    93 const TDesC& CCmdPing::Name() const
       
    94 	{
       
    95 	_LIT(KName, "ping");
       
    96 	return KName;
       
    97 	}
       
    98 
       
    99 void CCmdPing::ArgumentsL(RCommandArgumentList& aArguments)
       
   100 	{
       
   101 	aArguments.AppendStringL(iAddress, _L("address"));
       
   102 	}
       
   103 
       
   104 void CCmdPing::OptionsL(RCommandOptionList& aOptions)
       
   105 	{
       
   106 	aOptions.AppendIntL(iOptTotalPackets, _L("num-packets"));
       
   107 	aOptions.AppendIntL(iOptSecInterval, _L("time"));
       
   108 #ifdef IAPSETTING
       
   109 	aOptions.AppendIntL(iIap, _L("iap"));
       
   110 #endif
       
   111 	}
       
   112 
       
   113 void CCmdPing::DoRunL()
       
   114 	{
       
   115 	InitModelL();
       
   116 		
       
   117 	iPingModel->SetConsole(this);
       
   118 	
       
   119 	//set host name, if it is specified
       
   120 	if (iAddress && iAddress->Length()>0 )
       
   121 		{
       
   122 		iPingModel->SetHostName(*iAddress);
       
   123 		}
       
   124 	
       
   125 	//start to ping
       
   126     if (!(iPingModel->IsRunning()))
       
   127     	{
       
   128         iPingModel->BeginL();
       
   129         TBool bPingIsRunning = iPingModel->IsRunning();
       
   130         
       
   131         if (bPingIsRunning)
       
   132             iActiveWait->Start();
       
   133         else
       
   134 	        LeaveIfErr(KErrGeneral, _L("Could not connect to socket server or connect to internet"));
       
   135     	}
       
   136 	}
       
   137 
       
   138 void CCmdPing::InitModelL()
       
   139 	{
       
   140 	TPreferences param;
       
   141 	CPing::DefaultPreferences(param);
       
   142     
       
   143 	if (iOptTotalPackets > 0)
       
   144 		param.iTotalPackets = iOptTotalPackets; 
       
   145 		
       
   146 	if (iOptSecInterval > 0)
       
   147 		param.iSecWait = iOptSecInterval; 
       
   148 
       
   149 #ifdef IAPSETTING
       
   150 	if (iIap > 0)
       
   151 		param.iIAP = iIap;
       
   152 #endif	
       
   153 
       
   154 	iPingModel= new (ELeave) CPing();
       
   155 	iPingModel->ConstructL(param);
       
   156 	}
       
   157 
       
   158 
       
   159 EXE_BOILER_PLATE(CCmdPing)
       
   160