useriomanager.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "../include/useriomanager.hpp"
  2. #include "../include/commands.hpp"
  3. #include <iostream>
  4. #include <jsoncpp/json/json.h>
  5. #include <readline/readline.h>
  6. #include <readline/history.h>
  7. using boost::asio::buffer;
  8. void UserIoManager::run() {
  9. std::cout << "UserIoManager::run() says hello!" << std::endl;
  10. bool keep_reading = true;
  11. COMMANDID cmd;
  12. char *line = NULL;
  13. while(keep_reading){
  14. free(line);
  15. // read an input line
  16. line = readline ("ccats> ");
  17. // if no input, do not add to history, and go to reading next line
  18. if (strlen(line) == 0) {
  19. continue;
  20. }
  21. // add the line to history
  22. add_history(line);
  23. cmd = getCmdIdFromString(line);
  24. switch(cmd) {
  25. case CMD_STATUS: {
  26. boost::asio::streambuf recvbuf;
  27. Json::Value root, checkok;
  28. const char *recvjson;
  29. std::string jsonerror;
  30. Json::CharReaderBuilder rbuilder;
  31. const std::unique_ptr<Json::CharReader> reader(rbuilder.newCharReader());
  32. Json::StreamWriterBuilder wbuilder;
  33. // ask for status
  34. root["command"] = "status";
  35. boost::asio::write(*tcpsock, buffer(Json::writeString(wbuilder, root)), errcode);
  36. if(errcode) {
  37. std::cerr << "couldnt send status query to server " << ipstring << std::endl
  38. << errcode.message() << std::endl;
  39. keep_reading = false;
  40. continue;
  41. }
  42. // recieve answer to status query
  43. boost::asio::read(*tcpsock, recvbuf, boost::asio::transfer_at_least(1), errcode);
  44. if (errcode && errcode != boost::asio::error::eof) {
  45. std::cerr << "couldnt recieve status from " << ipstring << std::endl
  46. << errcode.message() << std::endl;
  47. keep_reading = false;
  48. continue;
  49. }
  50. // parse json
  51. recvjson = boost::asio::buffer_cast<const char *>(recvbuf.data());
  52. if (!reader->parse(recvjson, recvjson + recvbuf.size(), &root, &jsonerror)) {
  53. std::cerr << "couldnt parse recieved json" << std::endl
  54. << jsonerror << std::endl;
  55. keep_reading = false;
  56. continue;
  57. }
  58. // remove processed data from recvbuf
  59. recvbuf.consume(recvbuf.size());
  60. // check if received answer to right query
  61. checkok = root["command"];
  62. if(checkok.asString().compare("status") != 0) {
  63. std::cerr << "command check failed. client sent command \"status\"" << std::endl
  64. << "server replied to command \"" << checkok << "\"" << std::endl;
  65. keep_reading = false;
  66. continue;
  67. } else {
  68. std::cout << "server replied with status: " << root["response"] << std::endl;
  69. }
  70. break;
  71. }
  72. case CMD_DISCONNECT: {
  73. std::printf("disconnecting\n");
  74. keep_reading = false;
  75. break;
  76. }
  77. default: {
  78. std::printf("unknown command %s\n", line);
  79. }
  80. case CMD_HELP: {
  81. std::printf("\n");
  82. std::printf("AVAILABLE COMMANDS are:\n");
  83. // printCmds();
  84. std::printf("help - shows this help\n");
  85. std::printf("status - asks the server for status\n");
  86. std::printf("disconnect - closes the connection to the server\n");
  87. break;
  88. }
  89. }
  90. std::printf("\n");
  91. }
  92. free(line);
  93. return;
  94. }