userioman.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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["error"] = &UserIoMan::printError;
  16. printmap["connect"] = &UserIoMan::printConnect;
  17. printmap["help"] = &UserIoMan::printHelp;
  18. printmap["status"] = &UserIoMan::printStatus;
  19. printmap["disconnect"] = &UserIoMan::printDisconnect;
  20. printmap["put"] = &UserIoMan::printPut;
  21. printmap["get"] = &UserIoMan::printGet;
  22. printmap["list"] = &UserIoMan::printList;
  23. printmap["version"] = &UserIoMan::printVersion;
  24. printmap["login"] = &UserIoMan::printLogin;
  25. printmap["signup"] = &UserIoMan::printSignup;
  26. printmap["putdata"] = &UserIoMan::printPutdata;
  27. printmap["getdata"] = &UserIoMan::printGetdata;
  28. printmap["head"] = &UserIoMan::printHead;
  29. }
  30. UserIoMan::~UserIoMan() { delete reader; }
  31. void UserIoMan::printMessage(std::string msg, OutMsgType type) {
  32. Json::Value root;
  33. msgmutex.lock();
  34. switch (type) {
  35. case normal:
  36. case error: {
  37. // this should never happen outside of development
  38. if (!reader->parse(msg.c_str(), msg.c_str() + msg.size(), &root,
  39. &jsonerror)) {
  40. printMessage(string(__PRETTY_FUNCTION__) +
  41. " couldnt parse json data: " + jsonerror,
  42. debug);
  43. } else {
  44. printJson(root);
  45. }
  46. break;
  47. }
  48. //~ case error: {
  49. //~ std::cout << msg << std::endl;
  50. //~ break;
  51. //~ }
  52. case debug: {
  53. std::cerr << msg << std::endl;
  54. break;
  55. }
  56. }
  57. rl_redisplay();
  58. msgmutex.unlock();
  59. }
  60. void UserIoMan::printWelcomeMessage() {
  61. std::cout << "please login by entering \"login <username> <password>\" \nor "
  62. "sign up and log in with \"signup <username> <password>\""
  63. << std::endl;
  64. }
  65. std::string UserIoMan::getCmdPrompt() { return "ccats> "; }
  66. // currently not in use:
  67. //~ std::string UserIoMan::getUserPrompt() { return "User: "; }
  68. //~ std::string UserIoMan::getPassPrompt() { return "Pass: "; }
  69. void UserIoMan::printJson(Json::Value root) {
  70. map<string, void (UserIoMan::*)(Json::Value)>::iterator it =
  71. printmap.find(root["command"].asString());
  72. if (it == printmap.end()) {
  73. // this should never happen outside of development
  74. printMessage(string(__PRETTY_FUNCTION__) + " unknown command \"" +
  75. root["command"].asString() +
  76. "\".\nensure code is implemented.",
  77. debug);
  78. return;
  79. }
  80. (this->*(printmap[root["command"].asString()]))(root);
  81. }
  82. void UserIoMan::printError(Json::Value root) {
  83. std::cout << "Error: " << root["error"].asString() << std::endl;
  84. }
  85. void UserIoMan::printConnect(Json::Value root) {
  86. if (!root["accept"].asBool()) {
  87. std::cout << "Couldnt connect to " << root["address"].asString() << ":"
  88. << root["port"].asUInt() << std::endl
  89. << "Reason: " << root["error"].asString() << std::endl;
  90. }
  91. }
  92. void UserIoMan::printHelp(Json::Value root) {
  93. std::cout << "Available commands are: " << std::endl;
  94. for (Json::Value i : root["names"])
  95. std::cout << i.asString() << std::endl;
  96. }
  97. void UserIoMan::printStatus(Json::Value root) {
  98. std::cout << "Server reports status: " << root["response"].asString()
  99. << std::endl;
  100. }
  101. void UserIoMan::printDisconnect(Json::Value root) {
  102. if (!root["accept"].asBool()) {
  103. std::cout << "Disconnect failed." << std::endl;
  104. } else {
  105. std::cout << "Disconnect successful." << std::endl;
  106. }
  107. }
  108. void UserIoMan::printPut(Json::Value root) {
  109. if (!root["accept"].asBool()) {
  110. if (root.isMember("file")) {
  111. std::cout << "Upload request for file " << root["file"].asString()
  112. << " failed: " << root["error"].asString() << std::endl;
  113. } else {
  114. std::cout << "Upload request failed: " << root["error"].asString()
  115. << std::endl;
  116. }
  117. } else
  118. std::cout << "Begin uploading file " << root["file"].asString()
  119. << std::endl;
  120. }
  121. void UserIoMan::printGet(Json::Value root) {
  122. if (!root["accept"].asBool()) {
  123. if (root.isMember("file")) {
  124. std::cout << "Download request for file " << root["file"].asString()
  125. << " failed: " << root["error"].asString() << std::endl;
  126. } else {
  127. std::cout << "Download request failed: " << root["error"].asString()
  128. << std::endl;
  129. }
  130. } else
  131. std::cout << "Begin downloading file " << root["file"].asString()
  132. << std::endl;
  133. }
  134. void UserIoMan::printList(Json::Value root) {
  135. if (!root["accept"].asBool()) {
  136. std::cout << "Listing files failed: " << root["error"].asString()
  137. << std::endl;
  138. } else {
  139. std::cout << "Listing files stored on server: " << std::endl;
  140. for (Json::Value i : root["names"])
  141. std::cout << i.asString() << std::endl;
  142. std::cout << "End of list." << std::endl;
  143. }
  144. }
  145. void UserIoMan::printVersion(Json::Value root) {
  146. if (!root["accept"].asBool()) {
  147. std::cout << "Version check failed. Server reports "
  148. << root["serverversion"].asString() << " but client is "
  149. << root["clientversion"].asString() << std::endl;
  150. } else
  151. std::cout << "Version check ok." << std::endl;
  152. }
  153. void UserIoMan::printLogin(Json::Value root) {
  154. if (!root["accept"].asBool()) {
  155. std::cout << "Login failed: " << root["error"].asString() << std::endl;
  156. } else
  157. std::cout << "Login ok." << std::endl;
  158. }
  159. void UserIoMan::printSignup(Json::Value root) {
  160. if (!root["accept"].asBool()) {
  161. std::cout << "Signup failed: " << root["error"].asString() << std::endl;
  162. } else
  163. std::cout << "Signup ok. You are now logged in." << std::endl;
  164. }
  165. void UserIoMan::printPutdata(Json::Value root) {}
  166. void UserIoMan::printGetdata(Json::Value root) {}
  167. void UserIoMan::printListdata(Json::Value root) {}
  168. void UserIoMan::printHead(Json::Value root) {
  169. if (!root["accept"].asBool())
  170. std::cout << "Request of the first four bytes failed, server reports: "
  171. << root["error"].asString() << std::endl;
  172. else
  173. std::cout << "First four bytes of file " << root["file"].asString()
  174. << " are: " << root["data"].asString() << std::endl;
  175. }