Skip to content

Commit

Permalink
Major code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
taganaka committed Oct 11, 2018
1 parent e0c0964 commit 2a61d3f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 99 deletions.
15 changes: 7 additions & 8 deletions SpeedTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ bool SpeedTest::uploadSpeed(const ServerInfo &server, const TestConfig &config,
return true;
}

const int &SpeedTest::latency() {
const long &SpeedTest::latency() {
return mLatency;
}

Expand Down Expand Up @@ -339,12 +339,12 @@ std::vector<std::string> SpeedTest::splitString(const std::string &instr, const
std::size_t start = 0, end = 0;
while ((end = instr.find(separator, start)) != std::string::npos) {
std::string temp = instr.substr(start, end - start);
if (temp != "")
if (!temp.empty())
tokens.push_back(temp);
start = end + 1;
}
std::string temp = instr.substr(start);
if (temp != "")
if (!temp.empty())
tokens.push_back(temp);
return tokens;

Expand Down Expand Up @@ -432,7 +432,7 @@ bool SpeedTest::fetchServers(const std::string& url, std::vector<ServerInfo>& ta
}

size_t len = oss.str().length();
char *xmlbuff = (char*)calloc(len + 1, sizeof(char));
auto *xmlbuff = (char*)calloc(len + 1, sizeof(char));
if (!xmlbuff){
std::cerr << "Unable to calloc" << std::endl;
curl_easy_cleanup(curl);
Expand Down Expand Up @@ -484,7 +484,7 @@ bool SpeedTest::fetchServers(const std::string& url, std::vector<ServerInfo>& ta
return true;
}

const ServerInfo SpeedTest::findBestServerWithin(const std::vector<ServerInfo> &serverList, int &latency,
const ServerInfo SpeedTest::findBestServerWithin(const std::vector<ServerInfo> &serverList, long &latency,
const int sample_size, std::function<void(bool)> cb) {
int i = sample_size;
ServerInfo bestServer = serverList[0];
Expand All @@ -497,7 +497,6 @@ const ServerInfo SpeedTest::findBestServerWithin(const std::vector<ServerInfo> &
if (!client.connect()){
if (cb)
cb(false);
// std::cout << "E" << std::flush;
continue;
}

Expand All @@ -506,7 +505,7 @@ const ServerInfo SpeedTest::findBestServerWithin(const std::vector<ServerInfo> &
continue;
}

int current_latency = INT_MAX;
long current_latency = LONG_MAX;
if (testLatency(client, 20, current_latency)){
if (current_latency < latency){
latency = current_latency;
Expand All @@ -525,7 +524,7 @@ const ServerInfo SpeedTest::findBestServerWithin(const std::vector<ServerInfo> &
return bestServer;
}

bool SpeedTest::testLatency(SpeedTestClient &client, const int sample_size, int &latency) {
bool SpeedTest::testLatency(SpeedTestClient &client, const int sample_size, long &latency) {
if (!client.connect()){
return false;
}
Expand Down
16 changes: 8 additions & 8 deletions SpeedTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,25 @@ typedef void (*progressFn)(bool success);

class SpeedTest {
public:
SpeedTest(float minServerVersion);
explicit SpeedTest(float minServerVersion);
~SpeedTest();
CURLcode httpGet(const std::string& url, std::stringstream& os, CURL *handler = nullptr, long timeout = 30);
CURLcode httpPost(const std::string& url, const std::string& postdata, std::stringstream& os, CURL *handler = nullptr, long timeout = 30);
static std::map<std::string, std::string> parseQueryString(const std::string& query);
static std::vector<std::string> splitString(const std::string& instr, const char separator);
static std::vector<std::string> splitString(const std::string& instr, char separator);
bool ipInfo(IPInfo& info);
const std::vector<ServerInfo>& serverList();
const ServerInfo bestServer(const int sample_size = 5, std::function<void(bool)> cb = nullptr);
const ServerInfo bestServer(int sample_size = 5, std::function<void(bool)> cb = nullptr);
bool setServer(ServerInfo& server);
const int &latency();
const long &latency();
bool downloadSpeed(const ServerInfo& server, const TestConfig& config, double& result, std::function<void(bool)> cb = nullptr);
bool uploadSpeed(const ServerInfo& server, const TestConfig& config, double& result, std::function<void(bool)> cb = nullptr);
bool jitter(const ServerInfo& server, long& result, const int sample = 40);
bool jitter(const ServerInfo& server, long& result, int sample = 40);
bool share(const ServerInfo& server, std::string& image_url);
private:
bool fetchServers(const std::string& url, std::vector<ServerInfo>& target, int &http_code);
bool testLatency(SpeedTestClient& client, const int sample_size, int& latency);
const ServerInfo findBestServerWithin(const std::vector<ServerInfo>& serverList, int& latency, const int sample_size = 5, std::function<void(bool)> cb = nullptr);
bool testLatency(SpeedTestClient& client, int sample_size, long& latency);
const ServerInfo findBestServerWithin(const std::vector<ServerInfo>& serverList, long& latency, int sample_size = 5, std::function<void(bool)> cb = nullptr);
static CURL* curl_setup(CURL* curl = nullptr);
static size_t writeFunc(void* buf, size_t size, size_t nmemb, void* userp);
static ServerInfo processServerXMLNode(xmlTextReaderPtr reader);
Expand All @@ -58,7 +58,7 @@ class SpeedTest {

IPInfo mIpInfo;
std::vector<ServerInfo> mServerList;
int mLatency;
long mLatency;
double mUploadSpeed;
double mDownloadSpeed;
float mMinSupportedServer;
Expand Down
79 changes: 0 additions & 79 deletions SpeedTestClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,85 +221,6 @@ bool SpeedTestClient::mkSocket() {
return ::connect(mSocketFd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) >= 0;
}

bool SpeedTestClient::ploss(const int size, const int wait_millisec, int &nploss) {
connect();
auto hostp = hostport();
struct sockaddr_in serveraddr;
int sockfd_udp = socket(AF_INET, SOCK_DGRAM, 0);

if (sockfd_udp < 0){
std::cerr << "ERROR UDP opening socket" << std::endl;
return false;
}


auto server = gethostbyname(hostp.first.c_str());
if (server == nullptr) {
std::cerr << "ERROR, no such host as " << hostp.first << std::endl;
return false;
}

bzero((char *) &serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
bcopy(server->h_addr, (char *)&serveraddr.sin_addr.s_addr, (size_t)server->h_length);
serveraddr.sin_port = htons(hostp.second);

auto cookie = now();

std::string reply;
std::stringstream cmd;
cmd << "ME " << cookie << " 0 settings\n";

if (!SpeedTestClient::writeLine(mSocketFd, cmd.str())){
std::cerr << cmd.str() << " " << " fail" << std::endl;
return false;
}

if (SpeedTestClient::readLine(mSocketFd, reply)){
if (reply.find("YOURIP") == std::string::npos)
return false;
}


std::stringstream ss;
ss << "LOSS " << cookie;
auto payload_len = static_cast<ssize_t >(ss.str().length());
socklen_t serverlen = sizeof(serveraddr);
/* send the message to the server */
for (long i = 0; i < size; i++){
auto n = sendto(sockfd_udp, ss.str().c_str(), payload_len, 0, (const sockaddr *) &serveraddr, serverlen);
if (n != payload_len){
std::cerr << "E";
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(wait_millisec));
cmd.str("");
cmd.clear();

cmd << "PLOSS\n";

if (!SpeedTestClient::writeLine(mSocketFd, cmd.str())){
std::cerr << cmd.str() << " " << " fail" << std::endl;
return false;
}

if (!SpeedTestClient::readLine(mSocketFd, reply)){
std::cerr << cmd.str() << " " << " read fail" << std::endl;
return false;
}
size_t pos;
if ((pos = reply.find(' ')) != std::string::npos){
if (reply.substr(0, pos) == "PLOSS"){
auto packet_loss = std::atoi(reply.substr(pos + 1).c_str());
nploss = size - packet_loss;
} else {
return false;
}
} else {
return false;
}
return true;
}

float SpeedTestClient::version() {
return mServerVersion;
Expand Down
7 changes: 3 additions & 4 deletions SpeedTestClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@
#include "DataTypes.h"
class SpeedTestClient {
public:
SpeedTestClient(const ServerInfo& serverInfo);
explicit SpeedTestClient(const ServerInfo& serverInfo);
~SpeedTestClient();

bool connect();
bool ping(long &millisec);
bool upload(const long size, const long chunk_size, long &millisec);
bool download(const long size, const long chunk_size, long &millisec);
bool ploss(const int size, const int wait_millisec, int &nploss);
bool upload(long size, long chunk_size, long &millisec);
bool download(long size, long chunk_size, long &millisec);
float version();
const std::pair<std::string, int> hostport();
void close();
Expand Down

0 comments on commit 2a61d3f

Please sign in to comment.