#include "../include/iomanager.hpp" #include using boost::asio::buffer; IoManager::IoManager(char *ipcstring) { ipstring = std::string(ipcstring); port = 1234; tcpsock = new tcp::socket(ios); wbuilder.settings_["indentation"] = ""; // const std::string output = Json::writeString(wbuilder, root); reader = rbuilder.newCharReader(); } bool IoManager::sendJson(Json::Value root) { boost::asio::write(*tcpsock, buffer(Json::writeString(wbuilder, root)), errcode); if (errcode) { std::cerr << "couldnt send json data" << std::endl << errcode.message() << std::endl; return false; } return true; } bool IoManager::receiveJson(boost::asio::streambuf &recvbuf) { // use transfer_at_least(1) to avoid deadlock with transfer_all() boost::asio::read(*tcpsock, recvbuf, boost::asio::transfer_at_least(1), errcode); if (errcode && errcode != boost::asio::error::eof) { std::cerr << "couldnt recieve json data" << std::endl << errcode.message() << std::endl; return false; } return true; } bool IoManager::parseJson(Json::Value *root, boost::asio::streambuf &recvbuf) { const char *recvjson; std::string jsonerror; recvjson = boost::asio::buffer_cast(recvbuf.data()); if (!reader->parse(recvjson, recvjson + recvbuf.size(), root, &jsonerror)) { std::cerr << "couldnt parse json data" << std::endl << jsonerror << std::endl; return false; } recvbuf.consume(recvbuf.size()); return true; } bool IoManager::connect() { boost::asio::streambuf recvbuf; tcp::endpoint *ep; Json::Value root, checkok; bool bcheckok; ep = new tcp::endpoint(boost::asio::ip::address::from_string(ipstring), 1234); // establish connection std::cerr << "connecting to " << ipstring << std::endl; tcpsock->connect(*ep, errcode); if (errcode) { std::cerr << "couldnt connect to " << ipstring << std::endl << errcode.message() << std::endl; return false; } // send version check root["version"] = VERSION; std::cerr << root << std::endl; if (!sendJson(root)) return false; // receive answer if (!receiveJson(recvbuf)) return false; if (!parseJson(&root, recvbuf)) return false; // dump for GUI std::cout << root << std::endl; // check if version check was ok checkok = root["accept"]; if (!checkok.asBool()) { std::cerr << "version check failed. client version is " << VERSION << std::endl << "server reports version " << root["version"] << std::endl; return false; } printf("check ok\n\n\n"); fflush(stdout); /* */ // TODO remove hardcoded login root = Json::Value(); // send version check // TODO make client version global root["user"] = "user"; root["pass"] = "pass"; std::cerr << root << std::endl; // send login if (!sendJson(root)) return false; // receive answer to login if (!receiveJson(recvbuf)) return false; if (!parseJson(&root, recvbuf)) return false; // dump for GUI std::cout << root << std::endl; /* */ // clean up delete ep; return true; } IoManager::~IoManager() { /* */ boost::asio::streambuf recvbuf; Json::Value root, checkok; const char *recvjson; std::string jsonerror; // TODO remove hardcoded login root = Json::Value(); // send version check // TODO make client version global root["command"] = "close"; std::cerr << root << std::endl; // send disconnect sendJson(root); // recieve answer to disconnect receiveJson(recvbuf); parseJson(&root, recvbuf); // dump to GUI std::cout << root << std::endl; /* */ tcpsock->close(); delete tcpsock; delete reader; }