Commit 83f13d5b authored by Lefebvre, Jordan's avatar Lefebvre, Jordan
Browse files

#1. Adding hostconfig pod and persistence.

parent 64605ac9
Pipeline #94094 passed with stages
in 4 minutes and 44 seconds
......@@ -5,6 +5,7 @@ sessionworker.hh
sessioncontroller.hh
)
SET(SOURCES
hostconfig.cc
sessionworker.cc
sessioncontroller.cc
)
......@@ -15,6 +16,7 @@ QT5_WRAP_CPP(MOC_FILES
SET(HEADERS
${HEADERS}
declspec.hh
hostconfig.hh
sessionlog.hh
)
TRIBITS_ADD_LIBRARY(rsmcore
......
#include "radixbug/bug.hh"
#include "rsmcore/hostconfig.hh"
#include <fstream>
namespace rsm
{
QString HostConfig::hostName() const { return mHostName; }
void HostConfig::setHostName(const QString &hostName) { mHostName = hostName; }
QString HostConfig::proxyCommand() const { return mProxyCommand; }
void HostConfig::setProxyCommand(const QString &proxyCommand)
{
mProxyCommand = proxyCommand;
}
int HostConfig::port() const { return mPort; }
void HostConfig::setPort(int port) { mPort = port; }
HostConfig::HostConfig() {}
QString HostConfig::userName() const { return mUserName; }
void HostConfig::setUserName(const QString &userName) { mUserName = userName; }
void write_host_config(const HostConfig &config, const QString &path)
{
std::ofstream stream;
stream.open(path.toStdString());
if (!stream.is_open())
{
std::string message = path.toStdString();
message = message + " Failed to open.";
throw std::runtime_error(message);
}
{ // host name
QString var = config.hostName();
stream.write(var.toStdString().c_str(), var.size());
stream << std::endl;
}
{ // user name
QString var = config.userName();
stream.write(var.toStdString().c_str(), var.size());
stream << std::endl;
}
{ // proxy command
QString var = config.proxyCommand();
stream.write(var.toStdString().c_str(), var.size());
stream << std::endl;
}
{ // port number
stream << config.port() << std::endl;
}
stream.close();
}
void read_host_config(HostConfig &config, const QString &path)
{
std::ifstream stream;
stream.open(path.toStdString());
if (!stream.is_open())
{
std::string message = path.toStdString();
message = message + " Failed to open.";
throw std::runtime_error(message);
}
{ // host name
std::string line;
std::getline(stream, line);
radix_tagged_line("Reading host name: " << line);
config.setHostName(line.c_str());
}
{ // user name
std::string line;
std::getline(stream, line);
radix_tagged_line("Reading user name: " << line);
config.setUserName(line.c_str());
}
{ // proxy command
std::string line;
std::getline(stream, line);
radix_tagged_line("Reading proxy command: " << line);
config.setProxyCommand(line.c_str());
}
{ // port number
std::string line;
std::getline(stream, line);
bool ok = false;
int port = QString(line.c_str()).toInt(&ok);
radix_tagged_line("Reading port number: " << line);
if (!ok)
{
port = 22; // default ssh port
radix_tagged_line("\tdefaulting to ssh port (22)");
}
config.setPort(port);
}
}
} // namespace rsm
#ifndef RSMCORE_HOSTCONFIG_HH_
#define RSMCORE_HOSTCONFIG_HH_
#include <QString>
namespace rsm
{
/**
* @brief The HostConfig class
* POD for host configuration design to simplify persistence
*/
class HostConfig
{
protected:
QString mUserName;
QString mHostName;
QString mProxyCommand;
int mPort = 22; // default ssh port
public:
HostConfig();
QString userName() const;
void setUserName(const QString& userName);
QString hostName() const;
void setHostName(const QString& hostName);
QString proxyCommand() const;
void setProxyCommand(const QString& proxyCommand);
int port() const;
void setPort(int port);
}; // class
/**
* @brief write_host_config
* @param config const HostConfig&
* @param path file path to write contents to
*/
void write_host_config(const HostConfig& config, const QString& path);
/**
* @brief read_host_config
* @param config HostConfig&
* @param path file path to read contents from
*/
void read_host_config(HostConfig& config, const QString& path);
} // namespace rsm
#endif /* RSMCORE_HOSTCONFIG_HH_ */
INCLUDE(GoogleTest)
ADD_GOOGLE_TEST(tstHostConfig.cc NP 1)
ADD_GOOGLE_TEST(tstSessionWorker.cc NP 1)
ADD_GOOGLE_TEST(tstSessionController.cc QTCOREAPP NP 1)
#include "gtest/gtest.h"
#include "rsmcore/hostconfig.hh"
#include "radixcore/system.hh"
using namespace rsm;
TEST(RSM, HostConfig)
{
{
HostConfig config;
config.setHostName("romulus.ornl.gov");
config.setUserName("jap");
config.setProxyCommand("corkscrew snowman.ornl.gov 3128 %h %p");
write_host_config(config, "host_romulus.cfg");
}
{
HostConfig config;
config.setPort(40);
config.setProxyCommand("blablabla");
read_host_config(config, "host_romulus.cfg");
EXPECT_EQ("romulus.ornl.gov", config.hostName());
EXPECT_EQ("jap", config.userName());
EXPECT_EQ(22, config.port());
EXPECT_EQ("corkscrew snowman.ornl.gov 3128 %h %p", config.proxyCommand());
}
// radix::remove_file("host_romulus.cfg");
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment