From ea518e82f752ba24989a415e9368f758c14b0dde Mon Sep 17 00:00:00 2001 From: Francesco Laurita Date: Sat, 4 Jun 2016 20:57:12 -0700 Subject: [PATCH] Code cleanup --- .gitignore | 2 +- SpeedTest.cpp | 5 +-- SpeedTestClient.cpp | 80 +++++++++++++++++---------------- {build => cmake_build}/.gitkeep | 0 4 files changed, 44 insertions(+), 43 deletions(-) rename {build => cmake_build}/.gitkeep (100%) diff --git a/.gitignore b/.gitignore index ad0acdb..03d29d7 100644 --- a/.gitignore +++ b/.gitignore @@ -27,5 +27,5 @@ *.exe *.out *.app -build/ +cmake_build/ .idea/ \ No newline at end of file diff --git a/SpeedTest.cpp b/SpeedTest.cpp index 405c586..3ae2d80 100644 --- a/SpeedTest.cpp +++ b/SpeedTest.cpp @@ -6,13 +6,10 @@ #include "SpeedTest.h" #include "SpeedTestClient.h" -SpeedTest::SpeedTest() { +SpeedTest::SpeedTest(): mLatency(0), mUploadSpeed(0), mDownloadSpeed(0) { curl_global_init(CURL_GLOBAL_DEFAULT); mIpInfo = IPInfo(); mServerList = std::vector(); - mLatency = 0; - mUploadSpeed = 0; - mDownloadSpeed = 0; } SpeedTest::~SpeedTest() { diff --git a/SpeedTestClient.cpp b/SpeedTestClient.cpp index 3346928..98b1a65 100644 --- a/SpeedTestClient.cpp +++ b/SpeedTestClient.cpp @@ -5,14 +5,13 @@ #include #include "SpeedTestClient.h" -SpeedTestClient::SpeedTestClient(const ServerInfo &serverInfo): mServerInfo(serverInfo) { - mSocketFd = 0; -} +SpeedTestClient::SpeedTestClient(const ServerInfo &serverInfo): mServerInfo(serverInfo), mSocketFd(0) {} SpeedTestClient::~SpeedTestClient() { close(); } +// It returns current timestamp in ms std::time_t SpeedTestClient::now() { return std::chrono::duration_cast @@ -20,6 +19,7 @@ std::time_t SpeedTestClient::now() { .count(); } +// It connects and initiates client/server handshacking bool SpeedTestClient::connect() { if (mSocketFd){ @@ -52,6 +52,7 @@ bool SpeedTestClient::connect() { return false; } +// It closes a connection void SpeedTestClient::close() { if (mSocketFd){ std::string quit = "QUIT\n"; @@ -61,41 +62,7 @@ void SpeedTestClient::close() { } -bool SpeedTestClient::mkSocket() { - mSocketFd = socket(AF_INET, SOCK_STREAM, 0); - - if (!mSocketFd){ - std::cerr << "ERROR, socket()" << std::endl; - return false; - } - - std::size_t found = mServerInfo.host.find(":"); - std::string host = mServerInfo.host.substr(0, found); - std::string port = mServerInfo.host.substr(found + 1, mServerInfo.host.length() - found); - - struct hostent *server = gethostbyname(host.c_str()); - if (server == nullptr) { - std::cerr << "ERROR, no such host " << host << std::endl; - return false; - } - - int portno = std::atoi(port.c_str()); - struct sockaddr_in serv_addr; - memset(&serv_addr, 0, sizeof(serv_addr)); - serv_addr.sin_family = AF_INET; - memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, (size_t)server->h_length); - - serv_addr.sin_port = htons(static_cast(portno)); - - /* Dial */ - if (::connect(mSocketFd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) { - perror("::connect"); - std::cerr << "ERROR connecting" << std::endl; - return false; - } - return true; -} - +// It executes PING command bool SpeedTestClient::ping(long &millisec) { std::stringstream cmd; auto start = now(); @@ -116,6 +83,7 @@ bool SpeedTestClient::ping(long &millisec) { return false; } +// It executes DOWNLOAD command bool SpeedTestClient::download(const long size, const long chunk_size, long &millisec) { std::stringstream cmd; cmd << "DOWNLOAD " << size << "\n"; @@ -145,6 +113,7 @@ bool SpeedTestClient::download(const long size, const long chunk_size, long &mil return true; } +// It executes UPLOAD command bool SpeedTestClient::upload(const long size, const long chunk_size, long &millisec) { std::stringstream cmd; cmd << "UPLOAD " << size << "\n"; @@ -194,3 +163,38 @@ bool SpeedTestClient::upload(const long size, const long chunk_size, long &milli return res.substr(0, ss.str().length()) == ss.str(); } + + +bool SpeedTestClient::mkSocket() { + mSocketFd = socket(AF_INET, SOCK_STREAM, 0); + + if (!mSocketFd){ + std::cerr << "ERROR, socket()" << std::endl; + return false; + } + + std::size_t found = mServerInfo.host.find(":"); + std::string host = mServerInfo.host.substr(0, found); + std::string port = mServerInfo.host.substr(found + 1, mServerInfo.host.length() - found); + + struct hostent *server = gethostbyname(host.c_str()); + if (server == nullptr) { + std::cerr << "ERROR, no such host " << host << std::endl; + return false; + } + + int portno = std::atoi(port.c_str()); + struct sockaddr_in serv_addr; + memset(&serv_addr, 0, sizeof(serv_addr)); + serv_addr.sin_family = AF_INET; + memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, (size_t)server->h_length); + + serv_addr.sin_port = htons(static_cast(portno)); + + /* Dial */ + if (::connect(mSocketFd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) { + std::cerr << "::connect() error" << std::endl; + return false; + } + return true; +} \ No newline at end of file diff --git a/build/.gitkeep b/cmake_build/.gitkeep similarity index 100% rename from build/.gitkeep rename to cmake_build/.gitkeep