neocomm/demo/main.c

102 lines
2.9 KiB
C
Raw Normal View History

2017-11-05 15:53:45 +00:00
/*
* 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/>.
2017-09-25 14:32:12 +00:00
*/
2017-10-03 15:09:18 +00:00
#include <neocomm.h>
2017-11-05 15:53:45 +00:00
#include <stdio.h>
2017-11-08 01:25:35 +00:00
#include <string.h>
2017-11-05 15:53:45 +00:00
void print_help() {
puts("Use `/' at the beginning to indicate a command (like in IRC).");
puts(" join -- Join a channel (channel names start with `#').");
puts(" leave -- Leave a channel (channel names start with `#').");
puts(" stats -- Give network statistics of the local node.");
puts(" exit | quit -- Close the program.");
puts(" help -- Show this help information.");
}
2017-11-05 16:27:05 +00:00
int main(int argc, char *argv[]) {
if(argc != 2 && argc != 4)
{
2017-11-08 00:54:14 +00:00
printf("Usage: %s <local_port> [<foreign_address> <foreign_port>]\n",
argv[0]);
2017-11-05 16:27:05 +00:00
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]);
}
2017-11-05 15:53:45 +00:00
2017-11-20 12:55:06 +00:00
// initialize NeoComm framework
2017-11-05 15:53:45 +00:00
if(!NeoComm_init(port))
{
2017-11-05 16:27:05 +00:00
fprintf(stderr, "%s\n", NeoComm_get_last_error());
2017-11-05 15:53:45 +00:00
return 1;
}
2017-11-20 12:55:06 +00:00
// connect to an individual node
2017-11-05 16:27:05 +00:00
if(connect_address)
NeoComm_connect(connect_address, connect_port);
int run = 1;
while(run) {
2017-11-08 01:25:35 +00:00
char in[128];
printf("> ");
scanf("%s", in);
if(strcmp(in, "/exit") == 0 || strcmp(in, "/quit") == 0)
run = 0;
2017-11-08 01:25:35 +00:00
else if(strcmp(in, "/stats") == 0)
2017-11-10 15:26:34 +00:00
{
2017-11-20 12:55:06 +00:00
// get statistics
2017-11-10 15:26:34 +00:00
struct stats statistics = NeoComm_get_node_stats();
2017-11-10 15:34:36 +00:00
printf("Good: %u\nDubious: %u\nCached: %u\nIncoming: %u\nTotal: %u\n",
2017-11-10 15:26:34 +00:00
statistics.good,
statistics.dubious,
statistics.cached,
2017-11-10 15:34:36 +00:00
statistics.incoming,
statistics.total);
2017-11-10 15:26:34 +00:00
}
2017-11-17 16:18:46 +00:00
else if(strstr(in, "/join") == in)
{
const char *chan = strchr(in, '#');
if(!chan)
puts("No channel name detected. Channel names start with `#'.");
2017-11-20 12:55:06 +00:00
// join a channel
2017-11-17 16:18:46 +00:00
NeoComm_join_channel(chan);
}
else if(strstr(in, "/leave") == in)
{
const char *chan = strchr(in, '#');
if(!chan)
puts("No channel name detected. Channel names start with `#'.");
2017-11-20 12:55:06 +00:00
// leave a channel
2017-11-17 16:18:46 +00:00
if(!NeoComm_leave_channel(chan))
printf("%s\n", NeoComm_get_last_error());
}
else if(strcmp(in, "/help") == 0)
print_help();
2017-11-08 01:25:35 +00:00
}
2017-11-05 15:53:45 +00:00
2017-11-20 12:55:06 +00:00
// deinitialize the network
2017-11-05 15:53:45 +00:00
NeoComm_deinit();
2017-10-03 15:09:18 +00:00
return 0;
}