ioman.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. #include "../include/ioman.h"
  2. #include "../include/global.h"
  3. #include <boost/asio.hpp>
  4. #include <iostream>
  5. #include <boost/algorithm/string.hpp>
  6. #include <string>
  7. #include <vector>
  8. #include <readline/history.h>
  9. #include <readline/readline.h>
  10. #include <poll.h>
  11. using std::string;
  12. using std::vector;
  13. using boost::asio::buffer;
  14. using boost::asio::ip::address;
  15. using boost::asio::ip::tcp;
  16. /* this will be provided by main.cpp for the readline callback */
  17. extern IoMan *gIOMAN;
  18. void ioman_externalDebugPrint(string msg) { gIOMAN->printMessage(msg, gIOMAN->OutMsgType::debug); }
  19. IoMan::IoMan(bool enablessl) : cmdman(fileman, &ioman_externalDebugPrint), recvbuf(16384) {
  20. ipstring = "";
  21. port = 0;
  22. tcpsock = new tcp::socket(ios);
  23. connected = false;
  24. /* to be put elsewhere */
  25. /* setup json stuff */
  26. Json::CharReaderBuilder rbuilder;
  27. wbuilder.settings_["indentation"] = "";
  28. reader = rbuilder.newCharReader();
  29. runnetwork = false;
  30. runinput = false;
  31. runresponse = false;
  32. usessl = enablessl;
  33. if (usessl) {
  34. sslctx = new boost::asio::ssl::context(boost::asio::ssl::context::sslv23);
  35. sslctx->set_verify_mode(boost::asio::ssl::verify_peer);
  36. sslctx->set_options(boost::asio::ssl::context::no_sslv2);
  37. sslctx->load_verify_file("rootca.crt");
  38. sslsock = new boost::asio::ssl::stream<tcp::socket &>(*tcpsock, *sslctx);
  39. }
  40. }
  41. IoMan::~IoMan() {
  42. if (connected) {
  43. disconnect();
  44. }
  45. if (runnetwork) {
  46. networkmutex.lock();
  47. runnetwork = false;
  48. networkmutex.unlock();
  49. tnetwork.join();
  50. }
  51. if (runinput) {
  52. inputmutex.lock();
  53. runinput = false;
  54. inputmutex.unlock();
  55. localcv.notify_all();
  56. tinput.join();
  57. }
  58. if (runresponse) {
  59. responsemutex.lock();
  60. runresponse = false;
  61. responsemutex.unlock();
  62. netcv.notify_all();
  63. tresponse.join();
  64. }
  65. if (usessl)
  66. delete sslsock;
  67. delete tcpsock;
  68. if (usessl)
  69. delete sslctx;
  70. delete reader;
  71. }
  72. void IoMan::printMessage(string nouse, OutMsgType nouse2) {}
  73. vector<string> IoMan::tokenizeInput(string in) {
  74. size_t prev, index, quot;
  75. vector<string> args;
  76. /* tokenize string into command and arguments vector*/
  77. if ((index = in.find(" ")) == string::npos) {
  78. // only command no args
  79. args.push_back(in);
  80. } else {
  81. args.push_back(in.substr(0, index));
  82. index++;
  83. bool end_tokenizing = false;
  84. while (!end_tokenizing) {
  85. // find first char thats not a space
  86. while (in[index] == ' ') {
  87. index++;
  88. // bounds check
  89. if (index == in.size())
  90. end_tokenizing = true;
  91. }
  92. if (end_tokenizing)
  93. break;
  94. in = in.substr(index);
  95. if (in[0] == '\"') {
  96. // quoted string
  97. in = in.substr(1);
  98. index = in.find("\"");
  99. args.push_back(in.substr(0, index));
  100. index++;
  101. /*
  102. tokens.push_back(in.substr(0, ++index));
  103. */
  104. // char after closing quote should be space while within bounds
  105. if (index == in.size())
  106. end_tokenizing = true;
  107. } else {
  108. // non-quoted string
  109. index = in.find(" ");
  110. if (index == string::npos) { // no spaces, last arg
  111. args.push_back(in);
  112. end_tokenizing = true;
  113. } else {
  114. args.push_back(in.substr(0, index));
  115. }
  116. }
  117. }
  118. }
  119. return args;
  120. }
  121. // callback for async connect, used to get timeout
  122. void connect_async_handler(const boost::system::error_code &error) { gIOMAN->errcode = error; }
  123. bool IoMan::connect() {
  124. tcp::endpoint *ep = NULL;
  125. address addr;
  126. Json::Value root;
  127. root["command"] = "connect";
  128. root["address"] = ipstring;
  129. root["port"] = port;
  130. addr = address::from_string(ipstring, errcode);
  131. if (errcode) {
  132. root["error"] = errcode.message();
  133. connected = false;
  134. } else {
  135. if (!ios.stopped())
  136. ios.stop();
  137. ios.restart();
  138. // establish connection
  139. printMessage(string(__PRETTY_FUNCTION__) + string(" connecting to ") + ipstring, debug);
  140. ep = new tcp::endpoint(addr, port);
  141. // connect never returns would_block, initialize errcode so we can determine whats up
  142. errcode = boost::asio::error::would_block;
  143. tcpsock->async_connect(*ep, &connect_async_handler);
  144. ios.run_for(std::chrono::seconds(5));
  145. //~ tcpsock->connect(*ep, errcode);
  146. if (errcode) {
  147. root["error"] = errcode.message();
  148. connected = false;
  149. } else {
  150. connected = true;
  151. root["error"] = "";
  152. }
  153. delete ep;
  154. }
  155. if (usessl) {
  156. // try to do ssl handshake
  157. printMessage(string(__PRETTY_FUNCTION__) + string(" doing ssl handshake with ") + ipstring, debug);
  158. sslsock->handshake(boost::asio::ssl::stream_base::client, errcode);
  159. if (errcode) {
  160. root["error"] = errcode.message();
  161. connected = false;
  162. } else {
  163. connected = true;
  164. root["error"] = "";
  165. }
  166. }
  167. root["accept"] = connected;
  168. printMessage(Json::writeString(wbuilder, root), normal);
  169. return connected;
  170. }
  171. void IoMan::disconnect() {
  172. printMessage("IoMan::disconnect()", debug);
  173. tcpsock->shutdown(tcp::socket::shutdown_both, errcode);
  174. if (errcode)
  175. printMessage(string(__PRETTY_FUNCTION__) + string("tcp shutdown says ") + errcode.message(), debug);
  176. tcpsock->close(errcode);
  177. if (errcode)
  178. printMessage(string(__PRETTY_FUNCTION__) + string("tcp shutdown says ") + errcode.message(), debug);
  179. connected = false;
  180. }
  181. bool IoMan::init() {
  182. CmdMan::CmdRet ret;
  183. string work;
  184. Json::Value root;
  185. std::unique_lock<std::mutex> ulock;
  186. printMessage(string(__PRETTY_FUNCTION__) + string(" begin"), debug);
  187. runinput = true;
  188. runresponse = true;
  189. tinput = std::thread(&IoMan::inputMain, this);
  190. tresponse = std::thread(&IoMan::responseMain, this);
  191. printWelcomeMessage();
  192. return true;
  193. }
  194. /* loop to fetch data from the network, doing light preprocessing on it to be
  195. * handled by responseMain */
  196. void IoMan::networkMain() {
  197. vector<Json::Value> toput;
  198. char *recvjson;
  199. Json::Value root;
  200. unsigned int jsonsize, readsize;
  201. printMessage("IoMan::networkMain() begin", debug);
  202. networkmutex.lock();
  203. while (runnetwork) {
  204. networkmutex.unlock();
  205. /*
  206. read from network until \n
  207. try to parse json
  208. - output error if not ok
  209. store all ok json in local vector
  210. get networkmutex
  211. put all local jsons into network vector
  212. release networkmutex
  213. */
  214. // read from network
  215. if (usessl)
  216. readsize = boost::asio::read_until(*sslsock, recvbuf, '\n', errcode);
  217. else
  218. readsize = boost::asio::read_until(*tcpsock, recvbuf, '\n', errcode);
  219. printMessage(string(__PRETTY_FUNCTION__) + string(" asio::read() ok ") + std::to_string(readsize), debug);
  220. // printMessage(string("have ") + std::to_string(toprocess.size()) +
  221. // string(" commands"), debug);
  222. if (readsize < 1) {
  223. break;
  224. }
  225. if (errcode && errcode != boost::asio::error::eof) {
  226. printMessage("IoMan::networkMain() couldnt read json data\n" + errcode.message(), debug);
  227. continue;
  228. }
  229. recvjson = (char *)(boost::asio::buffer_cast<const char *>(recvbuf.data()));
  230. recvjson[readsize] = 0;
  231. while (strchr(recvjson, '\n')) {
  232. // parse
  233. jsonsize = strchr(recvjson, '\n') - recvjson + 1;
  234. printMessage(string(__PRETTY_FUNCTION__) + string(" found jsondata ") + string(recvjson), debug);
  235. if (!reader->parse(recvjson, recvjson + jsonsize, &root, &jsonerror)) {
  236. printMessage("IoMan::networkMain() couldnt parse json data: " + jsonerror, debug);
  237. recvbuf.consume(jsonsize);
  238. recvjson += jsonsize;
  239. continue;
  240. }
  241. recvbuf.consume(jsonsize);
  242. readsize -= jsonsize;
  243. printMessage(string(__PRETTY_FUNCTION__) + string(" remaining recvbuf ") + string(boost::asio::buffer_cast<const char *>(recvbuf.data())), debug);
  244. recvjson += jsonsize;
  245. // store locally
  246. toput.push_back(root);
  247. }
  248. if (toput.size()) {
  249. // put into global vector
  250. netmutex.lock();
  251. printMessage(string(__PRETTY_FUNCTION__) + string(" get netmutex"), debug);
  252. netinput.insert(netinput.end(), toput.begin(), toput.end());
  253. netmutex.unlock();
  254. printMessage(string(__PRETTY_FUNCTION__) + string(" release netmutex"), debug);
  255. }
  256. netcv.notify_all();
  257. // clean up local stuff
  258. toput = vector<Json::Value>();
  259. recvbuf.consume(readsize);
  260. networkmutex.lock();
  261. }
  262. }
  263. /* loop to handle input from the user and responseMain, sending data via network
  264. * if required */
  265. void IoMan::inputMain() {
  266. vector<string> toprocess;
  267. string command;
  268. vector<string> args;
  269. CmdMan::CmdRet cmdret;
  270. std::unique_lock<std::mutex> ulock;
  271. printMessage(string(__PRETTY_FUNCTION__) + string(" begin"), debug);
  272. inputmutex.lock();
  273. while (runinput) {
  274. inputmutex.unlock();
  275. /*
  276. get inputmutex
  277. read all input vector into local vector
  278. release inputmutex
  279. process inputs
  280. send to server if required
  281. */
  282. // read into local vector
  283. ulock = std::unique_lock<std::mutex>(localmutex);
  284. while (!localinput.size() && runinput) {
  285. localcv.wait(ulock);
  286. }
  287. printMessage(string(__PRETTY_FUNCTION__) + string(" has localmutex"), debug);
  288. toprocess = vector<string>(localinput);
  289. localinput = vector<string>();
  290. localmutex.unlock();
  291. printMessage(string(__PRETTY_FUNCTION__) + string(" release localmutex"), debug);
  292. localcv.notify_all();
  293. if (!runinput)
  294. return;
  295. // printMessage(string("have ") + std::to_string(toprocess.size()) +
  296. // string(" commands"), debug);
  297. // process
  298. for (string cmd : toprocess) {
  299. args = tokenizeInput(cmd);
  300. command = args.front();
  301. args.erase(args.begin());
  302. cmdret = cmdman.execute(command, args);
  303. handleInCmdResponse(cmdret);
  304. }
  305. // clean up local stuff
  306. toprocess = vector<string>();
  307. inputmutex.lock();
  308. }
  309. }
  310. void IoMan::handleInCmdResponse(CmdMan::CmdRet cmdret) {
  311. // determine wether to send something and do so if required
  312. if (cmdret.type & CmdMan::rettype::print) {
  313. printMessage(Json::writeString(wbuilder, cmdret.msg), normal);
  314. }
  315. if (cmdret.type & CmdMan::rettype::send) {
  316. printMessage("IoMan::inputMain() sending json \"" + Json::writeString(wbuilder, cmdret.msg) + "\"", debug);
  317. if (usessl)
  318. boost::asio::write(*sslsock, buffer(Json::writeString(wbuilder, cmdret.msg) + "\n"), errcode);
  319. else
  320. boost::asio::write(*tcpsock, buffer(Json::writeString(wbuilder, cmdret.msg) + "\n"), errcode);
  321. if (errcode) {
  322. printMessage("IoMan::inputMain() couldnt send json data\n" + errcode.message() + "\n", debug);
  323. return;
  324. }
  325. }
  326. if (cmdret.type & CmdMan::rettype::error) {
  327. printMessage(Json::writeString(wbuilder, cmdret.msg), error);
  328. }
  329. if (cmdret.type & CmdMan::rettype::close) {
  330. // connection closed, stop network thread and shutdown any operations remaining
  331. networkmutex.lock();
  332. runnetwork = false;
  333. networkmutex.unlock();
  334. disconnect();
  335. tnetwork.join();
  336. }
  337. if (cmdret.type & CmdMan::rettype::connect) {
  338. ipstring = cmdret.msg["address"].asString();
  339. port = cmdret.msg["port"].asUInt();
  340. if (connect()) {
  341. runnetwork = true;
  342. tnetwork = std::thread(&IoMan::networkMain, this);
  343. // put new commands into global vector
  344. localmutex.lock();
  345. printMessage(string(__PRETTY_FUNCTION__) + string(" get localmutex"), debug);
  346. localinput.push_back("version");
  347. cmdman.stateSetConnectionOk();
  348. localmutex.unlock();
  349. printMessage(string(__PRETTY_FUNCTION__) + string(" release localmutex"), debug);
  350. localcv.notify_all();
  351. }
  352. }
  353. if (cmdret.type & CmdMan::rettype::exit) {
  354. mainmutex.lock();
  355. runmain = false;
  356. mainmutex.unlock();
  357. }
  358. if (cmdret.nextcommand.size()) {
  359. localmutex.lock();
  360. printMessage(string(__PRETTY_FUNCTION__) + string(" get localmutex"), debug);
  361. localinput.push_back(cmdret.nextcommand);
  362. localmutex.unlock();
  363. printMessage(string(__PRETTY_FUNCTION__) + string(" release localmutex"), debug);
  364. localcv.notify_all();
  365. }
  366. }
  367. /* loop to handle responses that have been fetched by netMain and possibly add
  368. * new commands to be handled by inputMain */
  369. void IoMan::responseMain() {
  370. vector<Json::Value> toprocess;
  371. vector<string> toput;
  372. CmdMan::CmdRet cmdret;
  373. std::unique_lock<std::mutex> ulock;
  374. printMessage(string(__PRETTY_FUNCTION__) + string(" begin"), debug);
  375. responsemutex.lock();
  376. while (runresponse) {
  377. responsemutex.unlock();
  378. /*
  379. get networkmutex
  380. read all network vector into local vector
  381. release networkmutex
  382. process all jsons
  383. process putdata
  384. process getdata
  385. process listdata
  386. get inputmutex
  387. place new commands into input vector
  388. release inputmutex
  389. */
  390. // read into local vector
  391. ulock = std::unique_lock<std::mutex>(netmutex);
  392. while (!netinput.size() && runresponse) {
  393. netcv.wait(ulock);
  394. }
  395. printMessage(string(__PRETTY_FUNCTION__) + string(" get netmutex"), debug);
  396. toprocess = vector<Json::Value>(netinput);
  397. netinput = vector<Json::Value>();
  398. netmutex.unlock();
  399. printMessage(string(__PRETTY_FUNCTION__) + string(" release netmutex"), debug);
  400. netcv.notify_all();
  401. if (!runresponse)
  402. return;
  403. // process jsons
  404. for (Json::Value root : toprocess) {
  405. cmdret = cmdman.handle(root);
  406. handleOutCmdResponse(cmdret, toput);
  407. }
  408. if (toput.size()) {
  409. // put new commands into global vector
  410. localmutex.lock();
  411. printMessage(string(__PRETTY_FUNCTION__) + string(" get localmutex"), debug);
  412. localinput.insert(localinput.end(), toput.begin(), toput.end());
  413. localmutex.unlock();
  414. printMessage(string(__PRETTY_FUNCTION__) + string(" release localmutex"), debug);
  415. }
  416. localcv.notify_all();
  417. // clean up local stuff
  418. toprocess = vector<Json::Value>();
  419. toput = vector<string>();
  420. responsemutex.lock();
  421. }
  422. }
  423. void IoMan::handleOutCmdResponse(CmdMan::CmdRet cmdret, vector<string> &toput) {
  424. if (cmdret.type & CmdMan::rettype::close) {
  425. // connection closed, stop network thread and shutdown any operations remaining
  426. networkmutex.lock();
  427. runnetwork = false;
  428. networkmutex.unlock();
  429. disconnect();
  430. tnetwork.join();
  431. if (cmdret.nextcommand.size()) {
  432. toput.push_back(cmdret.nextcommand);
  433. }
  434. }
  435. if (cmdret.type & CmdMan::rettype::error) {
  436. printMessage(Json::writeString(wbuilder, cmdret.msg), error);
  437. }
  438. if (cmdret.type & CmdMan::rettype::print) {
  439. printMessage(Json::writeString(wbuilder, cmdret.msg), normal);
  440. }
  441. if (cmdret.type & CmdMan::rettype::send) {
  442. if (cmdret.nextcommand.size()) {
  443. toput.push_back(cmdret.nextcommand);
  444. }
  445. }
  446. if (cmdret.type & CmdMan::rettype::exit) {
  447. mainmutex.lock();
  448. runmain = false;
  449. mainmutex.unlock();
  450. }
  451. }
  452. /* this is the handler that readlines alternative interface will use to process
  453. * user input */
  454. void ioman_readlineHandler(char *line) {
  455. vector<string> tokens;
  456. if (!line) {
  457. printf("\nNULLBURGER\n");
  458. gIOMAN->mainmutex.lock();
  459. gIOMAN->runmain = false;
  460. gIOMAN->mainmutex.unlock();
  461. } else {
  462. // split input line into tokens
  463. boost::algorithm::split(tokens, std::string(line), boost::algorithm::is_any_of(" "), boost::algorithm::token_compress_on);
  464. if (strlen(line) && tokens.size()) {
  465. add_history(line);
  466. gIOMAN->localmutex.lock();
  467. gIOMAN->printMessage(string(__PRETTY_FUNCTION__) + string(" get localmutex"), gIOMAN->debug);
  468. gIOMAN->localinput.push_back(line);
  469. gIOMAN->localmutex.unlock();
  470. gIOMAN->printMessage(string(__PRETTY_FUNCTION__) + string(" release localmutex"), gIOMAN->debug);
  471. gIOMAN->localcv.notify_all();
  472. }
  473. free(line);
  474. }
  475. }
  476. /* main user input loop */
  477. void IoMan::run() {
  478. printMessage(string(__PRETTY_FUNCTION__) + string(" begin"), debug);
  479. struct pollfd inpipestatus;
  480. inpipestatus.fd = STDIN_FILENO;
  481. inpipestatus.events = POLLIN;
  482. runmain = true;
  483. // Install readline handler
  484. rl_callback_handler_install(getCmdPrompt().c_str(), (rl_vcpfunc_t *)&ioman_readlineHandler);
  485. mainmutex.lock();
  486. while (runmain) {
  487. mainmutex.unlock();
  488. poll(&inpipestatus, 1, 100);
  489. if (inpipestatus.revents & POLLIN) {
  490. rl_callback_read_char();
  491. }
  492. mainmutex.lock();
  493. }
  494. mainmutex.unlock();
  495. // Remove the handler
  496. rl_callback_handler_remove();
  497. }