@ -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 ;
}
@ -50,7 +49,7 @@ auto get_ip_port(int mode)
ipaddress = " 0.0.0.0 " ;
else
ipaddress = get_input < std : : string > ( " Enter IP: " , " Invalid IP " , ip_validator ( ) ) ;
port = get_input < std : : uint16_t > ( " Enter Port: " ) ;
port = get_input < std : : uint16_t > ( " Enter Port: " ) ;
return std : : make_tuple ( mode = = 1 ? " Server " : mode = = 2 ? " Client " : " Unknown " , ipaddress , port ) ;
}
@ -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 ( [ & ] ( )
@ -81,9 +80,9 @@ auto execute_as_client(std::string ipaddress, std::uint16_t port)
auto client_num = this_job . client_metrics . size ( ) + 1 ;
//UX
sscout < < client_num < < math : : numerical_suffix ( client_num ) < < " client response \n "
< < message
< < " Round Trip Was " < < elapsed . count ( ) < < " ms \n "
< < hr ;
< < message
< < " Round Trip Was " < < elapsed . count ( ) < < " ms \n "
< < 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 \t 1. Server \n \t 2. 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
}