10 Commits

Author SHA1 Message Date
fc69790bd2 Code cleanup 2025-12-18 09:27:37 -05:00
6d87851117 Code cleanup 2025-12-16 18:23:21 -05:00
5c5f4d7d8f maintenance 2025-12-05 09:22:45 -05:00
ebb7628498 Code cleanup 2025-11-20 11:30:41 -05:00
9abd4b195b Add build info to main.cpp 2025-11-03 16:45:34 -05:00
f7facd3319 Add check for available disk space 2025-08-17 10:08:49 -04:00
2e4a713b1c Update Build to 4 2025-08-16 21:46:08 -04:00
6c70a3c141 Code cleanup. 2025-08-16 21:37:16 -04:00
ddc9b473cd Code cleanup. 2025-08-16 21:22:10 -04:00
ddb25c98b5 Refactoring code to use SmartPointers 2025-08-16 18:40:58 -04:00
11 changed files with 174 additions and 83 deletions

View File

@@ -3,6 +3,8 @@
#include <stdio.h>
#include <limits>
#include <cmath>
#include <filesystem>
#include <sys/statvfs.h>
#include <common/string.hpp>
class Utility
@@ -16,6 +18,7 @@ class Utility
static bool fmod(double dividend,double divisor);
static bool fmod(double dividend, double divisor,double epsilon);
static String byteCountToString(double bytesTransferred, bool perSecond = true);
static size_t getAvailableDiskSpace(const String &path);
private:
static String pad(String theString,char padChar,int toLength);
static String addCommas(String& theString);
@@ -172,4 +175,11 @@ bool Utility::fmod(double dividend, double divisor)
// Check if the absolute remainder is less than epsilon
return std::fabs(remainder) < epsilon;
}
inline
size_t Utility::getAvailableDiskSpace(const String& path)
{
std::filesystem::space_info si = std::filesystem::space(path.str());
return si.available;
}
#endif

View File

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

View File

@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.10.0)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I/home/pi/CPP")
project(sstp VERSION 0.1.0 LANGUAGES C CXX)
add_executable(sstp main.cpp clientsocketsender.cpp socketconnectionreceiver.cpp socketserver.cpp)
add_executable(sstp main.cpp clientsocketsender.cpp socketconnectionreceiver.cpp socketserver.cpp sstp.cpp)
add_subdirectory(/home/pi/CPP/common /home/pi/CPP/common/build)
target_link_libraries(sstp PRIVATE common)

View File

@@ -1,4 +1,11 @@
#include <sstp/clientsocketsender.hpp>
#include <common/utility.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
@@ -29,10 +36,10 @@ ClientSocketSender::ClientSocketSender(String ipAddress, int port)
}
}
/// @brief Close the connection on destruction
ClientSocketSender::~ClientSocketSender()
{
std::cout << "~ClientSocketSender" << std::endl;
close();
}
@@ -40,11 +47,10 @@ ClientSocketSender::~ClientSocketSender()
/// @param
void ClientSocketSender::close(void)
{
if(-1!=mSocket)
{
if(-1==mSocket)return;
std::cout << "ClientSocketSender::close, closing socket" << std::endl;
::close(mSocket);
mSocket=-1;
}
}
/// @brief Send the specified file through the connection
@@ -64,6 +70,7 @@ bool ClientSocketSender::sendFile(String &pathFileName)
sendPutIndicator(pathFileName, fileLength);
Array<char> readBuffer;
readBuffer.size(BUFFER_LENGTH);
std::cout << "Sending file " << pathFileName << " " << Utility::byteCountToString(fileLength, false) << std::endl;
while(true)
{
size_t bytesRead = readFile.read(&readBuffer[0], readBuffer.size());
@@ -83,6 +90,9 @@ bool ClientSocketSender::sendFile(String &pathFileName)
return true;
}
/// @brief Send PCKT with length of packet
/// @param bytesToSend
/// @return
bool ClientSocketSender::sendPacketIndicator(DWORD bytesToSend)
{
if(!isOkay())return false;
@@ -93,6 +103,10 @@ bool ClientSocketSender::sendPacketIndicator(DWORD bytesToSend)
return true;
}
/// @brief Send PUT with filename and file length
/// @param fileName
/// @param fileLength
/// @return
bool ClientSocketSender::sendPutIndicator(String fileName,DWORD fileLength)
{
if(!isOkay())return false;
@@ -107,6 +121,9 @@ bool ClientSocketSender::sendPutIndicator(String fileName,DWORD fileLength)
return true;
}
/// @brief Send QUIT
/// @param
/// @return
bool ClientSocketSender::sendQuit(void)
{
if(!isOkay())return false;
@@ -115,6 +132,10 @@ bool ClientSocketSender::sendQuit(void)
return true;
}
/// @brief items in buffer with length bytesToSend
/// @param buffer The buffer to send
/// @param bytesToSend The number of bytes to send
/// @return
bool ClientSocketSender::sendPacket(Array<char> &buffer,DWORD bytesToSend)
{
if(!isOkay())return false;

View File

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

View File

@@ -1,26 +1,24 @@
#include <stdio.h>
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <csignal>
#include <arpa/inet.h>
#include <thread>
#include <vector>
#include <common/string.hpp>
#include <common/profiler.hpp>
#include <common/block.hpp>
#include <common/fileio.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
// On Isonoe to receive we will point to the binaries here /home/pi/CPP/sstp/build
// On Adrastea we will point to the binaries here /home/pi/Boneyard/sstp
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}
@@ -29,7 +27,7 @@ int main(int argc, char **argv)
{
int returnCode(0);
Profiler profiler;
String version = "0.1.0.3"; // major version, minor version, patch, build
String version = "1.0.0.9"; // major version, minor version, patch, build
std::cout << "sstp version " << version.str() << std::endl;
if(!registerSignalHandler())
@@ -52,15 +50,16 @@ 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);
}
else if(arguments[1]=="CLIENTMODE")
{
handleClient(arguments);
sstp->handleClient(arguments);
}
else
{
@@ -89,6 +88,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 +109,3 @@ 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;
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;
}
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

@@ -2,7 +2,6 @@
#include <common/utility.hpp>
#include <common/stringbuffer.hpp>
SocketConnectionReceiver::SocketConnectionReceiver()
: mSocket(-1), mIsRunnable(true)
{
@@ -94,10 +93,20 @@ bool SocketConnectionReceiver::handlePut(Block<String> &commands)
size_t fileLength;
size_t totalBytesRead=0;
size_t totalPacketsRead=0;
size_t availableDiskSpace=0;
fileName = commands[1];
fileLength = commands[commands.size()-1].toLong();
std::cout << "PUT" << " " << fileName << " " << fileLength << std::endl;
std::cout << "PUT" << " " << fileName << " " << Utility::byteCountToString(fileLength,false) << std::endl;
availableDiskSpace=Utility::getAvailableDiskSpace(String("./"));
std::cout << Utility::byteCountToString(availableDiskSpace,false) << " available space on disk" << std::endl;
if(fileLength > availableDiskSpace)
{
std::cout << "Unsufficient space on disk. Required space " << Utility::byteCountToString(availableDiskSpace,false) << " , available space" << Utility::byteCountToString(availableDiskSpace,false) << std::endl;
return false;
}
FileIO writeFile(fileName, FileIO::ByteOrder::LittleEndian, FileIO::Mode::ReadWrite,FileIO::CreationFlags::CreateAlways);
if(!writeFile.isOkay())
@@ -140,7 +149,7 @@ bool SocketConnectionReceiver::handlePut(Block<String> &commands)
double elapsedTimeSeconds = profiler.elapsed()/1000.00;
double bytesPerSecond = totalBytesRead / elapsedTimeSeconds;
String strBytesPerSecond = Utility::byteCountToString(bytesPerSecond);
std::cout << "Transferred " << Utility::byteCountToString(totalBytesRead,false) << " of " << Utility::byteCountToString(fileLength,false) << " " << percent << " percent " << strBytesPerSecond << std::endl;
std::cout << "Received " << Utility::byteCountToString(totalBytesRead,false) << " of " << Utility::byteCountToString(fileLength,false) << " " << percent << " percent " << strBytesPerSecond << std::endl;
}
}
writeFile.close();

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)
@@ -25,8 +32,6 @@ SocketServer::SocketServer(int port)
// bind socket to network address and port
mInternalSocketAddress.sin_family = AF_INET;
mInternalSocketAddress.sin_addr.s_addr=INADDR_ANY;
mInternalSocketAddress.sin_port = htons(mListenPort);
result = ::bind(mSocketFileDescriptor,(struct sockaddr*)&mInternalSocketAddress,sizeof(mInternalSocketAddress));
if(result)
@@ -40,10 +45,14 @@ SocketServer::SocketServer(int port)
/// @brief Close down the listener
SocketServer::~SocketServer()
{
std::cout << "~SocketServer" << std::endl;
std::cout << "shutdownConnectionReceivers" << std::endl;
shutdownConnectionReceivers();
std::cout << "join" << std::endl;
join(); // Then join all socket threads
std::cout << "close" << std::endl;
close(); // close the listener socket
std::cout << "~SocketServer, done" << std::endl;
}
/// @brief Join all threads
@@ -74,8 +83,8 @@ bool SocketServer::isOkay(void)
}
/// @brief This is the listener. It will wait for incoming connections.
/// When an incoming connection is received it will create a SocketConnectionReceiver object and call it's threadFunction to handle further interactions
/// such as transmitting a file.
/// When an incoming connection is received it will create a SocketConnectionReceiver object and call it's threadFunction
/// to handle further interactions such as transmitting a file.
void SocketServer::listen(void)
{
while(isOkay())
@@ -103,9 +112,6 @@ void SocketServer::listen(void)
pSocketConnectionReceiver = ::new SocketConnectionReceiver(socket, internalSocketAddress);
pSocketConnectionReceiver.disposition(PointerDisposition::Delete);
mExecutionThreads.push_back(std::thread(&SocketConnectionReceiver::threadFunction, *pSocketConnectionReceiver, 0));
// SocketConnectionReceiver socketConnectionReceiver(socket, internalSocketAddress);
// mExecutionThreads.push_back(std::thread(&SocketConnectionReceiver::threadFunction, &socketConnectionReceiver, 0));
}
}
@@ -113,9 +119,8 @@ void SocketServer::listen(void)
/// @param
void SocketServer::close(void)
{
if(-1!=mSocketFileDescriptor)
{
if(-1==mSocketFileDescriptor)return;
std::cout << "SocketServer::close, closing socket" << std::endl;
::close(mSocketFileDescriptor);
mSocketFileDescriptor=-1;
}
}

View File

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

52
sstp/sstp.cpp Normal file
View File

@@ -0,0 +1,52 @@
#include <iostream>
#include <sstp/sstp.hpp>
#include <common/profiler.hpp>
#include <common/utility.hpp>
#include <sstp/socketserver.hpp>
#include <sstp/clientsocketsender.hpp>
/// @brief [0]=program [1]=SERVERMODE [2]={port}
/// @Note Currently this method will never return. The application is terminated with CTRL-C. Ideally, we would exit in 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 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;
mSocketServer = ::new SocketServer(commands[2].toInt());
mSocketServer.disposition(PointerDisposition::Delete);
mSocketServer->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 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());
mClientSocketSender.disposition(PointerDisposition::Delete);
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;
}

33
sstp/sstp.hpp Normal file
View File

@@ -0,0 +1,33 @@
#ifndef _SSTP_SSTP_HPP_
#define _SSTP_SSTP_HPP_
#include <common/block.hpp>
#include <common/pointer.hpp>
class SocketServer;
class ClientSocketSender;
class SSTP
{
public:
SSTP();
virtual ~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();
}
#endif