main.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (C) 2017 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
  3. * Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <neocomm.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. void print_help() {
  22. puts("Use `/' at the beginning to indicate a command (like in IRC).");
  23. puts(" join -- Join a channel (channel names start with `#').");
  24. puts(" leave -- Leave a channel (channel names start with `#').");
  25. puts(" stats -- Give network statistics of the local node.");
  26. puts(" exit | quit -- Close the program.");
  27. puts(" help -- Show this help information.");
  28. }
  29. int main(int argc, char *argv[]) {
  30. if(argc != 2 && argc != 4)
  31. {
  32. printf("Usage: %s <local_port> [<foreign_address> <foreign_port>]\n",
  33. argv[0]);
  34. return 1;
  35. }
  36. unsigned short port = atoi(argv[1]);
  37. char *connect_address = NULL;
  38. unsigned short connect_port = 0;
  39. if(argc == 4)
  40. {
  41. connect_address = argv[2];
  42. connect_port = atoi(argv[3]);
  43. }
  44. // initialize NeoComm framework
  45. if(!NeoComm_init(port))
  46. {
  47. fprintf(stderr, "%s\n", NeoComm_get_last_error());
  48. return 1;
  49. }
  50. // connect to an individual node
  51. if(connect_address)
  52. NeoComm_connect(connect_address, connect_port);
  53. int run = 1;
  54. while(run) {
  55. char in[128];
  56. printf("> ");
  57. scanf("%s", in);
  58. if(strcmp(in, "/exit") == 0 || strcmp(in, "/quit") == 0)
  59. run = 0;
  60. else if(strcmp(in, "/stats") == 0)
  61. {
  62. // get statistics
  63. struct NeoComm_stats statistics = NeoComm_get_node_stats();
  64. printf("Good: %u\nDubious: %u\nCached: %u\nIncoming: %u\nTotal: %u\n",
  65. statistics.good,
  66. statistics.dubious,
  67. statistics.cached,
  68. statistics.incoming,
  69. statistics.total);
  70. }
  71. else if(strstr(in, "/join") == in)
  72. {
  73. const char *chan = strchr(in, '#');
  74. if(!chan)
  75. puts("No channel name detected. Channel names start with `#'.");
  76. // join a channel
  77. NeoComm_join_channel(chan);
  78. }
  79. else if(strstr(in, "/leave") == in)
  80. {
  81. const char *chan = strchr(in, '#');
  82. if(!chan)
  83. puts("No channel name detected. Channel names start with `#'.");
  84. // leave a channel
  85. if(!NeoComm_leave_channel(chan))
  86. printf("%s\n", NeoComm_get_last_error());
  87. }
  88. else if(strcmp(in, "/help") == 0)
  89. print_help();
  90. }
  91. // deinitialize the network
  92. NeoComm_deinit();
  93. return 0;
  94. }