Refactoring code to use SmartPointers

This commit is contained in:
2025-08-16 18:40:58 -04:00
parent e111d4ab5c
commit ddb25c98b5
6 changed files with 135 additions and 39 deletions

View File

@@ -52,6 +52,7 @@
"semaphore": "cpp",
"stop_token": "cpp",
"thread": "cpp",
"*.tpp": "cpp"
"*.tpp": "cpp",
"csignal": "cpp"
}
}

View File

@@ -1,5 +1,11 @@
#include <sstp/clientsocketsender.hpp>
/// @Brief This is to provide a definition for SmartPointer
ClientSocketSender::ClientSocketSender()
: mIPAddress(""), mPort(0)
{
}
/// @brief Establish a connection to the socket specified by ipAddress and port
/// @param ipAddress The ipAddress
/// @param port The port

View File

@@ -16,6 +16,7 @@
class ClientSocketSender
{
public:
ClientSocketSender();
ClientSocketSender(String ipAddress,int port);
virtual ~ClientSocketSender();
bool sendFile(String &pathFileName);

View File

@@ -21,6 +21,83 @@ 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
class SSTP;
SmartPointer<SSTP> sstp;
class SSTP
{
public:
SSTP();
~SSTP();
void handleServer(Block<String>& arguments);
void handleClient(Block<String>& arguments);
private:
SmartPointer<SocketServer> mSocketServer;
SmartPointer<ClientSocketSender> mClientSocketSender;
};
inline
SSTP::SSTP()
{
}
inline
SSTP::~SSTP()
{
mSocketServer.destroy();
mClientSocketSender.destroy();
}
/// @brief [0]=program [1]=SERVERMODE [2]={port}
/// @Note Currently this method will never return. The application is terminate with CTRL-C. Ideally, we would exit is some cleaner fashion
/// perhaps by implementing a CTRL-C hook and then destroying the SocketServer properly by calling it's destructor. See SocketServer::~SocketServer()
/// @param commands
inline
void SSTP::handleServer(Block<String>& commands)
{
Profiler profiler;
if(commands.size()!=3)
{
std::cout << "Missing required parameters" << std::endl;
return;
}
int port = commands[2].toInt();
std::cout << commands[1] << ":" << commands[2] << std::endl;
SocketServer socketServer(commands[2].toInt());
socketServer.listen();
std::cout << "Done, total took " << Utility::formatNumber(profiler.end()) << "(ms)" << std::endl;
}
/// @brief [0]=program, [1]=CLIENTMODE, [2]=serveripaddress, [3]=serverport, [4]=pathfilename
/// @param commands
inline
void SSTP::handleClient(Block<String>& arguments)
{
Profiler profiler;
if(arguments.size()!=5)
{
std::cout << "Missing required parameters" << std::endl;
return;
}
mClientSocketSender.destroy();
mClientSocketSender = new ClientSocketSender(arguments[2],arguments[3].toInt());
bool returnCode = mClientSocketSender->sendFile(arguments[4]);
if(!returnCode)
{
std::cout << "The transfer failed." << std::endl;
}
else
{
std::cout << "Transfer complete" << std::endl;
}
std::cout << "Done, total took " << Utility::formatNumber(profiler.end()) << "(ms)" << std::endl;
}
// ****************************************************************************************************************************************************************
/// @brief
/// @param argc
/// @param argv [0] program. [1] SERVERMODE {port} [2] CLIENTMODE {serveripaddress} {serverport} SEND {FileName}
@@ -52,15 +129,18 @@ int main(int argc, char **argv)
String item(argv[index]);
arguments.insert(item);
}
sstp=::new SSTP();
sstp.disposition(PointerDisposition::Delete);
std::cout << argv[1] << std::endl;
if(arguments[1]=="SERVERMODE")
{
handleServer(arguments);
sstp->handleServer(arguments);
// handleServer(arguments);
}
else if(arguments[1]=="CLIENTMODE")
{
handleClient(arguments);
sstp->handleClient(arguments);
// handleClient(arguments);
}
else
{
@@ -89,6 +169,7 @@ int main(int argc, char **argv)
void signalHandler(int signal)
{
std::cout << "SSTP Received signal " << signal << std::endl;
if(sstp.isOkay())sstp.destroy();
exit(signal);
}
@@ -109,47 +190,46 @@ bool registerSignalHandler(void)
return true;
}
/// @brief [0]=program [1]=SERVERMODE [2]={port}
/// @Note Currently this method will never return. The application is terminate with CTRL-C. Ideally, we would exit is some cleaner fashion
/// perhaps by implementing a CTRL-C hook and then destroying the SocketServer properly by calling it's destructor. See SocketServer::~SocketServer()
/// @param commands
void handleServer(Block<String> &commands)
{
Profiler profiler;
// void handleServer(Block<String> &commands)
// {
// Profiler profiler;
if(commands.size()!=3)
{
std::cout << "Missing required parameters" << std::endl;
return;
}
int port = commands[2].toInt();
std::cout << commands[1] << ":" << commands[2] << std::endl;
SocketServer socketServer(commands[2].toInt());
socketServer.listen();
std::cout << "Done, total took " << Utility::formatNumber(profiler.end()) << "(ms)" << std::endl;
}
// if(commands.size()!=3)
// {
// std::cout << "Missing required parameters" << std::endl;
// return;
// }
// int port = commands[2].toInt();
// std::cout << commands[1] << ":" << commands[2] << std::endl;
// SocketServer socketServer(commands[2].toInt());
// socketServer.listen();
// std::cout << "Done, total took " << Utility::formatNumber(profiler.end()) << "(ms)" << std::endl;
// }
/// @brief [0]=program, [1]=CLIENTMODE, [2]=serveripaddress, [3]=serverport, [4]=pathfilename
/// @param commands
void handleClient(Block<String> &commands)
{
Profiler profiler;
if(commands.size()!=5)
{
std::cout << "Missing required parameters" << std::endl;
return;
}
// void handleClient(Block<String> &commands)
// {
// Profiler profiler;
// if(commands.size()!=5)
// {
// std::cout << "Missing required parameters" << std::endl;
// return;
// }
ClientSocketSender clientSocketSender(commands[2],commands[3].toInt());
bool returnCode = clientSocketSender.sendFile(commands[4]);
if(!returnCode)
{
std::cout << "The transfer failed." << std::endl;
}
else
{
std::cout << "Transfer complete" << std::endl;
}
std::cout << "Done, total took " << Utility::formatNumber(profiler.end()) << "(ms)" << std::endl;
}
// ClientSocketSender clientSocketSender(commands[2],commands[3].toInt());
// bool returnCode = clientSocketSender.sendFile(commands[4]);
// if(!returnCode)
// {
// std::cout << "The transfer failed." << std::endl;
// }
// else
// {
// std::cout << "Transfer complete" << std::endl;
// }
// std::cout << "Done, total took " << Utility::formatNumber(profiler.end()) << "(ms)" << std::endl;
// }

View File

@@ -1,6 +1,13 @@
#include <sstp/socketserver.hpp>
#include <sstp/socketconnectionreceiver.hpp>
/// @brief Defauly constructor. This is to provide a definition for SmartPointer
/// @param port
SocketServer::SocketServer()
: mListenPort(-1), mIsOkay(false), mSocketFileDescriptor(-1)
{
}
/// @brief This will construct the socket listener. You must then call listen() to listen for connections
/// @param port
SocketServer::SocketServer(int port)

View File

@@ -20,6 +20,7 @@ class SocketConnectionReceiver;
class SocketServer
{
public:
SocketServer();
SocketServer(int port);
virtual ~SocketServer();
void listen(void);