iomanager.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include "../include/iomanager.hpp"
  2. #include <iostream>
  3. using boost::asio::buffer;
  4. IoManager::IoManager(char *ipcstring) {
  5. ipstring = std::string(ipcstring);
  6. port = 1234;
  7. tcpsock = new tcp::socket(ios);
  8. wbuilder.settings_["indentation"] = "";
  9. // const std::string output = Json::writeString(wbuilder, root);
  10. reader = rbuilder.newCharReader();
  11. }
  12. bool IoManager::sendJson(Json::Value root) {
  13. boost::asio::write(*tcpsock, buffer(Json::writeString(wbuilder, root)),
  14. errcode);
  15. if (errcode) {
  16. std::cerr << "couldnt send json data" << std::endl
  17. << errcode.message() << std::endl;
  18. return false;
  19. }
  20. return true;
  21. }
  22. bool IoManager::receiveJson(boost::asio::streambuf &recvbuf) {
  23. // use transfer_at_least(1) to avoid deadlock with transfer_all()
  24. boost::asio::read(*tcpsock, recvbuf, boost::asio::transfer_at_least(1),
  25. errcode);
  26. if (errcode && errcode != boost::asio::error::eof) {
  27. std::cerr << "couldnt recieve json data" << std::endl
  28. << errcode.message() << std::endl;
  29. return false;
  30. }
  31. return true;
  32. }
  33. bool IoManager::parseJson(Json::Value *root, boost::asio::streambuf &recvbuf) {
  34. const char *recvjson;
  35. std::string jsonerror;
  36. recvjson = boost::asio::buffer_cast<const char *>(recvbuf.data());
  37. if (!reader->parse(recvjson, recvjson + recvbuf.size(), root, &jsonerror)) {
  38. std::cerr << "couldnt parse json data" << std::endl
  39. << jsonerror << std::endl;
  40. return false;
  41. }
  42. recvbuf.consume(recvbuf.size());
  43. return true;
  44. }
  45. bool IoManager::connect() {
  46. boost::asio::streambuf recvbuf;
  47. tcp::endpoint *ep;
  48. Json::Value root, checkok;
  49. bool bcheckok;
  50. ep = new tcp::endpoint(boost::asio::ip::address::from_string(ipstring), 1234);
  51. // establish connection
  52. std::cerr << "connecting to " << ipstring << std::endl;
  53. tcpsock->connect(*ep, errcode);
  54. if (errcode) {
  55. std::cerr << "couldnt connect to " << ipstring << std::endl
  56. << errcode.message() << std::endl;
  57. return false;
  58. }
  59. // send version check
  60. root["version"] = VERSION;
  61. std::cerr << root << std::endl;
  62. if (!sendJson(root))
  63. return false;
  64. // receive answer
  65. if (!receiveJson(recvbuf))
  66. return false;
  67. if (!parseJson(&root, recvbuf))
  68. return false;
  69. // dump for GUI
  70. std::cout << root << std::endl;
  71. // check if version check was ok
  72. checkok = root["accept"];
  73. if (!checkok.asBool()) {
  74. std::cerr << "version check failed. client version is " << VERSION
  75. << std::endl
  76. << "server reports version " << root["version"] << std::endl;
  77. return false;
  78. }
  79. printf("check ok\n\n\n");
  80. fflush(stdout);
  81. /* */
  82. // TODO remove hardcoded login
  83. root = Json::Value();
  84. // send version check
  85. // TODO make client version global
  86. root["user"] = "user";
  87. root["pass"] = "pass";
  88. std::cerr << root << std::endl;
  89. // send login
  90. if (!sendJson(root))
  91. return false;
  92. // receive answer to login
  93. if (!receiveJson(recvbuf))
  94. return false;
  95. if (!parseJson(&root, recvbuf))
  96. return false;
  97. // dump for GUI
  98. std::cout << root << std::endl;
  99. /* */
  100. // clean up
  101. delete ep;
  102. return true;
  103. }
  104. IoManager::~IoManager() {
  105. /* */
  106. boost::asio::streambuf recvbuf;
  107. Json::Value root, checkok;
  108. const char *recvjson;
  109. std::string jsonerror;
  110. // TODO remove hardcoded login
  111. root = Json::Value();
  112. // send version check
  113. // TODO make client version global
  114. root["command"] = "close";
  115. std::cerr << root << std::endl;
  116. // send disconnect
  117. sendJson(root);
  118. // recieve answer to disconnect
  119. receiveJson(recvbuf);
  120. parseJson(&root, recvbuf);
  121. // dump to GUI
  122. std::cout << root << std::endl;
  123. /* */
  124. tcpsock->close();
  125. delete tcpsock;
  126. delete reader;
  127. }