Skip to content

Commit

Permalink
Enhance: Provide list of available speed test servers taganaka#63
Browse files Browse the repository at this point in the history
  • Loading branch information
mudassaralichouhan committed Jun 23, 2024
1 parent b743996 commit 7a56c17
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
7 changes: 6 additions & 1 deletion CmdOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ typedef struct program_options_t {
bool upload = false;
bool share = false;
bool insecure = false;
bool list_servers = false;
std::string selected_server = "";
OutputType output_type = OutputType::verbose;
} ProgramOptions;
Expand All @@ -27,12 +28,13 @@ static struct option CmdLongOptions[] = {
{"upload", no_argument, 0, 'u' },
{"share", no_argument, 0, 's' },
{"insecure", no_argument, 0, 'i' },
{"list-servers",no_argument, 0, 'L' },
{"test-server", required_argument, 0, 't' },
{"output", required_argument, 0, 'o' },
{0, 0, 0, 0 }
};

const char *optStr = "hldusiqt:o:";
const char *optStr = "hldusiqLt:o:";

bool ParseOptions(const int argc, const char **argv, ProgramOptions& options){
int long_index =0;
Expand All @@ -57,6 +59,9 @@ bool ParseOptions(const int argc, const char **argv, ProgramOptions& options){
case 'i':
options.insecure = true;
break;
case 'L':
options.list_servers = true;
break;
case 't':
options.selected_server.append(optarg);
break;
Expand Down
27 changes: 27 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ void usage(const char* name){
std::cerr << " --upload Perform upload test only. It includes latency test\n";
std::cerr << " --share Generate and provide a URL to the speedtest.net share results image\n";
std::cerr << " --insecure Skip SSL certificate verify (Useful for Embedded devices)\n";
std::cerr << " --list-servers List available speed test servers\n";
std::cerr << " --test-server host:port Run speed test against a specific server\n";
std::cerr << " --quality-server host:port Run line quality test against a specific server\n";
std::cerr << " --output verbose|text|json Set output type. Default: verbose\n";
Expand Down Expand Up @@ -55,6 +56,32 @@ int main(const int argc, const char **argv) {


auto sp = SpeedTest(SPEED_TEST_MIN_SERVER_VERSION);

if (programOptions.list_servers) {
if (programOptions.output_type == OutputType::json) {
std::cout << "[";
const auto &servers = sp.serverList();
bool first = true;
for (const auto &server : servers) {
if (!first) {
std::cout << ",";
}
std::cout << "{\"id\":\"" << server.id << "\",\"host\":\"" << server.host
<< "\",\"country\":\"" << server.country << "\",\"sponsor\":\"" << server.sponsor << "\"}";
first = false;
}
std::cout << "]" << std::endl;
} else {
const auto &servers = sp.serverList();
for (const auto &server : servers) {
std::cout << "ID: " << server.id << ", Host: " << server.host
<< ", Country: " << server.country << ", Sponsor: " << server.sponsor << std::endl;
}
}

return EXIT_SUCCESS;
}

IPInfo info;
ServerInfo serverInfo;
ServerInfo serverQualityInfo;
Expand Down

0 comments on commit 7a56c17

Please sign in to comment.