userioman.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include "../include/userioman.h"
  2. #include <boost/algorithm/string.hpp>
  3. #include <fstream>
  4. #include <iostream>
  5. #include <vector>
  6. #include <readline/history.h>
  7. #include <readline/readline.h>
  8. using boost::asio::buffer;
  9. UserIoMan::UserIoMan(char *ipcstring) : IoMan(ipcstring) {
  10. /* setup json stuff */
  11. Json::CharReaderBuilder rbuilder;
  12. wbuilder.settings_["indentation"] = "";
  13. reader = rbuilder.newCharReader();
  14. /* initialize print command map */
  15. printmap["connect"] = &UserIoMan::printConnect;
  16. printmap["help"] = &UserIoMan::printHelp;
  17. printmap["status"] = &UserIoMan::printStatus;
  18. printmap["disconnect"] = &UserIoMan::printDisconnect;
  19. printmap["put"] = &UserIoMan::printPut;
  20. printmap["get"] = &UserIoMan::printGet;
  21. printmap["list"] = &UserIoMan::printList;
  22. printmap["version"] = &UserIoMan::printVersion;
  23. printmap["login"] = &UserIoMan::printLogin;
  24. printmap["putdata"] = &UserIoMan::printPutdata;
  25. printmap["getdata"] = &UserIoMan::printGetdata;
  26. }
  27. UserIoMan::~UserIoMan() { delete reader; }
  28. void UserIoMan::printMessage(std::string msg, OutMsgType type) {
  29. Json::Value root;
  30. msgmutex.lock();
  31. switch (type) {
  32. case normal: {
  33. // this should never happen outside of development
  34. if (!reader->parse(msg.c_str(), msg.c_str() + msg.size(), &root,
  35. &jsonerror)) {
  36. printMessage(string(__PRETTY_FUNCTION__) +
  37. " couldnt parse json data: " + jsonerror,
  38. error);
  39. } else {
  40. printJson(root);
  41. }
  42. break;
  43. }
  44. case error: {
  45. std::cout << msg << std::endl;
  46. break;
  47. }
  48. case debug: {
  49. //~ std::cerr << msg << std::endl;
  50. break;
  51. }
  52. }
  53. rl_redisplay();
  54. msgmutex.unlock();
  55. }
  56. void UserIoMan::printWelcomeMessage() {
  57. std::cout << "please enter user and password" << std::endl;
  58. }
  59. std::string UserIoMan::getCmdPrompt() { return "ccats> "; }
  60. std::string UserIoMan::getUserPrompt() { return "User: "; }
  61. std::string UserIoMan::getPassPrompt() { return "Pass: "; }
  62. void UserIoMan::printJson(Json::Value root) {
  63. map<string, void (UserIoMan::*)(Json::Value)>::iterator it =
  64. printmap.find(root["command"].asString());
  65. if (it == printmap.end()) {
  66. // this should never happen outside of development
  67. printMessage(string(__PRETTY_FUNCTION__) + " unknown command \"" +
  68. root["command"].asString() +
  69. "\".\nensure code is implemented.",
  70. error);
  71. return;
  72. }
  73. (this->*(printmap[root["command"].asString()]))(root);
  74. }
  75. void UserIoMan::printConnect(Json::Value root) {
  76. if (!root["accept"].asBool()) {
  77. std::cout << "Couldnt connect to " << root["address"].asString() << ":"
  78. << root["port"].asUInt() << std::endl
  79. << "Reason: " << root["error"].asString() << std::endl;
  80. }
  81. }
  82. void UserIoMan::printHelp(Json::Value root) {
  83. std::cout << "Available commands are: " << std::endl;
  84. for (Json::Value i : root["names"])
  85. std::cout << i.asString() << std::endl;
  86. }
  87. void UserIoMan::printStatus(Json::Value root) {
  88. std::cout << "Server reports status: " << root["response"].asString()
  89. << std::endl;
  90. }
  91. void UserIoMan::printDisconnect(Json::Value root) {
  92. if (!root["accept"].asBool()) {
  93. std::cout << "Disconnect failed." << std::endl;
  94. } else {
  95. std::cout << "Disconnect successful." << std::endl;
  96. }
  97. }
  98. void UserIoMan::printPut(Json::Value root) {
  99. if (!root["accept"].asBool()) {
  100. std::cout << "Upload request for file " << root["file"].asString()
  101. << " failed: " << root["error"].asString() << std::endl;
  102. } else
  103. std::cout << "Begin uploading file " << root["file"].asString()
  104. << std::endl;
  105. }
  106. void UserIoMan::printGet(Json::Value root) {
  107. if (!root["accept"].asBool()) {
  108. std::cout << "Download request for file " << root["file"].asString()
  109. << " failed: " << root["error"].asString() << std::endl;
  110. } else
  111. std::cout << "Begin downloading file " << root["file"].asString()
  112. << std::endl;
  113. }
  114. void UserIoMan::printList(Json::Value root) {
  115. if (!root["accept"].asBool()) {
  116. std::cout << "Listing files failed: " << root["error"].asString()
  117. << std::endl;
  118. } else {
  119. std::cout << "Listing files stored on server: " << std::endl;
  120. for (Json::Value i : root["names"])
  121. std::cout << i.asString() << std::endl;
  122. std::cout << "End of list." << std::endl;
  123. }
  124. }
  125. void UserIoMan::printVersion(Json::Value root) {
  126. if (!root["accept"].asBool()) {
  127. std::cout << "Version check failed. Server reports "
  128. << root["serverversion"].asString() << " but client is "
  129. << root["clientversion"].asString() << std::endl;
  130. } else
  131. std::cout << "Version check ok." << std::endl;
  132. }
  133. void UserIoMan::printLogin(Json::Value root) {
  134. if (!root["accept"].asBool()) {
  135. std::cout << "Login failed: " << root["error"].asString() << std::endl;
  136. } else
  137. std::cout << "Login ok." << std::endl;
  138. }
  139. void UserIoMan::printPutdata(Json::Value root) {}
  140. void UserIoMan::printGetdata(Json::Value root) {}
  141. void UserIoMan::printListdata(Json::Value root) {}