Ver Fonte

add network capabilities to cli

Missingmew há 5 anos atrás
pai
commit
efd24b4681
1 ficheiros alterados com 63 adições e 13 exclusões
  1. 63 13
      cli/src/main.cpp

+ 63 - 13
cli/src/main.cpp

@@ -1,4 +1,5 @@
 #include <boost/program_options.hpp>
+#include <boost/asio.hpp>
 #include <cctype>
 #include <iostream>
 
@@ -6,6 +7,8 @@
 #define sizeofarr(a) (sizeof(a) / sizeof(a[0]))
 
 namespace bpo = boost::program_options;
+namespace bai = boost::asio;
+using bai::ip::tcp;
 
 typedef enum {
   CMD_HELP,
@@ -37,6 +40,10 @@ CMD commands[]{{CMD_HELP, "help", "show help"},
                {CMD_SETUP, "setup", "configure server at IP"},
                {CMD_LOG, "log", "show log from IP"}};
 
+/*  takes a string in str and checks if it is a valid command
+    returns the matching COMMANDTYPES or CMD_UNKNOWN
+    if isshort is nonzero, only the first character of str will be checked
+*/
 COMMANDTYPES getCommand(char *str, unsigned int isshort) {
   COMMANDTYPES ret = CMD_UNKNOWN;
   char temp[COMMANDLEN + 1] = {0};
@@ -74,11 +81,18 @@ int main(int argc, char **argv) {
   bpo::options_description desc{"Options"};
   desc.add_options()("cooloption", "Cooloption to use with command FOON");
   bpo::variables_map vm;
+  bai::io_service ios;
+  tcp::socket socket(ios);
+  boost::system::error_code err;
+  bai::streambuf recvbuf;
 
+  // handle no command
   if (argc < 2) {
     show_help(argv[0]);
     exit(1);
   }
+  
+  // identify command, print error message if unknown
   COMMANDTYPES cmd = getCommand(argv[1], (strlen(argv[1]) == 1));
   switch (cmd) {
   case CMD_UNKNOWN:
@@ -93,30 +107,66 @@ int main(int argc, char **argv) {
     show_help(argv[0]);
     exit(1);
   }
+  
+  // parse additional arguments, possibly drop this in the future for full
+  // interactivity
+  try {
+    store(parse_command_line(argc - 2, argv + 2, desc), vm);
+    notify(vm);
+
+    if (vm.count("help")) {
+      std::cout << desc;
+    } else {
+      std::printf("no additional options\n");
+    }
+  } catch (const bpo::error &ex) {
+    std::fprintf(stderr, "%s\n", ex.what());
+  }
 
-  // have enough valid arguments
+  // have enough valid arguments, work through them
   switch (cmd) {
   case CMD_CONNECT:
     std::printf("connecting to %s\n", argv[2]);
+    socket.connect(tcp::endpoint(bai::ip::address::from_string(argv[2]), 1234));
+    std::printf("connected to %s - sending hello\n", argv[2]);
+    bai::write(socket, bai::buffer("Client CONNECT test\n"), err);
+    if(!err) {
+	    std::printf("OK: sent message\n");
+    }
+    else {
+	    std::printf("ERR: couldnt send message: %s\n", err.message());
+    }
+    bai::read(socket, recvbuf, bai::transfer_all(), err);
+    if(err && err != bai::error::eof) {
+	    std::printf("ERR: couldnt recieve message: %s\n", err.message());
+    }
+    else {
+	    std::printf("OK: recieved message %s\n", bai::buffer_cast<const char*>(recvbuf.data()));
+    }
     break;
   case CMD_DISCONNECT:
     std::printf("disconnecting from %s\n", argv[2]);
+    socket.connect(tcp::endpoint(bai::ip::address::from_string(argv[2]), 1234));
+    std::printf("connected to %s - sending goodbye\n", argv[2]);
+    bai::write(socket, bai::buffer("Client DISCONNECT test\n"), err);
+    if(!err) {
+	    std::printf("OK: sent message\n");
+    }
+    else {
+	    std::printf("ERR: couldnt send message: %s\n", err.message());
+    }
+    bai::read(socket, recvbuf, bai::transfer_all(), err);
+    if(err && err != bai::error::eof) {
+	    std::printf("ERR: couldnt recieve message: %s\n", err.message());
+    }
+    else {
+	    std::printf("OK: recieved message %s\n", bai::buffer_cast<const char*>(recvbuf.data()));
+    }
     break;
   default:
     std::printf("command %s not implemented\n", argv[1]);
     break;
   }
 
-  try {
-    store(parse_command_line(argc - 2, argv + 2, desc), vm);
-    notify(vm);
-
-    if (vm.count("help")) {
-      std::cout << desc;
-    } else {
-      std::printf("no additional options\n");
-    }
-  } catch (const bpo::error &ex) {
-    std::fprintf(stderr, "%s\n", ex.what());
-  }
+  std::printf("done\n");
 }