|
@@ -1,5 +1,6 @@
|
|
#include "../include/ioman.h"
|
|
#include "../include/ioman.h"
|
|
|
|
|
|
|
|
+#include <boost/asio.hpp>
|
|
#include <iostream>
|
|
#include <iostream>
|
|
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
#include <boost/algorithm/string.hpp>
|
|
@@ -12,41 +13,96 @@
|
|
using std::string;
|
|
using std::string;
|
|
using std::vector;
|
|
using std::vector;
|
|
|
|
|
|
|
|
+using boost::asio::ip::tcp;
|
|
using boost::asio::buffer;
|
|
using boost::asio::buffer;
|
|
|
|
|
|
-IoMan::IoMan(char *ipcstring) : netman(ipcstring),cmdman(fileman) {
|
|
|
|
|
|
+IoMan::IoMan(char *ipcstring) : cmdman(fileman) {
|
|
|
|
+ ipstring = std::string(ipcstring);
|
|
|
|
+ port = 1234;
|
|
|
|
+ tcpsock = new tcp::socket(ios);
|
|
|
|
+ connected = false;
|
|
}
|
|
}
|
|
|
|
|
|
IoMan::~IoMan() {
|
|
IoMan::~IoMan() {
|
|
|
|
+ if(connected) {
|
|
|
|
+ disconnect();
|
|
|
|
+ }
|
|
|
|
+ delete tcpsock;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+bool IoMan::connect() {
|
|
|
|
+ tcp::endpoint *ep;
|
|
|
|
+
|
|
|
|
+ ep = new tcp::endpoint(boost::asio::ip::address::from_string(ipstring), 1234);
|
|
|
|
+
|
|
|
|
+ // establish connection
|
|
|
|
+ std::cerr << "IoMan::connect() connecting to " << ipstring << std::endl;
|
|
|
|
+ tcpsock->connect(*ep, errcode);
|
|
|
|
+ if (errcode) {
|
|
|
|
+ delete ep;
|
|
|
|
+ printErrorMessage("IoMan::connect() couldnt connect to " + ipstring + "\n" + errcode.message() + "\n");
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ connected = true;
|
|
|
|
+ delete ep;
|
|
|
|
+ return true;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+void IoMan::disconnect() {
|
|
|
|
+ std::cerr << "IoMan::disconnect()" << std::endl;
|
|
|
|
+ tcpsock->close();
|
|
|
|
+ connected = false;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+bool IoMan::sendJsonString(std::string msg) {
|
|
|
|
+ std::cerr << "IoMan::sendJsonString() " << msg << std::endl;
|
|
|
|
+ boost::asio::write(*tcpsock, buffer(msg + "\n"), errcode);
|
|
|
|
+ if (errcode) {
|
|
|
|
+ printErrorMessage("IoMan::sendJsonString() couldnt send json data\n" + errcode.message() + "\n");
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ return true;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+std::string IoMan::receiveJsonString() {
|
|
|
|
+ std::string retstring;
|
|
|
|
+ std::cerr << "IoMan::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) {
|
|
|
|
+ printErrorMessage("IoMan::receiveJsonString() couldnt read json data\n" + errcode.message() + "\n");
|
|
|
|
+ }
|
|
|
|
+ retstring = boost::asio::buffer_cast<const char *>(recvbuf.data());
|
|
|
|
+ recvbuf.consume(recvbuf.size());
|
|
|
|
+ std::cerr << "IoMan::receiveJsonString(): returning string \"" << retstring << "\"" << std::endl;
|
|
|
|
+ return retstring;
|
|
}
|
|
}
|
|
|
|
|
|
-//~ bool IoMan::parseJson(Json::Value *root, string jsonstring) {
|
|
|
|
- //~ const char *recvjson;
|
|
|
|
- //~ std::string jsonerror;
|
|
|
|
- //~ recvjson = jsonstring.c_str();
|
|
|
|
- //~ if (!reader->parse(recvjson, recvjson + jsonstring.size(), root, &jsonerror)) {
|
|
|
|
- //~ std::cerr << "couldnt parse json data" << std::endl
|
|
|
|
- //~ << jsonerror << std::endl;
|
|
|
|
- //~ return false;
|
|
|
|
- //~ }
|
|
|
|
- //~ recvbuf.consume(recvbuf.size());
|
|
|
|
- //~ return true;
|
|
|
|
-//~ }
|
|
|
|
|
|
+bool IoMan::parseJson(Json::Value *root, string jsonstring) {
|
|
|
|
+ const char *recvjson;
|
|
|
|
+ std::string jsonerror;
|
|
|
|
+ recvjson = jsonstring.c_str();
|
|
|
|
+ if (!cmdman.reader->parse(recvjson, recvjson + jsonstring.size(), root, &jsonerror)) {
|
|
|
|
+ printErrorMessage("couldnt parse json data: " + jsonerror + "\n");
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ return true;
|
|
|
|
+}
|
|
|
|
|
|
bool IoMan::init() {
|
|
bool IoMan::init() {
|
|
CmdMan::CmdRet ret;
|
|
CmdMan::CmdRet ret;
|
|
string work;
|
|
string work;
|
|
Json::Value root;
|
|
Json::Value root;
|
|
|
|
|
|
- try { netman.connect(); }
|
|
|
|
- catch(string err) { printErrorMessage(err); return false; }
|
|
|
|
|
|
+ if(!connect()) return false;
|
|
|
|
|
|
ret = cmdman.cmdVersion(protoVersion);
|
|
ret = cmdman.cmdVersion(protoVersion);
|
|
- try { netman.sendJsonString(ret.msg); }
|
|
|
|
- catch(string err) { printErrorMessage(err); return false; }
|
|
|
|
|
|
|
|
- try { work = netman.receiveJsonString(); }
|
|
|
|
- catch(string err) { printErrorMessage(err); return false; }
|
|
|
|
|
|
+ if(!sendJsonString(ret.msg)) return false;
|
|
|
|
+
|
|
|
|
+ work = receiveJsonString();
|
|
|
|
+ if(work.empty()) return false;
|
|
|
|
|
|
if(!parseJson(&root, work)) return false;
|
|
if(!parseJson(&root, work)) return false;
|
|
|
|
|
|
@@ -73,22 +129,25 @@ void IoMan::run() {
|
|
Json::Value root;
|
|
Json::Value root;
|
|
|
|
|
|
while(!user) {
|
|
while(!user) {
|
|
- user = readline(promptuser.c_str());
|
|
|
|
|
|
+ user = readline(getUserPrompt().c_str());
|
|
|
|
+ printErrorMessage("Using user: " + string(user));
|
|
}
|
|
}
|
|
while(!pass) {
|
|
while(!pass) {
|
|
- user = readline(promptpass.c_str());
|
|
|
|
|
|
+ pass = readline(getPassPrompt().c_str());
|
|
|
|
+ printErrorMessage("Using pass: " + string(pass));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ std::cerr << "IoMan::run() begin" << std::endl;
|
|
|
|
+
|
|
/* move login elsewhere? */
|
|
/* move login elsewhere? */
|
|
cmdret = cmdman.cmdLogin(string(user), string(pass));
|
|
cmdret = cmdman.cmdLogin(string(user), string(pass));
|
|
free(user);
|
|
free(user);
|
|
free(pass);
|
|
free(pass);
|
|
|
|
|
|
- try { netman.sendJsonString(cmdret.msg); }
|
|
|
|
- catch(string err) { printErrorMessage(err); return; }
|
|
|
|
|
|
+ if(!sendJsonString(cmdret.msg)) return;
|
|
|
|
|
|
- try { work = netman.receiveJsonString(); }
|
|
|
|
- catch(string err) { printErrorMessage(err); return; }
|
|
|
|
|
|
+ work = receiveJsonString();
|
|
|
|
+ if(work.empty()) return;
|
|
|
|
|
|
if(!parseJson(&root, work)) return;
|
|
if(!parseJson(&root, work)) return;
|
|
if(!root["accept"].asBool()) {
|
|
if(!root["accept"].asBool()) {
|
|
@@ -102,7 +161,7 @@ void IoMan::run() {
|
|
while(running) {
|
|
while(running) {
|
|
free(line);
|
|
free(line);
|
|
|
|
|
|
- line = readline(promptcmd.c_str());
|
|
|
|
|
|
+ line = readline(getCmdPrompt().c_str());
|
|
|
|
|
|
// if no input, do not add to history, and go to reading next line
|
|
// if no input, do not add to history, and go to reading next line
|
|
if (strlen(line) == 0) {
|
|
if (strlen(line) == 0) {
|
|
@@ -137,12 +196,11 @@ void IoMan::run() {
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
case CmdMan::rettype::json: {
|
|
case CmdMan::rettype::json: {
|
|
- try { netman.sendJsonString(cmdret.msg); }
|
|
|
|
- catch(string err) { printErrorMessage(err); continue; }
|
|
|
|
|
|
+ if(!sendJsonString(cmdret.msg)) continue;
|
|
|
|
|
|
while(1) {
|
|
while(1) {
|
|
- try { work = netman.receiveJsonString(); }
|
|
|
|
- catch(string err) { printErrorMessage(err); break; }
|
|
|
|
|
|
+ work = receiveJsonString();
|
|
|
|
+ if(work.empty()) break;
|
|
|
|
|
|
if(!work.size()) break;
|
|
if(!work.size()) break;
|
|
|
|
|
|
@@ -156,7 +214,7 @@ void IoMan::run() {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- if(!netman.isConnected()) break;
|
|
|
|
|
|
+ if(!connected) break;
|
|
}
|
|
}
|
|
|
|
|
|
free(line);
|
|
free(line);
|