commands/rsocket/rsocket.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // rsocket.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 #include <es_sock.h>
       
    15 #include <es_enum.h>
       
    16 #include <in_sock.h>
       
    17 #include <nifvar.h>
       
    18 #include <commdbconnpref.h>
       
    19 
       
    20 using namespace IoUtils;
       
    21 
       
    22 class CCmdRsocket : public CCommandBase
       
    23 	{
       
    24 public:
       
    25 	static CCommandBase* NewLC();
       
    26 	~CCmdRsocket();
       
    27 private:
       
    28 	CCmdRsocket();
       
    29 	void StartConnectionL();
       
    30 	void TcpConnectL();
       
    31 	void TcpDisconnect();
       
    32 private: // From CCommandBase.
       
    33 	virtual const TDesC& Name() const;
       
    34 	virtual void DoRunL();
       
    35 	virtual void ArgumentsL(RCommandArgumentList& aArguments);
       
    36 	virtual void OptionsL(RCommandOptionList& aOptions);
       
    37 private:
       
    38 	RSocketServ iSocketSession;
       
    39 	RConnection iConnection;
       
    40 	RSocket iSocket;
       
    41 	enum 
       
    42 		{
       
    43 		EConnect, EListen
       
    44 		} iOperation;
       
    45 	HBufC* iHost;
       
    46 	TUint iPort;
       
    47 	TUint iIapId;
       
    48 	TUint iNetworkId;
       
    49 	TBool iVerbose;
       
    50 	};
       
    51 
       
    52 
       
    53 CCommandBase* CCmdRsocket::NewLC()
       
    54 	{
       
    55 	CCmdRsocket* self = new(ELeave) CCmdRsocket();
       
    56 	CleanupStack::PushL(self);
       
    57 	self->BaseConstructL();
       
    58 	return self;
       
    59 	}
       
    60 
       
    61 CCmdRsocket::~CCmdRsocket()
       
    62 	{
       
    63 	delete iHost;
       
    64 	iSocket.Close();
       
    65 	iConnection.Close();
       
    66 	iSocketSession.Close();
       
    67 	}
       
    68 
       
    69 CCmdRsocket::CCmdRsocket()
       
    70 	{
       
    71 	}
       
    72 
       
    73 const TDesC& CCmdRsocket::Name() const
       
    74 	{
       
    75 	_LIT(KName, "rsocket");	
       
    76 	return KName;
       
    77 	}
       
    78 
       
    79 void CCmdRsocket::DoRunL()
       
    80 	{
       
    81 	LeaveIfErr(iSocketSession.Connect(), _L("Couldn't open socket server session"));
       
    82 	LeaveIfErr(iConnection.Open(iSocketSession), _L("Couldn't open connection handle"));
       
    83 
       
    84 	switch (iOperation)
       
    85 		{
       
    86 		case EConnect:
       
    87 			TcpConnectL();
       
    88 			break;
       
    89 		case EListen:
       
    90 			LeaveIfErr(KErrNotSupported, _L("Listen operation not yet implemented"));
       
    91 			break;
       
    92 		};
       
    93 	}
       
    94 
       
    95 void CCmdRsocket::StartConnectionL()
       
    96 	{
       
    97 	if (iVerbose)
       
    98 		{
       
    99 		Write(_L("Starting RConnection...\r\n"));
       
   100 		}
       
   101 
       
   102 	TCommDbConnPref prefs;
       
   103 	if (iIapId > 0)
       
   104 		{
       
   105 		prefs.SetIapId(iIapId);
       
   106 		prefs.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);
       
   107 		}
       
   108 	if (iNetworkId > 0)
       
   109 		{
       
   110 		prefs.SetNetId(iNetworkId);
       
   111 		}
       
   112 	LeaveIfErr(iConnection.Start(prefs), _L("Failed to start connection"));
       
   113 	}
       
   114 
       
   115 void CCmdRsocket::TcpConnectL()
       
   116 	{
       
   117 	StartConnectionL();
       
   118 
       
   119 	TInetAddr addr(iPort);
       
   120 	TInt err = addr.Input(*iHost);
       
   121 	if (err)
       
   122 		{
       
   123 		if (iVerbose)
       
   124 			{
       
   125 			Write(_L("Resolving name...\r\n"));
       
   126 			}
       
   127 		RHostResolver resolver;
       
   128 		LeaveIfErr(resolver.Open(iSocketSession, KAfInet, KProtocolInetTcp, iConnection), _L("Unable to open host resolver"));
       
   129 		CleanupClosePushL(resolver);
       
   130 		TNameEntry nameEntry;
       
   131 		LeaveIfErr(resolver.GetByName(*iHost, nameEntry), _L("Unable to resolve name \"%S\""), iHost);
       
   132 		addr.SetAddress(TInetAddr::Cast(nameEntry().iAddr).Address());
       
   133 		CleanupStack::PopAndDestroy(&resolver);
       
   134 		}
       
   135 
       
   136 	if (iVerbose)
       
   137 		{
       
   138 		Write(_L("Opening socket...\r\n"));
       
   139 		}
       
   140 	LeaveIfErr(iSocket.Open(iSocketSession, KAfInet, KSockStream, KProtocolInetTcp, iConnection), _L("Unable to open TCP socket"));
       
   141 
       
   142 	if (iVerbose)
       
   143 		{
       
   144 		Write(_L("Connecting socket...\r\n"));
       
   145 		}
       
   146 	TRequestStatus status;
       
   147 	iSocket.Connect(addr, status);
       
   148 	User::WaitForRequest(status);
       
   149 	LeaveIfErr(status.Int(), _L("Unable to connect to %S : %u"), iHost, iPort);
       
   150 
       
   151 	TcpDisconnect();
       
   152 	}
       
   153 
       
   154 void CCmdRsocket::TcpDisconnect()
       
   155 	{
       
   156 	if (iVerbose)
       
   157 		{
       
   158 		Write(_L("Shutting down socket...\r\n"));
       
   159 		}
       
   160 	TRequestStatus status;
       
   161 	iSocket.Shutdown(RSocket::ENormal, status);
       
   162 	User::WaitForRequest(status);
       
   163 	}
       
   164 
       
   165 void CCmdRsocket::ArgumentsL(RCommandArgumentList& aArguments)
       
   166 	{
       
   167 	_LIT(KArgOperation, "operation");
       
   168 	aArguments.AppendEnumL((TInt&)iOperation, KArgOperation);
       
   169 
       
   170 	_LIT(KOptHost, "host");
       
   171 	aArguments.AppendStringL(iHost, KOptHost);
       
   172 
       
   173 	_LIT(KOptPort, "port");
       
   174 	aArguments.AppendUintL(iPort, KOptPort);
       
   175 	}
       
   176 
       
   177 void CCmdRsocket::OptionsL(RCommandOptionList& aOptions)
       
   178 	{
       
   179 	_LIT(KOptVerbose, "verbose");
       
   180 	aOptions.AppendBoolL(iVerbose, KOptVerbose);
       
   181 
       
   182 	_LIT(KOptIapId, "iap");
       
   183 	aOptions.AppendUintL(iIapId, KOptIapId);
       
   184 
       
   185 	_LIT(KOptNetworkId, "network");
       
   186 	aOptions.AppendUintL(iNetworkId, KOptNetworkId);
       
   187 	}
       
   188 
       
   189 
       
   190 EXE_BOILER_PLATE(CCmdRsocket)
       
   191