mirror of
https://github.com/ultimatepp/ultimatepp.git
synced 2026-05-15 06:05:58 -06:00
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:
parent
a15023e1bb
commit
a3f8c79a19
4 changed files with 48 additions and 0 deletions
42
reference/SshBasics/Polymorphism.cpp
Normal file
42
reference/SshBasics/Polymorphism.cpp
Normal 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());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ file
|
|||
ExecMT.cpp,
|
||||
"Misc. examples" readonly separator,
|
||||
PickSemantics.cpp,
|
||||
Polymorphism.cpp,
|
||||
VerboseLogging.cpp;
|
||||
|
||||
mainconfig
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue