From a3f8c79a19f3815f1985a42d8eea9dd451e1ae44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0smail=20Y=C4=B1lmaz?= <32938453+ismail-yilmaz@users.noreply.github.com> Date: Fri, 4 Oct 2024 01:00:16 +0300 Subject: [PATCH] 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. --- reference/SshBasics/Polymorphism.cpp | 42 ++++++++++++++++++++++++++++ reference/SshBasics/SshBasics.h | 2 ++ reference/SshBasics/SshBasics.upp | 1 + reference/SshBasics/main.cpp | 3 ++ 4 files changed, 48 insertions(+) create mode 100644 reference/SshBasics/Polymorphism.cpp diff --git a/reference/SshBasics/Polymorphism.cpp b/reference/SshBasics/Polymorphism.cpp new file mode 100644 index 000000000..75d54a343 --- /dev/null +++ b/reference/SshBasics/Polymorphism.cpp @@ -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 channels; + + channels.Create(session); + channels.Create(session); + channels.Create(session); + + for(Ssh& channel : channels){ + if(channel.Is()) { + LOG("\nFound: Scp object"); + LOG("-----------------\n"); + LOG(channel.To().LoadFile(path)); + } + else + if(channel.Is()) { + LOG("\nFound: Sftp object"); + LOG("------------------\n"); + LOG(channel.To().GetInfo(path).GetName()); + } + else + if(channel.Is()) { + LOG("\nFound: Exec object"); + LOG("------------------\n"); + String out, err; + channel.To().Execute("ls -l", out, err); + LOG(out); + LOG(err); + } + if(channel.IsError()) { + LOG("Operation failed. Reason: " << channel.GetErrorDesc()); + } + + } +} diff --git a/reference/SshBasics/SshBasics.h b/reference/SshBasics/SshBasics.h index fb9300fa1..2d7e82eda 100644 --- a/reference/SshBasics/SshBasics.h +++ b/reference/SshBasics/SshBasics.h @@ -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 + diff --git a/reference/SshBasics/SshBasics.upp b/reference/SshBasics/SshBasics.upp index 85646c638..b3a5f4e84 100644 --- a/reference/SshBasics/SshBasics.upp +++ b/reference/SshBasics/SshBasics.upp @@ -22,6 +22,7 @@ file ExecMT.cpp, "Misc. examples" readonly separator, PickSemantics.cpp, + Polymorphism.cpp, VerboseLogging.cpp; mainconfig diff --git a/reference/SshBasics/main.cpp b/reference/SshBasics/main.cpp index 5a471bdb1..038923fc4 100644 --- a/reference/SshBasics/main.cpp +++ b/reference/SshBasics/main.cpp @@ -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()); } +