neocomm/demo/main.c
Nicolás Ortega Froysa 64fbe3ec9c
Fixed statistics count.
2017-11-10 16:26:34 +01:00

81 lines
2.3 KiB
C

/*
* Copyright (C) 2017 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <neocomm.h>
#include <stdio.h>
#include <string.h>
void print_help() {
puts("Use `/' at the beginning to indicate a command (like in IRC).");
puts(" stats -- Give network statistics of the local node.");
puts(" num_nodes -- Get total number of nodes.");
puts(" exit | quit -- Close the program.");
puts(" help -- Show this help information.");
}
int main(int argc, char *argv[]) {
if(argc != 2 && argc != 4)
{
printf("Usage: %s <local_port> [<foreign_address> <foreign_port>]\n",
argv[0]);
return 1;
}
unsigned short port = atoi(argv[1]);
char *connect_address = NULL;
unsigned short connect_port = 0;
if(argc == 4)
{
connect_address = argv[2];
connect_port = atoi(argv[3]);
}
if(!NeoComm_init(port))
{
fprintf(stderr, "%s\n", NeoComm_get_last_error());
return 1;
}
if(connect_address)
NeoComm_connect(connect_address, connect_port);
int run = 1;
while(run) {
char in[128];
printf("> ");
scanf("%s", in);
if(strcmp(in, "/exit") == 0 || strcmp(in, "/quit") == 0)
run = 0;
else if(strcmp(in, "/stats") == 0)
{
struct stats statistics = NeoComm_get_node_stats();
printf("Good: %u\nDubious: %u\nCached: %u\nIncoming: %u\n",
statistics.good,
statistics.dubious,
statistics.cached,
statistics.incoming);
}
else if(strcmp(in, "/num_nodes") == 0)
printf("%u", NeoComm_count_connected_nodes());
else if(strcmp(in, "/help") == 0)
print_help();
}
NeoComm_deinit();
return 0;
}