reference/SshBasics: Ssh polymorphism/RTTI example for the SshBasics reference example set. (#207)

This patch aims to add a polymorphism & RTTI example to the SShBasics reference examples set.

Demonstrates:
- Creating three different channels (Scp, SFtp, and SshExec) in the same Ssh object array,
- Identifying and converting the stored Ssh objects to the proper channels respectively and calling them.
This commit is contained in:
İsmail Yılmaz 2024-10-04 01:00:16 +03:00 committed by GitHub
parent a15023e1bb
commit a3f8c79a19
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 48 additions and 0 deletions

View file

@ -0,0 +1,42 @@
#include "SshBasics.h"
// SshPolymorphism:
// Demonstrates polymorphism and RTTI for Ssh objects.
void SshPolymorphism(SshSession& session)
{
constexpr const char *path = "/readme.txt";
Array<Ssh> channels;
channels.Create<Scp>(session);
channels.Create<SFtp>(session);
channels.Create<SshExec>(session);
for(Ssh& channel : channels){
if(channel.Is<Scp>()) {
LOG("\nFound: Scp object");
LOG("-----------------\n");
LOG(channel.To<Scp>().LoadFile(path));
}
else
if(channel.Is<SFtp>()) {
LOG("\nFound: Sftp object");
LOG("------------------\n");
LOG(channel.To<SFtp>().GetInfo(path).GetName());
}
else
if(channel.Is<SshExec>()) {
LOG("\nFound: Exec object");
LOG("------------------\n");
String out, err;
channel.To<SshExec>().Execute("ls -l", out, err);
LOG(out);
LOG(err);
}
if(channel.IsError()) {
LOG("Operation failed. Reason: " << channel.GetErrorDesc());
}
}
}

View file

@ -18,6 +18,8 @@ void ScpGet(SshSession& session);
void ForwardTcpIp(SshSession& session);
void X11Forwarding(SshSession& session);
void SshPick(SshSession& session);
void SshPolymorphism(SshSession& session);
void TraceVerbose();
#endif

View file

@ -22,6 +22,7 @@ file
ExecMT.cpp,
"Misc. examples" readonly separator,
PickSemantics.cpp,
Polymorphism.cpp,
VerboseLogging.cpp;
mainconfig

View file

@ -58,8 +58,11 @@ CONSOLE_APP_MAIN
ForwardTcpIp(session);
#elif defined(SSH_PICK_SEMANTICS)
SshPick(session);
#elif defined(SSH_POLYMORPHISM)
SshPolymorphism(session);
#endif
return;
}
LOG(session.GetErrorDesc());
}