Files
CPP/sstp/main.cpp
2025-11-03 16:45:34 -05:00

113 lines
3.5 KiB
C++

#include <iostream>
#include <csignal>
#include <common/string.hpp>
#include <common/profiler.hpp>
#include <common/block.hpp>
#include <common/utility.hpp>
#include <sstp/socketserver.hpp>
#include <sstp/clientsocketsender.hpp>
#include <sstp/sstp.hpp>
// To Clean: From the top menu choose "View", "Command Palette", Then choose CMake: Clean OR /usr/bin/cmake --build /home/pi/CPP/sstp/build --config Debug --target clean --
// To Build: From the top menu choose "View", "Command Palette", Then choose CMake: Build OR /usr/bin/cmake --build /home/pi/CPP/sstp/build --config Debug --target all --
// Output will be here. /home/pi/CPP/sstp/build
void handleServer(Block<String> &commands); // handler for server mode
void handleClient(Block<String> &commands); // handler for client mode
bool registerSignalHandler(void); // registers a Control-c handler
void signalHandler(int signal); // The Control-C handler
SmartPointer<SSTP> sstp;
/// @brief
/// @param argc
/// @param argv [0] program. [1] SERVERMODE {port} [2] CLIENTMODE {serveripaddress} {serverport} SEND {FileName}
/// @return -1:Error 1:Success
int main(int argc, char **argv)
{
int returnCode(0);
Profiler profiler;
String version = "0.1.0.5"; // major version, minor version, patch, build
std::cout << "sstp version " << version.str() << std::endl;
if(!registerSignalHandler())
{
std::cout << "Unable to register CTRL-C handler" << std::endl;
return -1;
}
try
{
if(argc<2)
{
std::cout << "sstp SERVERMODE {port} | CLIENTMODE {serveripaddress} {serverport} {pathfileName}." << std::endl;
std::cout << "SERVERMODE will listen on the specified port for a connection and receive the specified file." << std::endl;
std::cout << "CLIENTMODE willl connect to the specified address and port and send the specfied file." << std::endl;
return 1;
}
Block<String> arguments;
for(int index=0;index<argc;index++)
{
String item(argv[index]);
arguments.insert(item);
}
sstp=::new SSTP();
sstp.disposition(PointerDisposition::Delete);
std::cout << argv[1] << std::endl;
if(arguments[1]=="SERVERMODE")
{
sstp->handleServer(arguments);
}
else if(arguments[1]=="CLIENTMODE")
{
sstp->handleClient(arguments);
}
else
{
std::cout << "Unknown command " << arguments[1] << std::endl;
returnCode=-1;
}
}
catch(Exception& exception)
{
std::cout << exception.toString() << std::endl;
returnCode=-1;
}
catch(...)
{
std::cout << "An unhandled exception was encountered" << std::endl;
returnCode=-1;
}
std::cout << "Done, total took " << Utility::formatNumber(profiler.end()) << "(ms)" << std::endl;
return returnCode;
}
/// @brief The signal handler
/// TODO: If we are sending then we need to destroy the sender, if we are listening then we need to destroy the listener
/// Currently we exit the app without cleaning anything up.
void signalHandler(int signal)
{
std::cout << "SSTP Received signal " << signal << std::endl;
if(sstp.isOkay())sstp.destroy();
exit(signal);
}
/// @brief Method that registers the signal handler
/// @param
bool registerSignalHandler(void)
{
struct sigaction sa;
sa.sa_handler = signalHandler;
sigemptyset(&sa.sa_mask); // Clear the mask of blocked signals
sa.sa_flags = 0; // No special flags
if (-1==sigaction(SIGINT, &sa, nullptr))
{
std::cerr << "Error registering SIGINT handler." << std::endl;
return false;
}
return true;
}