Finish project

This commit is contained in:
Substitute 2021-02-20 00:44:06 -05:00
parent 5b0fae4589
commit 3562653d7e
4 changed files with 99 additions and 45 deletions

View File

@ -2,4 +2,5 @@ cmake_minimum_required(VERSION 3.17)
project(Project_1)
set(CMAKE_CXX_STANDARD 17)
add_executable(Project_1 main.cpp modernize/sockets.cpp)

View File

@ -7,21 +7,22 @@
#include <ostream>
#include <iostream>
template<typename T>
class osyncstream_locked final
{
std::unique_lock<std::recursive_mutex> lock_;
std::ostream& stream_;
std::basic_ostream<T>& stream_;
public:
osyncstream_locked(std::recursive_mutex& mtx, std::ostream& stream) : lock_(mtx), stream_(stream) {} //construct and lock the mutex from osyncstream.
osyncstream_locked(std::recursive_mutex& mtx, std::basic_ostream<T>& stream) : lock_(mtx), stream_(stream) {} //construct and lock the mutex from osyncstream.
osyncstream_locked(const osyncstream_locked& o) = delete;
/* A recursive mutex isn't great here (reference forwarding would be better except...only a recursive mutex can assure lock-before-release on ownership transfer */
template<typename T>
friend auto operator<<(osyncstream_locked&& os, const T& d) -> osyncstream_locked&&
template<typename J>
friend auto operator<<(osyncstream_locked&& os, const J& d) -> osyncstream_locked&&
{
os.stream_ << d;
return std::move(os); /* move semantics :( ... thanks Clang/GCC */
}
friend auto operator<<(osyncstream_locked&& os, std::ostream& (*func)(std::ostream&)) -> std::ostream&
friend auto operator<<(osyncstream_locked&& os, std::basic_ostream<T>& (*func)(std::basic_ostream<T>&)) -> std::basic_ostream<T>&
{
return os.stream_ << func;
}
@ -31,20 +32,21 @@ public:
~osyncstream_locked() = default;
};
template<typename T>
class osyncstream final
{
std::recursive_mutex mutex_{};
std::ostream& stream_;
std::basic_ostream<T>& stream_;
public:
explicit osyncstream(std::ostream& stream) : stream_(stream) {}
template<typename T>
friend auto operator<<(osyncstream& os, const T& d) -> osyncstream_locked
explicit osyncstream(std::basic_ostream<T>& stream) : stream_(stream) {}
template<typename J>
friend auto operator<<(osyncstream& os, const J& d) -> osyncstream_locked<T>
{
std::unique_lock<std::recursive_mutex> lock(os.mutex_);
os.stream_ << d;
return osyncstream_locked { os.mutex_, os.stream_ };
return osyncstream_locked<T> { os.mutex_, os.stream_ };
}
friend auto operator<<(osyncstream& os, std::ostream& (*func)(std::ostream&)) -> osyncstream_locked
friend auto operator<<(osyncstream& os, std::basic_ostream<T>& (*func)(std::basic_ostream<T>&)) -> osyncstream_locked<T>
{
std::unique_lock<std::recursive_mutex> lock(os.mutex_);
os.stream_ << func;
@ -54,11 +56,17 @@ public:
struct streams
{
static std::ostream& cout;
static std::ostream& cerr;
static std::basic_ostream<char>& cout;
static std::basic_ostream<char>& cerr;
static std::basic_ostream<wchar_t>& wcout;
static std::basic_ostream<wchar_t>& wcerr;
};
inline std::ostream& streams::cout = std::cout;
inline std::ostream& streams::cerr = std::cerr;
inline std::basic_ostream<char>& streams::cout = std::cout;
inline std::basic_ostream<char>& streams::cerr = std::cerr;
inline std::basic_ostream<wchar_t>& streams::wcout = std::wcout;
inline std::basic_ostream<wchar_t>& streams::wcerr = std::wcerr;
[[maybe_unused]] inline osyncstream sscout(streams::cout); // NOLINT(cert-err58-cpp)
[[maybe_unused]] inline osyncstream sscerr(streams::cerr); // NOLINT(cert-err58-cpp)
[[maybe_unused]] inline osyncstream sswcout(streams::wcout); // NOLINT(cert-err58-cpp)
[[maybe_unused]] inline osyncstream sswcerr(streams::wcerr); // NOLINT(cert-err58-cpp)

View File

@ -1,18 +1,34 @@
#pragma once
#include <sys/ioctl.h>
#include <iomanip>
#include <codecvt>
inline auto terminal_size()
namespace term
{
inline auto use_unicode()
{
std::locale unicode( std::locale(), new std::codecvt_utf8_utf16<wchar_t> );
std::cout.imbue(unicode);
std::wcout.imbue(unicode);
}
inline auto size()
{
struct winsize result{};
ioctl(STDOUT_FILENO, TIOCGWINSZ, &result);
ioctl(STDOUT_FILENO, TIOCGWINSZ, &result); //POSIX
return std::make_tuple(result.ws_col, result.ws_row);
}
template<class CharT, class Traits>
std::basic_ostream<CharT,Traits>& hr(std::basic_ostream<CharT, Traits>& os)
std::basic_ostream<char>& hr(std::basic_ostream<char>& os)
{
auto [width, height] = terminal_size();
auto [width, height] = size();
os << std::setfill('-') << std::setw(width) << "\n" << std::setfill(' ') << std::setw(0);
return os;
}
std::basic_ostream<wchar_t>& hr(std::basic_ostream<wchar_t>& os)
{
auto [width, height] = size();
os << std::setfill(L'') << std::setw(width) << "\n" << std::setfill(L' ') << std::setw(0);
return os;
}
}

View File

@ -1,5 +1,3 @@
#pragma clang diagnostic push
#pragma ide diagnostic ignored "EndlessLoop"
#include "modernize/sockets.h"
#include "libraries/user_input.h"
#include "libraries/osyncstream.h"
@ -12,33 +10,34 @@
#include <iostream>
#include <fstream>
#include <locale>
#include <codecvt>
#include <string>
using json = nlohmann::json;
auto onClientConnected(networked& client)
{
sscout << "Accepted Client on Server.\n";
using namespace std::chrono_literals;
auto start = std::chrono::high_resolution_clock::now();
//std::this_thread::sleep_for(2000ms);
return true;
}
auto onClientMessaged(networked& client, std::string& message)
{
//TODO vm_stat -> vmstat
std::string commands[6] = {"date", "uptime", "vm_stat", "netstat -ant", "who", "ps"};
std::string commands[6] = {"date", "uptime", "vmstat", "netstat -ant", "who", "ps"};
auto badOperand = []() {sscout << "Invalid OPCode, Ignored.\n";};
try
{
int operation = std::stoi(message);
sscout << "OPCode is " << operation << ".\n";
sscout << "Received OPCode " << operation << " from Client.\n";
if (math::is_between<0,7>(operation))
client.send_message(net::system(commands[operation-1]));
else
badOperand();
}catch(std::invalid_argument& invalid_number) { badOperand(); }
client.disconnect();
sscout << "Disconnected Client.\n";
return true;
}
@ -67,7 +66,7 @@ auto execute_as_client(std::string ipaddress, std::uint16_t port)
metrics::job this_job{};
this_job.operation = operation;
this_job.client_metrics.reserve(num_clients);
auto failures = 0;
for(int i = 0; i < num_clients; ++i)
{
threads.emplace_back(std::thread([&]()
@ -83,7 +82,7 @@ auto execute_as_client(std::string ipaddress, std::uint16_t port)
sscout << client_num << math::numerical_suffix(client_num) << " client response\n"
<< message
<< "Round Trip Was " << elapsed.count() << " ms\n"
<< hr;
<< term::hr;
//Metrics
metrics::client_message client_metric{};
client_metric.client_number = client_num;
@ -96,12 +95,16 @@ auto execute_as_client(std::string ipaddress, std::uint16_t port)
{
net_client.send_message(std::to_string(operation));
res.join();
}else{
++failures;
}
}));
}
sscout << "All clients connected.\n" << hr;
sscout << "Client setup complete.\n" << term::hr;
for(auto&& thread : threads)
thread.join();
if (failures)
sscout << failures << " client(s) failed to connect.";
metrics::job_metrics.emplace_back(this_job);
sscout << "Finished!\n";
}
@ -110,15 +113,36 @@ auto execute_as_client(std::string ipaddress, std::uint16_t port)
metrics.open ("metrics.json", std::ios::out | std::ios::trunc); //again, metrics data
metrics << metrics_json;
metrics.close();
sscout << "Metrics Saved to metrics.json\n";
}
auto operator<<(std::basic_ostream<wchar_t>& os, const std::string& str) -> std::basic_ostream<wchar_t>&
{
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
std::wstring wide = converter.from_bytes(str);
os << wide;
return os;
}
int main() {
term::use_unicode();
sswcout
<< L"┌───────────╖\n"
<< L"│ Project 1 ║\n"
<< L"╘═══════════╝\n";
sswcout
<< L"┌────────────────────╖\n"
<< L"│ Riley Strickland ║\n"
<< L"│ Jacob Jerris ║\n"
<< L"│ Jacqueline Shrader ║\n"
<< L"╘════════════════════╝\n";
net::prologue();
const auto mode = get_input<int>("Select Mode\n\t1. Server\n\t2. Client\n>:", "Invalid Choice!",
math::is_between<0, 3>);
auto[prefix, ipaddress, port] = get_ip_port(mode);
sscout << prefix << " selected, ip/port is " << ipaddress << "/" << port << ".\n";
sscout << prefix << " selected, ip/port is " << ipaddress << ":" << port << ".\n";
if (mode == 2) {
execute_as_client(ipaddress, port);
} else {
@ -128,6 +152,11 @@ int main() {
net_server.listen(ipaddress, port).join();
}
net::epilogue();
sswcout
<< L"┌────────────────────╖\n"
<< L"│ Thanks for using ║\n"
<< L"╘════════════════════╝\n";
return 0;
}
#pragma clang diagnostic pop