useriomanager.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "../include/useriomanager.hpp"
  2. #include "../include/commands.hpp"
  3. #include <iostream>
  4. #include <json/json.h>
  5. #include <readline/history.h>
  6. #include <readline/readline.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)),
  36. errcode);
  37. if (errcode) {
  38. std::cerr << "couldnt send status query to server " << ipstring
  39. << std::endl
  40. << errcode.message() << std::endl;
  41. keep_reading = false;
  42. continue;
  43. }
  44. // recieve answer to status query
  45. boost::asio::read(*tcpsock, recvbuf, boost::asio::transfer_at_least(1),
  46. errcode);
  47. if (errcode && errcode != boost::asio::error::eof) {
  48. std::cerr << "couldnt recieve status from " << ipstring << std::endl
  49. << errcode.message() << std::endl;
  50. keep_reading = false;
  51. continue;
  52. }
  53. // parse json
  54. recvjson = boost::asio::buffer_cast<const char *>(recvbuf.data());
  55. if (!reader->parse(recvjson, recvjson + recvbuf.size(), &root,
  56. &jsonerror)) {
  57. std::cerr << "couldnt parse recieved json" << std::endl
  58. << jsonerror << std::endl;
  59. keep_reading = false;
  60. continue;
  61. }
  62. // remove processed data from recvbuf
  63. recvbuf.consume(recvbuf.size());
  64. // check if received answer to right query
  65. checkok = root["command"];
  66. if (checkok.asString().compare("status") != 0) {
  67. std::cerr << "command check failed. client sent command \"status\""
  68. << std::endl
  69. << "server replied to command \"" << checkok << "\""
  70. << std::endl;
  71. keep_reading = false;
  72. continue;
  73. } else {
  74. std::cout << "server replied with status: " << root["response"]
  75. << std::endl;
  76. }
  77. break;
  78. }
  79. case CMD_DISCONNECT: {
  80. std::printf("disconnecting\n");
  81. keep_reading = false;
  82. break;
  83. }
  84. default: {
  85. std::printf("unknown command %s\n", line);
  86. }
  87. case CMD_HELP: {
  88. std::printf("\n");
  89. std::printf("AVAILABLE COMMANDS are:\n");
  90. // printCmds();
  91. std::printf("help - shows this help\n");
  92. std::printf("status - asks the server for status\n");
  93. std::printf("disconnect - closes the connection to the server\n");
  94. break;
  95. }
  96. }
  97. std::printf("\n");
  98. }
  99. free(line);
  100. return;
  101. }