123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- #include "../include/iomanager.hpp"
- #include <iostream>
- 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<const char *>(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;
- }
|