Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
taganaka committed Jun 5, 2016
1 parent 7178111 commit ea518e8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@
*.exe
*.out
*.app
build/
cmake_build/
.idea/
5 changes: 1 addition & 4 deletions SpeedTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ServerInfo>();
mLatency = 0;
mUploadSpeed = 0;
mDownloadSpeed = 0;
}

SpeedTest::~SpeedTest() {
Expand Down
80 changes: 42 additions & 38 deletions SpeedTestClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
#include <arpa/inet.h>
#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<std::chrono::milliseconds>
(std::chrono::system_clock::now().time_since_epoch())
.count();
}

// It connects and initiates client/server handshacking
bool SpeedTestClient::connect() {

if (mSocketFd){
Expand Down Expand Up @@ -52,6 +52,7 @@ bool SpeedTestClient::connect() {
return false;
}

// It closes a connection
void SpeedTestClient::close() {
if (mSocketFd){
std::string quit = "QUIT\n";
Expand All @@ -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<uint16_t>(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();
Expand All @@ -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";
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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<uint16_t>(portno));

/* Dial */
if (::connect(mSocketFd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
std::cerr << "::connect() error" << std::endl;
return false;
}
return true;
}
File renamed without changes.

0 comments on commit ea518e8

Please sign in to comment.