|
@@ -1,19 +1,77 @@
|
|
|
-#include "../include/iomanager.h"
|
|
|
+#include "../include/netman.h"
|
|
|
|
|
|
+#include <boost/asio.hpp>
|
|
|
#include <iostream>
|
|
|
|
|
|
+using boost::asio::ip::tcp;
|
|
|
using boost::asio::buffer;
|
|
|
|
|
|
-IoManager::IoManager(char *ipcstring) {
|
|
|
+NetMan::NetMan(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();
|
|
|
+ connected = false;
|
|
|
}
|
|
|
|
|
|
-bool IoManager::sendJson(Json::Value root) {
|
|
|
+void NetMan::connect() {
|
|
|
+ tcp::endpoint *ep;
|
|
|
+
|
|
|
+ ep = new tcp::endpoint(boost::asio::ip::address::from_string(ipstring), 1234);
|
|
|
+
|
|
|
+ // establish connection
|
|
|
+ std::cerr << "NetMan::connect() connecting to " << ipstring << std::endl;
|
|
|
+ tcpsock->connect(*ep, errcode);
|
|
|
+ if (errcode) {
|
|
|
+ delete ep;
|
|
|
+ throw "NetMan::connect() couldnt connect to " + ipstring + "\n" + errcode.message() + "\n";
|
|
|
+ //~ std::cerr << "couldnt connect to " << ipstring << std::endl
|
|
|
+ //~ << errcode.message() << std::endl;
|
|
|
+ //~ return false;
|
|
|
+ }
|
|
|
+ connected = true;
|
|
|
+ delete ep;
|
|
|
+}
|
|
|
+
|
|
|
+void NetMan::disconnect() {
|
|
|
+ std::cerr << "NetMan::disconnect()" << std::endl;
|
|
|
+ tcpsock->close();
|
|
|
+}
|
|
|
+
|
|
|
+void NetMan::sendJsonString(std::string msg) {
|
|
|
+ std::cerr << "NetMan::sendJsonString() " << msg << std::endl;
|
|
|
+ boost::asio::write(*tcpsock, buffer(msg + "\n"), errcode);
|
|
|
+ if (errcode) {
|
|
|
+ throw "NetMan::sendJsonString() couldnt send json data\n" + errcode.message() + "\n";
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+std::string NetMan::receiveJsonString() {
|
|
|
+ std::string retstring;
|
|
|
+ std::cerr << "NetMan::receiveJsonString()" << std::endl;
|
|
|
+ // use transfer_at_least(1) to avoid deadlock with transfer_all()
|
|
|
+ boost::asio::read_until(*tcpsock, recvbuf, '\n', errcode);
|
|
|
+ if (errcode && errcode != boost::asio::error::eof) {
|
|
|
+ throw "NetMan::receiveJsonString() couldnt read json data\n" + errcode.message() + "\n";
|
|
|
+ }
|
|
|
+ retstring = boost::asio::buffer_cast<const char *>(recvbuf.data());
|
|
|
+ std::cerr << retstring << std::endl;
|
|
|
+ return retstring;
|
|
|
+}
|
|
|
+
|
|
|
+bool NetMan::isConnected() {
|
|
|
+ return connected;
|
|
|
+}
|
|
|
+
|
|
|
+NetMan::~NetMan() {
|
|
|
+ if(connected) {
|
|
|
+ disconnect();
|
|
|
+ }
|
|
|
+ delete tcpsock;
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+
|
|
|
+bool IoMan::sendJson(Json::Value root) {
|
|
|
boost::asio::write(*tcpsock, buffer(Json::writeString(wbuilder, root)),
|
|
|
errcode);
|
|
|
if (errcode) {
|
|
@@ -24,7 +82,7 @@ bool IoManager::sendJson(Json::Value root) {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
-bool IoManager::receiveJson(boost::asio::streambuf &recvbuf) {
|
|
|
+bool IoMan::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);
|
|
@@ -36,20 +94,8 @@ bool IoManager::receiveJson(boost::asio::streambuf &recvbuf) {
|
|
|
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() {
|
|
|
+bool IoMan::connect() {
|
|
|
boost::asio::streambuf recvbuf;
|
|
|
tcp::endpoint *ep;
|
|
|
Json::Value root, checkok;
|
|
@@ -95,7 +141,6 @@ bool IoManager::connect() {
|
|
|
printf("check ok\n\n\n");
|
|
|
fflush(stdout);
|
|
|
|
|
|
- /* */
|
|
|
// TODO remove hardcoded login
|
|
|
root = Json::Value();
|
|
|
// send version check
|
|
@@ -116,7 +161,6 @@ bool IoManager::connect() {
|
|
|
|
|
|
// dump for GUI
|
|
|
std::cout << root << std::endl;
|
|
|
- /* */
|
|
|
|
|
|
// clean up
|
|
|
delete ep;
|
|
@@ -124,9 +168,7 @@ bool IoManager::connect() {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
-IoManager::~IoManager() {
|
|
|
-
|
|
|
- /* */
|
|
|
+IoMan::~IoMan() {
|
|
|
|
|
|
boost::asio::streambuf recvbuf;
|
|
|
Json::Value root, checkok;
|
|
@@ -149,9 +191,10 @@ IoManager::~IoManager() {
|
|
|
|
|
|
// dump to GUI
|
|
|
std::cout << root << std::endl;
|
|
|
- /* */
|
|
|
|
|
|
tcpsock->close();
|
|
|
delete tcpsock;
|
|
|
delete reader;
|
|
|
}
|
|
|
+
|
|
|
+*/
|